001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * NamingDirector.java
020: *
021: * This object is responsible for instanciating the naming server.
022: */
023:
024: // package path
025: package com.rift.coad.lib.naming;
026:
027: // java imports
028: import java.lang.reflect.Constructor;
029: import java.rmi.registry.LocateRegistry;
030: import java.rmi.registry.Registry;
031: import org.omg.CORBA.ORB;
032: import org.omg.PortableServer.POA;
033: import org.omg.PortableServer.POAHelper;
034: import javax.naming.Context;
035:
036: // logging import
037: import org.apache.log4j.Logger;
038:
039: // carol imports
040: import org.objectweb.carol.util.configuration.ConfigurationRepository;
041:
042: // coadunation imports
043: import com.rift.coad.lib.naming.*;
044: import com.rift.coad.lib.configuration.ConfigurationFactory;
045: import com.rift.coad.lib.configuration.Configuration;
046: import com.rift.coad.lib.thread.CoadunationThreadGroup;
047:
048: /**
049: * This object is responsible for instanciating the naming server.
050: *
051: * @author Brett Chaldecott
052: */
053: public class NamingDirector {
054:
055: // the class constants
056: private final static String ORB_MANAGER = "orb_manager";
057: private final static String NAMING_CONTEXT_MANAGER = "naming_context_manager";
058: private final static String INSTANCE_IDENTIFIER = "instance_identifier";
059: public final static String PRIMARY_URL = "primary_jndi_url";
060: public final static String PRIMARY = "primary";
061:
062: // the class log variable
063: protected Logger log = Logger.getLogger(NamingDirector.class
064: .getName());
065:
066: // The singleton reference
067: private static NamingDirector singleton = null;
068:
069: // private member variables
070: private String instanceId = null;
071: private String jndiBase = null;
072: private String primaryJNDIUrl = null;
073: private boolean primary = false;
074: private OrbManager orbManager = null;
075: private NamingContextManager namingContextManager = null;
076: private CoadunationThreadGroup threadGroup = null;
077:
078: /** Creates a new instance of NamingDirector */
079: private NamingDirector(CoadunationThreadGroup threadGroup)
080: throws NamingException {
081: try {
082: // init carol
083: ConfigurationRepository.init();
084: ConfigurationRepository
085: .addInterceptors("iiop",
086: "com.rift.coad.lib.interceptor.iiop.InterceptorIntializer");
087:
088: // the reference to the configuration class
089: Configuration config = ConfigurationFactory.getInstance()
090: .getConfig(this .getClass());
091:
092: // retrieve the instance identifier
093: instanceId = config.getString(INSTANCE_IDENTIFIER);
094: primary = config.getBoolean(PRIMARY);
095: primaryJNDIUrl = config.getString(PRIMARY_URL);
096:
097: if (primary) {
098: jndiBase = primaryJNDIUrl;
099: } else {
100: jndiBase = primaryJNDIUrl + "/"
101: + NamingConstants.SUBCONTEXT + "/" + instanceId;
102: }
103:
104: // init the orb manager
105: Class ref = Class.forName(config.getString(ORB_MANAGER));
106: Constructor orgManagerConstructor = ref
107: .getConstructor(threadGroup.getClass());
108: orbManager = (OrbManager) orgManagerConstructor
109: .newInstance(threadGroup);
110:
111: // init the naming context manager
112: ref = Class.forName(config
113: .getString(NAMING_CONTEXT_MANAGER));
114: Constructor namingConstructor = ref.getConstructor(
115: threadGroup.getClass(), OrbManager.class,
116: String.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 ex) {
127: log.error("Failed to start the naming director : "
128: + ex.getMessage(), ex);
129: throw new NamingException("Failed to init carol :"
130: + ex.getMessage(), ex);
131: }
132: }
133:
134: /**
135: * This method inits the naming director and starts carol.
136: */
137: public synchronized static NamingDirector init(
138: CoadunationThreadGroup threadGroup) throws NamingException {
139: if (singleton == null) {
140: singleton = new NamingDirector(threadGroup);
141: }
142: return singleton;
143: }
144:
145: /**
146: * This method returns a reference to the current instancance in memory.
147: *
148: * @exception Exception
149: */
150: public synchronized static NamingDirector getInstance()
151: throws NamingException {
152: if (singleton == null) {
153: throw new NamingException(
154: "The naming director has not been initialized.");
155: }
156: return singleton;
157: }
158:
159: /**
160: * This method returns the id for this coadunation instance.
161: *
162: * @return The string containing the id of this coadunation instance.
163: */
164: public String getInstanceId() {
165: return instanceId;
166: }
167:
168: /**
169: * This method returns true if this is a primary.
170: *
171: * @return TRUE if primary, FALSE if not.
172: */
173: public boolean isPrimary() {
174: return primary;
175: }
176:
177: /**
178: * This method returns the JNDI base for this instance.
179: *
180: * @return The string containing the jndi base for this instance.
181: */
182: public String getJNDIBase() {
183: return jndiBase;
184: }
185:
186: /**
187: * This method returns the primary JNDI url.
188: *
189: * @return The string containing the jndi base for this instance.
190: */
191: public String getPrimaryJNDIUrl() {
192: return primaryJNDIUrl;
193: }
194:
195: /**
196: * Retrieve a reference to the ORB.
197: *
198: * @return The reference to the orb.
199: */
200: public ORB getORB() {
201: return orbManager.getORB();
202: }
203:
204: /**
205: * Retrieve a reference to the POA.
206: *
207: * @return The reference to the POA.
208: */
209: public POA getPOA() {
210: return orbManager.getPOA();
211: }
212:
213: /**
214: * This method is called to init the context for a class loader.
215: */
216: public void initContext() throws NamingException {
217: namingContextManager.initContext();
218: }
219:
220: /**
221: * This method is called to release the context for class loader.
222: */
223: public void releaseContext() {
224: namingContextManager.releaseContext();
225: }
226:
227: /**
228: * This method is responsible for
229: */
230: public synchronized void shutdown() {
231: namingContextManager.shutdown();
232: orbManager.terminate();
233: }
234: }
|