KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > adapter > Adapter


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.adapter;
20
21 import gcc.rmi.iiop.RemoteInterface;
22
23 import java.util.HashMap;
24 import java.util.Vector;
25 import java.lang.reflect.Method;
26 import java.lang.reflect.InvocationTargetException;
27
28 public class Adapter
29 {
30     //
31
// Public Accessible Properties
32
//
33
protected String _bindName;
34     protected String _remoteClassName;
35     protected String _remoteInterfaceName;
36     protected Vector _idVector;
37     protected boolean _shared;
38     protected ClassLoader _cl;
39     protected RemoteInterface _ri;
40
41     //
42
// Internal Properrties
43
//
44

45     protected Object _sharedObject;
46     protected HashMap _objects;
47     protected Class _remoteClassClass;
48     protected Class _remoteInterfaceClass;
49
50     public Adapter()
51     {
52         _objects = new HashMap();
53         _idVector = new Vector();
54     }
55
56     /*
57      * BindName is the name that will be registered with the INS (Inter-operable Name Service)
58      */

59     public String getBindName()
60     {
61         return _bindName;
62     }
63
64     public void setBindName( String bindName )
65     {
66         _bindName = bindName;
67     }
68
69     /*
70      * Is this a shared component? If so this will invoke the getInstance method on
71      * the component ...
72      */

73     public boolean isShared()
74     {
75         return _shared;
76     }
77
78     public void setShared( boolean shared )
79     {
80         _shared = shared;
81     }
82
83     /*
84      * The classloader that will load any dependancies of the adapter or corba skel interfaces.
85      * Its should be set by the ejb container
86      */

87     public ClassLoader getClassLoader()
88     {
89         return _cl;
90     }
91
92     public void setClassLoader( ClassLoader cl )
93     {
94         _cl = cl;
95     }
96
97     /*
98      * This is the name of the remote class that implements the remote interface.
99      *
100      * This is only used if this adapter is going to directly invoke an object. For the
101      * EJB Container, the adapter will pass through the method invocations.
102      */

103     public String getRemoteClassName()
104     {
105         return _remoteClassName;
106     }
107
108     public void setRemoteClassName( String rcName )
109     {
110         _remoteClassName = rcName;
111     }
112
113     /*
114      * The remote interface name for the remote object. This will most likely be the name
115      * of the EJB's RemoteInterface and RemoteHomeInterface
116      *
117      * The stub/skel generator will use this interface name.
118      */

119     public String getRemoteInterfaceName()
120     {
121         return _remoteInterfaceName;
122     }
123
124     public void setRemoteInterfaceName( String riName )
125     {
126         _remoteInterfaceName = riName;
127     }
128
129     /*
130      * A list of public IDs that the remote object implements:
131      *
132      * IDL:....:1.0
133      * RMI:....:X:Y
134      */

135     public Vector getIds()
136     {
137         return _idVector;
138     }
139
140     public void addId( String id )
141     {
142         _idVector.add(id);
143     }
144
145     public void removeId( String id )
146     {
147         _idVector.remove( id );
148     }
149
150     /*
151      * Return the skeleton implemention for the remote interface. This interface has the
152      * invoke method to handle the rmi/iiop messages.
153      */

154     public RemoteInterface getRemoteInterface()
155     {
156         if (_ri == null)
157         {
158             synchronized( this )
159             {
160                 String riName = _remoteInterfaceName + "_Skeleton";
161                 _remoteInterfaceClass = loadClass( riName );
162
163                 try
164                 {
165                     _ri = (RemoteInterface)_remoteInterfaceClass.newInstance();
166                 }
167                 catch (InstantiationException e)
168                 {
169                     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
170
}
171                 catch (IllegalAccessException e)
172                 {
173                     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
174
}
175             }
176         }
177
178         return _ri;
179     }
180
181     /*
182      * Get an object instance to invoke based on the object key.
183      *
184      * The objectKey could probably be passed to the EJB container so that the
185      * container can directly invoke the ejb object as required.
186      */

187     public Object getInstance( byte[] objectKey )
188     {
189         String key = new String( objectKey );
190         return getInstance( key );
191     }
192
193     public Object getInstance( String key )
194     {
195         Object o = _objects.get( key );
196
197         if (o == null)
198         {
199             o = newInstance( key );
200         }
201
202         return o;
203     }
204
205     public Object newInstance( byte[] objectKey )
206     {
207         String key = new String( objectKey );
208         return newInstance( key );
209     }
210
211     public Object newInstance( String key )
212     {
213         Object o = null;
214
215         if (_remoteClassClass == null)
216         {
217             synchronized( this )
218             {
219                 _remoteClassClass = loadClass( _remoteClassName );
220             }
221
222             try
223             {
224                 if (_shared)
225                 {
226                     synchronized( this )
227                     {
228                         Method m = _remoteClassClass.getMethod( "getInstance", (Class[]) null );
229                         o = m.invoke( _remoteClassClass, (Object[]) null );
230
231                         if (o != null)
232                         {
233                             _objects.put( key, o );
234                         }
235                     }
236                 }
237                 else
238                 {
239                     o = _remoteClassClass.newInstance();
240                     _objects.put( key, o );
241                 }
242             }
243             catch (InstantiationException e)
244             {
245                 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
246
}
247             catch (IllegalAccessException e)
248             {
249                 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
250
}
251             catch( NoSuchMethodException e )
252             {
253                 e.printStackTrace();
254             }
255             catch( InvocationTargetException e )
256             {
257                 e.printStackTrace();
258             }
259         }
260
261         return o;
262     }
263
264     /*
265      * Invoke method from the IIOP Message Handler. The adapter is bound to the INS name service.
266      * When an RMI/IIOP message is processed by the server, the message handler will perform a lookup
267      * on the name service to get the Adapter, then the invocation will be passed to the adapter
268      * The adapter will obtain the object key and then determine which object instance to pass the
269      * invocation to.
270      */

271     public void invoke( java.lang.String methodName, byte[] objectKey, gcc.rmi.iiop.ObjectInputStream input, gcc.rmi.iiop.ObjectOutputStream output )
272     {
273         RemoteInterface skeleton = getRemoteInterface();
274         Object instance = getInstance( objectKey );
275
276         if (instance != null)
277         {
278             skeleton.$invoke( methodName, objectKey, instance, input, output );
279         }
280         else
281         {
282             throw new org.omg.CORBA.OBJECT_NOT_EXIST(new String(objectKey));
283         }
284     }
285
286     /*
287      * Helper function to load a class. This uses classloader for the adapter.
288      */

289     protected Class loadClass( String name )
290     {
291         Class c = null;
292
293         try
294         {
295             c = _cl.loadClass( name );
296         }
297         catch( Exception e )
298         {
299             e.printStackTrace();
300         }
301
302         return c;
303     }
304 }
305
Popular Tags