KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > connector > ConnectorHelper


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4 package org.jfox.ioc.connector;
5
6 import java.io.BufferedInputStream JavaDoc;
7 import java.io.BufferedOutputStream JavaDoc;
8 import java.io.ObjectInputStream JavaDoc;
9 import java.io.PrintStream JavaDoc;
10 import java.net.Socket JavaDoc;
11 import java.net.URI JavaDoc;
12 import java.net.URISyntaxException JavaDoc;
13 import java.rmi.MarshalledObject JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16 import javax.rmi.PortableRemoteObject JavaDoc;
17
18 /**
19  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
20  */

21
22 public class ConnectorHelper {
23
24     /**
25      * cache connector stub
26      */

27     private static Map JavaDoc connectors = new HashMap JavaDoc();
28
29     public static ConnectorRemote lookupConnector(String JavaDoc providerURL) {
30         URI JavaDoc uri;
31         try {
32             uri = new URI JavaDoc(providerURL);
33         }
34         catch(URISyntaxException JavaDoc e){
35             throw new RuntimeException JavaDoc(e);
36         }
37         String JavaDoc host = uri.getHost();
38         int port = uri.getPort();
39         String JavaDoc protocol = uri.getScheme();
40         String JavaDoc key = providerURL;
41         if(connectors.containsKey(key)) { // return from cache
42
ConnectorRemote remote = (ConnectorRemote) connectors.get(key);
43             try {
44                 remote.ping();
45                 return remote;
46             }
47             catch(Throwable JavaDoc e) {
48                 connectors.remove(key);
49                 e.printStackTrace();
50             }
51         }
52
53         try {
54             ConnectorRemote stub = lookupConnector(host,port,protocol);
55             connectors.put(key, stub);
56             return stub;
57         }
58         catch(Throwable JavaDoc e) {
59             e.printStackTrace();
60             connectors.remove(key);
61             return null;
62         }
63     }
64
65     public static ConnectorRemote lookupConnector(String JavaDoc host, int port, String JavaDoc protocol) throws Exception JavaDoc {
66         Socket JavaDoc csocket = new Socket JavaDoc(host, port);
67         // try get the remote of the protocol
68
PrintStream JavaDoc out = new PrintStream JavaDoc(new BufferedOutputStream JavaDoc(csocket.getOutputStream()));
69         out.println(protocol);
70         out.flush();
71
72         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(csocket.getInputStream()));
73         Object JavaDoc obj = in.readObject();
74         csocket.close();
75         ConnectorRemote stub;
76         if(obj instanceof MarshalledObject JavaDoc) {
77             MarshalledObject JavaDoc mobj = (MarshalledObject JavaDoc) PortableRemoteObject.narrow(obj, MarshalledObject JavaDoc.class);
78             stub = (ConnectorRemote) mobj.get();
79         }
80         else {
81             stub = (ConnectorRemote) PortableRemoteObject.narrow(obj, ConnectorRemote.class);
82         }
83         stub.ping();
84         return stub;
85     }
86
87     public static void main(String JavaDoc[] args) {
88
89     }
90 }
91
92
Popular Tags