KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Vector JavaDoc;
10 import java.io.Serializable JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.rmi.RemoteException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.net.HttpURLConnection JavaDoc;
17
18 import org.apache.xmlrpc.XmlRpcHandler;
19 import org.apache.xmlrpc.XmlRpcClientLite;
20 import org.jfox.ioc.connector.Invocation;
21 import org.jfox.ioc.connector.ConnectorRemote;
22
23 /**
24  * SOAPContainerServiceBinder use to bound in jndi, so client can lookup it to client side,
25  * after look down, can use it to invoke soap protocol
26  * <p/>
27  * it's a Serializable class, readObject method is be overwrote
28  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
29  */

30
31 public class SOAPConnectorRemote implements ConnectorRemote, Serializable JavaDoc {
32     private transient XmlRpcHandler client;
33     private String JavaDoc soapURL;
34
35     public SOAPConnectorRemote(String JavaDoc soapUrl) {
36         this.soapURL = soapUrl;
37         try {
38             client = new XmlRpcClientLite(soapUrl);
39         }
40         catch(Exception JavaDoc e) {
41             e.printStackTrace();
42         }
43     }
44
45     public String JavaDoc getProtocol() throws RemoteException JavaDoc {
46         return "SOAP";
47     }
48
49     public Object JavaDoc invoke(Invocation invocation) throws RemoteException JavaDoc {
50         Vector JavaDoc params = new Vector JavaDoc(1);
51         params.add(marshelledObject(invocation));
52         try {
53             // handler can be any String, because has use $default handle in server side
54
return client.execute("xmlrpc", params);
55         }
56         catch(Exception JavaDoc e) {
57             throw new RemoteException JavaDoc(e.getMessage(), e);
58         }
59
60     }
61
62     /**
63      * after lookup, re initialize XmlRpcClient according as the soap servlet url
64      *
65      * @param in
66      * @throws IOException
67      * @throws ClassNotFoundException
68      */

69     private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
70         in.defaultReadObject();
71         client = new XmlRpcClientLite(soapURL);
72     }
73
74     public void ping() throws RemoteException JavaDoc {
75         HttpURLConnection JavaDoc conn = null;
76         try {
77             conn = (HttpURLConnection JavaDoc) new URL JavaDoc(soapURL).openConnection();
78         }
79         catch(Exception JavaDoc e) {
80             throw new RemoteException JavaDoc("can't get connection with the soap url: " + soapURL, e);
81         }
82         finally {
83             if(conn != null) {
84                 try {
85                     conn.disconnect();
86                 }
87                 catch(Exception JavaDoc e) {
88                 }
89             }
90         }
91 // System.out.println("SOAPContainerServiceBinder: I'm alive :) " + new Date());
92
}
93
94     private static byte[] marshelledObject(Serializable JavaDoc obj) {
95         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
96         try {
97             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(out);
98             oos.writeObject(obj);
99             oos.flush();
100             oos.close();
101         }
102         catch(IOException JavaDoc e) {
103             e.printStackTrace();
104         }
105         return out.toByteArray();
106     }
107
108     public static void main(String JavaDoc[] args) {
109
110     }
111 }
Popular Tags