1 23 24 package com.rift.coad.lib.naming; 26 27 import java.lang.reflect.Constructor ; 29 import java.rmi.registry.LocateRegistry ; 30 import java.rmi.registry.Registry ; 31 import org.omg.CORBA.ORB ; 32 import org.omg.PortableServer.POA ; 33 import org.omg.PortableServer.POAHelper ; 34 import javax.naming.Context ; 35 36 import org.apache.log4j.Logger; 38 39 import com.rift.coad.lib.naming.*; 41 import org.objectweb.carol.util.configuration.ConfigurationRepository; 42 43 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 53 public class NamingDirector { 54 55 private final static String ORB_MANAGER = "orb_manager"; 57 private final static String NAMING_CONTEXT_MANAGER = 58 "naming_context_manager"; 59 private final static String INSTANCE_IDENTIFIER = 60 "instance_identifier"; 61 public final static String PRIMARY_URL = "primary_jndi_url"; 62 public final static String PRIMARY = "primary"; 63 64 65 protected Logger log = 67 Logger.getLogger(NamingDirector.class.getName()); 68 69 private static NamingDirector singleton = null; 71 72 private String instanceId = null; 74 private String jndiBase = null; 75 private String primaryJNDIUrl = null; 76 private boolean primary = false; 77 private OrbManager orbManager = null; 78 private NamingContextManager namingContextManager = null; 79 private CoadunationThreadGroup threadGroup = null; 80 81 82 private NamingDirector(CoadunationThreadGroup threadGroup) throws 83 NamingException { 84 try { 85 ConfigurationRepository.init(); 87 ConfigurationRepository.addInterceptors("iiop", 88 "com.rift.coad.lib.interceptor.iiop.InterceptorIntializer"); 89 90 Configuration config = ConfigurationFactory.getInstance().getConfig( 92 this.getClass()); 93 94 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 Class ref = Class.forName(config.getString(ORB_MANAGER)); 108 Constructor orgManagerConstructor = ref.getConstructor( 109 threadGroup.getClass()); 110 orbManager = (OrbManager)orgManagerConstructor.newInstance( 111 threadGroup); 112 113 ref = Class.forName(config.getString(NAMING_CONTEXT_MANAGER)); 115 Constructor namingConstructor = ref.getConstructor( 116 threadGroup.getClass(),OrbManager.class,String .class); 117 namingContextManager = (NamingContextManager)namingConstructor. 118 newInstance(threadGroup,orbManager,instanceId); 119 120 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, 122 namingContextManager.getInitialContextFactory()); 123 System.setProperty(Context.URL_PKG_PREFIXES, 125 namingContextManager.getURLContextFactory()); 126 } catch (Exception 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 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 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 166 public String getInstanceId() { 167 return instanceId; 168 } 169 170 171 176 public boolean isPrimary() { 177 return primary; 178 } 179 180 181 186 public String getJNDIBase() { 187 return jndiBase; 188 } 189 190 191 196 public String getPrimaryJNDIUrl() { 197 return primaryJNDIUrl; 198 } 199 200 201 206 public ORB getORB() { 207 return orbManager.getORB(); 208 } 209 210 211 216 public POA getPOA() { 217 return orbManager.getPOA(); 218 } 219 220 221 224 public void initContext() throws NamingException { 225 namingContextManager.initContext(); 226 } 227 228 229 232 public void releaseContext() { 233 namingContextManager.releaseContext(); 234 } 235 236 237 240 public synchronized void shutdown() { 241 namingContextManager.shutdown(); 242 orbManager.terminate(); 243 } 244 } 245 | Popular Tags |