KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > connector > soap > JRMPXmlRpcHandler


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.connector.soap;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import java.util.Vector JavaDoc;
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 /**
26  * 通过 JRMP 转发给 JRMPContainerService 来执行
27  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
28  */

29
30 public class JRMPXmlRpcHandler implements XmlRpcHandler {
31     private ConnectorRemote stub = null;
32 // private ContainerRemote local = LOCALContainerService.getInstance();
33

34     public JRMPXmlRpcHandler() {
35         try{
36             stub = ConnectorHelper.lookupConnector("localhost", 1099, "JRMP");
37         }
38         catch(Exception JavaDoc e){
39             e.printStackTrace();
40         }
41
42     }
43
44     public Object JavaDoc execute(String JavaDoc string, Vector JavaDoc vector) throws Exception JavaDoc {
45         Invocation invocation = (Invocation) unmarshelledObject((byte[]) vector.get(0));
46         ObjectId objectId = invocation.getObjectId();
47
48         // if soap servlet web server and the ejb container in same jvm, use LOCAL protocol
49
if(objectId.isCurrentVM()) {
50             ConnectorRemote local = (ConnectorRemote) (Registry.getInstance().getComponentInstance(LOCALConnectorRemote.class));
51             return marshelledObject((Serializable JavaDoc) local.invoke(invocation));
52         }
53         // if not int same jvm, use JRMP protocol
54
else {
55             return marshelledObject((Serializable JavaDoc) stub.invoke(invocation));
56         }
57     }
58
59     public static Object JavaDoc unmarshelledObject(byte[] bytes) {
60         try {
61             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
62             Object JavaDoc obj = in.readObject();
63             in.close();
64             return obj;
65         }
66         catch(Exception JavaDoc e) {
67             e.printStackTrace();
68             return bytes;
69         }
70     }
71
72     public static byte[] marshelledObject(Serializable JavaDoc obj) {
73         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
74         try {
75             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(out);
76             oos.writeObject(obj);
77             oos.flush();
78         }
79         catch(IOException JavaDoc e) {
80             e.printStackTrace();
81         }
82         return out.toByteArray();
83     }
84
85     public static void main(String JavaDoc[] args) {
86
87     }
88 }
Popular Tags