alex's newer changes. bytecode serialisation now happens within the

gskc [2002-12-17 22:14:42]
alex's newer changes. bytecode serialisation now happens within the
Worklet code, ie transparent to whoever codes the WJ
Filename
Worklet.java
WorkletJunction.java
WorkletTest4.java
diff --git a/Worklet.java b/Worklet.java
index 84a700d..757646b 100644
--- a/Worklet.java
+++ b/Worklet.java
@@ -67,11 +67,10 @@ public final class Worklet implements Serializable {
   private boolean _defaultSecurity = true;
   /** default security */
   private boolean _isSecure = false;
-
-    //class loader to load junctions reconstructed from byte array
     private transient WorkletClassLoader _ldr = null;
-    //junctions kept as byte arrays
     public Hashtable byteArrays = new Hashtable();
+    //just a reference for serialization stuff...
+    private WorkletJunction lastAdded = null;

   /**
    * Creates a Worklet with the given {@link WorkletJunction} as the
@@ -85,9 +84,8 @@ public final class Worklet implements Serializable {
       _wjClasses.add(_oj.getClass());
       _oj._worklet = this;
     }
-
-    // create s String worklet Id
     wid = new String((new Long(new Date().getTime())).toString()+WVM.wvm_id);
+    // System.out.println("WORKLET ID IS " + wid);
   }

   /**
@@ -96,35 +94,72 @@ public final class Worklet implements Serializable {
    * @param _wj: {@link WorkletJunction} to add
    */
   public void addJunction(WorkletJunction _wj) {
+      // System.out.println("Worklet: addJunction");
     if (_wj == null) return;
     synchronized(_junctions) {
+	lastAdded = _wj;
       _junctions.addElement(_wj);
       _wjClasses.add(_wj.getClass());
+      // System.out.println("_wj.getClass, name: " + _wj.getClass().getName());
       _wj.setOriginWorkletJunction(_originJunction);
       _wj._worklet = this;
     }
   }

+
+
+    // Old add Junction for serialization
+    // does not work because worklet pulls in all the classes
+    // for junction even before any junction is being loaded....
+/*   public void addJunction(WorkletJunction _wj,boolean ba){
+	 Class []cls = _wj.getClass().getClasses();
+      for(int i=0;i<java.lang.reflect.Array.getLength(cls);i++){
+	  System.out.println("NESTED CLASS: " + cls[i].getName());
+      }
+	if(ba == false){
+	    addJunction(_wj);
+	} else {
+	    try{
+		addJunction(new WorkletJunction(_wj._host, _wj._name, _wj._port,_wj.appName,wid,_wj.getIndex()) {
+			public void init(Object system, WVM wvm) { }
+			public void execute() { }
+		    });
+
+		ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
+		ObjectOutputStream ooStream = new ObjectOutputStream(baoStream);
+		ooStream.writeObject(_wj);
+		byte []wjbytes = baoStream.toByteArray();
+		baoStream.close();
+		System.out.println("PUTTING WJ WITH INDEX: "+_wj.getIndex());
+		//  byte []wjbytes = ...;
+		byteArrays.put(_wj.getIndex(),wjbytes);
+	    } catch (Exception e){
+	    e.printStackTrace();
+	    }
+	    }
+	    }*/

+
     /*Add a dummy junction, with actual junction being supplied as byte code*/
-    /*code base for actual junction must be supplied also*/
-    public void addJunction(WorkletJunction _wj, byte []bc,URL url){
-	_wj._originClassServer = url;
-	//add dummy junction
-	addJunction(_wj);
-	//store byte array with actual junction
-	byteArrays.put(_wj.getIndex(),bc);
-
-	// TO DO:
-	//encrypt the byte array
+    /*code base for actual junction must be supplied also*/
+    public void addJunction(WorkletJunction _wj,URL url){
+	byte [] bc = _wj.getBytes();
+	//	_wj._originClassServer = url;
+	addJunction(new WorkletJunction(_wj._host,_wj._name,_wj._port,url){
+	    public void init(Object system, WVM wvm) {}
+	    public void execute() {}
+	});
+	byteArrays.put(lastAdded.getIndex(),bc);
     }

+
   /**
    * Gets the {@link WorkletJunction} at the origin
    *
    * @return {@link WorkletJunction} at the origin
    */
   public WorkletJunction getOriginJunction() {
+      // System.out.println("Worklet: getOriginJunction");
     return (_originJunction);
   }

@@ -135,6 +170,7 @@ public final class Worklet implements Serializable {
    * @param wvm: reference to local {@link WVM}
    */
   void init(Object system, WVM wvm) {
+      //  System.out.println("Worklet: init");
     _wvm = wvm;
     _system = system;
     if (_atOrigin) {
@@ -157,41 +193,52 @@ public final class Worklet implements Serializable {
     (new Thread() {
       // create a new thread regardless
       public void run() {
+	  // get the worklet class loader for this worklet
+
+	  if(WVM.wkltRepository.containsKey(wid)){
+	      _ldr = (WorkletClassLoader)WVM.wkltRepository.get(wid);
+
+	  } else {
+	      WVM.err.println("LOADER FOR WORKLET HAS NOT BEEN FOUND!");
+	      WVM.err.println("MOVING ON TO THE NEXT JUNCTION");
+	      if (!_atOrigin) {
+		  if (_junctions.isEmpty()) returnToOrigin();
+		  else {
+		      moveToNextJunction();
+		      return;
+		  }
+	      }
+	  }
+	  // do not send worklet id for classes loaded during execution
+	  _ldr.dontSendWorkletId();
         // 2-do: create _priority variable in WJ:
         // 2-do: existing super-priority thread to inherit higher priorities from ...
         WorkletJunction _wj = _atOrigin ? _originJunction : _currentJunction;
 	if(byteArrays.containsKey(_wj.getIndex())){
 	    try{
 		 _junctions.removeElement(_wj);
-		 //TO DO
-		 // decrypt the byte array if encrypted
 		byte []bytecode = (byte[])byteArrays.get(_wj.getIndex());
 		ByteArrayInputStream baiStream = new ByteArrayInputStream(bytecode);
-		if(WVM.wkltRepository.containsKey(wid)){
-		    _ldr = (WorkletClassLoader)WVM.wkltRepository.get(wid);
-		} else {
-		    if (!_atOrigin) {
-			if (_junctions.isEmpty()) returnToOrigin();
-			else {
-			    WVM.err.println("FAILED TO LOAD WJ from BYTES ARRAY: LOADER NOT FOUND!");
-			    WVM.err.println("MOVING ON TO THE NEXT JUNCTION");
-			    moveToNextJunction();
-			    return;
-			}
-		    }
-		}
+		//   ObjectInputStream oiStream = new ObjectInputStream(baiStream);
+		// workletJunction = (WorkletJunction) oiStream.readObject();
+		//baiStream.close();
+
 		//this must be present
 		_ldr.addTopCodebase(_wj._originClassServer);
-		_ldr.dontSendWorkletId();
+
 		ObjectInputStream ois = new ObjectInputStream(baiStream) {
 			protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException {
 			    String name = v.getName();
+			    //	System.out.println("processSocketStream: Class.forName");
 			    Class c = Class.forName(name, true, _ldr);
+			    WVM.out.println("In custom ObjectInputStream, trying to resolve class: " + c);
 			    return ( (c == null) ? super.resolveClass(v) : c );
 			}
 		    };
 		_wj = (WorkletJunction) ois.readObject();
 		_wj.sysInit(_system, _wvm);
+		//	 _currentJunction.sysInit(system, wvm);
+		WVM.out.println("RECREATED THE JUNCTION FROM THE BYTES ARRAY");
 		baiStream.close();
 	    } catch(Exception e){
 		WVM.err.println("FAILED TO LOAD WJ from BYTES ARRAY:");
@@ -205,6 +252,8 @@ public final class Worklet implements Serializable {
 		    }
 		}
 	    }
+	    // _ldr.sendWorkletId();
+
 	}
         Thread t = new Thread(_wj, _hashCode);
 	t.setPriority(_wj.getPriority());
diff --git a/WorkletJunction.java b/WorkletJunction.java
index a18bbb3..c2f6159 100644
--- a/WorkletJunction.java
+++ b/WorkletJunction.java
@@ -149,19 +149,28 @@ public abstract class WorkletJunction implements Serializable, Runnable {
     this(host, name, WVM_Host.PORT, port, false, new String("default"), null);
   }

-    public WorkletJunction(String host, String name, int port,String apName) {
+    public WorkletJunction(String host, String name, int port, URL u) {
 	this(host, name, WVM_Host.PORT, port, false, new String("default"), null);
-	appName = apName;
-    }
-     public WorkletJunction(String host, String name, int port,String apName,String id) {
-	this(host, name, WVM_Host.PORT, port, false, id, null);
-	appName = apName;
-    }
-
-     public WorkletJunction(String host, String name, int port,String apName,String id,String i) {
-	this(host, name, WVM_Host.PORT, port, false, id, null);
-	appName = apName;
-	index = i;
+	_originClassServer = u;
+
+    }
+
+    // return this junction as byte array for serialized transportation
+    // and storage
+    public byte[] getBytes(){
+	try{
+	    ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
+	    ObjectOutputStream ooStream = new ObjectOutputStream(baoStream);
+
+	    //create byte array out of actual junction to use first
+	    ooStream.writeObject(this);
+	    byte []wjbytes = baoStream.toByteArray();
+	    baoStream.close();
+	    return wjbytes;
+	} catch (IOException ioe){
+	    ioe.printStackTrace();
+	    return null;
+	}
     }

     public String getIndex(){
@@ -198,7 +207,6 @@ public abstract class WorkletJunction implements Serializable, Runnable {
    * @param origin: WorkletJunction holding addressing info of the origin
    */
   final void setOriginWorkletJunction(WorkletJunction origin) {
-     	// System.out.println("WorkletJunction: setOriginWorkletJunction");
     _originJunction = origin;
   }

@@ -233,7 +241,6 @@ public abstract class WorkletJunction implements Serializable, Runnable {
    * @return priority level
    */
   final int getPriority() {
-    	//  System.out.println("WorkletJunction: getPriority");
     return _priority;
   }

@@ -243,7 +250,6 @@ public abstract class WorkletJunction implements Serializable, Runnable {
    * @param priority: level of priority
    */
   final void setPriority(int priority) {
-     	// System.out.println("WorkletJunction: setPriority");
     _priority = priority;
   }

@@ -275,37 +281,31 @@ public abstract class WorkletJunction implements Serializable, Runnable {

   /** @return target hostname */
   final String getHost() {
-     	// System.out.println("WorkletJunction: getHost");
     return (_host);
   }

   /** @return target RMI port number */
   final int getRMIPort() {
-    	//  System.out.println("WorkletJunction: getRMIPort");
     return (_rmiport);
   }

   /** @return target RMI server name */
   final String getName() {
-     	// System.out.println("WorkletJunction: getName");
     return (_name);
   }

   /** @return target socket number */
   final int getPort() {
-    	//  System.out.println("WorkletJunction: getPort");
     return (_port);
   }

   /** @return name@host:port */
   public String toString() {
-    	//  System.out.println("WorkletJunction: toString");
     return (_name + "@" + _host + ":" + _port);
   }

   /** @return {@link WorkletID} */
   final String id() {
-    	//  System.out.println("WorkletJunction: id");
     return wid;
   }

@@ -360,7 +360,6 @@ public abstract class WorkletJunction implements Serializable, Runnable {
    * @param tm: array of methods
    */
   public final void setTransportMethods(String[] tm){
-    	//  System.out.println("WorkletJunction: setTransportMethods");
     _transportMethods = tm;
   }

@@ -370,7 +369,6 @@ public abstract class WorkletJunction implements Serializable, Runnable {
    * @return current transport methods
    */
   public final String[] getTransportMethods(){
-    	//  System.out.println("WorkletJunction: getTransportmethods");
     if (_transportMethods == null){
       if (isSecure())
 	_transportMethods = secureDefault;
diff --git a/WorkletTest4.java b/WorkletTest4.java
index ad8bce4..c0418e0 100644
--- a/WorkletTest4.java
+++ b/WorkletTest4.java
@@ -96,13 +96,14 @@ public class WorkletTest4 implements Serializable {
 				//  	    // final Class appClass = Class.forName("gskc.TicTacToe");


-
-				try{
-				    ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
-				    ObjectOutputStream ooStream = new ObjectOutputStream(baoStream);
+				String u = new String("http://"+java.net.InetAddress.getLocalHost().getHostAddress()+":"+wvm.transporter._webPort+"/");
+				URL x = new URL(u);
+			// 	try{
+// 				    ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
+// 				    ObjectOutputStream ooStream = new ObjectOutputStream(baoStream);

 				    //create byte array out of actual junction to use first
-				    ooStream.writeObject(new WorkletJunction(rHost, rName, rPort,App,wkl.wid) {
+				    wkl.addJunction(new WorkletJunction(rHost, rName, rPort) {
 					public void init(Object system, WVM wvm) {
 					    System.out.println("ORIGINAL JUNCTION"); }
 					public void execute() {
@@ -125,25 +126,25 @@ public class WorkletTest4 implements Serializable {
 						e.printStackTrace();
 					    }
 					}
-				    });
+				    },x);

-				    byte []wjbytes = baoStream.toByteArray();
-				    baoStream.close();
+				  //   byte []wjbytes = baoStream.toByteArray();
+// 				    baoStream.close();

-				    String u = new String("http://"+java.net.InetAddress.getLocalHost().getHostAddress()+":"+wvm.transporter._webPort+"/");
-			  	//   System.out.println("\n\nSENDING URL: " + u);
-				    URL x = new URL(u);
+// 				    String u = new String("http://"+java.net.InetAddress.getLocalHost().getHostAddress()+":"+wvm.transporter._webPort+"/");
+// 			  	//   System.out.println("\n\nSENDING URL: " + u);
+// 				    URL x = new URL(u);

-				    //add byte array along with dummy empty junction tht has only destination info
-				    wkl.addJunction(new WorkletJunction(rHost, rName, rPort,App,wkl.wid) {
-					public void init(Object system, WVM wvm) {}
-					public void execute() {}
-				    },wjbytes,x);
+// 				    //add byte array along with dummy empty junction tht has only destination info
+// 				    wkl.addJunction(new WorkletJunction(rHost, rName, rPort,App,wkl.wid) {
+// 					public void init(Object system, WVM wvm) {}
+// 					public void execute() {}
+// 				    },wjbytes,x);

-				} catch (IOException ioe){
-				    ioe.printStackTrace();
-				    System.exit(-1);
-				}
+// 				} catch (IOException ioe){
+// 				    ioe.printStackTrace();
+// 				    System.exit(-1);
+// 				}

 				/*	} else {
 					wkl.addJunction(new WorkletJunction(rHost, rName, rPort,App,wkl.wid) {
@@ -196,6 +197,10 @@ public class WorkletTest4 implements Serializable {
 	} catch (ClassNotFoundException e) {
 	    WVM.out.println("Exception: " + e.getMessage());
 	    e.printStackTrace();
+	    System.exit(0);
+	} catch (Exception e) {
+	    WVM.out.println("Exception: " + e.getMessage());
+	    e.printStackTrace();
 	    System.exit(0);
 	}
     }