KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > naming > NamingDirector


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * NamingDirector.java
20  *
21  * This object is responsible for instanciating the naming server.
22  */

23
24 // package path
25
package com.rift.coad.lib.naming;
26
27 // java imports
28
import java.lang.reflect.Constructor JavaDoc;
29 import java.rmi.registry.LocateRegistry JavaDoc;
30 import java.rmi.registry.Registry JavaDoc;
31 import org.omg.CORBA.ORB JavaDoc;
32 import org.omg.PortableServer.POA JavaDoc;
33 import org.omg.PortableServer.POAHelper JavaDoc;
34 import javax.naming.Context JavaDoc;
35
36 // logging import
37
import org.apache.log4j.Logger;
38
39 // carol imports
40
import com.rift.coad.lib.naming.*;
41 import org.objectweb.carol.util.configuration.ConfigurationRepository;
42
43 // coadunation imports
44
import com.rift.coad.lib.configuration.ConfigurationFactory;
45 import com.rift.coad.lib.configuration.Configuration;
46 import com.rift.coad.lib.thread.CoadunationThreadGroup;
47
48 /**
49  * This object is responsible for instanciating the naming server.
50  *
51  * @author Brett Chaldecott
52  */

53 public class NamingDirector {
54     
55     // the class constants
56
private final static String JavaDoc ORB_MANAGER = "orb_manager";
57     private final static String JavaDoc NAMING_CONTEXT_MANAGER =
58             "naming_context_manager";
59     private final static String JavaDoc INSTANCE_IDENTIFIER =
60             "instance_identifier";
61     public final static String JavaDoc PRIMARY_URL = "primary_jndi_url";
62     public final static String JavaDoc PRIMARY = "primary";
63     
64     
65     // the class log variable
66
protected Logger log =
67         Logger.getLogger(NamingDirector.class.getName());
68     
69     // The singleton reference
70
private static NamingDirector singleton = null;
71     
72     // private member variables
73
private String JavaDoc instanceId = null;
74     private String JavaDoc jndiBase = null;
75     private String JavaDoc primaryJNDIUrl = null;
76     private boolean primary = false;
77     private OrbManager orbManager = null;
78     private NamingContextManager namingContextManager = null;
79     private CoadunationThreadGroup threadGroup = null;
80     
81     /** Creates a new instance of NamingDirector */
82     private NamingDirector(CoadunationThreadGroup threadGroup) throws
83             NamingException {
84         try {
85             // init carol
86
ConfigurationRepository.init();
87             ConfigurationRepository.addInterceptors("iiop",
88                     "com.rift.coad.lib.interceptor.iiop.InterceptorIntializer");
89             
90             // the reference to the configuration class
91
Configuration config = ConfigurationFactory.getInstance().getConfig(
92                     this.getClass());
93             
94             // retrieve the instance identifier
95
instanceId = config.getString(INSTANCE_IDENTIFIER);
96             primary = config.getBoolean(PRIMARY);
97             primaryJNDIUrl = config.getString(PRIMARY_URL);
98             
99             if (primary){
100                 jndiBase = primaryJNDIUrl;
101             } else {
102                 jndiBase = primaryJNDIUrl + "/" + NamingConstants.SUBCONTEXT
103                         + "/" + instanceId;
104             }
105             
106             // init the orb manager
107
Class JavaDoc ref = Class.forName(config.getString(ORB_MANAGER));
108             Constructor JavaDoc orgManagerConstructor = ref.getConstructor(
109                     threadGroup.getClass());
110             orbManager = (OrbManager)orgManagerConstructor.newInstance(
111                     threadGroup);
112             
113             // init the naming context manager
114
ref = Class.forName(config.getString(NAMING_CONTEXT_MANAGER));
115             Constructor JavaDoc namingConstructor = ref.getConstructor(
116                     threadGroup.getClass(),OrbManager.class,String JavaDoc.class);
117             namingContextManager = (NamingContextManager)namingConstructor.
118                     newInstance(threadGroup,orbManager,instanceId);
119             
120             // set the initial context factory.
121
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
122                     namingContextManager.getInitialContextFactory());
123             // set the initial context factory.
124
System.setProperty(Context.URL_PKG_PREFIXES,
125                     namingContextManager.getURLContextFactory());
126         } catch (Exception JavaDoc ex) {
127             log.error("Failed to start the naming director : " + ex.getMessage(),ex);
128             throw new NamingException("Failed to init carol :" + ex.getMessage(),
129                     ex);
130         }
131     }
132     
133     
134     /**
135      * This method inits the naming director and starts carol.
136      */

137     public synchronized static NamingDirector init(CoadunationThreadGroup
138             threadGroup) throws NamingException {
139         if (singleton == null) {
140             singleton = new NamingDirector(threadGroup);
141         }
142         return singleton;
143     }
144     
145     
146     /**
147      * This method returns a reference to the current instancance in memory.
148      *
149      * @exception Exception
150      */

151     public synchronized static NamingDirector getInstance() throws
152             NamingException {
153         if (singleton == null) {
154             throw new NamingException(
155                     "The naming director has not been initialized.");
156         }
157         return singleton;
158     }
159     
160     
161     /**
162      * This method returns the id for this coadunation instance.
163      *
164      * @return The string containing the id of this coadunation instance.
165      */

166     public String JavaDoc getInstanceId() {
167         return instanceId;
168     }
169     
170     
171     /**
172      * This method returns true if this is a primary.
173      *
174      * @return TRUE if primary, FALSE if not.
175      */

176     public boolean isPrimary() {
177         return primary;
178     }
179     
180     
181     /**
182      * This method returns the JNDI base for this instance.
183      *
184      * @return The string containing the jndi base for this instance.
185      */

186     public String JavaDoc getJNDIBase() {
187         return jndiBase;
188     }
189     
190     
191     /**
192      * This method returns the primary JNDI url.
193      *
194      * @return The string containing the jndi base for this instance.
195      */

196     public String JavaDoc getPrimaryJNDIUrl() {
197         return primaryJNDIUrl;
198     }
199     
200     
201     /**
202      * Retrieve a reference to the ORB.
203      *
204      * @return The reference to the orb.
205      */

206     public ORB JavaDoc getORB() {
207         return orbManager.getORB();
208     }
209     
210     
211     /**
212      * Retrieve a reference to the POA.
213      *
214      * @return The reference to the POA.
215      */

216     public POA JavaDoc getPOA() {
217         return orbManager.getPOA();
218     }
219     
220     
221     /**
222      * This method is called to init the context for a class loader.
223      */

224     public void initContext() throws NamingException {
225         namingContextManager.initContext();
226     }
227     
228     
229     /**
230      * This method is called to release the context for class loader.
231      */

232     public void releaseContext() {
233         namingContextManager.releaseContext();
234     }
235     
236     
237     /**
238      * This method is responsible for
239      */

240     public synchronized void shutdown() {
241         namingContextManager.shutdown();
242         orbManager.terminate();
243     }
244 }
245
Popular Tags