KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2004-2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
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: JacORBIIOPContextWrapperFactory.java,v 1.4 2005/03/14 10:48:17 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.jndi.spi;
26
27 import java.util.Hashtable JavaDoc;
28
29 import javax.naming.Context JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.naming.spi.InitialContextFactory JavaDoc;
32
33 import org.omg.CORBA.ORB JavaDoc;
34 import org.omg.PortableServer.POA JavaDoc;
35
36 import org.objectweb.carol.jndi.ns.JacORBCosNaming;
37 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
38 import org.objectweb.carol.util.configuration.CarolDefaultValues;
39 import org.objectweb.carol.util.csiv2.SasComponent;
40
41 /**
42  * Class <code> JacORBIIOPContextWrapperFactory </code> is the CAROL JNDI
43  * Context factory for JacORB. This context factory build the iiop context for
44  * reference wrapping to/from a remote object
45  * @author Florent BENOIT
46  * @see javax.naming.spi.InitialContextFactory
47  */

48 public class JacORBIIOPContextWrapperFactory extends AbsInitialContextFactory implements InitialContextFactory JavaDoc {
49
50     /**
51      * Object to use (specific POA) when using csiv2
52      */

53     public static final String JavaDoc SAS_COMPONENT = "org.objectweb.carol.util.csiv2.SasComponent";
54
55
56     /**
57      * Referencing factory
58      */

59     public static final String JavaDoc REFERENCING_FACTORY = "com.sun.jndi.cosnaming.CNCtxFactory";
60
61     /**
62      * @return the real factory of this wrapper
63      */

64     protected String JavaDoc getReferencingFactory() {
65         return REFERENCING_FACTORY;
66     }
67
68     /**
69      * @return class of the wrapper (to be instantiated + pool).
70      */

71     protected Class JavaDoc getWrapperClass() {
72         return JacORBIIOPContext.class;
73     }
74
75     /**
76      * Unique instance of the ORB running in the JVM
77      */

78     private static ORB JavaDoc orb = null;
79
80     /**
81      * The orb was started or not ?
82      */

83     private static boolean orbStarted = false;
84
85     /**
86      * Root POA used by Carol
87      */

88     private static POA JavaDoc rootPOA = null;
89
90     /**
91      * Store orb in the environment
92      * @param environment hashtable containing the environment
93      */

94     protected void addExtraConfInEnvironment(Hashtable JavaDoc environment) {
95         environment.put("java.naming.corba.orb", orb);
96     }
97
98     /**
99      * For some protocols, there are some initialization stuff to do
100      * @throws NamingException if there is an exception
101      */

102     protected void init() throws NamingException JavaDoc {
103         // Initialize ORB if null
104
if (orb == null) {
105             orb = JacORBCosNaming.getOrb();
106         }
107
108         if (!orbStarted && System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) {
109             // Start ORB if it was not run and if we are in server mode
110
new Thread JavaDoc(new Runnable JavaDoc() {
111
112                 public void run() {
113                     orb.run();
114                 }
115             }).start();
116             orbStarted = true;
117         }
118
119         if (rootPOA == null) {
120             try {
121                 rootPOA = org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
122                 rootPOA.the_POAManager().activate();
123             } catch (Exception JavaDoc e) {
124                 throw NamingExceptionHelper.create("Cannot get a single instance of rootPOA : " + e.getMessage(), e);
125             }
126         }
127
128     }
129
130
131     /**
132      * @param environment env to determine the key
133      * @return the key or null if we don't want to cache it
134      */

135     protected String JavaDoc getKey(Hashtable JavaDoc environment) {
136         String JavaDoc key = null;
137         SasComponent sasComponent = null;
138         if (environment != null) {
139             key = (String JavaDoc) environment.get(Context.PROVIDER_URL);
140             sasComponent = (SasComponent) environment.get(SAS_COMPONENT);
141         }
142         // there is a key (and need to cache) only if sas component is not present.
143
if (sasComponent == null) {
144             return key;
145         } else {
146             return null;
147         }
148     }
149 }
Popular Tags