1 22 package org.jboss.naming; 23 24 import java.io.InputStream ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.UndeclaredThrowableException ; 28 import java.net.UnknownHostException ; 29 import java.util.Collections ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.Hashtable ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.RefAddr ; 38 import javax.naming.Reference ; 39 import javax.naming.StringRefAddr ; 40 41 import org.jboss.invocation.Invocation; 42 import org.jboss.invocation.MarshalledInvocation; 43 import org.jboss.invocation.jrmp.server.JRMPProxyFactoryMBean; 44 import org.jboss.system.ServiceMBeanSupport; 45 import org.jboss.util.threadpool.ThreadPool; 46 import org.jboss.util.threadpool.BasicThreadPoolMBean; 47 import org.jnp.interfaces.Naming; 48 import org.jnp.interfaces.MarshalledValuePair; 49 import org.jnp.server.Main; 50 51 62 public class NamingService 63 extends ServiceMBeanSupport 64 implements NamingServiceMBean 65 { 66 67 private Main naming; 68 69 private Map marshalledInvocationMapping = new HashMap (); 70 71 private JRMPProxyFactoryMBean proxyFactory; 72 73 public NamingService() 74 { 75 naming = new Main(this.getClass().getName()); 76 } 77 78 84 public void setLookupPool(BasicThreadPoolMBean poolMBean) 85 { 86 ThreadPool lookupPool = poolMBean.getInstance(); 87 naming.setLookupPool(lookupPool); 88 } 89 90 96 public boolean getCallByValue() 97 { 98 return MarshalledValuePair.getEnableCallByReference() == false; 99 } 100 106 public void setCallByValue(boolean flag) 107 { 108 boolean callByValue = ! flag; 109 MarshalledValuePair.setEnableCallByReference(callByValue); 110 } 111 112 public void setPort(int port) 113 { 114 naming.setPort(port); 115 } 116 117 public int getPort() 118 { 119 return naming.getPort(); 120 } 121 122 public void setRmiPort(int port) 123 { 124 naming.setRmiPort(port); 125 } 126 127 public int getRmiPort() 128 { 129 return naming.getRmiPort(); 130 } 131 132 public String getBindAddress() 133 { 134 return naming.getBindAddress(); 135 } 136 137 public void setBindAddress(String host) throws UnknownHostException 138 { 139 naming.setBindAddress(host); 140 } 141 142 public String getRmiBindAddress() 143 { 144 return naming.getRmiBindAddress(); 145 } 146 147 public void setRmiBindAddress(String host) throws UnknownHostException 148 { 149 naming.setRmiBindAddress(host); 150 } 151 152 public int getBacklog() 153 { 154 return naming.getBacklog(); 155 } 156 157 public void setBacklog(int backlog) 158 { 159 naming.setBacklog(backlog); 160 } 161 162 public boolean getInstallGlobalService() 163 { 164 return naming.getInstallGlobalService(); 165 } 166 public void setInstallGlobalService(boolean flag) 167 { 168 naming.setInstallGlobalService(flag); 169 } 170 171 public String getClientSocketFactory() 172 { 173 return naming.getClientSocketFactory(); 174 } 175 176 public void setClientSocketFactory(String factoryClassName) 177 throws ClassNotFoundException , InstantiationException , IllegalAccessException 178 { 179 naming.setClientSocketFactory(factoryClassName); 180 } 181 182 public String getServerSocketFactory() 183 { 184 return naming.getServerSocketFactory(); 185 } 186 187 public void setServerSocketFactory(String factoryClassName) 188 throws ClassNotFoundException , InstantiationException , IllegalAccessException 189 { 190 naming.setServerSocketFactory(factoryClassName); 191 } 192 193 public void setJNPServerSocketFactory(String factoryClassName) 194 throws ClassNotFoundException , InstantiationException , IllegalAccessException 195 { 196 naming.setJNPServerSocketFactory(factoryClassName); 197 } 198 199 public void setInvokerProxyFactory(JRMPProxyFactoryMBean proxyFactory) 200 { 201 this.proxyFactory = proxyFactory; 202 } 203 204 public void createAlias(String fromName, String toName) throws Exception 205 { 206 Util.createLinkRef(fromName, toName); 207 log.info("Created alias " + fromName + "->" + toName); 208 } 209 210 public void removeAlias(String name) throws Exception 211 { 212 log.info("Removing alias " + name); 213 Util.removeLinkRef(name); 214 } 215 216 protected void startService() 217 throws Exception 218 { 219 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 221 InputStream is = loader.getResourceAsStream("jndi.properties"); 222 if (is == null) 223 throw new RuntimeException ("Cannot find jndi.properties, it should be at conf/jndi.properties by default."); 224 Properties props = new Properties (); 225 try 226 { 227 props.load(is); 228 } 229 finally 230 { 231 is.close(); 232 } 233 234 for (Enumeration keys = props.propertyNames(); keys.hasMoreElements(); ) 235 { 236 String key = (String ) keys.nextElement(); 237 String value = props.getProperty(key); 238 log.debug("System.setProperty, key="+key+", value="+value); 239 System.setProperty(key, value); 240 } 241 if( proxyFactory != null ) 242 naming.setNamingProxy(proxyFactory.getProxy()); 243 naming.start(); 244 245 249 InitialContext iniCtx = new InitialContext (); 250 Hashtable env = iniCtx.getEnvironment(); 251 log.debug("InitialContext Environment: "); 252 Object providerURL = null; 253 for (Enumeration keys = env.keys(); keys.hasMoreElements(); ) 254 { 255 Object key = keys.nextElement(); 256 Object value = env.get(key); 257 String type = value == null ? "" : value.getClass().getName(); 258 log.debug("key="+key+", value("+type+")="+value); 259 if( key.equals(Context.PROVIDER_URL) ) 260 providerURL = value; 261 } 262 if( providerURL != null ) 264 log.warn("Context.PROVIDER_URL in server jndi.properties, url="+providerURL); 265 266 270 ClassLoader topLoader = Thread.currentThread().getContextClassLoader(); 271 ENCFactory.setTopClassLoader(topLoader); 272 RefAddr refAddr = new StringRefAddr ("nns", "ENC"); 273 Reference envRef = new Reference ("javax.naming.Context", refAddr, ENCFactory.class.getName(), null); 274 Context ctx = (Context )iniCtx.lookup("java:"); 275 ctx.rebind("comp", envRef); 276 log.debug("Listening on port "+naming.getPort()); 277 ctx.close(); 278 iniCtx.close(); 279 280 HashMap tmpMap = new HashMap (13); 282 Method [] methods = Naming.class.getMethods(); 283 for(int m = 0; m < methods.length; m ++) 284 { 285 Method method = methods[m]; 286 Long hash = new Long (MarshalledInvocation.calculateHash(method)); 287 tmpMap.put(hash, method); 288 } 289 marshalledInvocationMapping = Collections.unmodifiableMap(tmpMap); 290 } 291 292 protected void stopService() 293 throws Exception 294 { 295 naming.stop(); 296 log.debug("JNP server stopped"); 297 } 298 299 308 protected Main getNamingServer() 309 { 310 return naming; 311 } 313 314 320 public Map getMethodMap() 321 { 322 return marshalledInvocationMapping; 323 } 324 325 334 public Object invoke(Invocation invocation) throws Exception 335 { 336 Naming theServer = naming.getServer(); 337 if (invocation instanceof MarshalledInvocation) 339 { 340 MarshalledInvocation mi = (MarshalledInvocation) invocation; 341 mi.setMethodMap(marshalledInvocationMapping); 342 } 343 Method method = invocation.getMethod(); 345 Object [] args = invocation.getArguments(); 346 Object value = null; 347 try 348 { 349 value = method.invoke(theServer, args); 350 } 351 catch(InvocationTargetException e) 352 { 353 Throwable t = e.getTargetException(); 354 if( t instanceof Exception ) 355 throw (Exception ) t; 356 else 357 throw new UndeclaredThrowableException (t, method.toString()); 358 } 359 360 return value; 361 } 362 } 363 | Popular Tags |