1 6 7 package org.jfox.ioc.connector.soap; 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.ByteArrayOutputStream ; 11 import java.io.IOException ; 12 import java.io.ObjectInputStream ; 13 import java.io.ObjectOutputStream ; 14 import java.io.Serializable ; 15 import java.util.Vector ; 16 17 import org.apache.xmlrpc.XmlRpcHandler; 18 import org.jfox.ioc.Registry; 19 import org.jfox.ioc.connector.ConnectorRemote; 20 import org.jfox.ioc.connector.Invocation; 21 import org.jfox.ioc.connector.ObjectId; 22 import org.jfox.ioc.connector.ConnectorHelper; 23 import org.jfox.ioc.connector.local.LOCALConnectorRemote; 24 25 29 30 public class JRMPXmlRpcHandler implements XmlRpcHandler { 31 private ConnectorRemote stub = null; 32 34 public JRMPXmlRpcHandler() { 35 try{ 36 stub = ConnectorHelper.lookupConnector("localhost", 1099, "JRMP"); 37 } 38 catch(Exception e){ 39 e.printStackTrace(); 40 } 41 42 } 43 44 public Object execute(String string, Vector vector) throws Exception { 45 Invocation invocation = (Invocation) unmarshelledObject((byte[]) vector.get(0)); 46 ObjectId objectId = invocation.getObjectId(); 47 48 if(objectId.isCurrentVM()) { 50 ConnectorRemote local = (ConnectorRemote) (Registry.getInstance().getComponentInstance(LOCALConnectorRemote.class)); 51 return marshelledObject((Serializable ) local.invoke(invocation)); 52 } 53 else { 55 return marshelledObject((Serializable ) stub.invoke(invocation)); 56 } 57 } 58 59 public static Object unmarshelledObject(byte[] bytes) { 60 try { 61 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (bytes)); 62 Object obj = in.readObject(); 63 in.close(); 64 return obj; 65 } 66 catch(Exception e) { 67 e.printStackTrace(); 68 return bytes; 69 } 70 } 71 72 public static byte[] marshelledObject(Serializable obj) { 73 ByteArrayOutputStream out = new ByteArrayOutputStream (); 74 try { 75 ObjectOutputStream oos = new ObjectOutputStream (out); 76 oos.writeObject(obj); 77 oos.flush(); 78 } 79 catch(IOException e) { 80 e.printStackTrace(); 81 } 82 return out.toByteArray(); 83 } 84 85 public static void main(String [] args) { 86 87 } 88 } | Popular Tags |