KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > util > connection > RMIConnection


1 /*
2  * CoadunationUtil: The coadunation util library.
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * RMIConnection.java
20  */

21
22 // package path
23
package com.rift.coad.util.connection;
24
25 // java imports
26
import java.lang.reflect.Proxy JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.concurrent.ConcurrentHashMap JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31
32 // logging import
33
import org.apache.log4j.Logger;
34
35 /**
36  * This object controls the RMI based connection.
37  *
38  * @author Brett Chaldecott
39  */

40 public class RMIConnection implements Connection {
41     
42     // the logger reference
43
protected static Logger log =
44             Logger.getLogger(RMIConnection.class.getName());
45     
46     // the private member variables
47
private Context JavaDoc context = null;
48     private String JavaDoc jndiURL = null;
49     private Object JavaDoc ref = null;
50     private Map JavaDoc connectionMap = new ConcurrentHashMap JavaDoc();
51     
52     
53     /**
54      * Creates a new instance of RMIConnection
55      *
56      * @param context The context object responsible for returning information.
57      * @param jndiURL The url identifying the object.
58      */

59     public RMIConnection(Context JavaDoc context, String JavaDoc jndiURL) {
60         this.context = context;
61         this.jndiURL = jndiURL;
62     }
63     
64     
65     /**
66      * This object returns the connection to the object.
67      *
68      * @return The retrieve connection to the object.
69      * @param type The type of object to narrow.
70      * @exception ConnectionException
71      */

72     public Object JavaDoc getConnection(Class JavaDoc type) throws ConnectionException,
73             java.lang.ClassCastException JavaDoc {
74         try {
75             synchronized (type) {
76                 Object JavaDoc proxy = connectionMap.get(type);
77                 if (proxy != null) {
78                     return proxy;
79                 }
80                 
81                 // check the reference
82
Object JavaDoc ref = null;
83                 synchronized (this) {
84                     if (this.ref == null) {
85                         this.ref = context.lookup(jndiURL);
86                     }
87                     ref = this.ref;
88                 }
89                 Object JavaDoc reference = PortableRemoteObject.narrow(ref,type);
90                 proxy = createProxy(type, reference);
91                 connectionMap.put(type,proxy);
92                 return proxy;
93             }
94         } catch (javax.naming.NameNotFoundException JavaDoc ex) {
95             log.error("The name [" + jndiURL + "] is not found : " +
96                     ex.getMessage(),ex);
97             throw new com.rift.coad.util.connection.NameNotFound("The name [" +
98                     jndiURL + "] is not found : " + ex.getMessage(),ex);
99         } catch (java.lang.ClassCastException JavaDoc ex) {
100             log.error("Failed to cast object refered to by [" + jndiURL
101                     + "] to a [" + type.getName() + "] because : " +
102                     ex.getMessage(),ex);
103             throw ex;
104         } catch (Exception JavaDoc ex) {
105             log.error("Failed to make the connection : " +
106                     ex.getMessage(),ex);
107             throw new ConnectionException("Failed to make the connection : " +
108                     ex.getMessage(),ex);
109         }
110     }
111     
112     
113     /**
114      * This method returns true if the object being passed contains a local
115      * url.
116      *
117      * @return TRUE if a local URL, FALSE if not.
118      * @param jndiURL The string url to perform the check on.
119      */

120     public static boolean isRMIConnection(String JavaDoc jndiURL) {
121         if (jndiURL.trim().indexOf("java:comp") == -1) {
122             return true;
123         }
124         return false;
125     }
126     
127     
128     /**
129      * This method is called to invalidate a connection object.
130      */

131     public synchronized void invalidateConnection() {
132         log.debug("Calling clear");
133         ref = null;
134         connectionMap.clear();
135     }
136     
137     
138     /**
139      * This method creates the new proxy.
140      *
141      * @return The reference to the newly create proxy.
142      * @param type The type to create the proxy for.
143      * @param rmiRef The reference to object to make the call on.
144      */

145     private Object JavaDoc createProxy(Class JavaDoc type, Object JavaDoc rmiRef) {
146         ConnectionHandler handler = new ConnectionHandler(this,rmiRef);
147         return (Object JavaDoc)Proxy.newProxyInstance(
148                 type.getClassLoader(),
149                 new Class JavaDoc[] {type},handler);
150     }
151 }
152
Popular Tags