KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > spi > JacORBIIOPContext


1 /**
2  * Copyright (C) 2004-2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JacORBIIOPContext.java,v 1.8 2005/04/07 15:07:08 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.carol.jndi.spi;
27
28 import java.io.Serializable JavaDoc;
29 import java.rmi.Remote JavaDoc;
30
31 import javax.naming.CompositeName JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.Name JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.naming.Reference JavaDoc;
36 import javax.naming.Referenceable JavaDoc;
37 import javax.naming.spi.ObjectFactory JavaDoc;
38 import javax.rmi.PortableRemoteObject JavaDoc;
39 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
40 import javax.rmi.CORBA.Util JavaDoc;
41
42 import org.omg.CORBA.ORB JavaDoc;
43 import org.omg.CORBA.portable.ObjectImpl JavaDoc;
44 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
45 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
46 import org.omg.PortableServer.POA JavaDoc;
47
48 import org.objectweb.carol.jndi.ns.JacORBCosNaming;
49 import org.objectweb.carol.jndi.wrapping.JNDIReferenceWrapper;
50 import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource;
51 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper;
52 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
53 import org.objectweb.carol.util.configuration.ConfigurationRepository;
54 import org.objectweb.carol.util.csiv2.SasComponent;
55 import org.objectweb.carol.util.csiv2.SasPolicy;
56
57 import com.sun.jndi.rmi.registry.RemoteReference;
58
59 /**
60  * @author Florent Benoit
61  */

