KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2002,2004 - 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: JRMPContext.java,v 1.9 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 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 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
40
41 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper;
42 import org.objectweb.carol.jndi.wrapping.UnicastJNDIReferenceWrapper;
43 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
44 import org.objectweb.carol.rmi.util.PortNumber;
45 import org.objectweb.carol.util.configuration.CarolDefaultValues;
46 import org.objectweb.carol.util.configuration.ConfigurationRepository;
47
48 /**
49  * @author Guillaume Riviere
50  * @author Florent Benoit
51  */

52 public class JRMPContext extends AbsContext implements Context JavaDoc {
53
54     /**
55      * Constructs an JRMP Wrapper context
56      * @param jrmpContext the inital JRMP context
57      */

58     public JRMPContext(Context JavaDoc jrmpContext) {
59         super(jrmpContext);
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_JRMP_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 this object
77      * is a resource wrapper return the resource
78      * @param o the object to resolve
79      * @param name name of the object to unwrap
80      * @return the unwrapped object
81      * @throws NamingException if the object cannot be unwraped
82      */

83     protected Object JavaDoc unwrapObject(Object JavaDoc o, Name JavaDoc name) throws NamingException JavaDoc {
84         return super.defaultUnwrapObject(o, name);
85     }
86
87     /**
88      * Wrap an Object : If the object is a reference wrap it into a Reference
89      * Wrapper Object here the good way is to contact the carol configuration to
90      * get the portable remote object
91      * @param o the object to encode
92      * @param name of the object
93      * @param replace if the object need to be replaced
94      * @return a <code>Remote JNDIRemoteReference Object</code> if o is a
95      * resource o if else
96      * @throws NamingException if object cannot be wrapped
97      */

98     protected Object JavaDoc wrapObject(Object JavaDoc o, Name JavaDoc name, boolean replace) throws NamingException JavaDoc {
99             try {
100                 // Add wrapper for the two first cases. Then it will use PortableRemoteObject instead of UnicastRemoteObject
101
// and when fixing JRMP exported objects port, it use JRMPProdelegate which is OK.
102
if ((!(o instanceof Remote JavaDoc)) && (o instanceof Referenceable JavaDoc)) {
103                     return new UnicastJNDIReferenceWrapper(((Referenceable JavaDoc) o).getReference(), getObjectPort());
104                 } else if ((!(o instanceof Remote JavaDoc)) && (o instanceof Reference JavaDoc)) {
105                     return new UnicastJNDIReferenceWrapper((Reference JavaDoc) o, getObjectPort());
106                 } else if ((!(o instanceof Remote JavaDoc)) && (!(o instanceof Referenceable JavaDoc)) && (!(o instanceof Reference JavaDoc))
107                         && (o instanceof Serializable JavaDoc)) {
108                     // Only Serializable (not implementing Remote or Referenceable or
109
// Reference)
110
JNDIResourceWrapper irw = new JNDIResourceWrapper((Serializable JavaDoc) o);
111                     PortableRemoteObjectDelegate JavaDoc proDelegate = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject();
112                     proDelegate.exportObject(irw);
113
114                     Remote JavaDoc oldObj = (Remote JavaDoc) addToExported(name, irw);
115                     if (oldObj != null) {
116                         if (replace) {
117                             proDelegate.unexportObject(oldObj);
118                         } else {
119                             proDelegate.unexportObject(irw);
120                             addToExported(name, oldObj);
121                             throw new NamingException JavaDoc("Object '" + o + "' with name '" + name + "' is already bind");
122                         }
123                     }
124                     return irw;
125                 } else {
126                     return o;
127                 }
128             } catch (Exception JavaDoc e) {
129                 throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : "
130                         + e.getMessage(), e);
131             }
132     }
133 }
Popular Tags