Serializing Java Code Like data, how cool would that be?

  

Here’s a thought, a possibility and a question for you, all based on the question “What if you could Serialize java code, like data”?


The thought

   Many times,especially while writing Swing code, we split the code that need to be together in two files not because they need splitting logically for maintenance, but because, it gives better performance to execute a particular set of code in a different place.

As an example, It’s Always better that the code the executes queries to be on the server as a server is almost always close to the database than say Java Swing based client is.

Eg.,Consider this very simple application that runs as an applet
The Applicaton Screen

You enter a name, and click the button. If the name is in the database, the app will show “Name already exists” else will show “The name has been inserted”. As below.

You can imagine how I would hava written the code

In the Swing Client

public void button_clicked(){
  String name = nameTextField.getText();
  boolean res = callServerCodeToCheckAndInsertName(name);
  if(res){
    JOptionPane.showMessageDalog(null,"Name was added to the database");
  }
  else{
	JOptionPane.showMessageDalog(null,"Name already existed");
  }
}

Some where in the Server(EJB, or a servlet or a helper class called from those)..

public boolean callServerCodeToCheckAndInsertName(String name){	
  //Just assume that My Helper class Users has these methdos
  //That will execute and check the database to get the count
  int c = Users.getCountForWhereClause("name", name);
  if(c!=0){
	return false;
  }
  else{
	//Insert name into the database
	Users.insert(name);
	return true;
  }
}

Now,the method “callServerCodeToCheckAndInsertName” needed to be executed in the server, because the server is more closer to the database. If I had this same code in the client, I had to get a connection to the database from the client being executed remotely and execute the queries from there, which would have been a big performance hit.

The Possibility

   If I could serialize the code,I would have had something like this

public void button_clicked(){
  String name = nameTextField.getText();
  boolean res = 
	ServerUtils.executeOnServer(Boolean.class,
		new ServerCode(){
			//This whole method executes on the server
			//though it's decalred in only one place
			public Object ServerMethod(){
				int c = Users.getCountForWhereClause("name", name);
				if(c!=0){
					return false;
				}
				else{
					//Insert name into the database
					Users.insert(name);
					return true;
				}
			}
		}
	);
  if(res){
    JOptionPane.showMessageDalog(null,"Name was added to the database");
  }
  else{
	JOptionPane.showMessageDalog(null,"Name already existed");
  }
}

If this were a possibility, the ServerUtils.executeOnServer(), would serialize the code inside the new Class(ServerCode’s subclass), send it to server, Have it executed there,
get back the data, convert it to a boolean and give it back to me.

The Question

   In one line, I want to be able to do this. I want to have a generic Sever method that will take and execute any code from the client and return the result using a no-hassle technique. That’s all.
Assume that security is not a cocern for now. Is there anyway to achieve this now using Java? Any workarounds? Does any other language support this?







7 Responses to 'Serializing Java Code Like data, how cool would that be?'

  1. Anonymous - May 29th, 2008 at 12:04 pm

    Java RMI can do this. Go get a copy of the O’Reilly Java RMI book.

  2. SomeOneOnNet - May 29th, 2008 at 1:09 pm

    Anonymous,
    Java RMI is for invoking a remote method. Not for dynamically transferring a newly created code. You can’t use RMI for this.

  3. Karthik - May 29th, 2008 at 1:13 pm

    Hey Anonymous/others,
    If you think you can do this using RMI, you need to tell me how. To begin, How will you create the stub and skel, when you don’t know the method and code until runtime?

  4. raveman - May 29th, 2008 at 8:49 pm

    public static Class defineClass(final ClassLoader parent,
    final String name, final byte[] bytes) {
    return new ClassLoader(parent) {
    Class c = defineClass(name, bytes, 0, bytes.length);
    }.c;
    }

  5. raveman - May 29th, 2008 at 8:58 pm

    private void usage() throws Exception {
    byte[] bytes = FileUtils.readFileToByteArray(new File(
    “c:\\workspace\\ProjectA\\bin\\a\\A.class”));
    Class defineClass = defineClass(this.getClass().getClassLoader(),
    “a.A”, bytes);
    Object newInstance = defineClass.newInstance();
    Method method = defineClass.getMethod(”toString”);
    Object invoke = method.invoke(newInstance);
    System.out.println(invoke);
    }

  6. raveman - May 29th, 2008 at 9:04 pm

    and Java Compiler API
    http://today.java.net/pub/a/today/2008/04/10/source-code-analysis-using-java-6-compiler-apis.html#invoking-the-compiler-from-code-the-java-compiler-api

  7. sudar - June 2nd, 2008 at 10:05 am

    I accept to what Karthik is telling, Get into depth
    its not just RMI which is dealt with here..:)


Leave a Reply





Popular Articles

Blog Categories

Monthly Archives

Resources