62 public class JacORBIIOPContext extends AbsContext implements Context JavaDoc {
63
64     /**
65      * Object to use (specific POA) when using csiv2
66      */

67     public static final String JavaDoc SAS_COMPONENT = "org.objectweb.carol.util.csiv2.SasComponent";
68
69     /**
70      * Number of POA policies
71      */

72     private static final int POA_POLICIES_NUMBER = 3;
73
74     /**
75      * Root POA used by Carol
76      */

77     private static POA JavaDoc rootPOA = null;
78
79     /**
80      * SAS parameters (use for policy)
81      */

82     private SasComponent sasComponent = null;
83
84     /**
85      * Constructs an IIOP Wrapper context for JacORB
86      * @param iiopCtx the inital IIOP context
87      * @throws NamingException if POA cannot be activated
88      */

89     public JacORBIIOPContext(Context JavaDoc iiopCtx) throws NamingException JavaDoc {
90         super(iiopCtx);
91         this.sasComponent = (SasComponent) iiopCtx.getEnvironment().get(SAS_COMPONENT);
92
93         ORB JavaDoc orb = JacORBCosNaming.getOrb();
94         if (rootPOA == null) {
95             try {
96                 rootPOA = org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
97             } catch (Exception JavaDoc e) {
98                 throw NamingExceptionHelper.create("Cannot get a single instance of rootPOA : " + e.getMessage(), e);
99             }
100         }
101
102     }
103
104     /**
105      * @return the rootPOA.
106      */

107     public static POA JavaDoc getRootPOA() {
108         return rootPOA;
109     }
110
111     /**
112      * If this object is a reference wrapper return the reference If this object
113      * is a resource wrapper return the resource
114      * @param o the object to resolve
115      * @param name name of the object to unwrap
116      * @return the unwrapped object
117      * @throws NamingException if the object cannot be unwraped
118      */

119     protected Object JavaDoc unwrapObject(Object JavaDoc o, Name JavaDoc name) throws NamingException JavaDoc {
120         try {
121             // Is it a remote reference object ?
122
ObjectImpl JavaDoc objImpl = (ObjectImpl JavaDoc) PortableRemoteObject.narrow(o, ObjectImpl JavaDoc.class);
123             String JavaDoc[] ids = objImpl._ids();
124             // first item
125
String JavaDoc itf = ids[0];
126
127             if (itf.indexOf(":com.sun.jndi.rmi.registry.RemoteReference:") != -1) {
128                 // build of the Referenceable object with is Reference
129
Reference JavaDoc objRef = ((RemoteReference) PortableRemoteObject.narrow(o, RemoteReference.class))
130                         .getReference();
131                 ObjectFactory JavaDoc objFact = (ObjectFactory JavaDoc) (Class.forName(objRef.getFactoryClassName())).newInstance();
132                 return objFact.getObjectInstance(objRef, name, this, getEnvironment());
133             } else if (itf.indexOf("RMI:org.objectweb.carol.jndi.wrapping.JNDIRemoteResource:") != -1) {
134                 // Cast
135
JNDIRemoteResource jndiRemoteResource = (JNDIRemoteResource) PortableRemoteObject.narrow(o,
136                         JNDIRemoteResource.class);
137                 return jndiRemoteResource.getResource();
138             } else {
139                 return o;
140             }
141         } catch (Exception JavaDoc e) {
142             throw NamingExceptionHelper.create("Cannot unwrap object '" + o + "' with name '" + name + "' :" + e.getMessage(), e);
143         }
144     }
145
146     /**
147      * Wrap an Object : If the object is a reference wrap it into a Reference
148      * Wrapper Object here the good way is to contact the carol configuration to
149      * get the portable remote object
150      * @param o the object to encode
151      * @param name of the object
152      * @param replace if the object need to be replaced
153      * @return a <code>Remote JNDIRemoteReference Object</code> if o is a
154      * resource o if else
155      * @throws NamingException if object cannot be wrapped
156      */

157     protected Object JavaDoc wrapObject(Object JavaDoc o, Name JavaDoc name, boolean replace) throws NamingException JavaDoc {
158         try {
159             Remote JavaDoc wrappedObject = null;
160
161             // Wrap object if required
162
if ((!(o instanceof Remote JavaDoc)) && (o instanceof Referenceable JavaDoc)) {
163                 wrappedObject = new JNDIReferenceWrapper(((Referenceable JavaDoc) o).getReference());
164             } else if ((!(o instanceof Remote JavaDoc)) && (o instanceof Reference JavaDoc)) {
165                 wrappedObject = new JNDIReferenceWrapper((Reference JavaDoc) o);
166             } else if ((!(o instanceof Remote JavaDoc)) && (o instanceof Serializable JavaDoc)) {
167                 wrappedObject = new JNDIResourceWrapper((Serializable JavaDoc) o);
168             } else {
169                 // return object directly as it is not wrapped.
170
return o;
171             }
172
173             // Object has been wrapped, need to export it
174
PortableRemoteObjectDelegate JavaDoc proDelegate = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject();
175             proDelegate.exportObject(wrappedObject);
176
177             Remote JavaDoc oldObj = (Remote JavaDoc) addToExported(name, wrappedObject);
178             if (oldObj != null) {
179                 if (replace) {
180                     proDelegate.unexportObject(oldObj);
181                 } else {
182                     proDelegate.unexportObject(wrappedObject);
183                     addToExported(name, oldObj);
184                     throw new NamingException JavaDoc("Object '" + o + "' with name '" + name + "' is already bind");
185                 }
186             }
187             return wrappedObject;
188         } catch (Exception JavaDoc e) {
189             throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : " + e.getMessage(), e);
190         }
191     }
192
193
194     /**
195      * Retrieves the named object.
196      * @param name the name of the object to look up
197      * @return the object bound to <tt>name</tt>
198      * @throws NamingException if a naming exception is encountered
199      */

200     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
201         try {
202             return lookup(new CompositeName JavaDoc(name));
203         } catch (RuntimeException JavaDoc re) {
204             throw NamingExceptionHelper.create("Cannot lookup object with name '" + name + " : " + re.getMessage() , re);
205         } catch (NamingException JavaDoc e) {
206             // Seems that message with JacORB is null many times
207
// So to avoid 'null' as message, use toString() method.
208
// Also, print the stacktrace with traces enabled.
209
String JavaDoc msg = e.getMessage();
210             if (msg == null) {
211                 msg = e.toString();
212             }
213             throw NamingExceptionHelper.create("Cannot lookup object with name '" + name + " : " + msg , e);
214         }
215     }
216
217     /**
218      * Binds a name to an object.
219      * @param name the name to bind; may not be empty
220      * @param obj the object to bind; possibly null
221      * @throws NamingException if a naming exception is encountered
222      */

