1 23 package com.sun.enterprise.repository; 24 25 import java.util.*; 26 import java.io.*; 27 import javax.naming.*; 28 import java.security.*; 29 import com.sun.enterprise.util.FileUtil; 30 import java.util.logging.*; 32 import com.sun.logging.*; 33 35 39 class RepositoryContext implements Context { 40 43 private static Logger _logger=null; 45 static{ 46 _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER); 47 } 48 public static final String 50 REPOSITORY_NAME = "com.sun.enterprise.repository.name"; 51 public static final String 52 REPOSITORY_DIR = "com.sun.enterprise.repository.dir"; 53 54 Hashtable myEnv; 55 private Properties bindings; 56 private static final boolean debug = false; 57 static NameParser myParser = new RepositoryNameParser(); 58 59 RepositoryContext(Hashtable environment) { 60 myEnv = (environment != null) 61 ? (Hashtable)(environment.clone()) 62 : null; 63 resurrectTable(); 64 } 65 66 public String getNameInNamespace() throws NamingException 68 { 69 throw new OperationNotSupportedException("Context.getNameInNamespace() not implemented"); 70 } 71 72 public Object lookup(String name) throws NamingException { 73 if (name.equals("")) { 74 return (new RepositoryContext(myEnv)); 77 } 78 Object answer = bindings.get(name); 83 if (answer == null) { 84 throw new NameNotFoundException(name + " not found"); 85 } 86 return answer; 87 } 88 89 public Object lookup(Name name) throws NamingException { 90 return lookup(name.toString()); 92 } 93 94 public void bind(String name, Object obj) throws NamingException { 95 if (name.equals("")) { 96 throw new InvalidNameException("Cannot bind empty name"); 97 } 98 if (bindings.get(name) != null) { 99 throw new NameAlreadyBoundException( 100 "Use rebind to override"); 101 } 102 bindings.put(name, obj); 103 store(); 104 } 105 106 public void bind(Name name, Object obj) throws NamingException { 107 bind(name.toString(), obj); 109 } 110 111 public void rebind(String name, Object obj) throws NamingException { 112 if (name.equals("")) { 113 throw new InvalidNameException("Cannot bind empty name"); 114 } 115 bindings.put(name, obj); 116 store(); 117 } 118 119 public void rebind(Name name, Object obj) throws NamingException { 120 rebind(name.toString(), obj); 122 } 123 124 public void unbind(String name) throws NamingException { 125 if (name.equals("")) { 126 throw new InvalidNameException("Cannot unbind empty name"); 127 } 128 bindings.remove(name); 129 store(); 130 } 131 132 public void unbind(Name name) throws NamingException { 133 unbind(name.toString()); 135 } 136 137 public void rename(String oldname, String newname) 138 throws NamingException { 139 if (oldname.equals("") || newname.equals("")) { 140 throw new InvalidNameException("Cannot rename empty name"); 141 } 142 143 if (bindings.get(newname) != null) { 145 throw new NameAlreadyBoundException(newname + 146 " is already bound"); 147 } 148 149 Object oldBinding = bindings.remove(oldname); 151 if (oldBinding == null) { 152 throw new NameNotFoundException(oldname + " not bound"); 153 } 154 155 bindings.put(newname, oldBinding); 156 store(); 157 } 158 159 public void rename(Name oldname, Name newname) 160 throws NamingException { 161 rename(oldname.toString(), newname.toString()); 163 } 164 165 public NamingEnumeration list(String name) 166 throws NamingException { 167 if (name.equals("")) { 168 return new RepNames(bindings.keys()); 170 } 171 172 Object target = lookup(name); 174 if (target instanceof Context) { 175 return ((Context)target).list(""); 176 } 177 throw new NotContextException(name + " cannot be listed"); 178 } 179 180 public NamingEnumeration list(Name name) 181 throws NamingException { 182 return list(name.toString()); 184 } 185 186 public NamingEnumeration listBindings(String name) 187 throws NamingException { 188 if (name.equals("")) { 189 return new RepBindings(bindings.keys()); 191 } 192 193 Object target = lookup(name); 195 if (target instanceof Context) { 196 return ((Context)target).listBindings(""); 197 } 198 throw new NotContextException(name + " cannot be listed"); 199 } 200 201 public NamingEnumeration listBindings(Name name) 202 throws NamingException { 203 return listBindings(name.toString()); 205 } 206 207 public void destroySubcontext(String name) throws NamingException { 208 throw new OperationNotSupportedException( 209 "RepositoryContext does not support subcontexts"); 210 } 211 212 public void destroySubcontext(Name name) throws NamingException { 213 destroySubcontext(name.toString()); 215 } 216 217 public Context createSubcontext(String name) 218 throws NamingException { 219 throw new OperationNotSupportedException( 220 "RepositoryContext does not support subcontexts"); 221 } 222 223 public Context createSubcontext(Name name) throws NamingException { 224 return createSubcontext(name.toString()); 226 } 227 228 public Object lookupLink(String name) throws NamingException { 229 return lookup(name); 231 } 232 233 public Object lookupLink(Name name) throws NamingException { 234 return lookupLink(name.toString()); 236 } 237 238 public NameParser getNameParser(String name) 239 throws NamingException { 240 return myParser; 241 } 242 243 public NameParser getNameParser(Name name) throws NamingException { 244 return getNameParser(name.toString()); 246 } 247 248 public String composeName(String name, String prefix) 249 throws NamingException { 250 Name result = composeName(new CompositeName(name), 251 new CompositeName(prefix)); 252 return result.toString(); 253 } 254 255 public Name composeName(Name name, Name prefix) 256 throws NamingException { 257 Name result = (Name)(prefix.clone()); 258 result.addAll(name); 259 return result; 260 } 261 262 public Object addToEnvironment(String propName, Object propVal) 263 throws NamingException { 264 if (myEnv == null) { 265 myEnv = new Hashtable(5, 0.75f); 266 } 267 return myEnv.put(propName, propVal); 268 } 269 270 public Object removeFromEnvironment(String propName) 271 throws NamingException { 272 if (myEnv == null) 273 return null; 274 275 return myEnv.remove(propName); 276 } 277 278 public Hashtable getEnvironment() throws NamingException { 279 if (myEnv == null) { 280 myEnv = new Hashtable(3, 0.75f); 282 } 283 return myEnv; 284 } 285 286 public void close() throws NamingException { 287 store(); 288 myEnv = null; 289 bindings = null; 290 } 291 292 private void store() 293 { 294 try { 295 if (bindings != null) { 296 FileOutputStream fos = new FileOutputStream(getStoragePath()); 297 bindings.store(fos, "Repository resource mapping"); 298 fos.close(); 299 } 300 } catch (Exception e) { 301 _logger.log(Level.SEVERE,"enterprise.store_exception" ,e); 304 } 306 } 307 308 static String getFilePath(final String dirName, final String filename) { 309 310 return (String ) 311 AccessController.doPrivileged(new PrivilegedAction() { 312 public Object run() { 313 String path = filename + FILE_EXT; 314 315 if(path != null) { 316 String fname = FileUtil.getAbsolutePath(dirName + path); 318 File f = new File(fname); 319 320 if(!f.exists()) 321 path = DEFAULT_NAME + FILE_EXT; 322 } 323 else 324 path = DEFAULT_NAME + FILE_EXT; 325 326 return FileUtil.getAbsolutePath(dirName + path); 327 }}); 328 } 329 330 private String getStoragePath() { 331 String dirName = (String ) myEnv.get(REPOSITORY_DIR); 332 if (dirName == null) { 333 dirName = DEFAULT_NAMETABLE_DIR; 334 } 335 336 return getFilePath(dirName, (String ) myEnv.get(REPOSITORY_NAME)); 341 } 342 343 public static String getRepositoryName(String fname) { 344 String filepath = getFilePath(DEFAULT_NAMETABLE_DIR, fname); 345 int fileIndex = filepath.lastIndexOf(File.separator); 346 String filename = null; 347 348 if(fileIndex > 0) 349 filename = filepath.substring(fileIndex + 1); 350 else 351 filename = filepath; 352 353 int extIndex = filename.lastIndexOf(FILE_EXT); 354 String fwithoutext; 355 356 if(extIndex > 0) 357 fwithoutext = filename.substring(0, extIndex); 358 else 359 fwithoutext = filename; 360 361 return fwithoutext; 362 } 363 364 private void resurrectTable() { 365 AccessController.doPrivileged(new PrivilegedAction() { 366 public Object run() { 367 bindings = new Properties(); 368 try { 369 File f = new File(getStoragePath()); 370 if(debug) 371 _logger.log(Level.FINE,"Loaded File: " + f.getAbsolutePath()); 374 if(f.exists()) 376 bindings.load(new FileInputStream(f)); 377 } catch (Exception e) { 378 _logger.log(Level.SEVERE,"enterprise.load_exception" ,e); 381 } 383 return null; 384 }}); 385 } 386 387 private static void print(Hashtable ht) { 388 for (Enumeration en = ht.keys(); en.hasMoreElements(); ) { 389 Object key = en.nextElement(); 390 Object value = ht.get(key); 391 if (debug) 392 { 397 if(_logger.isLoggable(Level.FINE)) 398 _logger.log(Level.FINE,"[" + key + ":" + key.getClass().getName() +", " + value + ":" + value.getClass().getName()+ "]"); 399 } 400 } 402 } 403 404 class RepNames implements NamingEnumeration { 406 Enumeration names; 407 408 RepNames (Enumeration names) { 409 this.names = names; 410 } 411 412 public boolean hasMoreElements() { 413 return names.hasMoreElements(); 414 } 415 416 public boolean hasMore() throws NamingException { 417 return hasMoreElements(); 418 } 419 420 public Object nextElement() { 421 if(names.hasMoreElements()) 422 { 423 String name = (String )names.nextElement(); 424 String className = bindings.get(name).getClass().getName(); 425 return new NameClassPair(name, className); 426 } 427 else 428 return null; 429 } 430 431 public Object next() throws NamingException { 432 return nextElement(); 433 } 434 435 public void close() throws NamingException { 437 throw new OperationNotSupportedException("NamingEnumeration.close() not implemented"); 438 } 439 } 440 441 class RepBindings implements NamingEnumeration { 443 Enumeration names; 444 445 RepBindings (Enumeration names) { 446 this.names = names; 447 } 448 449 public boolean hasMoreElements() { 450 return names.hasMoreElements(); 451 } 452 453 public boolean hasMore() throws NamingException { 454 return hasMoreElements(); 455 } 456 457 public Object nextElement() { 458 if(hasMoreElements()) 459 { 460 String name = (String )names.nextElement(); 461 return new Binding(name, bindings.get(name)); 462 } 463 else 464 return null; 465 } 466 467 public Object next() throws NamingException { 468 return nextElement(); 469 } 470 471 public void close() throws NamingException { 472 throw new OperationNotSupportedException("NamingEnumeration.close() not implemented"); 473 } 474 } 475 476 private static final String 477 DEFAULT_NAMETABLE_DIR = "config/"; 478 private static final String DEFAULT_NAME = "default"; 479 private static final String FILE_EXT = ".properties"; 480 }; 481 482 483 | Popular Tags |