1 17 18 19 package org.apache.naming.resources; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Hashtable ; 25 26 import javax.naming.Context ; 27 import javax.naming.Name ; 28 import javax.naming.NameNotFoundException ; 29 import javax.naming.NameParser ; 30 import javax.naming.NamingEnumeration ; 31 import javax.naming.NamingException ; 32 import javax.naming.directory.Attributes ; 33 import javax.naming.directory.DirContext ; 34 import javax.naming.directory.ModificationItem ; 35 import javax.naming.directory.SearchControls ; 36 37 import org.apache.naming.StringManager; 38 39 45 46 public class ProxyDirContext implements DirContext { 47 48 49 51 52 public static final String CONTEXT = "context"; 53 public static final String HOST = "host"; 54 55 56 58 59 62 public ProxyDirContext(Hashtable env, DirContext dirContext) { 63 this.env = env; 64 this.dirContext = dirContext; 65 if (dirContext instanceof BaseDirContext) { 66 BaseDirContext baseDirContext = (BaseDirContext) dirContext; 69 if (baseDirContext.isCached()) { 70 try { 71 cache = (ResourceCache) 72 Class.forName(cacheClassName).newInstance(); 73 } catch (Exception e) { 74 e.printStackTrace(); 76 } 77 cache.setCacheMaxSize(baseDirContext.getCacheMaxSize()); 78 cacheTTL = baseDirContext.getCacheTTL(); 79 cacheObjectMaxSize = baseDirContext.getCacheMaxSize() / 20; 80 } 81 } 82 hostName = (String ) env.get(HOST); 83 contextName = (String ) env.get(CONTEXT); 84 } 85 86 87 91 108 109 110 112 113 116 protected ProxyDirContext proxy = this; 117 118 119 122 protected Hashtable env; 123 124 125 128 protected StringManager sm = StringManager.getManager(Constants.Package); 129 130 131 134 protected DirContext dirContext; 135 136 137 140 protected String vPath = null; 141 142 143 146 protected String hostName; 147 148 149 152 protected String contextName; 153 154 155 158 protected String cacheClassName = 159 "org.apache.naming.resources.ResourceCache"; 160 161 162 165 protected ResourceCache cache = null; 166 167 168 171 protected int cacheTTL = 5000; 173 174 177 protected int cacheObjectMaxSize = 512; 179 180 183 protected NameNotFoundException notFoundException = 184 new ImmutableNameNotFoundException(); 185 186 187 190 protected String [] nonCacheable = { "/WEB-INF/lib/", "/WEB-INF/classes/" }; 191 192 193 195 196 199 public ResourceCache getCache() { 200 return cache; 201 } 202 203 204 207 public DirContext getDirContext() { 208 return this.dirContext; 209 } 210 211 212 215 public String getDocBase() { 216 if (dirContext instanceof BaseDirContext) 217 return ((BaseDirContext) dirContext).getDocBase(); 218 else 219 return ""; 220 } 221 222 223 226 public String getHostName() { 227 return this.hostName; 228 } 229 230 231 234 public String getContextName() { 235 return this.contextName; 236 } 237 238 239 241 242 252 public Object lookup(Name name) 253 throws NamingException { 254 CacheEntry entry = cacheLookup(name.toString()); 255 if (entry != null) { 256 if (!entry.exists) { 257 throw notFoundException; 258 } 259 if (entry.resource != null) { 260 return entry.resource; 262 } else { 263 return entry.context; 264 } 265 } 266 Object object = dirContext.lookup(parseName(name)); 267 if (object instanceof InputStream ) 268 return new Resource((InputStream ) object); 269 else 270 return object; 271 } 272 273 274 281 public Object lookup(String name) 282 throws NamingException { 283 CacheEntry entry = cacheLookup(name); 284 if (entry != null) { 285 if (!entry.exists) { 286 throw notFoundException; 287 } 288 if (entry.resource != null) { 289 return entry.resource; 290 } else { 291 return entry.context; 292 } 293 } 294 Object object = dirContext.lookup(parseName(name)); 295 if (object instanceof InputStream ) { 296 return new Resource((InputStream ) object); 297 } else if (object instanceof DirContext ) { 298 return object; 299 } else if (object instanceof Resource) { 300 return object; 301 } else { 302 return new Resource(new ByteArrayInputStream 303 (object.toString().getBytes())); 304 } 305 } 306 307 308 320 public void bind(Name name, Object obj) 321 throws NamingException { 322 dirContext.bind(parseName(name), obj); 323 cacheUnload(name.toString()); 324 } 325 326 327 337 public void bind(String name, Object obj) 338 throws NamingException { 339 dirContext.bind(parseName(name), obj); 340 cacheUnload(name); 341 } 342 343 344 359 public void rebind(Name name, Object obj) 360 throws NamingException { 361 dirContext.rebind(parseName(name), obj); 362 cacheUnload(name.toString()); 363 } 364 365 366 375 public void rebind(String name, Object obj) 376 throws NamingException { 377 dirContext.rebind(parseName(name), obj); 378 cacheUnload(name); 379 } 380 381 382 396 public void unbind(Name name) 397 throws NamingException { 398 dirContext.unbind(parseName(name)); 399 cacheUnload(name.toString()); 400 } 401 402 403 411 public void unbind(String name) 412 throws NamingException { 413 dirContext.unbind(parseName(name)); 414 cacheUnload(name); 415 } 416 417 418 429 public void rename(Name oldName, Name newName) 430 throws NamingException { 431 dirContext.rename(parseName(oldName), parseName(newName)); 432 cacheUnload(oldName.toString()); 433 } 434 435 436 445 public void rename(String oldName, String newName) 446 throws NamingException { 447 dirContext.rename(parseName(oldName), parseName(newName)); 448 cacheUnload(oldName); 449 } 450 451 452 465 public NamingEnumeration list(Name name) 466 throws NamingException { 467 return dirContext.list(parseName(name)); 468 } 469 470 471 480 public NamingEnumeration list(String name) 481 throws NamingException { 482 return dirContext.list(parseName(name)); 483 } 484 485 486 499 public NamingEnumeration listBindings(Name name) 500 throws NamingException { 501 return dirContext.listBindings(parseName(name)); 502 } 503 504 505 514 public NamingEnumeration listBindings(String name) 515 throws NamingException { 516 return dirContext.listBindings(parseName(name)); 517 } 518 519 520 545 public void destroySubcontext(Name name) 546 throws NamingException { 547 dirContext.destroySubcontext(parseName(name)); 548 cacheUnload(name.toString()); 549 } 550 551 552 561 public void destroySubcontext(String name) 562 throws NamingException { 563 dirContext.destroySubcontext(parseName(name)); 564 cacheUnload(name); 565 } 566 567 568 581 public Context createSubcontext(Name name) 582 throws NamingException { 583 Context context = dirContext.createSubcontext(parseName(name)); 584 cacheUnload(name.toString()); 585 return context; 586 } 587 588 589 599 public Context createSubcontext(String name) 600 throws NamingException { 601 Context context = dirContext.createSubcontext(parseName(name)); 602 cacheUnload(name); 603 return context; 604 } 605 606 607 617 public Object lookupLink(Name name) 618 throws NamingException { 619 return dirContext.lookupLink(parseName(name)); 620 } 621 622 623 632 public Object lookupLink(String name) 633 throws NamingException { 634 return dirContext.lookupLink(parseName(name)); 635 } 636 637 638 652 public NameParser getNameParser(Name name) 653 throws NamingException { 654 return dirContext.getNameParser(parseName(name)); 655 } 656 657 658 666 public NameParser getNameParser(String name) 667 throws NamingException { 668 return dirContext.getNameParser(parseName(name)); 669 } 670 671 672 687 public Name composeName(Name name, Name prefix) 688 throws NamingException { 689 prefix = (Name ) prefix.clone(); 690 return prefix.addAll(name); 691 } 692 693 694 702 public String composeName(String name, String prefix) 703 throws NamingException { 704 return prefix + "/" + name; 705 } 706 707 708 717 public Object addToEnvironment(String propName, Object propVal) 718 throws NamingException { 719 return dirContext.addToEnvironment(propName, propVal); 720 } 721 722 723 730 public Object removeFromEnvironment(String propName) 731 throws NamingException { 732 return dirContext.removeFromEnvironment(propName); 733 } 734 735 736 746 public Hashtable getEnvironment() 747 throws NamingException { 748 return dirContext.getEnvironment(); 749 } 750 751 752 762 public void close() 763 throws NamingException { 764 dirContext.close(); 765 } 766 767 768 785 public String getNameInNamespace() 786 throws NamingException { 787 return dirContext.getNameInNamespace(); 788 } 789 790 791 793 794 802 public Attributes getAttributes(Name name) 803 throws NamingException { 804 CacheEntry entry = cacheLookup(name.toString()); 805 if (entry != null) { 806 if (!entry.exists) { 807 throw notFoundException; 808 } 809 return entry.attributes; 810 } 811 Attributes attributes = dirContext.getAttributes(parseName(name)); 812 if (!(attributes instanceof ResourceAttributes)) { 813 attributes = new ResourceAttributes(attributes); 814 } 815 return attributes; 816 } 817 818 819 826 public Attributes getAttributes(String name) 827 throws NamingException { 828 CacheEntry entry = cacheLookup(name); 829 if (entry != null) { 830 if (!entry.exists) { 831 throw notFoundException; 832 } 833 return entry.attributes; 834 } 835 Attributes attributes = dirContext.getAttributes(parseName(name)); 836 if (!(attributes instanceof ResourceAttributes)) { 837 attributes = new ResourceAttributes(attributes); 838 } 839 return attributes; 840 } 841 842 843 855 public Attributes getAttributes(Name name, String [] attrIds) 856 throws NamingException { 857 Attributes attributes = 858 dirContext.getAttributes(parseName(name), attrIds); 859 if (!(attributes instanceof ResourceAttributes)) { 860 attributes = new ResourceAttributes(attributes); 861 } 862 return attributes; 863 } 864 865 866 876 public Attributes getAttributes(String name, String [] attrIds) 877 throws NamingException { 878 Attributes attributes = 879 dirContext.getAttributes(parseName(name), attrIds); 880 if (!(attributes instanceof ResourceAttributes)) { 881 attributes = new ResourceAttributes(attributes); 882 } 883 return attributes; 884 } 885 886 887 901 public void modifyAttributes(Name name, int mod_op, Attributes attrs) 902 throws NamingException { 903 dirContext.modifyAttributes(parseName(name), mod_op, attrs); 904 cacheUnload(name.toString()); 905 } 906 907 908 920 public void modifyAttributes(String name, int mod_op, Attributes attrs) 921 throws NamingException { 922 dirContext.modifyAttributes(parseName(name), mod_op, attrs); 923 cacheUnload(name); 924 } 925 926 927 941 public void modifyAttributes(Name name, ModificationItem [] mods) 942 throws NamingException { 943 dirContext.modifyAttributes(parseName(name), mods); 944 cacheUnload(name.toString()); 945 } 946 947 948 959 public void modifyAttributes(String name, ModificationItem [] mods) 960 throws NamingException { 961 dirContext.modifyAttributes(parseName(name), mods); 962 cacheUnload(name); 963 } 964 965 966 981 public void bind(Name name, Object obj, Attributes attrs) 982 throws NamingException { 983 dirContext.bind(parseName(name), obj, attrs); 984 cacheUnload(name.toString()); 985 } 986 987 988 999 public void bind(String name, Object obj, Attributes attrs) 1000 throws NamingException { 1001 dirContext.bind(parseName(name), obj, attrs); 1002 cacheUnload(name); 1003 } 1004 1005 1006 1024 public void rebind(Name name, Object obj, Attributes attrs) 1025 throws NamingException { 1026 dirContext.rebind(parseName(name), obj, attrs); 1027 cacheUnload(name.toString()); 1028 } 1029 1030 1031 1042 public void rebind(String name, Object obj, Attributes attrs) 1043 throws NamingException { 1044 dirContext.rebind(parseName(name), obj, attrs); 1045 cacheUnload(name); 1046 } 1047 1048 1049 1066 public DirContext createSubcontext(Name name, Attributes attrs) 1067 throws NamingException { 1068 DirContext context = 1069 dirContext.createSubcontext(parseName(name), attrs); 1070 cacheUnload(name.toString()); 1071 return context; 1072 } 1073 1074 1075 1086 public DirContext createSubcontext(String name, Attributes attrs) 1087 throws NamingException { 1088 DirContext context = 1089 dirContext.createSubcontext(parseName(name), attrs); 1090 cacheUnload(name); 1091 return context; 1092 } 1093 1094 1095 1108 public DirContext getSchema(Name name) 1109 throws NamingException { 1110 return dirContext.getSchema(parseName(name)); 1111 } 1112 1113 1114 1122 public DirContext getSchema(String name) 1123 throws NamingException { 1124 return dirContext.getSchema(parseName(name)); 1125 } 1126 1127 1128 1139 public DirContext getSchemaClassDefinition(Name name) 1140 throws NamingException { 1141 return dirContext.getSchemaClassDefinition(parseName(name)); 1142 } 1143 1144 1145 1156 public DirContext getSchemaClassDefinition(String name) 1157 throws NamingException { 1158 return dirContext.getSchemaClassDefinition(parseName(name)); 1159 } 1160 1161 1162 1179 public NamingEnumeration search(Name name, Attributes matchingAttributes, 1180 String [] attributesToReturn) 1181 throws NamingException { 1182 return dirContext.search(parseName(name), matchingAttributes, 1183 attributesToReturn); 1184 } 1185 1186 1187 1203 public NamingEnumeration search(String name, Attributes matchingAttributes, 1204 String [] attributesToReturn) 1205 throws NamingException { 1206 return dirContext.search(parseName(name), matchingAttributes, 1207 attributesToReturn); 1208 } 1209 1210 1211 1226 public NamingEnumeration search(Name name, Attributes matchingAttributes) 1227 throws NamingException { 1228 return dirContext.search(parseName(name), matchingAttributes); 1229 } 1230 1231 1232 1245 public NamingEnumeration search(String name, Attributes matchingAttributes) 1246 throws NamingException { 1247 return dirContext.search(parseName(name), matchingAttributes); 1248 } 1249 1250 1251 1270 public NamingEnumeration search(Name name, String filter, 1271 SearchControls cons) 1272 throws NamingException { 1273 return dirContext.search(parseName(name), filter, cons); 1274 } 1275 1276 1277 1296 public NamingEnumeration search(String name, String filter, 1297 SearchControls cons) 1298 throws NamingException { 1299 return dirContext.search(parseName(name), filter, cons); 1300 } 1301 1302 1303 1327 public NamingEnumeration search(Name name, String filterExpr, 1328 Object [] filterArgs, SearchControls cons) 1329 throws NamingException { 1330 return dirContext.search(parseName(name), filterExpr, filterArgs, 1331 cons); 1332 } 1333 1334 1335 1359 public NamingEnumeration search(String name, String filterExpr, 1360 Object [] filterArgs, SearchControls cons) 1361 throws NamingException { 1362 return dirContext.search(parseName(name), filterExpr, filterArgs, 1363 cons); 1364 } 1365 1366 1367 1369 1370 1376 public CacheEntry lookupCache(String name) { 1377 CacheEntry entry = cacheLookup(name); 1378 if (entry == null) { 1379 entry = new CacheEntry(); 1380 entry.name = name; 1381 try { 1382 Object object = dirContext.lookup(parseName(name)); 1383 if (object instanceof InputStream ) { 1384 entry.resource = new Resource((InputStream ) object); 1385 } else if (object instanceof DirContext ) { 1386 entry.context = (DirContext ) object; 1387 } else if (object instanceof Resource) { 1388 entry.resource = (Resource) object; 1389 } else { 1390 entry.resource = new Resource(new ByteArrayInputStream 1391 (object.toString().getBytes())); 1392 } 1393 Attributes attributes = dirContext.getAttributes(parseName(name)); 1394 if (!(attributes instanceof ResourceAttributes)) { 1395 attributes = new ResourceAttributes(attributes); 1396 } 1397 entry.attributes = (ResourceAttributes) attributes; 1398 } catch (NamingException e) { 1399 entry.exists = false; 1400 } 1401 } 1402 return entry; 1403 } 1404 1405 1406 1408 1409 1414 protected String parseName(String name) 1415 throws NamingException { 1416 return name; 1417 } 1418 1419 1420 1425 protected Name parseName(Name name) 1426 throws NamingException { 1427 return name; 1428 } 1429 1430 1431 1434 protected CacheEntry cacheLookup(String name) { 1435 if (cache == null) 1436 return (null); 1437 if (name == null) 1438 name = ""; 1439 for (int i = 0; i < nonCacheable.length; i++) { 1440 if (name.startsWith(nonCacheable[i])) { 1441 return (null); 1442 } 1443 } 1444 CacheEntry cacheEntry = cache.lookup(name); 1445 if (cacheEntry == null) { 1446 cacheEntry = new CacheEntry(); 1447 cacheEntry.name = name; 1448 cacheLoad(cacheEntry); 1450 } else { 1451 if (!validate(cacheEntry)) { 1452 if (!revalidate(cacheEntry)) { 1453 cacheUnload(cacheEntry.name); 1454 return (null); 1455 } else { 1456 cacheEntry.timestamp = 1457 System.currentTimeMillis() + cacheTTL; 1458 } 1459 } 1460 cacheEntry.accessCount++; 1461 } 1462 return (cacheEntry); 1463 } 1464 1465 1466 1469 protected boolean validate(CacheEntry entry) { 1470 if (((!entry.exists) 1471 || (entry.context != null) 1472 || ((entry.resource != null) 1473 && (entry.resource.getContent() != null))) 1474 && (System.currentTimeMillis() < entry.timestamp)) { 1475 return true; 1476 } 1477 return false; 1478 } 1479 1480 1481 1484 protected boolean revalidate(CacheEntry entry) { 1485 if (!entry.exists) 1488 return false; 1489 if (entry.attributes == null) 1490 return false; 1491 long lastModified = entry.attributes.getLastModified(); 1492 long contentLength = entry.attributes.getContentLength(); 1493 if (lastModified <= 0) 1494 return false; 1495 try { 1496 Attributes tempAttributes = dirContext.getAttributes(entry.name); 1497 ResourceAttributes attributes = null; 1498 if (!(tempAttributes instanceof ResourceAttributes)) { 1499 attributes = new ResourceAttributes(tempAttributes); 1500 } else { 1501 attributes = (ResourceAttributes) tempAttributes; 1502 } 1503 long lastModified2 = attributes.getLastModified(); 1504 long contentLength2 = attributes.getContentLength(); 1505 return (lastModified == lastModified2) 1506 && (contentLength == contentLength2); 1507 } catch (NamingException e) { 1508 return false; 1509 } 1510 } 1511 1512 1513 1516 protected void cacheLoad(CacheEntry entry) { 1517 1518 String name = entry.name; 1519 1520 boolean exists = true; 1522 1523 if (entry.attributes == null) { 1525 try { 1526 Attributes attributes = dirContext.getAttributes(entry.name); 1527 if (!(attributes instanceof ResourceAttributes)) { 1528 entry.attributes = 1529 new ResourceAttributes(attributes); 1530 } else { 1531 entry.attributes = (ResourceAttributes) attributes; 1532 } 1533 } catch (NamingException e) { 1534 exists = false; 1535 } 1536 } 1537 1538 if ((exists) && (entry.resource == null) && (entry.context == null)) { 1540 try { 1541 Object object = dirContext.lookup(name); 1542 if (object instanceof InputStream ) { 1543 entry.resource = new Resource((InputStream ) object); 1544 } else if (object instanceof DirContext ) { 1545 entry.context = (DirContext ) object; 1546 } else if (object instanceof Resource) { 1547 entry.resource = (Resource) object; 1548 } else { 1549 entry.resource = new Resource(new ByteArrayInputStream 1550 (object.toString().getBytes())); 1551 } 1552 } catch (NamingException e) { 1553 exists = false; 1554 } 1555 } 1556 1557 if ((exists) && (entry.resource != null) 1559 && (entry.resource.getContent() == null) 1560 && (entry.attributes.getContentLength() >= 0) 1561 && (entry.attributes.getContentLength() < 1562 (cacheObjectMaxSize * 1024))) { 1563 int length = (int) entry.attributes.getContentLength(); 1564 entry.size += (entry.attributes.getContentLength() / 1024); 1567 InputStream is = null; 1568 try { 1569 is = entry.resource.streamContent(); 1570 int pos = 0; 1571 byte[] b = new byte[length]; 1572 while (pos < length) { 1573 int n = is.read(b, pos, length - pos); 1574 if (n < 0) 1575 break; 1576 pos = pos + n; 1577 } 1578 entry.resource.setContent(b); 1579 } catch (IOException e) { 1580 ; } finally { 1582 try { 1583 if (is != null) 1584 is.close(); 1585 } catch (IOException e) { 1586 ; } 1588 } 1589 } 1590 1591 entry.exists = exists; 1593 1594 entry.timestamp = System.currentTimeMillis() + cacheTTL; 1596 1597 synchronized (cache) { 1599 if ((cache.lookup(name) == null) && cache.allocate(entry.size)) { 1601 cache.load(entry); 1602 } 1603 } 1604 1605 } 1606 1607 1608 1611 protected boolean cacheUnload(String name) { 1612 if (cache == null) 1613 return false; 1614 synchronized (cache) { 1615 return cache.unload(name); 1616 } 1617 } 1618 1619 1620} 1621 1622 | Popular Tags |