1 package org.enhydra.spi.webxml; 2 3 import java.rmi.Remote ; 4 import java.util.Enumeration ; 5 import java.util.Hashtable ; 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 import java.io.File ; 9 import java.io.FileInputStream ; 10 11 import javax.naming.Binding ; 12 import javax.naming.CompositeName ; 13 import javax.naming.Context ; 14 import javax.naming.InvalidNameException ; 15 import javax.naming.Name ; 16 import javax.naming.NameAlreadyBoundException ; 17 import javax.naming.NameClassPair ; 18 import javax.naming.NameNotFoundException ; 19 import javax.naming.NameParser ; 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.NamingException ; 22 import javax.naming.NotContextException ; 23 import javax.naming.OperationNotSupportedException ; 24 import javax.naming.Reference ; 25 import javax.naming.Referenceable ; 26 import javax.naming.spi.ObjectFactory ; 27 import org.objectweb.carol.util.configuration.TraceCarol; 28 import org.enhydra.spi.webxml.util.*; 29 30 31 43 44 47 48 public class WebXmlInitialContext implements Context { 49 50 53 private static Hashtable fileEnv = new Hashtable (); 54 55 58 private static Hashtable bindings = new Hashtable (); 59 60 63 private static Context envContext = null; 64 65 68 private String filePath = null; 69 70 73 private boolean isFileParsed = false; 74 75 78 private static NameParser fileParser = new WebXmlNameParser(); 79 80 public final static String FILE_JNDI_ABS_PATH_NAME = "fileJndiAbsPathName"; 81 public final static String CONTEXT_FOR_ENV_BIND = "contextForEnvToBind"; 82 83 92 private Object resolveObject(Object o, Name name) { 93 try { 94 if (o instanceof Reference ) { 95 Reference objRef = (Reference ) o; 97 ObjectFactory objFact = (ObjectFactory ) (Thread.currentThread(). 98 getContextClassLoader().loadClass(objRef.getFactoryClassName())).newInstance(); 99 return objFact.getObjectInstance(objRef, name, this, 100 this.getEnvironment()); 101 } 102 else { 103 return o; 104 } 105 } 106 catch (Exception e) { 107 TraceCarol.error("WebXmlInitialContext.resolveObject()", e); 108 return o; 109 } 110 } 111 112 119 private Object encodeObject(Object o) throws NamingException { 120 try { 121 if ( (! (o instanceof Remote )) && (o instanceof Referenceable )) { 122 return ( (Referenceable ) o).getReference(); 123 } 124 else if ( (! (o instanceof Remote )) && (o instanceof Reference )) { 125 return (Reference ) o; 126 } 127 else { 128 return o; 129 } 130 } 131 catch (Exception e) { 132 throw new NamingException ("" + e); 133 } 134 } 135 136 140 public WebXmlInitialContext () throws NamingException { 141 if (TraceCarol.isDebugJndiCarol()) { 142 TraceCarol.debugJndiCarol("WebXmlInitialContext.WebXmlInitialContext()"); 143 } 144 envContext = null; 145 filePath = null; 146 isFileParsed = false; 147 } 148 149 154 public WebXmlInitialContext (Hashtable ev) throws NamingException { 155 if (TraceCarol.isDebugJndiCarol()) { 156 TraceCarol.debugJndiCarol("WebXmlInitialContext.WebXmlInitialContext(Hashtable env)"); 157 } 158 if (ev != null) { 159 fileEnv = (Hashtable )(ev.clone()); 160 } 161 envContext = null; 162 filePath = null; 163 isFileParsed = false; 164 } 165 166 167 public Object lookup(String name) throws NamingException { 169 if (TraceCarol.isDebugJndiCarol()) { 170 TraceCarol.debugJndiCarol("WebXmlInitialContext.lookup(\"" + name + "\")"); 171 } 172 if ( (name == null) || (name.equals(""))) { 173 return (new WebXmlInitialContext(fileEnv)); 174 } 175 Object o = bindings.get(name); 176 if (o != null) { 177 return resolveObject(o, new CompositeName (name)); 178 } 179 else { 180 throw new NameNotFoundException (name + " not found"); 181 } 182 } 183 184 public Object lookup(Name name) throws NamingException { 185 return lookup(name.toString()); 186 } 187 188 public void bind(String name, Object obj) throws NamingException { 189 if (TraceCarol.isDebugJndiCarol()) { 190 TraceCarol.debugJndiCarol("WebXmlInitialContext.bind(\"" + name + "\"," + 191 simpleClass(obj.getClass().getName()) + 192 " object)"); 193 } 194 if (name.equals("")) { 195 throw new InvalidNameException ("Cannot bind empty name"); 196 } 197 if (name.equals(FILE_JNDI_ABS_PATH_NAME)) { 198 this.filePath = (String ) obj; 199 if ( (this.envContext != null) && (!this.isFileParsed)) { 200 parseWebXmlFile(this.filePath); 201 this.isFileParsed = true; 202 } 203 } 204 else { 205 if (name.equals(CONTEXT_FOR_ENV_BIND)) { 206 this.envContext = (Context ) obj; 207 if (this.filePath != null) { 208 parseWebXmlFile(this.filePath); 209 this.isFileParsed = true; 210 } 211 } 212 else { 213 if (bindings.get(name) != null) { 214 throw new NameAlreadyBoundException ("Use rebind to override"); 215 } 216 bindings.put(name, encodeObject(obj)); 217 } 218 } 219 } 220 221 public void bind(Name name, Object obj) throws NamingException { 222 bind(name.toString(), obj); 223 } 224 225 public void rebind(String name, Object obj) throws NamingException { 226 if (TraceCarol.isDebugJndiCarol()) { 227 TraceCarol.debugJndiCarol("WebXmlInitialContext.rebind(\"" + name + "\"," + 228 simpleClass(obj.getClass().getName()) + 229 " object)"); 230 } 231 if (name.equals("")) { 232 throw new InvalidNameException ("Cannot bind empty name"); 233 } 234 if (name.equals(FILE_JNDI_ABS_PATH_NAME)) { 235 this.filePath = (String ) obj; 236 if (this.envContext != null) { 237 parseWebXmlFile(this.filePath); 238 this.isFileParsed = true; 239 } 240 } 241 else { 242 if (name.equals(CONTEXT_FOR_ENV_BIND)) { 243 this.envContext = (Context ) obj; 244 if (this.filePath != null) { 245 parseWebXmlFile(this.filePath); 246 this.isFileParsed = true; 247 } 248 } 249 else { 250 bindings.put(name, encodeObject(obj)); 251 } 252 } 253 } 254 255 public void rebind(Name name, Object obj) throws NamingException { 256 rebind(name.toString(), obj); 257 } 258 259 public void unbind(String name) throws NamingException { 260 if (TraceCarol.isDebugJndiCarol()) { 261 TraceCarol.debugJndiCarol("WebXmlInitialContext.unbind(\"" + name + "\")"); 262 } 263 if (name.equals("")) { 264 throw new InvalidNameException ("Cannot unbind empty name"); 265 } 266 bindings.remove(name); 267 } 268 269 public void unbind(Name name) throws NamingException { 270 unbind(name.toString()); 271 } 272 273 public void rename(String oldName, String newName) throws NamingException { 274 if (TraceCarol.isDebugJndiCarol()) { 275 TraceCarol.debugJndiCarol("WebXmlInitialContext.rename(\"" + oldName + 276 "\",\"" + newName + "\")"); 277 } 278 if (oldName.equals("") || newName.equals("")) 279 throw new InvalidNameException ("Cannot rename empty name"); 280 if (bindings.get(newName) != null) 281 throw new NameAlreadyBoundException (newName + " is already bound"); 282 283 Object oldb = bindings.remove(oldName); 284 if (oldb == null) 285 throw new NameNotFoundException (oldName + " not bound"); 286 bindings.put(newName, oldb); 287 } 288 289 public void rename(Name oldname, Name newname) throws NamingException { 290 rename(oldname.toString(), newname.toString()); 291 } 292 293 public NamingEnumeration list(String name) throws NamingException { 294 if (TraceCarol.isDebugJndiCarol()) { 295 TraceCarol.debugJndiCarol("WebXmlInitialContext.list(\"" + name + "\")"); 296 } 297 if (name.equals("")) { 298 return new WebXmlNames(bindings.keys()); 299 } 300 301 Object target = lookup(name); 302 if (target instanceof Context ) { 303 return ( (Context ) target).list(""); 304 } 305 throw new NotContextException (name + " cannot be listed"); 306 } 307 308 public NamingEnumeration list(Name name) throws NamingException { 309 return list(name.toString()); 310 } 311 312 public NamingEnumeration listBindings(String name) throws NamingException { 313 if (TraceCarol.isDebugJndiCarol()) { 314 TraceCarol.debugJndiCarol("WebXmlInitialContext.listBindings(\"" + name + 315 "\")/rmi name=\""); 316 } 317 if (name.equals("")) { 318 return new WebXmlBindings(bindings.keys()); 319 } 320 Object target = lookup(name); 321 if (target instanceof Context ) { 322 return ( (Context ) target).listBindings(""); 323 } 324 throw new NotContextException (name + " cannot be listed"); 325 } 326 327 public NamingEnumeration listBindings(Name name) throws NamingException { 328 return listBindings(name.toString()); 329 } 330 331 public void destroySubcontext(String name) throws NamingException { 332 TraceCarol.error("WebXmlInitialContext.destroySubcontext(\"" + name + 333 "\"): Not supported"); 334 throw new OperationNotSupportedException ( 335 "WebXmlInitialContext.destroySubcontext(\"" + name + 336 "\"): Not supported"); 337 } 338 339 public void destroySubcontext(Name name) throws NamingException { 340 destroySubcontext(name.toString()); 341 } 342 343 public Context createSubcontext(String name) throws NamingException { 344 TraceCarol.error("WebXmlInitialContext.createSubcontext(\"" + name + 345 "\"): Not supported"); 346 throw new OperationNotSupportedException ( 347 "WebXmlInitialContext.createSubcontext(\"" + name + 348 "\"): Not supported"); 349 } 350 351 public Context createSubcontext(Name name) throws NamingException { 352 return createSubcontext(name.toString()); 353 } 354 355 public Object lookupLink(String name) throws NamingException { 356 if (TraceCarol.isDebugJndiCarol()) { 357 TraceCarol.debugJndiCarol("WebXmlInitialContext.lookupLink(\"" + name + 358 "\")"); 359 } 360 return lookup(name); 361 } 362 363 public Object lookupLink(Name name) throws NamingException { 364 return lookupLink(name.toString()); 365 } 366 367 public NameParser getNameParser(String name) throws NamingException { 368 if (TraceCarol.isDebugJndiCarol()) { 369 TraceCarol.debugJndiCarol("WebXmlInitialContext.getNameParser(\"" + name + 370 "\")"); 371 } 372 return fileParser; 373 } 374 375 public NameParser getNameParser(Name name) throws NamingException { 376 return getNameParser(name.toString()); 377 } 378 379 public String composeName(String name, String prefix) throws NamingException { 380 if (TraceCarol.isDebugJndiCarol()) { 381 TraceCarol.debugJndiCarol("WebXmlInitialContext.composeName(" + name + 382 "," + prefix + ")"); 383 } 384 Name result = composeName(new CompositeName (name), new CompositeName (prefix)); 385 return result.toString(); 386 } 387 388 public Name composeName(Name name, Name prefix) throws NamingException { 389 if (TraceCarol.isDebugJndiCarol()) { 390 TraceCarol.debugJndiCarol("WebXmlInitialContext.composeName(" + name + 391 "," + prefix + ")"); 392 } 393 Name result = (Name ) (prefix.clone()); 394 result.addAll(name); 395 return result; 396 } 397 398 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 399 if (TraceCarol.isDebugJndiCarol()) { 400 TraceCarol.debugJndiCarol("WebXmlInitialContext.addToEnvironment(\"" + 401 propName + "\"," + 402 simpleClass(propVal.getClass().getName()) + 403 " object)"); 404 } 405 if (fileEnv == null) { 406 fileEnv = new Hashtable (); 407 } 408 409 return fileEnv.put(propName, propVal); 410 } 411 412 public Object removeFromEnvironment(String propName) throws NamingException { 413 if (TraceCarol.isDebugJndiCarol()) { 414 TraceCarol.debugJndiCarol("WebXmlInitialContext.removeFromEnvironment(\"" + 415 propName + "\")"); 416 } 417 if (fileEnv == null) 418 return null; 419 return fileEnv.remove(propName); 420 } 421 422 public Hashtable getEnvironment() throws NamingException { 423 if (TraceCarol.isDebugJndiCarol()) { 424 TraceCarol.debugJndiCarol("WebXmlInitialContext.getEnvironment()"); 425 } 426 if (fileEnv == null) { 427 fileEnv = new Hashtable (); 428 } 429 return fileEnv; 430 } 431 432 public void close() throws NamingException { 433 if (TraceCarol.isDebugJndiCarol()) { 434 TraceCarol.debugJndiCarol("WebXmlInitialContext.close()"); 435 } 436 } 437 438 public String getNameInNamespace() throws NamingException { 439 if (TraceCarol.isDebugJndiCarol()) { 440 TraceCarol.debugJndiCarol("WebXmlInitialContext.getNameInNamespace()"); 441 } 442 return "webXmlContext"; 443 } 444 445 451 private String simpleClass(String c) { 452 return c.substring(c.lastIndexOf('.') + 1); 453 } 454 455 class WebXmlNames implements NamingEnumeration { 457 Enumeration names; 458 459 WebXmlNames(Enumeration names) { 460 this.names = names; 461 } 462 463 public boolean hasMoreElements() { 464 return names.hasMoreElements(); 465 } 466 467 public boolean hasMore() throws NamingException { 468 return hasMoreElements(); 469 } 470 471 public Object nextElement() { 472 String name = (String ) names.nextElement(); 473 String className = bindings.get(name).getClass().getName(); 474 return new NameClassPair (name, className); 475 } 476 477 public Object next() throws NamingException { 478 return nextElement(); 479 } 480 481 public void close() throws NamingException { 482 names = null; 483 } 484 } 485 486 class WebXmlBindings 488 implements NamingEnumeration { 489 Enumeration names; 490 491 WebXmlBindings(Enumeration names) { 492 this.names = names; 493 } 494 495 public boolean hasMoreElements() { 496 return names.hasMoreElements(); 497 } 498 499 public boolean hasMore() throws NamingException { 500 return hasMoreElements(); 501 } 502 503 public Object nextElement() { 504 String name = (String ) names.nextElement(); 505 return new Binding (name, bindings.get(name)); 506 } 507 508 public Object next() throws NamingException { 509 return nextElement(); 510 } 511 512 public void close() throws NamingException { 513 names = null; 514 } 515 } 516 517 private void parseWebXmlFile(String path) { 518 File file = new File (path); WebXmlUtil xmlUtil = null; 521 try { 522 xmlUtil = new WebXmlUtil(file); 523 } 524 catch (Exception e){ 525 System.err.println("Error in forming FileInputStream"); 526 } 527 try { 528 xmlUtil.parseTree(this.envContext); 529 } 530 catch (Exception e) { 531 System.err.println("Error in processing web.xml file"); 532 e.printStackTrace(); 533 } 534 } 535 536 } 537 | Popular Tags |