KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > Containers > ServantLocatorImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2001-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Mathieu Vadet.
23 Contributor(s): Philippe Merle, Romain Rouvoy, Sylvain Leblanc.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.Containers;
28
29 /**
30  * Customized implementation of a ServantLocator to manage component servants.
31  *
32  * @author <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
33  *
34  * @version 0.3
35  */

36
37 public class ServantLocatorImpl
38        extends org.objectweb.openccm.corba.ServantLocatorBase
39 {
40     // ==================================================================
41
//
42
// Internal state.
43
//
44
// ==================================================================
45

46     /**
47      **
48      **/

49     protected org.omg.PortableServer.POA JavaDoc poa_;
50
51     /**
52      **
53      **/

54     protected java.util.Random JavaDoc random_;
55
56     /**
57      **
58      **/

59     protected org.objectweb.ccm.util.WeakTable oid2cs_;
60
61     /**
62      **
63      **/

64     protected org.objectweb.ccm.util.WeakTable oid2uid_;
65
66     // ==================================================================
67
//
68
// Constructor.
69
//
70
// ==================================================================
71

72     /**
73      ** The constructor.
74      **
75      **/

76     public
77     ServantLocatorImpl()
78     {
79         poa_ = null;
80         oid2cs_ = new org.objectweb.ccm.util.WeakTable();
81         oid2uid_ = new org.objectweb.ccm.util.WeakTable();
82         random_ = new java.util.Random JavaDoc(java.lang.System.currentTimeMillis());
83     }
84
85     // ==================================================================
86
//
87
// Internal methods.
88
//
89
// ==================================================================
90

91     /**
92      **
93      **/

94     protected org.omg.CORBA.Object JavaDoc
95     createReference(HomeServant home_servant,
96                     org.omg.Components.EnterpriseComponent instance,
97                     org.omg.Components.PrimaryKeyBase key)
98     {
99         HomeExecutorBase home = home_servant.the_home_executor();
100         java.lang.String JavaDoc comp_uid = home._the_component_uid();
101         java.lang.String JavaDoc oid = null;
102         org.omg.CORBA.Object JavaDoc result = null;
103
104         //
105
// TODO : is 128 bits enough for an oid ????
106
//
107
java.math.BigInteger JavaDoc big = new java.math.BigInteger JavaDoc(128, random_);
108
109         // create reference for the component base facet.
110
oid = new java.lang.String JavaDoc(big.toByteArray());
111
112         try
113         {
114            result = poa_.create_reference_with_id(big.toByteArray(), comp_uid);
115            // For org.omg.PortableServer.POAPackage.WrongPolicy on VisiBroker 5
116
} catch(Exception JavaDoc ex) {
117             ex.printStackTrace(System.err);
118         }
119
120         ComponentServantImpl comp_servant = new ComponentServantImpl(this, home_servant, instance, key);
121
122         oid2uid_.put(oid, comp_uid);
123         oid2cs_.put(oid, comp_servant);
124
125         // must be done after the hashtable update
126
// because the narrow implies a preinvoke call to check _is_a
127
// one consequence is that the component will be activated but we don't want that
128
// so early (before a real business call)
129
//
130
// TODO : think about another solution to avoid this problem
131
// such as trapping _is_a call in preinvoke
132
//
133
comp_servant.setComponentRef(org.omg.Components.CCMObjectHelper.narrow(result));
134
135         return result;
136     }
137
138     // ==================================================================
139
//
140
// Public methods for the ServantLocatorOperations interface.
141
//
142
// ==================================================================
143

144     //
145
// IDL:omg.org/PortableServer/ServantLocator/preinvoke:1.0
146
//
147
/**
148      **
149      **/

150     public org.omg.PortableServer.Servant JavaDoc
151     preinvoke(byte[] oid,
152               org.omg.PortableServer.POA JavaDoc adapter,
153               java.lang.String JavaDoc operation,
154               org.omg.PortableServer.ServantLocatorPackage.CookieHolder JavaDoc the_cookie)
155     throws org.omg.PortableServer.ForwardRequest JavaDoc
156     {
157         java.lang.String JavaDoc soid = new java.lang.String JavaDoc(oid);
158         java.lang.String JavaDoc uid = (java.lang.String JavaDoc)oid2uid_.get(soid);
159         ComponentServant servant = (ComponentServant)oid2cs_.get(soid);
160
161         if (servant == null) return null;
162
163         if (servant.activation_context().state()==ActivationContext.DEACTIVATED)
164             servant.activation_context().state(ActivationContext.ACTIVATED);
165
166         return (org.omg.PortableServer.Servant JavaDoc)servant.get_native_interface(uid);
167     }
168
169     //
170
// IDL:omg.org/PortableServer/ServantLocator/postinvoke:1.0
171
//
172
/**
173      **
174      **/

175     public void
176     postinvoke(byte[] oid,
177               org.omg.PortableServer.POA JavaDoc adapter,
178               java.lang.String JavaDoc operation,
179               java.lang.Object JavaDoc the_cookie,
180               org.omg.PortableServer.Servant JavaDoc the_servant)
181     {
182             java.lang.String JavaDoc soid = new java.lang.String JavaDoc(oid);
183             java.lang.String JavaDoc uid = (java.lang.String JavaDoc)oid2uid_.get(soid);
184             ComponentServant servant = (ComponentServant)oid2cs_.get(soid);
185             
186         if (servant != null) {
187
188             servant.release_native_interface((Interceptor)the_servant);
189         }
190     }
191
192     // ==================================================================
193
//
194
// Public methods.
195
//
196
// ==================================================================
197

198     /**
199      **
200      **/

201     public void
202     setPOA(org.omg.PortableServer.POA JavaDoc poa)
203     {
204         poa_ = poa;
205     }
206
207     /**
208      **
209      **/

210     public org.omg.CORBA.Object JavaDoc
211     createPortReference(ComponentServant servant,
212                         java.lang.String JavaDoc port_uid)
213     {
214         HomeExecutorBase home = servant.the_home_servant().the_home_executor();
215         org.omg.CORBA.Object JavaDoc result = null;
216
217         //
218
// TODO : is 128 bits enough for an oid ????
219
//
220
java.math.BigInteger JavaDoc big = new java.math.BigInteger JavaDoc(128, random_);
221         java.lang.String JavaDoc oid = "";
222
223         // create reference for the port.
224
big = new java.math.BigInteger JavaDoc(128, random_);
225         oid = new java.lang.String JavaDoc(big.toByteArray());
226         oid2uid_.put(oid, port_uid);
227         oid2cs_.put(oid, servant);
228
229         try
230         {
231           result = poa_.create_reference_with_id(big.toByteArray(), home._get_port_type_uid(port_uid));
232           // For org.omg.PortableServer.POAPackage.WrongPolicy on VisiBroker 5
233
} catch(Exception JavaDoc ex) {
234             ex.printStackTrace(System.err);
235         }
236
237         return result;
238     }
239
240     /**
241      **
242      **/

243     public org.omg.CORBA.Object JavaDoc
244     createReference(HomeServant home_servant)
245     {
246         return createReference(home_servant, null, null);
247     }
248
249     /**
250      **
251      **/

252     public org.omg.CORBA.Object JavaDoc
253     createReference(HomeServant home_servant,
254                     org.omg.Components.EnterpriseComponent instance)
255     {
256         return createReference(home_servant, instance, null);
257     }
258
259     /**
260      **
261      **/

262     public org.omg.CORBA.Object JavaDoc
263     findReference(HomeServant home_servant,
264                   org.omg.Components.EnterpriseComponent instance)
265     {
266         ComponentServantImpl[] cs = null;
267         cs = (ComponentServantImpl[])oid2cs_.values().toArray(new ComponentServantImpl[0]);
268         org.omg.CORBA.Object JavaDoc result = null;
269         for (int i=0;i<cs.length;i++)
270         {
271             result = cs[i].getReferenceWithInstance(instance);
272             if (result!=null)
273                 break;
274         }
275
276         if (result==null)
277             result = createReference(home_servant, instance);
278
279         return result;
280     }
281
282     /**
283      **
284      **/

285     public org.omg.CORBA.Object JavaDoc
286     createReference(HomeServant home_servant,
287                     org.omg.Components.PrimaryKeyBase key)
288     {
289         return createReference(home_servant, null, key);
290     }
291
292     /**
293      ** Remove the reference associated with the CORBA Object.
294      **/

295     public void
296     removeReference(org.omg.CORBA.Object JavaDoc obj)
297         throws org.omg.Components.RemoveFailure
298     {
299         try {
300             String JavaDoc oid = new String JavaDoc(poa_.reference_to_id(obj));
301             oid2uid_.remove(oid);
302             oid2cs_.remove(oid);
303         } catch (org.omg.PortableServer.POAPackage.WrongAdapter JavaDoc ex) {
304             throw new org.omg.Components.RemoveFailure();
305         } catch (org.omg.PortableServer.POAPackage.WrongPolicy JavaDoc ex) {
306             throw new org.omg.Components.RemoveFailure();
307         }
308     }
309 }
310
Popular Tags