1 18 package org.apache.geronimo.interop.rmi.iiop.client; 19 20 import java.util.HashMap ; 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import javax.naming.Context ; 25 import javax.naming.Name ; 26 import javax.naming.NameNotFoundException ; 27 import javax.naming.NameParser ; 28 import javax.naming.NamingEnumeration ; 29 import javax.naming.NamingException ; 30 import javax.naming.OperationNotSupportedException ; 31 32 import org.apache.geronimo.interop.properties.IntProperty; 33 import org.apache.geronimo.interop.properties.PropertyMap; 34 import org.apache.geronimo.interop.properties.StringProperty; 35 import org.apache.geronimo.interop.properties.SystemProperties; 36 import org.apache.geronimo.interop.rmi.iiop.ObjectRef; 37 import org.apache.geronimo.interop.rmi.iiop.compiler.StubFactory; 38 import org.apache.geronimo.interop.util.ExceptionUtil; 39 import org.omg.CosNaming.NameComponent ; 40 import org.omg.CosNaming.NamingContext ; 41 import org.omg.CosNaming.NamingContextPackage.NotFound ; 42 43 public class ClientNamingContext implements Context , java.io.Serializable 44 { 45 46 public static ClientNamingContext getInstance(Hashtable env) 47 { 48 ClientNamingContext nc = (ClientNamingContext) contextMap.get(env); 49 if (nc == null) 50 { 51 synchronized (contextMap) 52 { 53 nc = (ClientNamingContext) contextMap.get(env); 54 if (nc == null) 55 { 56 nc = new ClientNamingContext(); 57 nc.init(env); 58 contextMap.put(env, nc); 59 } 60 } 61 } 62 return nc; 63 } 64 65 66 public static final IntProperty idleConnectionTimeoutProperty = 67 new IntProperty(SystemProperties.class, "idleConnectionTimeout") 68 .defaultValue(60); 70 public static final IntProperty lookupCacheTimeoutProperty = 71 new IntProperty(SystemProperties.class, "lookupCacheTimeout") 72 .defaultValue(600); 74 public static final StringProperty usernameSystemProperty = 75 new StringProperty(SystemProperties.class, "java.naming.security.principal"); 76 77 public static final StringProperty passwordSystemProperty = 78 new StringProperty(SystemProperties.class, "java.naming.security.credentials"); 79 80 private static long idleConnectionTimeout; 81 82 private static long lookupCacheTimeout; 83 84 private static int socketTimeout; 85 86 private static HashMap contextMap = new HashMap (); 87 88 private static HashMap hostListCache = new HashMap (); 89 90 92 private HashMap cache = new HashMap (); 93 94 private Hashtable env; 95 96 private ConnectionPool connectionPool; 97 98 private PropertyMap connectionProperties; 99 100 static private HashMap nameMap = new HashMap (); 101 102 private String username; 103 104 private String password; 105 106 private String namePrefix; 107 108 private NamingContext serverNamingContext; 109 110 public ConnectionPool getConnectionPool() { 111 return connectionPool; 112 } 113 114 public PropertyMap getConnectionProperties() 115 { 116 return connectionProperties; 117 } 118 119 public long getIdleConnectionTimeout() 120 { 121 return idleConnectionTimeout; 122 } 123 124 public String getUsername() 125 { 126 return username; 127 } 128 129 public String getPassword() 130 { 131 return password; 132 } 133 134 138 public Object lookup(Name name) throws NamingException { 139 return lookup(name.toString()); 140 } 141 142 public Object lookup(String name) throws NamingException { 143 if (name.startsWith("java:comp/env/")) { 144 name = name.substring(14); 145 } 146 147 String newName = (String ) nameMap.get(name); 148 if (newName != null) { 149 name = newName; 150 } 151 152 NameBinding nb = (NameBinding) cache.get(name); 153 if (nb == null) { 154 synchronized (cache) { 155 nb = (NameBinding) cache.get(name); 156 if (nb != null && nb.hasExpired()) { 157 cache.remove(name); 158 nb = null; 159 } 160 if (nb == null) { 161 nb = resolve(name); 162 cache.put(name, nb); 163 } 164 } 165 } 166 return nb.object; 167 } 168 169 public HostList lookupHost(String name) { 170 NameBinding nb = (NameBinding) hostListCache.get(name); 171 if (nb == null) { 172 synchronized (hostListCache) { 173 nb = (NameBinding) hostListCache.get(name); 174 if (nb != null && nb.hasExpired()) { 175 hostListCache.remove(name); 176 nb = null; 177 } 178 if (nb == null) { 179 hostListCache.put(name, nb); 180 } 181 } 182 } 183 return (HostList) nb.object; 184 } 185 186 public static void bind(String bindName, String name) throws NamingException { 187 nameMap.put(bindName, name); 188 } 189 190 public void bind(Name name, Object obj) throws NamingException { 191 throw new OperationNotSupportedException (); 192 } 193 194 public void bind(String name, Object obj) throws NamingException { 195 throw new OperationNotSupportedException (); 196 } 197 198 public void rebind(Name name, Object obj) throws NamingException { 199 throw new OperationNotSupportedException (); 200 } 201 202 public void rebind(String name, Object obj) throws NamingException { 203 throw new OperationNotSupportedException (); 204 } 205 206 public void unbind(Name name) throws NamingException { 207 throw new OperationNotSupportedException (); 208 } 209 210 public void unbind(String name) throws NamingException { 211 throw new OperationNotSupportedException (); 212 } 213 214 public void rename(Name oldName, Name newName) throws NamingException { 215 throw new OperationNotSupportedException (); 216 } 217 218 public void rename(String oldName, String newName) throws NamingException { 219 throw new OperationNotSupportedException (); 220 } 221 222 public NamingEnumeration list(Name name) throws NamingException { 223 throw new OperationNotSupportedException (); 224 } 225 226 public NamingEnumeration list(String name) throws NamingException { 227 throw new OperationNotSupportedException (); 228 } 229 230 public NamingEnumeration listBindings(Name name) throws NamingException { 231 throw new OperationNotSupportedException (); 232 } 233 234 public NamingEnumeration listBindings(String name) throws NamingException { 235 throw new OperationNotSupportedException (); 236 } 237 238 public void destroySubcontext(Name name) throws NamingException { 239 throw new OperationNotSupportedException (); 240 } 241 242 public void destroySubcontext(String name) throws NamingException { 243 throw new OperationNotSupportedException (); 244 } 245 246 public Context createSubcontext(Name name) throws NamingException { 247 throw new OperationNotSupportedException (); 248 } 249 250 public Context createSubcontext(String name) throws NamingException { 251 throw new OperationNotSupportedException (); 252 } 253 254 public Object lookupLink(Name name) throws NamingException { 255 throw new OperationNotSupportedException (); 256 } 257 258 public Object lookupLink(String name) throws NamingException { 259 throw new OperationNotSupportedException (); 260 } 261 262 public NameParser getNameParser(Name name) throws NamingException { 263 throw new OperationNotSupportedException (); 264 } 265 266 public NameParser getNameParser(String name) throws NamingException { 267 throw new OperationNotSupportedException (); 268 } 269 270 public Name composeName(Name name, Name prefix) throws NamingException { 271 throw new OperationNotSupportedException (); 272 } 273 274 public String composeName(String name, String prefix) throws NamingException { 275 throw new OperationNotSupportedException (); 276 } 277 278 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 279 throw new OperationNotSupportedException (); 280 } 281 282 public Object removeFromEnvironment(String propName) throws NamingException { 283 throw new OperationNotSupportedException (); 284 } 285 286 public Hashtable getEnvironment() throws NamingException { 287 throw new OperationNotSupportedException (); 288 } 289 290 public String getNameInNamespace() throws NamingException { 291 throw new OperationNotSupportedException (); 292 } 293 294 public void close() throws NamingException { 295 throw new OperationNotSupportedException (); 296 } 297 298 protected void init(Hashtable env) 299 { 300 this.env = env; 301 Object urlObject = env.get(Context.PROVIDER_URL); 302 if (urlObject == null) { 303 urlObject = SystemProperties.getInstance().getProperty("java.naming.provider.url", 304 "iiop://" + "delafran-t30" + ":2000"); 305 } 306 String url = urlObject.toString(); 307 UrlInfo urlInfo = UrlInfo.getInstance(url); 308 serverNamingContext = (NamingContext ) 309 StubFactory.getInstance().getStub(NamingContext .class); 310 311 namePrefix = urlInfo.getNamePrefix(); 312 313 ObjectRef ncRef = (ObjectRef) serverNamingContext; 314 ncRef.$setNamingContext(this); 315 ncRef.$setProtocol(urlInfo.getProtocol()); 316 ncRef.$setHost("ns~" + urlInfo.getHost()); 317 ncRef.$setPort(urlInfo.getPort()); 318 ncRef.$setObjectKey(urlInfo.getObjectKey()); 319 connectionPool = ConnectionPool.getInstance(this); 320 Object u = env.get(Context.SECURITY_PRINCIPAL); 321 Object p = env.get(Context.SECURITY_CREDENTIALS); 322 if (u == null) 323 { 324 u = usernameSystemProperty.getString(); 325 } 326 if (p == null) 327 { 328 p = passwordSystemProperty.getString(); 329 } 330 username = u != null ? u.toString() : null; 331 password = p != null ? p.toString() : null; 332 333 PropertyMap props = urlInfo.getProperties(); 334 props.putAll(env); 335 PropertyMap copyProps = new PropertyMap(); 336 copyProps.putAll(props); 337 for (Iterator i = copyProps.entrySet().iterator(); i.hasNext();) 338 { 339 Map.Entry entry = (Map.Entry )i.next(); 340 String property = (String )entry.getKey(); 341 Object value = entry.getValue(); 342 343 String startsWith = "org.apache.geronimo.interop.rmi."; 344 if (property.startsWith(startsWith)) 345 { 346 int replace = startsWith.length(); 347 props.remove(property); 348 props.put(property.substring(replace), value); 349 } 350 } 351 for (Iterator i = SystemProperties.getInstance().entrySet().iterator(); i.hasNext();) 352 { 353 Map.Entry entry = (Map.Entry )i.next(); 354 String property = (String )entry.getKey(); 355 Object value = entry.getValue(); 356 if (property.startsWith("djc.")) 357 { 358 props.put(property.substring(4), value); 359 } 360 } 361 connectionProperties = props; 362 idleConnectionTimeout = 1000 * idleConnectionTimeoutProperty.getInt(url, props); 363 lookupCacheTimeout = 1000 * lookupCacheTimeoutProperty.getInt(url, props); 364 } 365 366 protected NameBinding resolve(String name) throws NamingException 367 { 368 Object value = org.apache.geronimo.interop.naming.NameService.getInitialContext().lookupReturnNullIfNotFound(name); 369 if (value != null) 370 { 371 NameBinding nb = new NameBinding(); 372 nb.object = value; 373 nb.cacheTimeout = System.currentTimeMillis() + lookupCacheTimeout; 374 return nb; 375 } 376 try 377 { 378 NameComponent [] resolveName = 379 { new NameComponent (namePrefix + name, "") }; 380 org.omg.CORBA.Object object = serverNamingContext.resolve(resolveName); 381 NameBinding nb = new NameBinding(); 382 nb.object = object; 383 nb.cacheTimeout = System.currentTimeMillis() + lookupCacheTimeout; 384 return nb; 385 } 386 catch (NotFound notFound) 387 { 388 throw new NameNotFoundException (name); 389 } 390 catch (Exception ex) 391 { 392 throw new javax.naming.NamingException (ExceptionUtil.getStackTrace(ex)); 393 } 394 } 395 } 396 | Popular Tags |