1 23 24 package org.enhydra.spi.conf; 25 26 import java.rmi.Remote ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 34 import javax.naming.Binding ; 35 import javax.naming.CompositeName ; 36 import javax.naming.Context ; 37 import javax.naming.InvalidNameException ; 38 import javax.naming.Name ; 39 import javax.naming.NameAlreadyBoundException ; 40 import javax.naming.NameClassPair ; 41 import javax.naming.NameNotFoundException ; 42 import javax.naming.NameParser ; 43 import javax.naming.NamingEnumeration ; 44 import javax.naming.NamingException ; 45 import javax.naming.NotContextException ; 46 import javax.naming.OperationNotSupportedException ; 47 import javax.naming.Reference ; 48 import javax.naming.Referenceable ; 49 import javax.naming.spi.ObjectFactory ; 50 51 import org.objectweb.carol.util.configuration.TraceCarol; 52 import org.enhydra.spi.conf.util.ConfigToContextParser; 53 54 64 public class ConfFileInitialContext implements Context { 65 66 69 private static Hashtable fileEnv = new Hashtable (); 70 71 74 private static Hashtable bindings = new Hashtable (); 75 76 79 private static Context envContext = null; 80 81 84 private String filePath = null; 85 86 89 private boolean isFileParsed = false; 90 91 94 private static NameParser fileParser = new ConfFileNameParser(); 95 96 public final static String FILE_JNDI_ABS_PATH_NAME = "fileJndiAbsPathName"; 97 public final static String CONTEXT_FOR_ENV_BIND = "contextForEnvToBind"; 98 99 108 private Object resolveObject(Object o, Name name) { 109 try { 110 if (o instanceof Reference ) { 111 Reference objRef = (Reference )o; 113 ObjectFactory objFact = (ObjectFactory )(Thread.currentThread().getContextClassLoader().loadClass(objRef.getFactoryClassName())).newInstance(); 114 return objFact.getObjectInstance(objRef,name,this,this.getEnvironment()); 115 } else { 116 return o; 117 } 118 } catch (Exception e) { 119 TraceCarol.error("ConfFileInitialContext.resolveObject()", e); 120 return o; 121 } 122 } 123 124 133 private Object encodeObject(Object o) throws NamingException { 134 try { 135 if ((!(o instanceof Remote )) && (o instanceof Referenceable )) { 136 return ((Referenceable )o).getReference(); 137 } else if ((!(o instanceof Remote )) && (o instanceof Reference )) { 138 return (Reference )o; 139 } else { 140 return o; 141 } 142 } catch (Exception e) { 143 throw new NamingException ("" +e); 144 } 145 } 146 147 153 public ConfFileInitialContext () throws NamingException { 154 if (TraceCarol.isDebugJndiCarol()) { 155 TraceCarol.debugJndiCarol("ConfFileInitialContext.ConfFileInitialContext()"); 156 } 157 envContext = null; 159 filePath = null; 160 isFileParsed = false; 161 } 162 163 170 public ConfFileInitialContext (Hashtable ev) throws NamingException { 171 if (TraceCarol.isDebugJndiCarol()) { 172 TraceCarol.debugJndiCarol("ConfFileInitialContext.ConfFileInitialContext(Hashtable env)"); 173 } 174 if (ev != null) { 175 fileEnv = (Hashtable )(ev.clone()); 176 } 177 envContext = null; 179 filePath = null; 180 isFileParsed = false; 181 } 182 183 184 public Object lookup(String name) throws NamingException { 186 if (TraceCarol.isDebugJndiCarol()) { 188 TraceCarol.debugJndiCarol("ConfFileInitialContext.lookup(\"" + name + 189 "\")"); 190 } 191 if ( (name == null) || (name.equals(""))) { 192 return (new ConfFileInitialContext(fileEnv)); 193 } 194 Object o = bindings.get(name); 195 if (o != null) { 196 return resolveObject(o, new CompositeName (name)); 197 } 198 else { 199 throw new NameNotFoundException (name + " not found"); 200 } 201 } 202 203 public Object lookup(Name name) throws NamingException { 204 return lookup(name.toString()); 205 } 206 207 public void bind(String name, Object obj) throws NamingException { 208 if (TraceCarol.isDebugJndiCarol()) { 209 TraceCarol.debugJndiCarol("ConfFileInitialContext.bind(\""+name+"\","+simpleClass(obj.getClass().getName())+" object)"); 210 } 211 if (name.equals("")) { 213 throw new InvalidNameException ("Cannot bind empty name"); 214 } 215 if (name.equals(FILE_JNDI_ABS_PATH_NAME)) { 216 this.filePath = (String ) obj; 222 if ((this.envContext != null) && (!this.isFileParsed)) { 223 parseConfFile(this.filePath); 225 this.isFileParsed = true; 226 } 227 } 228 else { 229 if (name.equals(CONTEXT_FOR_ENV_BIND)) { 230 this.envContext = (Context ) obj; 237 if (this.filePath != null) { 239 parseConfFile(this.filePath); 240 this.isFileParsed = true; 241 } 242 } 245 else { 246 if (bindings.get(name) != null) { 247 throw new NameAlreadyBoundException ("Use rebind to override"); 248 } 249 bindings.put(name, encodeObject(obj)); 250 } 251 } 252 } 253 254 public void bind(Name name, Object obj) throws NamingException { 255 bind(name.toString(), obj); 256 } 257 258 public void rebind(String name, Object obj) throws NamingException { 259 if (TraceCarol.isDebugJndiCarol()) { 260 TraceCarol.debugJndiCarol("ConfFileInitialContext.rebind(\""+name+"\","+simpleClass(obj.getClass().getName())+" object)"); 261 } 262 if (name.equals("")) { 264 throw new InvalidNameException ("Cannot bind empty name"); 265 } 266 if (name.equals(FILE_JNDI_ABS_PATH_NAME)) { 267 this.filePath = (String ) obj; 272 if (this.envContext != null){ 274 parseConfFile(this.filePath); 275 this.isFileParsed = true; 276 } 277 } 278 else { 279 if (name.equals(CONTEXT_FOR_ENV_BIND)) { 280 this.envContext = (Context ) obj; 286 if (this.filePath != null) { 288 parseConfFile(this.filePath); 289 this.isFileParsed = true; 290 } 291 } 294 else { 295 bindings.put(name, encodeObject(obj)); 296 } 297 } 298 } 299 300 public void rebind(Name name, Object obj) throws NamingException { 301 rebind(name.toString(), obj); 302 } 303 304 public void unbind(String name) throws NamingException { 305 if (TraceCarol.isDebugJndiCarol()) { 306 TraceCarol.debugJndiCarol("ConfFileInitialContext.unbind(\""+name+"\")"); 307 } 308 if (name.equals("")) { 309 throw new InvalidNameException ("Cannot unbind empty name"); 310 } 311 bindings.remove(name); 312 } 313 314 public void unbind(Name name) throws NamingException { 315 unbind(name.toString()); 316 } 317 public void rename(String oldName, String newName) throws NamingException { 318 if (TraceCarol.isDebugJndiCarol()) { 319 TraceCarol.debugJndiCarol("ConfFileInitialContext.rename(\""+oldName+"\",\""+newName+"\")"); 320 } 321 if (oldName.equals("") || newName.equals("")) throw new InvalidNameException ("Cannot rename empty name"); 322 if (bindings.get(newName) != null) throw new NameAlreadyBoundException (newName + " is already bound"); 323 324 Object oldb = bindings.remove(oldName); 325 if (oldb == null) throw new NameNotFoundException (oldName + " not bound"); 326 bindings.put(newName, oldb); 327 } 328 329 public void rename(Name oldname, Name newname) throws NamingException { 330 rename(oldname.toString(), newname.toString()); 331 } 332 333 public NamingEnumeration list(String name) throws NamingException { 334 if (TraceCarol.isDebugJndiCarol()) { 335 TraceCarol.debugJndiCarol("ConfFileInitialContext.list(\""+name+"\")"); 336 } 337 if (name.equals("")) { 339 return new ConfFileNames(bindings.keys()); 340 } 341 342 Object target = lookup(name); 343 if (target instanceof Context ) { 344 return ((Context )target).list(""); 345 } 346 throw new NotContextException (name + " cannot be listed"); 347 } 348 349 public NamingEnumeration list(Name name) throws NamingException { 350 return list(name.toString()); 351 } 352 353 public NamingEnumeration listBindings(String name) throws NamingException { 354 if (TraceCarol.isDebugJndiCarol()) { 355 TraceCarol.debugJndiCarol("ConfFileInitialContext.listBindings(\""+name+"\")/rmi name=\""); 356 } 357 if (name.equals("")) { 358 return new ConfFileBindings(bindings.keys()); 359 } 360 Object target = lookup(name); 361 if (target instanceof Context ) { 362 return ((Context )target).listBindings(""); 363 } 364 throw new NotContextException (name + " cannot be listed"); 365 } 366 367 public NamingEnumeration listBindings(Name name) throws NamingException { 368 return listBindings(name.toString()); 369 } 370 371 public void destroySubcontext(String name) throws NamingException { 372 TraceCarol.error("ConfFileInitialContext.destroySubcontext(\""+name+"\"): Not supported"); 373 throw new OperationNotSupportedException ("ConfFileInitialContext.destroySubcontext(\""+name+"\"): Not supported"); 374 } 375 376 public void destroySubcontext(Name name) throws NamingException { 377 destroySubcontext(name.toString()); 378 } 379 380 public Context createSubcontext(String name) throws NamingException { 381 TraceCarol.error("ConfFileInitialContext.createSubcontext(\""+name+"\"): Not supported"); 382 throw new OperationNotSupportedException ("ConfFileInitialContext.createSubcontext(\""+name+"\"): Not supported"); 383 } 384 385 public Context createSubcontext(Name name) throws NamingException { 386 return createSubcontext(name.toString()); 387 } 388 389 public Object lookupLink(String name) throws NamingException { 390 if (TraceCarol.isDebugJndiCarol()) { 391 TraceCarol.debugJndiCarol("ConfFileInitialContext.lookupLink(\""+name+"\")"); 392 } 393 return lookup(name); 394 } 395 396 public Object lookupLink(Name name) throws NamingException { 397 return lookupLink(name.toString()); 398 } 399 400 public NameParser getNameParser(String name) throws NamingException { 401 if (TraceCarol.isDebugJndiCarol()) { 402 TraceCarol.debugJndiCarol("ConfFileInitialContext.getNameParser(\""+name+"\")"); 403 } 404 return fileParser; 405 } 406 407 public NameParser getNameParser(Name name) throws NamingException { 408 return getNameParser(name.toString()); 409 } 410 411 public String composeName(String name, String prefix) throws NamingException { 412 if (TraceCarol.isDebugJndiCarol()) { 413 TraceCarol.debugJndiCarol("ConfFileInitialContext.composeName("+name+","+prefix+")"); 414 } 415 Name result = composeName(new CompositeName (name), new CompositeName (prefix)); 416 return result.toString(); 417 } 418 419 public Name composeName(Name name, Name prefix) throws NamingException { 420 if (TraceCarol.isDebugJndiCarol()) { 421 TraceCarol.debugJndiCarol("ConfFileInitialContext.composeName("+name+","+prefix+")"); 422 } 423 Name result = (Name )(prefix.clone()); 424 result.addAll(name); 425 return result; 426 } 427 428 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 429 if (TraceCarol.isDebugJndiCarol()) { 430 TraceCarol.debugJndiCarol("ConfFileInitialContext.addToEnvironment(\""+propName+"\","+simpleClass(propVal.getClass().getName())+" object)"); 431 } 432 if (fileEnv == null) { 433 fileEnv = new Hashtable (); 434 } 435 return fileEnv.put(propName, propVal); 436 } 437 438 public Object removeFromEnvironment(String propName) throws NamingException { 439 if (TraceCarol.isDebugJndiCarol()) { 440 TraceCarol.debugJndiCarol("ConfFileInitialContext.removeFromEnvironment(\""+propName+"\")"); 441 } 442 if (fileEnv == null) return null; 443 return fileEnv.remove(propName); 444 } 445 public Hashtable getEnvironment() throws NamingException { 446 if (TraceCarol.isDebugJndiCarol()) { 447 TraceCarol.debugJndiCarol("ConfFileInitialContext.getEnvironment()"); 448 } 449 if (fileEnv == null) { 450 fileEnv = new Hashtable (); 451 } 452 return fileEnv; 453 } 454 455 public void close() throws NamingException { 456 if (TraceCarol.isDebugJndiCarol()) { 457 TraceCarol.debugJndiCarol("ConfFileInitialContext.close()"); 458 } 459 } 460 461 public String getNameInNamespace() throws NamingException { 462 if (TraceCarol.isDebugJndiCarol()) { 463 TraceCarol.debugJndiCarol("ConfFileInitialContext.getNameInNamespace()"); 464 } 465 return "conffileContext"; 466 } 467 468 473 private String simpleClass(String c) { 474 return c.substring(c.lastIndexOf('.') +1); 475 } 476 477 class ConfFileNames implements NamingEnumeration { 479 Enumeration names; 480 481 ConfFileNames (Enumeration names) { 482 this.names = names; 483 } 484 485 public boolean hasMoreElements() { 486 return names.hasMoreElements(); 487 } 488 489 public boolean hasMore() throws NamingException { 490 return hasMoreElements(); 491 } 492 493 public Object nextElement() { 494 String name = (String )names.nextElement(); 495 String className = bindings.get(name).getClass().getName(); 496 return new NameClassPair (name, className); 497 } 498 499 public Object next() throws NamingException { 500 return nextElement(); 501 } 502 503 public void close() throws NamingException { 504 names=null; 505 } 506 } 507 508 class ConfFileBindings implements NamingEnumeration { 510 Enumeration names; 511 512 ConfFileBindings (Enumeration names) { 513 this.names = names; 514 } 515 516 public boolean hasMoreElements() { 517 return names.hasMoreElements(); 518 } 519 520 public boolean hasMore() throws NamingException { 521 return hasMoreElements(); 522 } 523 524 public Object nextElement() { 525 String name = (String )names.nextElement(); 526 return new Binding (name, bindings.get(name)); 527 } 528 529 public Object next() throws NamingException { 530 return nextElement(); 531 } 532 533 public void close() throws NamingException { 534 names = null; 535 } 536 } 537 538 private void parseConfFile(String path) { 539 File file = new File (path); ConfigToContextParser parser = null; 542 try { 543 parser = new ConfigToContextParser(new FileInputStream (file)); 544 } 545 catch (Exception e){ 546 System.err.println("Error in forming FileInputStream"); 547 } 548 int ii = bindings.size(); 549 try { 550 parser.process(this.envContext); 551 } 552 catch (Exception e) { 553 System.err.println("Error in processing conf file"); 554 e.printStackTrace(); 555 } 556 } 557 558 } 559 | Popular Tags |