223     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
224         try {
225             if (sasComponent != null) {
226                 Remote JavaDoc r = (Remote JavaDoc) wrapObject(obj, name, false);
227                 bindWithSpecificPoa(name, r);
228             } else {
229                 super.bind(name, obj);
230             }
231         } catch (Exception JavaDoc e) {
232             throw NamingExceptionHelper.create("Cannot bind object '" + obj + "' with name '" + name + "' :" + e.getMessage(), e);
233         }
234
235     }
236
237
238     /**
239      * Binds a name to an object, overwriting any existing binding. All
240      * intermediate contexts and the target context (that named by all but
241      * terminal atomic component of the name) must already exist.
242      * @param name the name to bind; may not be empty
243      * @param obj the object to bind; possibly null
244      * @throws NamingException if a naming exception is encountered
245      */

246     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
247         try {
248             if (sasComponent != null) {
249                 Remote JavaDoc r = (Remote JavaDoc) wrapObject(obj, name, true);
250                 rebindWithSpecificPoa(name, r);
251             } else {
252                 super.rebind(name, obj);
253             }
254         } catch (Exception JavaDoc e) {
255             throw NamingExceptionHelper.create("Cannot rebind object '" + obj + "' with name '" + name + "' :" + e.getMessage(), e);
256         }
257     }
258
259     /**
260      * Rebind an object by using a secure POA (csiv2)
261      * @param name name of the object
262      * @param r remote object to bind
263      * @throws Exception if the object cannot be bound
264      */

265     private void rebindWithSpecificPoa(Name JavaDoc name, Remote JavaDoc r) throws Exception JavaDoc {
266         POA JavaDoc securedPOA = createSecurePOA(name.toString());
267         org.omg.PortableServer.Servant JavaDoc servant = (org.omg.PortableServer.Servant JavaDoc) Util.getTie(r);
268         securedPOA.activate_object_with_id(name.toString().getBytes(), servant);
269         getWrappedContext().rebind(name, securedPOA.servant_to_reference(servant));
270     }
271
272     /**
273      * Bind an object by using a secure POA (csiv2)
274      * @param name name of the object
275      * @param r remote object to bind
276      * @throws Exception if the object cannot be bound
277      */

278     private void bindWithSpecificPoa(Name JavaDoc name, Remote JavaDoc r) throws Exception JavaDoc {
279         POA JavaDoc securedPOA = createSecurePOA(name.toString());
280         org.omg.PortableServer.Servant JavaDoc servant = (org.omg.PortableServer.Servant JavaDoc) Util.getTie(r);
281         securedPOA.activate_object_with_id(name.toString().getBytes(), servant);
282         getWrappedContext().bind(name, securedPOA.servant_to_reference(servant));
283     }
284
285     /**
286      * Build a new POA (with csiv2 policy)
287      * @param nameId name for POA
288      * @return a POA
289      * @throws Exception if the POA cannot be created
290      */

291     private POA JavaDoc createSecurePOA(String JavaDoc nameId) throws Exception JavaDoc {
292
293         //TODO : Detect if a POA with this name already exists and avoid to
294
// create it.
295
// use random for now
296

297         // Create policies
298
org.omg.CORBA.Policy JavaDoc[] policies = new org.omg.CORBA.Policy JavaDoc[POA_POLICIES_NUMBER];
299         policies[0] = rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
300         policies[1] = rootPOA.create_lifespan_policy(LifespanPolicyValue.TRANSIENT);
301         policies[2] = new SasPolicy(sasComponent);
302         return rootPOA.create_POA(nameId + Math.random(), rootPOA.the_POAManager(), policies);
303     }
304
305 }
Popular Tags