KickJava   Java API By Example, From Geeks To Geeks.

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


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

28 package org.objectweb.carol.jndi.spi;
29
30 import java.io.Serializable JavaDoc;
31 import java.rmi.Remote JavaDoc;
32
33 import javax.naming.Context JavaDoc;
34 import javax.naming.Name JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.naming.Reference JavaDoc;
37 import javax.naming.Referenceable JavaDoc;
38 import javax.naming.spi.ObjectFactory JavaDoc;
39 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
40
41 import org.omg.CORBA.ORB JavaDoc;
42 import org.omg.PortableServer.POA JavaDoc;
43
44 import org.objectweb.carol.jndi.ns.IIOPCosNaming;
45 import org.objectweb.carol.jndi.wrapping.JNDIReferenceWrapper;
46 import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource;
47 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper;
48 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
49 import org.objectweb.carol.util.configuration.ConfigurationRepository;
50
51 import com.sun.jndi.rmi.registry.RemoteReference;
52
53 /**
54  * @author Guillaume Riviere
55  * @author Florent Benoit (POA model, Inheritance refactoring)
56  */

57 public class IIOPContext extends AbsContext implements Context JavaDoc {
58
59     /**
60      * Root POA used by Carol
61      */

62     private static POA JavaDoc rootPOA = null;
63
64     /**
65      * Constructs an IIOP Wrapper context
66      * @param iiopCtx the inital IIOP context
67      * @throws NamingException if POA cannot be activated
68      */

69     public IIOPContext(Context JavaDoc iiopCtx) throws NamingException JavaDoc {
70         super(iiopCtx);
71
72         // Initialize ORB if null
73
ORB JavaDoc orb = IIOPCosNaming.getOrb();
74         if (rootPOA == null) {
75             try {
76                 rootPOA = org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
77             } catch (Exception JavaDoc e) {
78                 throw NamingExceptionHelper.create("Cannot get a single instance" + e.getMessage(), e);
79             }
80         }
81     }
82
83
84     /**
85      * If this object is a reference wrapper return the reference If this object
86      * is a resource wrapper return the resource
87      * @param o the object to resolve
88      * @param name name of the object to unwrap
89      * @return the unwrapped object
90      * @throws NamingException if the object cannot be unwraped
91      */

92     protected Object JavaDoc unwrapObject(Object JavaDoc o, Name JavaDoc name) throws NamingException JavaDoc {
93         try {
94             // Unwrap object if required.
95
if (o instanceof RemoteReference) {
96                 // build of the Referenceable object with is Reference
97
Reference JavaDoc objRef = ((RemoteReference) o).getReference();
98                 ObjectFactory JavaDoc objFact = (ObjectFactory JavaDoc) (Class.forName(objRef.getFactoryClassName())).newInstance();
99                 return objFact.getObjectInstance(objRef, name, this, getEnvironment());
100             } else if (o instanceof JNDIRemoteResource) {
101                 return ((JNDIRemoteResource) o).getResource();
102             } else {
103                 return o;
104             }
105         } catch (Exception JavaDoc e) {
106             throw NamingExceptionHelper.create("Cannot unwrap object '" + o + "' with name '" + name + "' :" + e.getMessage(), e);
107         }
108     }
109
110     /**
111      * Wrap an Object : If the object is a reference wrap it into a Reference
112      * Wrapper Object here the good way is to contact the carol configuration to
113      * get the portable remote object
114      * @param o the object to encode
115      * @param name of the object
116      * @param replace if the object need to be replaced
117      * @return a <code>Remote JNDIRemoteReference Object</code> if o is a
118      * resource o if else
119      * @throws NamingException if object cannot be wrapped
120      */

121     protected Object JavaDoc wrapObject(Object JavaDoc o, Name JavaDoc name, boolean replace) throws NamingException JavaDoc {
122         try {
123             Remote JavaDoc wrappedObject = null;
124
125             // Wrap object if required
126
if ((!(o instanceof Remote JavaDoc)) && (o instanceof Referenceable JavaDoc)) {
127                 wrappedObject = new JNDIReferenceWrapper(((Referenceable JavaDoc) o).getReference());
128             } else if ((!(o instanceof Remote JavaDoc)) && (o instanceof Reference JavaDoc)) {
129                 wrappedObject = new JNDIReferenceWrapper((Reference JavaDoc) o);
130             } else if ((!(o instanceof Remote JavaDoc)) && (o instanceof Serializable JavaDoc)) {
131                 wrappedObject = new JNDIResourceWrapper((Serializable JavaDoc) o);
132             } else {
133                 // return object directly as it is not wrapped.
134
return o;
135             }
136
137             // Object has been wrapped, need to export it
138
PortableRemoteObjectDelegate JavaDoc proDelegate = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject();
139             proDelegate.exportObject(wrappedObject);
140             Remote JavaDoc oldObj = (Remote JavaDoc) addToExported(name, wrappedObject);
141             if (oldObj != null) {
142                 if (replace) {
143                     proDelegate.unexportObject(oldObj);
144                 } else {
145                     proDelegate.unexportObject(wrappedObject);
146                     addToExported(name, oldObj);
147                     throw new NamingException JavaDoc("Object '" + o + "' with name '" + name + "' is already bind");
148                 }
149             }
150             return wrappedObject;
151         } catch (Exception JavaDoc e) {
152             throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : " + e.getMessage(), e);
153         }
154     }
155
156 }
Popular Tags