Monday, May 10, 2010

Super class Serialization

The superclasses of your serialized classes need not be Serializable. However, those superclass fields won’t be saved/restored. The fields will be restored to whatever you would get running the non-arg constructor.


Groovy: an agile dynamic language for the Java Platform

Closure: Groovy's way of providing transparent callback, used for two particular areas: collection iteration and resources handling.
{ [closureArguments->] statements }

(1..10).each{counter -> log += counter}

// method inject(object, closure)
[1, 2, 3].inject(0) {count, item -> count + item}

// multiple parameter
map = ['a':1, 'b':2]
map.each{ key, value -> map[key] = value * 2}

// call closure
def adder = {x, y -> return x + y}
assert adder(4, 3) == 7
assert{2, 6} == 8

// curried closure
def adder = {x, y -> return x + y}
def addone = adder.curry(1)
assert addone(5) == 6

Think of the arrow as an indication that parameters are passed from the method on the left into the closure body on the right.

The real power for currying comes when the closure's parameters are themselves closures. Using composition, two or more simple closures can be combined to produce a more elaborate one.

GroovyBeans: when no visibility modifier attached to field declaration, a property is generated. i.e., a private field and two public accessor methods (overridable). (Note that property!=field). When final keyword is used, the property will only be readable, not setter method created.

class MyBean implements Serializable {
String myprop; // default visibility

Multimethods: Groovy's mechanics of method lookup take the dynamic type of method arguments into account, whereas Java relies on the static type. (Note: The dynamic binding of Java (Polymorphism) is for caller object, not method argument). e.g., Object's default equals method can be overridden as:

class Equalizer {
boolean equals(Equalizer e) {
return true

GPaths spread-dot operator: list*.member where *. is called spread-dot operator and member can be a field access, a property access, or a method call.

list*.member <==> list.collect{ item -> item?.member } <==> list.collect{ item -> item?.property } // abbre of special case for property

Friday, May 7, 2010

Groovy-Eclipse plugin installation

I have been trying to install Groovy Eclipse plugin using update sites listed in Groovy, but always failed with errors such as "No repository found containing:org.codehaus.groovy/osgi.bundle/" or missing dependency etc.

Finally, I downloaded the snapshot (archived zip) to local and install it successfully.
# Help -> Install new software -> Add...
# Select the location of the zip you just downloaded
# Install as you would from the regular update site

Thursday, May 6, 2010

JNDI in JBoss

There are three levels of naming scope in JBoss:

- Name underjava:comp, only available to the application component such as EJB Bean1, Web application web1 etc. java:comp/env refers to as enterprise naming context (ENC)

- Names under java:, visible only within the JBoss server and not to remote clients

- Any other names, global visible, provided that the context or object support serialization.

With JNDI reference, it allows us to bind non-serializable object to JNDI. It actually only bind object reference to it and retrieve the object from an ObjectFactory based on the reference. Spring's BeanFactory is bound to JNDI in this way. The limitation is that the object is only available in the same JVM. Even a backend service is serializable, it doesn't make sense to bind it to JNDI directly, because the implementation class gets loaded to client side and unserialized.

A code snippet from JBoss's NonSerializableFactory

public static synchronized void rebind(Context ctx, String key, Object target)
throws NamingException
rebind(key, target);
String className = target.getClass().getName();
String factory = (org.jboss.util.naming.NonSerializableFactory.class).getName();
StringRefAddr addr = new StringRefAddr("nns", key);
Reference memoryRef = new Reference(className, addr, factory, null);
ctx.rebind(key, memoryRef);

Tuesday, May 4, 2010

Drop database from DOS batch script

mysql -uroot -pabc123 -e "drop database netsight" >nul 2>1&

The option -e to run execute a command. --help (-?) to display help information.

Redirect to null to keep quiet. Otherwise, it may display an error "ERROR 1010 (HY000) at line 1: Error dropping database (can't rmdir '.\netsight', errno: 41)". Since when dropping a database, it will try to delete folder mysql/data/netsight, it fails if the folder contains some non-db files.