1 55 package org.lateralnz.panther.naming; 56 57 import java.util.Hashtable ; 58 import java.util.HashMap ; 59 import javax.naming.*; 60 import javax.naming.spi.*; 61 62 import org.apache.log4j.Logger; 63 64 import org.lateralnz.common.util.Constants; 65 import org.lateralnz.common.util.StringUtils; 66 import org.lateralnz.panther.util.EJBRef; 67 68 72 public class LocalContext implements Context, Constants, Referenceable { 73 public static final String NAMING_CONTEXT_NAME = "panther.naming.contextname"; 74 75 private static final String SPLIT_REGEX = "/"; 76 77 private static final Logger log = Logger.getLogger(LocalContext.class.getName()); 78 79 private static HashMap contexts = new HashMap (); 80 private static HashMap localobjects = new HashMap (); 81 private static HashMap localenv = new HashMap (); 82 private static LocalContextNameParser nameparser = new LocalContextNameParser(); 83 84 protected HashMap objects; 85 private Hashtable environ; 86 private String contextName; 87 88 class Subcontext { 89 String name; 90 LocalContext ctx; 91 92 public Subcontext(String name, LocalContext ctx) { 93 this.name = name; 94 this.ctx = ctx; 95 } 96 } 97 98 protected LocalContext(HashMap objects, Hashtable environ) { 99 this.objects = objects; 100 this.environ = environ; 101 } 102 103 public LocalContext(Hashtable ht) throws NamingException { 104 String key; 105 106 if (ht != null) { 107 key = ht.toString(); 108 contextName = (String )ht.get(NAMING_CONTEXT_NAME); 109 if (StringUtils.isEmpty(contextName)) { 110 contextName = System.getProperty(NAMING_CONTEXT_NAME); 111 } 112 } 113 else { 114 key = EMPTY; 115 contextName = EMPTY; 116 } 117 118 contexts.put(getKey(), this); 119 120 System.out.println("local context " + contextName + " constructed with properties " + ht + " keyed on " + key); 121 if (log.isInfoEnabled()) { 122 log.info("local context " + contextName + " constructed with properties " + ht + " keyed on " + key); 123 } 124 125 if (!localobjects.containsKey(key)) { 126 synchronized (localobjects) { 127 if (!localobjects.containsKey(key)) { 128 HashMap objects = new HashMap (); 129 Hashtable env = new Hashtable (); 130 localobjects.put(key, objects); 131 localenv.put(key, env); 132 } 133 } 134 } 135 objects = (HashMap )localobjects.get(key); 136 environ = (Hashtable )localenv.get(key); 137 } 138 139 private String getKey() { 140 return contextName + UNDERSCORE + Integer.toString(System.identityHashCode(this)); 141 } 142 143 protected LocalContext create(HashMap objects, Hashtable environ) { 144 return new LocalContext(objects, environ); 145 } 146 147 protected Subcontext getSubcontext(String name, boolean create) throws NamingException { 148 if (name.startsWith("java:")) { 149 name = name.substring(5); 150 } 151 String [] split = name.split(SPLIT_REGEX); 152 LocalContext ctx = this; 153 for (int i = 0; i < split.length-1; i++) { 154 Object tmp = ctx.lookup(split[i]); 155 if (tmp == null) { 156 if (!create) { 157 throw new NameNotFoundException("subcontext " + split[i] + " does not exist"); 158 } 159 tmp = create(new HashMap (), new Hashtable ()); 160 ctx.bind(split[i], tmp); 161 } 162 else if (!(tmp instanceof LocalContext)) { 163 throw new NotContextException(split[i] + " is not a subcontext"); 164 } 165 166 ctx = (LocalContext)tmp; 167 } 168 return new Subcontext(split[split.length-1], ctx); 169 } 170 171 public static Context getContext(String name) { 172 return (Context)contexts.get(name); 173 } 174 175 public Object addToEnvironment(String name, Object obj) throws NamingException { 176 return environ.put(name, obj); 177 } 178 179 public void bind(Name name, Object obj) throws NamingException { 180 bind(name.toString(), obj); 181 } 182 183 public void bind(String name, Object obj) throws NamingException { 184 if (objects.containsKey(name)) { 185 throw new NameAlreadyBoundException(name); 186 } 187 else { 188 rebind(name, obj); 189 } 190 } 191 192 public void close() throws NamingException { 193 objects = null; 194 environ = null; 195 contexts.remove(getKey()); 196 } 197 198 public String composeName(String str, String prefix) throws NamingException { 199 if (!prefix.endsWith(FORWARD_SLASH)) { 200 prefix = prefix + FORWARD_SLASH; 201 } 202 return prefix + str; 203 } 204 205 public Name composeName(Name name, Name prefix) throws NamingException { 206 return nameparser.parse(composeName(name.toString(), prefix.toString())); 207 } 208 209 public Context createSubcontext(Name name) throws NamingException { 210 return createSubcontext(name.toString()); 211 } 212 213 public Context createSubcontext(String name) throws NamingException { 214 Subcontext sctx = getSubcontext(name, false); 215 if (objects.containsKey(sctx.name)) { 216 throw new NameAlreadyBoundException(); 217 } 218 LocalContext newctx = create(new HashMap (), new Hashtable ()); 219 sctx.ctx.objects.put(sctx.name, new Binding(sctx.name, newctx.getClass().getName(), newctx)); 220 221 return newctx; 222 } 223 224 public void destroySubcontext(Name name) throws NamingException { 225 destroySubcontext(name.toString()); 226 } 227 228 public void destroySubcontext(String name) throws NamingException { 229 if (!objects.containsKey(name)) { 230 throw new NameNotFoundException(name); 231 } 232 Binding binding = (Binding)objects.get(name); 233 Object obj = null; 234 if (binding != null) { 235 obj = binding.getObject(); 236 } 237 238 if (obj != null && obj instanceof Context) { 239 objects.remove(name); 240 } 241 else { 242 throw new NotContextException(name); 243 } 244 } 245 246 public Hashtable getEnvironment() throws NamingException { 247 return environ; 248 } 249 250 public String getNameInNamespace() throws NamingException { 251 throw new OperationNotSupportedException(); 252 } 253 254 public NameParser getNameParser(Name name) throws NamingException { 255 return nameparser; 256 } 257 258 public NameParser getNameParser(String str) throws NamingException { 259 return nameparser; 260 } 261 262 public Reference getReference() { 263 Reference ref = new Reference(getClass().getName(), LocalContextFactory.class.getName(), null); 264 ref.add(new StringRefAddr("name", getKey())); 265 return ref; 266 } 267 268 public NamingEnumeration list(String name) throws NamingException { 269 Object obj; 270 if (StringUtils.isEmpty(name)) { 271 obj = this; 272 } 273 else { 274 obj = lookup(name); 275 } 276 277 if (obj != null && obj instanceof LocalContext) { 278 return new LocalContextNamingEnumeration((LocalContext)obj); 279 } 280 else { 281 return new LocalContextNamingEnumeration(); 282 } 283 } 284 285 public NamingEnumeration list(Name name) throws NamingException { 286 return list(name.toString()); 287 } 288 289 public NamingEnumeration listBindings(String name) throws NamingException { 290 return list(name); 291 } 292 293 public javax.naming.NamingEnumeration listBindings(Name name) throws NamingException { 294 return listBindings(name.toString()); 295 } 296 297 public Object lookup(String name) throws NamingException { 298 if (StringUtils.isEmpty(name)) { 299 return create(objects, (Hashtable )environ.clone()); 300 } 301 302 Subcontext sctx = getSubcontext(name, false); 303 if (!sctx.ctx.objects.containsKey(sctx.name)) { 304 String ejbname = EJBRef.getEJBName(); 305 if (!StringUtils.isEmpty(ejbname) && !name.startsWith(ejbname + UNDERSCORE)) { 306 return lookup(ejbname + UNDERSCORE + name); 307 } 308 else { 309 throw new NameNotFoundException(name); 310 } 311 } 312 else { 313 Binding binding = (Binding)sctx.ctx.objects.get(sctx.name); 314 if (binding != null) { 315 Object obj = binding.getObject(); 316 if (obj instanceof Reference) { 317 try { 319 Reference ref = (Reference)obj; 320 Class c = Class.forName(ref.getFactoryClassName()); 321 ObjectFactory of = (ObjectFactory)c.newInstance(); 322 return of.getObjectInstance(obj, nameparser.parse(name), sctx.ctx, sctx.ctx.getEnvironment()); 323 } 324 catch (Exception e) { 325 throw new NamingException(e.getMessage()); 326 } 327 } 328 else { 329 return obj; 330 } 331 } 332 else { 333 return null; 334 } 335 } 336 } 337 338 public Object lookup(Name name) throws NamingException { 339 return lookup(name.toString()); 340 } 341 342 public Object lookupLink(Name name) throws NamingException { 343 return lookup(name); 344 } 345 346 public Object lookupLink(String name) throws NamingException { 347 return lookup(name); 348 } 349 350 public void rebind(String name, Object obj) throws NamingException { 351 Subcontext sctx = getSubcontext(name, false); 352 353 Binding binding = new Binding(sctx.name, obj.getClass().getName(), obj); 354 sctx.ctx.objects.put(sctx.name, binding); 355 } 356 357 public void rebind(Name name, Object obj) throws NamingException { 358 rebind(name.toString(), obj); 359 } 360 361 public Object removeFromEnvironment(String name) throws NamingException { 362 return environ.remove(name); 363 } 364 365 public void rename(String name1, String name2) throws NamingException { 366 Subcontext sctx1 = getSubcontext(name1, false); 367 Subcontext sctx2 = getSubcontext(name2, false); 368 Object obj = sctx1.ctx.objects.remove(sctx1.name); 369 370 sctx2.ctx.objects.put(sctx2.name, obj); 371 } 372 373 public void rename(Name name1, Name name2) throws NamingException { 374 rename(name1.toString(), name2.toString()); 375 } 376 377 public void unbind(String name) throws NamingException { 378 Subcontext sctx = getSubcontext(name, false); 379 sctx.ctx.objects.remove(name); 380 } 381 382 public void unbind(Name name) throws NamingException { 383 unbind(name.toString()); 384 } 385 386 387 public static void main(String [] args) throws Exception { 388 LocalContext ctx = (LocalContext)(new LocalContextFactory().getInitialContext(null)); 389 390 Context ctx2 = ctx.createSubcontext("java:comp"); 391 ctx2.createSubcontext("env"); 392 393 NamingEnumeration en = ctx.list(""); 394 while (en.hasMore()) { 395 System.out.println("root.... " + en.next()); 396 } 397 398 en = ctx.list("java:comp"); 399 while (en.hasMore()) { 400 System.out.println("java:comp.... " + en.next()); 401 } 402 } 403 }
| Popular Tags
|