1 45 46 47 package org.openejb.core.ivm.naming; 48 49 50 import java.io.ObjectStreamException ; 51 import java.util.Enumeration ; 52 import java.util.HashMap ; 53 import java.util.HashSet ; 54 import java.util.Hashtable ; 55 import java.util.Set ; 56 import java.util.StringTokenizer ; 57 import java.util.Vector ; 58 59 import javax.naming.Binding ; 60 import javax.naming.CompositeName ; 61 import javax.naming.Context ; 62 import javax.naming.Name ; 63 import javax.naming.NameClassPair ; 64 import javax.naming.NameNotFoundException ; 65 import javax.naming.NameParser ; 66 import javax.naming.NamingEnumeration ; 67 import javax.naming.NamingException ; 68 import javax.naming.spi.ObjectFactory ; 69 70 import org.openejb.core.ThreadContext; 71 import org.openejb.OpenEJB; 72 73 import com.sun.naming.internal.ResourceManager; 74 75 80 public class IvmContext implements Context , java.io.Serializable { 81 Hashtable myEnv; 82 boolean readOnly = false; 83 HashMap fastCache = new HashMap (); 84 public NameNode mynode; 85 86 89 public static IvmContext createRootContext() { 90 return new IvmContext(new NameNode(null,new ParsedName("/"),null)); 91 } 92 93 public IvmContext(){ 94 this(new NameNode(null, new ParsedName("root"), null)); 95 } 96 public IvmContext(NameNode node){ 97 mynode = node; 98 } 99 public IvmContext(Hashtable environment) throws NamingException{ 100 this(); 101 if(environment ==null) 102 throw new NamingException("Invalid Argument"); 103 else 104 myEnv = (Hashtable )environment.clone(); 105 106 } 107 public Object lookup(String compositName) throws NamingException { 108 if (compositName.equals("")) { 109 return this; 110 } 111 112 if ( compositName.equals("java:comp/UserTransaction") && ThreadContext.getThreadContext().getDeploymentInfo() == null ) { 115 return new org.openejb.core.CoreUserTransaction(); 116 } 117 118 119 String compoundName = null; 120 int indx = compositName.indexOf(":"); 121 if(indx>-1){ 122 126 compoundName = compositName.substring(indx+1); 127 }else{ 128 133 compoundName = mynode.atomicName+'/'+compositName; 134 } 135 136 141 Object obj = fastCache.get(compoundName); 142 if(obj==null){ 143 try{ 145 obj = mynode.resolve(new ParsedName(compoundName)); 146 }catch(NameNotFoundException nnfe){ 147 obj = federate(compositName); 148 } 149 fastCache.put(compoundName,obj); 151 } 152 if(obj.getClass() == IvmContext.class) 153 ((IvmContext)obj).myEnv = myEnv; 154 else if(obj instanceof Reference){ 155 163 obj = ((Reference)obj).getObject(); 164 } 165 return obj; 166 } 167 protected Object federate(String compositName)throws NamingException{ 168 ObjectFactory factories [] = getFederatedFactories(); 169 for(int i =0; i < factories.length; i++){ 170 try{ 171 javax.naming.CompositeName name = new javax.naming.CompositeName (compositName); 172 Object obj = factories[i].getObjectInstance(null, name, null,null); 173 174 if(obj instanceof Context ) 175 return ((Context )obj).lookup(compositName); 176 else if(obj!=null) 177 return obj; 178 }catch(Exception nnfe){ 179 } 181 } 182 throw new javax.naming.NameNotFoundException ("Name \""+compositName+"\" not found."); 185 } 186 187 static ObjectFactory [] federatedFactories = null; 188 189 public static ObjectFactory [] getFederatedFactories( ) throws NamingException{ 190 if(federatedFactories == null){ 191 Set factories = new HashSet (); 192 Hashtable jndiProps = ResourceManager.getInitialEnvironment(null); 193 String pkgs = (String )jndiProps.get(Context.URL_PKG_PREFIXES); 194 if( pkgs == null) { 195 return new ObjectFactory [0]; 196 } 197 StringTokenizer parser = new StringTokenizer (pkgs, ":"); 198 199 while (parser.hasMoreTokens()) { 200 String className = parser.nextToken() + ".java.javaURLContextFactory"; 201 if(className.equals("org.openejb.core.ivm.naming.java.javaURLContextFactory")) 202 continue; 203 try { 204 ClassLoader cl = OpenEJB.getContextClassLoader(); 205 Class factoryClass = Class.forName(className, true, cl); 206 ObjectFactory factoryInstance = (ObjectFactory )factoryClass.newInstance(); 207 factories.add(factoryInstance); 208 }catch (ClassNotFoundException cnfe){ 209 }catch (Throwable e) { 211 NamingException ne = 212 new NamingException("Federation failed: Cannot instantiate " + className); 213 ne.setRootCause(e); 214 throw ne; 215 } 216 } 217 Object [] temp = factories.toArray(); 218 federatedFactories = new ObjectFactory [temp.length]; 219 System.arraycopy(temp, 0, federatedFactories, 0, federatedFactories.length); 220 } 221 return federatedFactories; 222 } 223 224 225 public Object lookup(Name compositName) throws NamingException { 226 return lookup(compositName.toString()); 227 } 228 231 public void bind(String name, Object obj) throws NamingException { 232 checkReadOnly(); 233 int indx = name.indexOf(":"); 234 if(indx>-1){ 235 239 name = name.substring(indx+1); 240 } 241 if(fastCache.containsKey(name)) 242 throw new javax.naming.NameAlreadyBoundException (); 243 else 244 mynode.bind(new ParsedName(name), obj); 245 } 246 249 public void bind(Name name, Object obj) throws NamingException { 250 bind(name.toString(), obj); 251 } 252 public void rebind(String name, Object obj) throws NamingException { 253 throw new javax.naming.OperationNotSupportedException (); 254 } 255 public void rebind(Name name, Object obj) throws NamingException { 256 rebind(name.toString(), obj); 257 } 258 public void unbind(String name) throws NamingException { 259 throw new javax.naming.OperationNotSupportedException (); 260 } 261 public void unbind(Name name) throws NamingException { 262 unbind(name.toString()); 263 } 264 public void rename(String oldname, String newname) 265 throws NamingException { 266 throw new javax.naming.OperationNotSupportedException (); 267 } 268 public void rename(Name oldname, Name newname) 269 throws NamingException { 270 rename(oldname.toString(), newname.toString()); 271 } 272 public NamingEnumeration list(String name) 273 throws NamingException { 274 Object obj = lookup(name); 275 if(obj.getClass() == IvmContext.class) 276 return new MyListEnumeration(((IvmContext)obj).mynode); 277 else { 278 return null; } 280 } 281 public NamingEnumeration list(Name name) 282 throws NamingException { 283 return list(name.toString()); 284 } 285 public NamingEnumeration listBindings(String name) 286 throws NamingException { 287 Object obj = lookup(name); 288 if(obj.getClass() == IvmContext.class) 289 return new MyListEnumeration(((IvmContext)obj).mynode); 290 else { 291 return null; } 293 } 294 public NamingEnumeration listBindings(Name name) 295 throws NamingException { 296 return listBindings(name.toString()); 297 } 298 public void destroySubcontext(String name) throws NamingException { 299 throw new javax.naming.OperationNotSupportedException (); 300 } 301 public void destroySubcontext(Name name) throws NamingException { 302 destroySubcontext(name.toString()); 303 } 304 307 public Context createSubcontext(String name) throws NamingException { 308 checkReadOnly(); 309 int indx = name.indexOf(":"); 310 if(indx>-1){ 311 315 name = name.substring(indx+1); 316 } 317 if(fastCache.containsKey(name)) 318 throw new javax.naming.NameAlreadyBoundException (); 319 else 320 return mynode.createSubcontext(new ParsedName(name)); 321 } 322 325 public Context createSubcontext(Name name) throws NamingException { 326 return createSubcontext(name.toString()); 327 } 328 public Object lookupLink(String name) throws NamingException { 329 return lookup(name); 330 } 331 public Object lookupLink(Name name) throws NamingException { 332 return lookupLink(name.toString()); 333 } 334 public NameParser getNameParser(String name) 335 throws NamingException { 336 throw new javax.naming.OperationNotSupportedException (); 337 } 338 public NameParser getNameParser(Name name) throws NamingException { 339 return getNameParser(name.toString()); 340 } 341 public String composeName(String name, String prefix) 342 throws NamingException { 343 Name result = composeName(new CompositeName (name), 344 new CompositeName (prefix)); 345 return result.toString(); 346 } 347 public Name composeName(Name name, Name prefix) 348 throws NamingException { 349 Name result = (Name )(prefix.clone()); 350 result.addAll(name); 351 return result; 352 } 353 public Object addToEnvironment(String propName, Object propVal) 354 throws NamingException { 355 if (myEnv == null) { 356 myEnv = new Hashtable (5, 0.75f); 357 } 358 return myEnv.put(propName, propVal); 359 } 360 public Object removeFromEnvironment(String propName) 361 throws NamingException { 362 if (myEnv == null) 363 return null; 364 return myEnv.remove(propName); 365 } 366 public Hashtable getEnvironment() throws NamingException { 367 if (myEnv == null) { 368 return new Hashtable (3, 0.75f); 370 } else { 371 return (Hashtable )myEnv.clone(); 372 } 373 } 374 public String getNameInNamespace() throws NamingException { 375 return ""; 376 } 377 public void close() throws NamingException { 378 } 379 380 protected void checkReadOnly( )throws javax.naming.OperationNotSupportedException { 381 if(readOnly)throw new javax.naming.OperationNotSupportedException (); 382 } 383 384 386 protected class MyBindingEnumeration extends MyNamingEnumeration { 387 public MyBindingEnumeration(NameNode parentNode){ 388 super(parentNode); 389 } 390 protected void buildEnumeration(Vector vect){ 391 for(int i = 0; i < vect.size(); i++){ 392 NameNode node = (NameNode)vect.elementAt(i); 393 String className = node.getBinding().getClass().getName(); 394 vect.setElementAt(new Binding (node.atomicName,className, node.getBinding()), i); 395 } 396 myEnum = vect.elements(); 397 } 398 399 } 400 401 protected class MyListEnumeration extends MyNamingEnumeration { 402 public MyListEnumeration(NameNode parentNode){ 403 super(parentNode); 404 } 405 protected void buildEnumeration(Vector vect){ 406 for(int i = 0; i < vect.size(); i++){ 407 NameNode node = (NameNode)vect.elementAt(i); 408 String className = node.getBinding().getClass().getName(); 409 vect.setElementAt(new NameClassPair (node.atomicName,className), i); 410 } 411 myEnum = vect.elements(); 412 } 413 414 } 415 protected abstract class MyNamingEnumeration implements javax.naming.NamingEnumeration { 416 Enumeration myEnum; 417 418 public MyNamingEnumeration(NameNode parentNode){ 419 Vector vect = new Vector (); 420 NameNode node = parentNode.subTree; 430 431 if ( node == null ) { 433 node = parentNode; 434 } else { 435 vect.addElement(node); 436 } 437 439 gatherNodes(node,vect); 440 441 buildEnumeration(vect); 442 } 443 abstract protected void buildEnumeration(Vector vect); 444 445 protected void gatherNodes(NameNode node, Vector vect){ 446 if(node.lessTree!=null){ 447 vect.addElement(node.lessTree); 448 gatherNodes(node.lessTree,vect); 449 } 450 if(node.grtrTree!=null){ 451 vect.addElement(node.grtrTree); 452 gatherNodes(node.grtrTree,vect); 453 } 454 } 455 456 public void close(){ 457 myEnum = null; 458 } 459 460 public boolean hasMore() { 461 return hasMoreElements(); 462 } 463 public Object next() { 464 return nextElement(); 465 } 466 public boolean hasMoreElements(){ 467 return myEnum.hasMoreElements(); 468 } 469 public Object nextElement() { 470 return myEnum.nextElement(); 471 } 472 } 473 474 protected Object writeReplace() throws ObjectStreamException { 475 if(org.openejb.core.ivm.IntraVmCopyMonitor.isStatefulPassivationOperation()){ 476 return new JndiEncArtifact(this); 478 } 479 throw new java.io.NotSerializableException ("IntraVM java.naming.Context objects can not be passed as arguments"); 481 } 482 483 484 485 public static void main(String str []) throws Exception { 486 String str1 = "root/comp/env/rate/work/doc/lot/pop"; 487 String str2 = "root/comp/env/rate/work/doc/lot/price"; 488 String str3 = "root/comp/env/rate/work/doc/lot"; 489 String str4 = "root/comp/env/rate/work/doc/lot/break/story"; 490 491 IvmContext context = new IvmContext(); 492 context.bind(str1, new Integer (1)); 493 context.bind(str2, new Integer (2)); 494 context.bind(str4, new Integer (3)); 495 508 509 Context subcntx = (Context )context.lookup(str3); 510 org.openejb.core.ivm.IntraVmCopyMonitor x = null; 511 java.io.FileOutputStream fos = new java.io.FileOutputStream ("x.ser"); 512 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (fos); 513 org.openejb.core.ivm.IntraVmCopyMonitor.prePassivationOperation(); 514 oos.writeObject(subcntx); 515 org.openejb.core.ivm.IntraVmCopyMonitor.postPassivationOperation(); 516 oos.flush(); 517 oos.close(); 518 java.io.FileInputStream fis = new java.io.FileInputStream ("x.ser"); 519 java.io.ObjectInputStream ois = new java.io.ObjectInputStream (fis); 520 Object newObj = ois.readObject(); 521 ois.close(); 522 } 523 } 525 | Popular Tags |