KickJava   Java API By Example, From Geeks To Geeks.

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


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: JEREMIEContext.java,v 1.11 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.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
39
40 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper;
41 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
42 import org.objectweb.carol.util.configuration.ConfigurationRepository;
43
44 /**
45  * @author Guillaume Riviere
46  * @author Florent Benoit
47  */

48 public class JEREMIEContext extends AbsContext implements Context JavaDoc {
49
50     /**
51      * Constructs an JEREMIE Wrapper context
52      * @param jeremieCtx the inital JEREMIE context
53      */

54     public JEREMIEContext(Context JavaDoc jeremieCtx) {
55         super(jeremieCtx);
56     }
57
58
59     /**
60      * If this object is a reference wrapper return the reference If this object
61      * is a resource wrapper return the resource
62      * @param o the object to resolve
63      * @param name name of the object to unwrap
64      * @return the unwrapped object
65      * @throws NamingException if the object cannot be unwraped
66      */

67     protected Object JavaDoc unwrapObject(Object JavaDoc o, Name JavaDoc name) throws NamingException JavaDoc {
68         return super.defaultUnwrapObject(o, name);
69     }
70
71     /**
72      * Wrap an Object : If the object is a reference wrap it into a Reference
73      * Wrapper Object here the good way is to contact the carol configuration to
74      * get the portable remote object
75      * @param o the object to encode
76      * @param name of the object
77      * @param replace if the object need to be replaced
78      * @return a <code>Remote JNDIRemoteReference Object</code> if o is a
79      * resource o if else
80      * @throws NamingException if object cannot be wrapped
81      */

82     protected Object JavaDoc wrapObject(Object JavaDoc o, Name JavaDoc name, boolean replace) throws NamingException JavaDoc {
83         try {
84             if ((!(o instanceof Remote JavaDoc)) && (!(o instanceof Referenceable JavaDoc)) && (!(o instanceof Reference JavaDoc))
85                     && (o instanceof Serializable JavaDoc)) {
86                 // Only Serializable (not implementing Remote or Referenceable or
87
// Reference)
88
JNDIResourceWrapper irw = new JNDIResourceWrapper((Serializable JavaDoc) o);
89                 PortableRemoteObjectDelegate JavaDoc proDelegate = ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject();
90                 proDelegate.exportObject(irw);
91
92                 Remote JavaDoc oldObj = (Remote JavaDoc) addToExported(name, irw);
93                 if (oldObj != null) {
94                     if (replace) {
95                         proDelegate.unexportObject(oldObj);
96                     } else {
97                         proDelegate.unexportObject(irw);
98                         addToExported(name, oldObj);
99                         throw new NamingException JavaDoc("Object '" + o + "' with name '" + name + "' is already bind");
100                     }
101                 }
102                 return irw;
103             } else {
104                 return o;
105             }
106         } catch (Exception JavaDoc e) {
107             throw NamingExceptionHelper.create("Cannot wrap object '" + o + "' with name '" + name + "' : "
108                     + e.getMessage(), e);
109         }
110
111     }
112 }
Popular Tags