1 23 24 package org.enhydra.util; 25 26 import java.util.Hashtable ; 27 import java.util.StringTokenizer ; 28 import java.util.Vector ; 29 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NameClassPair ; 33 import javax.naming.NamingEnumeration ; 34 import javax.naming.NamingException ; 35 36 import com.lutris.util.KeywordValueException; 37 38 46 public class JNDIAdapter { 47 48 52 private static final int GET_ALL = -1; 53 54 57 private Context context = null; 58 private Context envCtx = null; 59 60 63 public final static String FILE_JNDI_ABS_PATH_NAME = "fileJndiAbsPathName"; 64 65 68 public final static String CONTEXT_FOR_ENV_BIND = "contextForEnvToBind"; 69 70 71 74 public JNDIAdapter() { 75 try { 76 context = new InitialContext (); 77 } 78 catch (NamingException ex){ 79 context = null; 81 } 82 83 try { 84 envCtx = (Context )context.lookup("java:comp/env"); 85 } 86 catch (NamingException ex){ 87 envCtx = null; 89 90 } 91 102 128 129 168 169 191 } 192 193 201 public JNDIAdapter(String fileAbsolutePath, String factoryClass) { 202 boolean toCompileFile = false; 203 try { 204 if ((factoryClass != null) && (!factoryClass.equals(""))) { 205 Hashtable env = new Hashtable (); 206 env.put(Context.INITIAL_CONTEXT_FACTORY, factoryClass); 207 context = new InitialContext (env); 208 toCompileFile = true; 209 } 210 else { 211 System.err.println("Warning: InitialContextFactory class not defined. Default will be used."); 212 context = new InitialContext (); 213 } 214 } 215 catch (NamingException ex){ 216 context = null; 218 } 219 try { 220 envCtx = (Context )context.lookup("java:comp/env"); 221 } 222 catch (NamingException ex){ 223 envCtx = null; 225 } 226 227 try { 228 if (toCompileFile){ 229 context.rebind(CONTEXT_FOR_ENV_BIND, envCtx); 230 context.rebind(FILE_JNDI_ABS_PATH_NAME, fileAbsolutePath); 231 } 232 } 233 catch (Exception ex){ 234 System.err.println("Error in creating JNDIAdapter(String)"); 235 } 237 238 } 239 240 246 public boolean containsKey(String key) { 247 try { 248 if (envCtx.lookup(key) != null) 249 return true; 250 } 251 catch (Exception ex){ } 252 253 try { 254 if (envCtx.lookup(key+"[]") != null) 255 return true; 256 else 257 return false; 258 } 259 catch (Exception ex){ 260 return false; 261 } 262 } 263 264 270 public synchronized String [] keys() { 271 Vector keyList = null; 272 try { 273 if (context != null) { 274 NamingEnumeration enumeration = envCtx.list(""); 275 if ((enumeration != null) && (enumeration.hasMore())) { 276 keyList = new Vector (); 277 String toAdd = null; 278 for (NamingEnumeration en = enumeration; en.hasMore();) { 279 toAdd = ((NameClassPair )en.next()).getName(); 280 keyList.addElement(toAdd); 281 } } 283 } 284 } 287 catch (NamingException ex){ 288 System.err.println("Error in keys() method"); 289 keyList = null; 291 } 292 293 String [] keyStrings = null; 294 if (keyList != null) { 295 keyStrings = new String [keyList.size()]; 296 for (int idx = 0; idx < keyList.size (); idx++) { 297 keyStrings [idx] = (String ) keyList.elementAt (idx); 298 } 299 } 300 return keyStrings; 301 } 302 303 309 public synchronized String [] leafKeys() { 310 NamingEnumeration keyEnum = null; 311 Vector keyList = new Vector (); 312 try { 313 keyEnum = envCtx.list(""); 314 } 315 catch (NamingException ex){ 316 System.err.println("Error in leafKeys() method"); 317 keyEnum = null; 319 } 320 try { 321 if ((keyEnum != null) && (keyEnum.hasMore())) { 322 while (keyEnum.hasMore()) { 323 String key = ((NameClassPair )keyEnum.next()).getName(); 324 Object value = envCtx.lookup(key); 325 if (implementsContextInterface(value)) { 326 String [] subKeys = leafKeysInternal((Context )value); 327 for (int idx = 0; idx < subKeys.length; idx++) { 328 keyList.addElement( key+"/"+subKeys[idx]); 329 } 330 } else { 331 keyList.addElement(key); 332 } 333 } 334 } 335 } 336 catch (NamingException ex){ 337 System.err.println("Error occured in leafKeys() method"); 338 keyList = null; 340 } 341 if (keyList != null){ 342 String [] keyStrings = new String [keyList.size()]; 343 for (int idx = 0; idx < keyList.size(); idx++) { 344 keyStrings [idx] = (String )keyList.elementAt(idx); 345 } 346 return keyStrings; 347 } 348 return null; 349 } 350 351 private synchronized String [] leafKeysInternal(Context con) { 352 NamingEnumeration keyEnum = null; 353 Vector keyList = new Vector (); 354 try { 355 keyEnum = con.list(""); 356 } 357 catch (NamingException ex){ 358 System.err.println("Error in leafKeys() method"); 359 keyEnum = null; 361 } 362 try { 363 if ((keyEnum != null) && (keyEnum.hasMore())) { 364 while (keyEnum.hasMore()) { 365 String key = ((NameClassPair )keyEnum.next()).getName(); 366 Object value = con.lookup(key); 367 368 if (implementsContextInterface(value)) { 369 370 String [] subKeys = leafKeysInternal((Context )value); 371 for (int idx = 0; idx < subKeys.length; idx++) { 372 keyList.addElement( key + "/" + subKeys[idx]); 373 } 374 } else { 375 keyList.addElement(key); 376 } 377 } 378 } 379 } 380 catch (NamingException ex){ 381 System.err.println("Error occured in leafKeys() method"); 382 keyList = null; 384 } 385 if (keyList != null){ 386 String [] keyStrings = new String [keyList.size()]; 387 for (int idx = 0; idx < keyList.size(); idx++) { 388 keyStrings [idx] = (String )keyList.elementAt(idx); 389 } 390 return keyStrings; 391 } 392 return null; 393 } 394 395 404 public synchronized Object get(String keyword) 405 throws NamingException { 406 Object obj = null; 407 try { 408 obj = envCtx.lookup(keyword); 409 } 410 catch (Exception ex ){ 411 obj = null; 412 } 413 if (obj != null) 414 return obj; 415 else { 416 return envCtx.lookup(keyword + "[]"); 417 } 418 } 419 420 432 public synchronized Object get(String keyword, Object defaultValue) 433 throws NamingException { 434 Object value = null; 435 try { 436 value = get(keyword); 437 } 438 catch (Exception e){ 439 value = null; 440 } 441 if (value == null) { 442 return defaultValue; 443 } 444 return value; 445 } 446 447 public synchronized Object getResource(String jndiName) 448 throws NamingException { 449 Object obj = null; 450 try { 451 obj = context.lookup(jndiName); 452 } 453 catch (Exception ex ){ 454 obj = null; 455 } 457 if (obj == null) { 458 try { 459 obj = envCtx.lookup(jndiName); 460 } 461 catch (Exception ex ){ 462 obj = null; 463 } 465 } 466 return obj; 467 } 468 469 475 public synchronized void set(String keyword, String value) { 476 if (envCtx != null) { 477 try { 478 envCtx.rebind(keyword, value); 479 } 480 catch (NamingException ex){ 481 } 483 } 484 else { 485 } 488 } 489 490 495 public synchronized void remove(String keyword) { 496 if (envCtx != null) { 497 try { 498 envCtx.unbind(keyword); 499 } 500 catch (NamingException ex){ 501 } 503 } 504 } 505 506 511 519 520 528 private static boolean implementsContextInterface(Object obj) { 529 Class [] interfaces = obj.getClass().getInterfaces(); 530 int length = interfaces.length; 531 int i = 0; 532 while (i<length){ 533 if (interfaces[i].getName().equals("javax.naming.Context")) 534 return true; 535 i++; 536 } 537 return false; 538 } 539 540 547 public static String makeConfigString (String oldString) { 548 if (oldString == null) 549 return null; 550 String newString = null; 551 newString = oldString.replaceAll("/", "."); 552 return newString; 553 } 554 555 562 public static String makeContextString (String oldString) { 563 if (oldString == null) 564 return null; 565 String newString = null; 566 int len = oldString.length(); 567 if (len >0) 568 newString = new String (""); 569 for (int i=0; i<len; i++){ 570 char ch = oldString.charAt(i); 571 if (ch == '.') { 572 newString = newString + "/"; 573 } 574 else { 575 newString = newString + ch; 576 } 577 } 578 return newString; 579 } 580 581 588 public static String makeStringFromStrings (String [] strings) { 589 if (strings == null) 590 return null; 591 String newString = null; 592 int len = strings.length; 593 if (len >0) 594 newString = strings[0].trim(); 595 if (len > 1) { 596 for (int i=1; i<len; i++){ 597 String str = strings[i].trim(); 598 newString = newString + "," + str; 599 } 600 } 601 return newString; 602 } 603 604 612 public static boolean isArray(String key) { 613 int len = key.length(); 614 if (len < 3) 615 return false; 616 if (key.substring(len-2).equals("[]")) 617 return true; 618 else 619 return false; 620 } 621 622 628 public String removeArrayMark (String key) { 629 if (isArray(key)) { 630 int len = key.length(); 631 return key.substring(0, len-2); 632 } 633 else 634 return null; 635 } 636 637 644 public String [] getStrings (String key) { 645 try { 646 Object returnObject = get(key); 647 if (returnObject.getClass().isArray()) { 648 return (String [])returnObject; 649 } 650 String oldValue = (String )returnObject; 651 StringTokenizer tok = new StringTokenizer (oldValue, new String (",")); 652 String [] newValue = new String [tok.countTokens()]; 653 int i = 0; 654 while (tok.hasMoreTokens()) { 655 newValue[i] = tok.nextToken().trim(); 656 i++; 657 } 658 return newValue; 659 } 660 catch (Exception e){ 661 System.err.println("Error in getStrings() method of JNDIAdapter."); 663 return null; 664 } 665 } 666 667 668 669 670 } 671 | Popular Tags |