KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
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: JRMPLocalContext.java,v 1.9 2005/03/15 14:40:30 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.jndi.spi;
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.Name JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.naming.Reference JavaDoc;
31 import javax.naming.spi.ObjectFactory JavaDoc;
32
33 import org.objectweb.carol.jndi.registry.RegistryWrapperContext;
34 import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource;
35 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
36
37 import com.sun.jndi.rmi.registry.RemoteReference;
38
39 /**
40  * Use the wrapper on registry object defined by RegistryWrapperContext class.
41  * This class has been refactored to split :
42  * <ul>
43  * <li>- wrapper on registry object</li>
44  * <li>- Single instance</li>
45  * <li>- Wrapping of Serializable/Referenceable/... objects</li>
46  * </ul>
47  * @author Florent Benoit
48  */

49 public class JRMPLocalContext extends JRMPContext implements Context JavaDoc {
50
51     /**
52      * Constructs an JRMP local Wrapper context
53      * @param jrmpLocalContext the inital Local JRMP context
54      * @throws NamingException if the registry wrapper cannot be build
55      */

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

68     protected Object JavaDoc unwrapObject(Object JavaDoc o, Name JavaDoc name) throws NamingException JavaDoc {
69         try {
70             if (o instanceof RemoteReference) {
71                 // build of the Referenceable object with is Reference
72
Reference JavaDoc objRef = ((RemoteReference) o).getReference();
73                 ObjectFactory JavaDoc objFact = (ObjectFactory JavaDoc) (Class.forName(objRef.getFactoryClassName())).newInstance();
74                 return objFact.getObjectInstance(objRef, name, this, getEnvironment());
75             } else if (o instanceof JNDIRemoteResource) {
76                 return ((JNDIRemoteResource) o).getResource();
77             } else {
78                 return o;
79             }
80         } catch (Exception JavaDoc e) {
81             throw NamingExceptionHelper.create("Cannot unwrap object '" + o + "' with name '" + name + "'.", e);
82         }
83     }
84
85 }
86
Popular Tags