1 16 17 package org.apache.naming.modules.cache; 18 19 import java.util.Collections ; 20 import java.util.Hashtable ; 21 import java.util.Map ; 22 23 import javax.naming.Context ; 24 import javax.naming.Name ; 25 import javax.naming.NameParser ; 26 import javax.naming.NamingEnumeration ; 27 import javax.naming.NamingException ; 28 import javax.naming.directory.Attributes ; 29 import javax.naming.directory.DirContext ; 30 import javax.naming.directory.ModificationItem ; 31 import javax.naming.directory.SearchControls ; 32 33 import org.apache.commons.collections.LRUMap; 34 import org.apache.naming.core.BaseDirContext; 35 import org.apache.naming.util.AttributeHelper; 36 import org.apache.tomcat.util.res.StringManager; 37 38 54 55 56 57 66 public class ProxyDirContext implements DirContext { 67 private static org.apache.commons.logging.Log log= 68 org.apache.commons.logging.LogFactory.getLog( ProxyDirContext.class ); 69 70 71 73 74 77 public ProxyDirContext(Hashtable env, DirContext dirContext) { 78 this.env = env; 79 this.dirContext = dirContext; 80 if (dirContext instanceof BaseDirContext) { 81 if (((BaseDirContext) dirContext).isCached()) { 84 cache = Collections.synchronizedMap(new LRUMap(cacheSize)); 85 cacheTTL = ((BaseDirContext) dirContext).getCacheTTL(); 86 cacheObjectMaxSize = 87 ((BaseDirContext) dirContext).getCacheObjectMaxSize(); 88 } 89 } 90 } 91 92 96 protected ProxyDirContext(ProxyDirContext proxyDirContext, 97 DirContext dirContext) { 98 this.env = proxyDirContext.env; 99 this.dirContext = dirContext; 100 this.cache = proxyDirContext.cache; 101 this.cacheSize = proxyDirContext.cacheSize; 102 this.cacheTTL = proxyDirContext.cacheTTL; 103 this.cacheObjectMaxSize = proxyDirContext.cacheObjectMaxSize; 104 } 105 106 107 109 110 113 protected Hashtable env; 114 115 116 119 protected StringManager sm = StringManager.getManager("org.apache.naming.res"); 120 121 122 125 protected DirContext dirContext; 126 127 128 132 protected Map cache = null; 133 134 135 138 protected int cacheSize = 1000; 139 140 141 144 protected int cacheTTL = 5000; 146 147 150 protected int cacheObjectMaxSize = 32768; 152 153 155 156 159 public DirContext getDirContext() { 160 return this.dirContext; 161 } 162 163 164 166 167 177 public Object lookup(Name name) 178 throws NamingException { 179 CacheEntry entry = cacheLookupAndLoad(name.toString()); 180 if (entry != null) { 181 if (entry.resource != null) { 182 184 return entry.resource; 185 } else { 186 return entry.context; 187 } 188 } 189 log.info("Strange, entry was no loadeded " + name ); 190 Object object = dirContext.lookup(parseName(name)); 191 return object; 195 } 196 197 198 205 public Object lookup(String name) 206 throws NamingException { 207 CacheEntry entry = cacheLookupAndLoad(name); 208 if (entry != null) { 209 if (entry.resource != null) { 210 return entry.resource; 211 } else { 212 return entry.context; 213 } 214 } 215 log.info("Strange, entry was no loadeded " + name ); 216 217 Object object = dirContext.lookup(parseName(name)); 218 return object; 229 } 230 231 232 244 public void bind(Name name, Object obj) 245 throws NamingException { 246 dirContext.bind(parseName(name), obj); 247 cacheUnload(name.toString()); 248 } 249 250 251 261 public void bind(String name, Object obj) 262 throws NamingException { 263 dirContext.bind(parseName(name), obj); 264 cacheUnload(name); 265 } 266 267 268 283 public void rebind(Name name, Object obj) 284 throws NamingException { 285 dirContext.rebind(parseName(name), obj); 286 cacheUnload(name.toString()); 287 } 288 289 290 299 public void rebind(String name, Object obj) 300 throws NamingException { 301 dirContext.rebind(parseName(name), obj); 302 cacheUnload(name); 303 } 304 305 306 320 public void unbind(Name name) 321 throws NamingException { 322 dirContext.unbind(parseName(name)); 323 cacheUnload(name.toString()); 324 } 325 326 327 335 public void unbind(String name) 336 throws NamingException { 337 dirContext.unbind(parseName(name)); 338 cacheUnload(name); 339 } 340 341 342 353 public void rename(Name oldName, Name newName) 354 throws NamingException { 355 dirContext.rename(parseName(oldName), parseName(newName)); 356 cacheUnload(oldName.toString()); 357 } 358 359 360 369 public void rename(String oldName, String newName) 370 throws NamingException { 371 dirContext.rename(parseName(oldName), parseName(newName)); 372 cacheUnload(oldName); 373 } 374 375 376 389 public NamingEnumeration list(Name name) 390 throws NamingException { 391 return dirContext.list(parseName(name)); 392 } 393 394 395 404 public NamingEnumeration list(String name) 405 throws NamingException { 406 return dirContext.list(parseName(name)); 407 } 408 409 410 423 public NamingEnumeration listBindings(Name name) 424 throws NamingException { 425 return dirContext.listBindings(parseName(name)); 426 } 427 428 429 438 public NamingEnumeration listBindings(String name) 439 throws NamingException { 440 return dirContext.listBindings(parseName(name)); 441 } 442 443 444 469 public void destroySubcontext(Name name) 470 throws NamingException { 471 dirContext.destroySubcontext(parseName(name)); 472 cacheUnload(name.toString()); 473 } 474 475 476 485 public void destroySubcontext(String name) 486 throws NamingException { 487 dirContext.destroySubcontext(parseName(name)); 488 cacheUnload(name); 489 } 490 491 492 505 public Context createSubcontext(Name name) 506 throws NamingException { 507 return dirContext.createSubcontext(parseName(name)); 508 } 509 510 511 521 public Context createSubcontext(String name) 522 throws NamingException { 523 return dirContext.createSubcontext(parseName(name)); 524 } 525 526 527 537 public Object lookupLink(Name name) 538 throws NamingException { 539 return dirContext.lookupLink(parseName(name)); 540 } 541 542 543 552 public Object lookupLink(String name) 553 throws NamingException { 554 return dirContext.lookupLink(parseName(name)); 555 } 556 557 558 572 public NameParser getNameParser(Name name) 573 throws NamingException { 574 return dirContext.getNameParser(parseName(name)); 575 } 576 577 578 586 public NameParser getNameParser(String name) 587 throws NamingException { 588 return dirContext.getNameParser(parseName(name)); 589 } 590 591 592 607 public Name composeName(Name name, Name prefix) 608 throws NamingException { 609 prefix = (Name ) name.clone(); 610 return prefix.addAll(name); 611 } 612 613 614 622 public String composeName(String name, String prefix) 623 throws NamingException { 624 return prefix + "/" + name; 625 } 626 627 628 637 public Object addToEnvironment(String propName, Object propVal) 638 throws NamingException { 639 return dirContext.addToEnvironment(propName, propVal); 640 } 641 642 643 650 public Object removeFromEnvironment(String propName) 651 throws NamingException { 652 return dirContext.removeFromEnvironment(propName); 653 } 654 655 656 666 public Hashtable getEnvironment() 667 throws NamingException { 668 return dirContext.getEnvironment(); 669 } 670 671 672 682 public void close() 683 throws NamingException { 684 dirContext.close(); 685 } 686 687 688 705 public String getNameInNamespace() 706 throws NamingException { 707 return dirContext.getNameInNamespace(); 708 } 709 710 711 713 714 722 public Attributes getAttributes(Name name) 723 throws NamingException { 724 CacheEntry entry = cacheLookupAndLoad(name.toString()); 725 if (entry != null) { 726 return entry.attributes; 727 } 728 Attributes attributes = dirContext.getAttributes(parseName(name)); 729 730 return attributes; 735 } 736 737 738 745 public Attributes getAttributes(String name) 746 throws NamingException { 747 CacheEntry entry = cacheLookupAndLoad(name); 748 if (entry != null) { 749 return entry.attributes; 750 } 751 Attributes attributes = dirContext.getAttributes(parseName(name)); 752 return attributes; 756 } 757 758 759 771 public Attributes getAttributes(Name name, String [] attrIds) 772 throws NamingException { 773 Attributes attributes = 774 dirContext.getAttributes(parseName(name), attrIds); 775 return attributes; 779 } 780 781 782 792 public Attributes getAttributes(String name, String [] attrIds) 793 throws NamingException { 794 Attributes attributes = 795 dirContext.getAttributes(parseName(name), attrIds); 796 return attributes; 800 } 801 802 803 817 public void modifyAttributes(Name name, int mod_op, Attributes attrs) 818 throws NamingException { 819 dirContext.modifyAttributes(parseName(name), mod_op, attrs); 820 } 821 822 823 835 public void modifyAttributes(String name, int mod_op, Attributes attrs) 836 throws NamingException { 837 dirContext.modifyAttributes(parseName(name), mod_op, attrs); 838 } 839 840 841 855 public void modifyAttributes(Name name, ModificationItem [] mods) 856 throws NamingException { 857 dirContext.modifyAttributes(parseName(name), mods); 858 } 859 860 861 872 public void modifyAttributes(String name, ModificationItem [] mods) 873 throws NamingException { 874 dirContext.modifyAttributes(parseName(name), mods); 875 } 876 877 878 893 public void bind(Name name, Object obj, Attributes attrs) 894 throws NamingException { 895 dirContext.bind(parseName(name), obj, attrs); 896 } 897 898 899 910 public void bind(String name, Object obj, Attributes attrs) 911 throws NamingException { 912 dirContext.bind(parseName(name), obj, attrs); 913 } 914 915 916 934 public void rebind(Name name, Object obj, Attributes attrs) 935 throws NamingException { 936 dirContext.rebind(parseName(name), obj, attrs); 937 } 938 939 940 951 public void rebind(String name, Object obj, Attributes attrs) 952 throws NamingException { 953 dirContext.rebind(parseName(name), obj, attrs); 954 } 955 956 957 974 public DirContext createSubcontext(Name name, Attributes attrs) 975 throws NamingException { 976 return dirContext.createSubcontext(parseName(name), attrs); 977 } 978 979 980 991 public DirContext createSubcontext(String name, Attributes attrs) 992 throws NamingException { 993 return dirContext.createSubcontext(parseName(name), attrs); 994 } 995 996 997 1010 public DirContext getSchema(Name name) 1011 throws NamingException { 1012 return dirContext.getSchema(parseName(name)); 1013 } 1014 1015 1016 1024 public DirContext getSchema(String name) 1025 throws NamingException { 1026 return dirContext.getSchema(parseName(name)); 1027 } 1028 1029 1030 1041 public DirContext getSchemaClassDefinition(Name name) 1042 throws NamingException { 1043 return dirContext.getSchemaClassDefinition(parseName(name)); 1044 } 1045 1046 1047 1058 public DirContext getSchemaClassDefinition(String name) 1059 throws NamingException { 1060 return dirContext.getSchemaClassDefinition(parseName(name)); 1061 } 1062 1063 1064 1081 public NamingEnumeration search(Name name, Attributes matchingAttributes, 1082 String [] attributesToReturn) 1083 throws NamingException { 1084 return dirContext.search(parseName(name), matchingAttributes, 1085 attributesToReturn); 1086 } 1087 1088 1089 1105 public NamingEnumeration search(String name, Attributes matchingAttributes, 1106 String [] attributesToReturn) 1107 throws NamingException { 1108 return dirContext.search(parseName(name), matchingAttributes, 1109 attributesToReturn); 1110 } 1111 1112 1113 1128 public NamingEnumeration search(Name name, Attributes matchingAttributes) 1129 throws NamingException { 1130 return dirContext.search(parseName(name), matchingAttributes); 1131 } 1132 1133 1134 1147 public NamingEnumeration search(String name, Attributes matchingAttributes) 1148 throws NamingException { 1149 return dirContext.search(parseName(name), matchingAttributes); 1150 } 1151 1152 1153 1172 public NamingEnumeration search(Name name, String filter, 1173 SearchControls cons) 1174 throws NamingException { 1175 return dirContext.search(parseName(name), filter, cons); 1176 } 1177 1178 1179 1198 public NamingEnumeration search(String name, String filter, 1199 SearchControls cons) 1200 throws NamingException { 1201 return dirContext.search(parseName(name), filter, cons); 1202 } 1203 1204 1205 1229 public NamingEnumeration search(Name name, String filterExpr, 1230 Object [] filterArgs, SearchControls cons) 1231 throws NamingException { 1232 return dirContext.search(parseName(name), filterExpr, filterArgs, 1233 cons); 1234 } 1235 1236 1237 1261 public NamingEnumeration search(String name, String filterExpr, 1262 Object [] filterArgs, SearchControls cons) 1263 throws NamingException { 1264 return dirContext.search(parseName(name), filterExpr, filterArgs, 1265 cons); 1266 } 1267 1268 1269 1271 1272 1277 protected String parseName(String name) 1278 throws NamingException { 1279 return name; 1280 } 1281 1282 1283 1288 protected Name parseName(Name name) 1289 throws NamingException { 1290 return name; 1291 } 1292 1293 1294 1297 protected CacheEntry cacheLookupAndLoad(String name) { 1298 if (cache == null) 1299 return (null); 1300 CacheEntry cacheEntry = (CacheEntry) cache.get(name); 1301 if (cacheEntry == null) { 1302 cacheEntry = new CacheEntry(); 1303 cacheEntry.name = name; 1304 if (!cacheLoad(cacheEntry)) 1306 return null; 1307 return (cacheEntry); 1308 } else { 1309 if (!validate(cacheEntry)) { 1310 if (!revalidate(cacheEntry)) { 1311 cacheUnload(cacheEntry.name); 1312 return (null); 1313 } else { 1314 cacheEntry.timestamp = 1315 System.currentTimeMillis() + cacheTTL; 1316 } 1317 } 1318 return (cacheEntry); 1319 } 1320 } 1321 1322 1323 1326 protected boolean validate(CacheEntry entry) { 1327 if ((entry.resource != null) 1328 && (System.currentTimeMillis() < entry.timestamp)) { 1330 return true; 1331 } 1332 return false; 1333 } 1334 1335 1336 1339 protected boolean revalidate(CacheEntry entry) { 1340 if (entry.attributes == null) 1343 return false; 1344 long lastModified = AttributeHelper.getLastModified(entry.attributes); 1345 if (lastModified <= 0) 1347 return false; 1348 try { 1349 Attributes attributes = dirContext.getAttributes(entry.name); 1351 long lastModified2 = AttributeHelper.getLastModified(attributes); 1358 return (lastModified == lastModified2) ; 1360 } catch (NamingException e) { 1362 return false; 1363 } 1364 } 1365 1366 1367 1370 protected boolean cacheLoad(CacheEntry entry) { 1371 1372 if (cache == null) 1373 return false; 1374 1375 String name = entry.name; 1376 1377 1379 if (entry.attributes == null) { 1381 try { 1382 Attributes attributes = dirContext.getAttributes(entry.name); 1383 } catch (NamingException e) { 1390 return false; 1391 } 1392 } 1393 1394 if ((entry.resource == null) && (entry.context == null)) { 1396 try { 1397 Object object = dirContext.lookup(name); 1398 entry.resource=object; 1409 } catch (NamingException e) { 1410 return false; 1411 } 1412 } 1413 1414 1417 1447 entry.timestamp = System.currentTimeMillis() + cacheTTL; 1449 1450 cache.put(name, entry); 1452 1453 return true; 1454 1455 } 1456 1457 1458 1461 protected boolean cacheUnload(String name) { 1462 if (cache == null) 1463 return false; 1464 return (cache.remove(name) != null); 1465 } 1466 1467 1469 1470 protected class CacheEntry { 1471 1472 1473 1475 1476 long timestamp = -1; 1477 String name = null; 1478 Attributes attributes = null; 1479 Object resource = null; 1481 DirContext context = null; 1482 1483 1484 1486 1487 public void recycle() { 1488 timestamp = -1; 1489 name = null; 1490 attributes = null; 1491 resource = null; 1492 context = null; 1493 } 1494 1495 1496 public String toString() { 1497 return ("Cache entry: " + name + "\n" 1498 + "Attributes: " + attributes + "\n" 1499 + "Resource: " + resource + "\n" 1500 + "Context: " + context); 1501 } 1502 1503 1504 } 1505 1506 1507} 1508 1509 | Popular Tags |