KickJava   Java API By Example, From Geeks To Geeks.

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


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: IRMIContext.java,v 1.2 2005/05/16 08:51:12 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 import java.util.Properties JavaDoc;
33
34 import javax.naming.Context JavaDoc;
35 import javax.naming.Name JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.Reference JavaDoc;
38 import javax.naming.Referenceable JavaDoc;
39
40 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper;
41 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
42 import org.objectweb.carol.rmi.util.PortNumber;
43 import org.objectweb.carol.util.configuration.CarolDefaultValues;
44 import org.objectweb.carol.util.configuration.ConfigurationRepository;
45
46
47 /**
48  * @author Rafael H. Schloming <rhs@mit.edu>
49  */

50
51 public class IRMIContext extends AbsContext implements Context JavaDoc {
52
53     /**
54      * Constructs a IRMI Wrapper context
55      *
56      * @param context the inital context
57      */

58     public IRMIContext(Context JavaDoc context) {
59         super(context);
60     }
61
62     /**
63      * @return the object port used for exporting object
64      */

65     protected int getObjectPort() {
66         Properties JavaDoc prop = ConfigurationRepository.getProperties();
67         if (prop != null) {
68             String JavaDoc propertyName = CarolDefaultValues.SERVER_IRMI_PORT;
69             return PortNumber.strToint(prop.getProperty(propertyName, "0"), propertyName);
70         }
71         return 0;
72     }
73
74
75     /**
76      * If this object is a reference wrapper return the reference If
77      * this object is a resource wrapper return the resource
78      *
79      * @param o the object to resolve
80      * @param name name of the object to unwrap
81      * @return the unwrapped object
82      * @throws NamingException if the object cannot be unwraped
83      */

84     protected Object JavaDoc unwrapObject(Object JavaDoc o, Name JavaDoc name) throws NamingException JavaDoc {
85         Object JavaDoc result = super.defaultUnwrapObject(o, name);
86         if (result instanceof Reference JavaDoc) {
87             try {
88                 return javax.naming.spi.NamingManager.getObjectInstance
89                     (result, null, null, getEnvironment());
90             } catch (Exception JavaDoc e) {
91                 throw NamingExceptionHelper.create
92                     ("Cannot resolve reference", e);
93             }
94         } else {
95             return result;
96         }
97     }
98
99     /**
100      * Wrap an Object : If the object is a reference wrap it into a Reference
101      * Wrapper Object here the good way is to contact the carol configuration to
102      * get the portable remote object
103      * @param o the object to encode
104      * @param name of the object
105      * @param replace if the object need to be replaced
106      * @return a <code>Remote JNDIRemoteReference Object</code> if o is a
107      * resource o if else
108      * @throws NamingException if object cannot be wrapped
109      */

110     protected Object JavaDoc wrapObject(Object JavaDoc o, Name JavaDoc name, boolean replace) throws NamingException JavaDoc {
111         try {
112             javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc pro =
113                 ConfigurationRepository.getCurrentConfiguration().getProtocol()
114                 .getPortableRemoteObject();
115             if (!(o instanceof Remote JavaDoc)) {
116                 if (o instanceof Referenceable JavaDoc) {
117                     o = ((Referenceable JavaDoc) o).getReference();
118                 }
119                 o = new JNDIResourceWrapper((Serializable JavaDoc) o);
120                 pro.exportObject((Remote JavaDoc) o);
121                 Remote JavaDoc old = (Remote JavaDoc) addToExported(name, o);
122                 if (old != null) {
123                     if (replace) {
124                         pro.unexportObject(old);
125                     } else {
126                         pro.unexportObject((Remote JavaDoc) o);
127                         addToExported(name, old);
128                         throw new NamingException JavaDoc("Object '" + o + "' with name '" + name + "' is already bind");
129                     }
130                 }
131             }
132
133             if (o instanceof Remote JavaDoc) {
134                 o = pro.toStub((Remote JavaDoc) o);
135                 return o;
136             } else {
137                 return o;
138             }
139         } catch (java.rmi.RemoteException JavaDoc e) {
140             throw (NamingException JavaDoc) new NamingException JavaDoc().initCause(e);
141         }
142     }
143 }
144
Popular Tags