1 17 18 19 package org.apache.naming.resources; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Date ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipException ; 31 import java.util.zip.ZipFile ; 32 33 import javax.naming.CompositeName ; 34 import javax.naming.Name ; 35 import javax.naming.NamingEnumeration ; 36 import javax.naming.NamingException ; 37 import javax.naming.OperationNotSupportedException ; 38 import javax.naming.directory.Attributes ; 39 import javax.naming.directory.DirContext ; 40 import javax.naming.directory.ModificationItem ; 41 import javax.naming.directory.SearchControls ; 42 43 import org.apache.naming.NamingContextBindingsEnumeration; 44 import org.apache.naming.NamingContextEnumeration; 45 import org.apache.naming.NamingEntry; 46 47 53 54 public class WARDirContext extends BaseDirContext { 55 56 private static org.apache.commons.logging.Log log= 57 org.apache.commons.logging.LogFactory.getLog( WARDirContext.class ); 58 59 61 62 65 public WARDirContext() { 66 super(); 67 } 68 69 70 73 public WARDirContext(Hashtable env) { 74 super(env); 75 } 76 77 78 81 protected WARDirContext(ZipFile base, Entry entries) { 82 this.base = base; 83 this.entries = entries; 84 } 85 86 87 89 90 93 protected ZipFile base = null; 94 95 96 99 protected Entry entries = null; 100 101 102 104 105 115 public void setDocBase(String docBase) { 116 117 if (docBase == null) 119 throw new IllegalArgumentException 120 (sm.getString("resources.null")); 121 if (!(docBase.endsWith(".war"))) 122 throw new IllegalArgumentException 123 (sm.getString("warResources.notWar")); 124 125 File base = new File (docBase); 127 128 if (!base.exists() || !base.canRead() || base.isDirectory()) 130 throw new IllegalArgumentException 131 (sm.getString("warResources.invalidWar", docBase)); 132 try { 133 this.base = new ZipFile (base); 134 } catch (Exception e) { 135 throw new IllegalArgumentException 136 (sm.getString("warResources.invalidWar", e.getMessage())); 137 } 138 super.setDocBase(docBase); 139 140 loadEntries(); 141 142 } 143 144 145 147 148 151 public void release() { 152 153 entries = null; 154 if (base != null) { 155 try { 156 base.close(); 157 } catch (IOException e) { 158 log.warn 159 ("Exception closing WAR File " + base.getName(), e); 160 } 161 } 162 base = null; 163 super.release(); 164 165 } 166 167 168 170 171 178 public Object lookup(String name) 179 throws NamingException { 180 return lookup(new CompositeName (name)); 181 } 182 183 184 194 public Object lookup(Name name) 195 throws NamingException { 196 if (name.isEmpty()) 197 return this; 198 Entry entry = treeLookup(name); 199 if (entry == null) 200 throw new NamingException 201 (sm.getString("resources.notFound", name)); 202 ZipEntry zipEntry = entry.getEntry(); 203 if (zipEntry.isDirectory()) 204 return new WARDirContext(base, entry); 205 else 206 return new WARResource(entry.getEntry()); 207 } 208 209 210 224 public void unbind(String name) 225 throws NamingException { 226 throw new OperationNotSupportedException (); 227 } 228 229 230 241 public void rename(String oldName, String newName) 242 throws NamingException { 243 throw new OperationNotSupportedException (); 244 } 245 246 247 260 public NamingEnumeration list(String name) 261 throws NamingException { 262 return list(new CompositeName (name)); 263 } 264 265 266 279 public NamingEnumeration list(Name name) 280 throws NamingException { 281 if (name.isEmpty()) 282 return new NamingContextEnumeration(list(entries).iterator()); 283 Entry entry = treeLookup(name); 284 if (entry == null) 285 throw new NamingException 286 (sm.getString("resources.notFound", name)); 287 return new NamingContextEnumeration(list(entry).iterator()); 288 } 289 290 291 304 public NamingEnumeration listBindings(String name) 305 throws NamingException { 306 return listBindings(new CompositeName (name)); 307 } 308 309 310 323 public NamingEnumeration listBindings(Name name) 324 throws NamingException { 325 if (name.isEmpty()) 326 return new NamingContextBindingsEnumeration(list(entries).iterator(), 327 this); 328 Entry entry = treeLookup(name); 329 if (entry == null) 330 throw new NamingException 331 (sm.getString("resources.notFound", name)); 332 return new NamingContextBindingsEnumeration(list(entry).iterator(), 333 this); 334 } 335 336 337 362 public void destroySubcontext(String name) 363 throws NamingException { 364 throw new OperationNotSupportedException (); 365 } 366 367 368 378 public Object lookupLink(String name) 379 throws NamingException { 380 return lookup(name); 382 } 383 384 385 402 public String getNameInNamespace() 403 throws NamingException { 404 return docBase; 405 } 406 407 408 410 411 423 public Attributes getAttributes(String name, String [] attrIds) 424 throws NamingException { 425 return getAttributes(new CompositeName (name), attrIds); 426 } 427 428 429 437 public Attributes getAttributes(Name name, String [] attrIds) 438 throws NamingException { 439 440 Entry entry = null; 441 if (name.isEmpty()) 442 entry = entries; 443 else 444 entry = treeLookup(name); 445 if (entry == null) 446 throw new NamingException 447 (sm.getString("resources.notFound", name)); 448 449 ZipEntry zipEntry = entry.getEntry(); 450 451 ResourceAttributes attrs = new ResourceAttributes(); 452 attrs.setCreationDate(new Date (zipEntry.getTime())); 453 attrs.setName(entry.getName()); 454 if (!zipEntry.isDirectory()) 455 attrs.setResourceType(""); 456 attrs.setContentLength(zipEntry.getSize()); 457 attrs.setLastModified(zipEntry.getTime()); 458 459 return attrs; 460 461 } 462 463 464 478 public void modifyAttributes(String name, int mod_op, Attributes attrs) 479 throws NamingException { 480 throw new OperationNotSupportedException (); 481 } 482 483 484 498 public void modifyAttributes(String name, ModificationItem [] mods) 499 throws NamingException { 500 throw new OperationNotSupportedException (); 501 } 502 503 504 519 public void bind(String name, Object obj, Attributes attrs) 520 throws NamingException { 521 throw new OperationNotSupportedException (); 522 } 523 524 525 543 public void rebind(String name, Object obj, Attributes attrs) 544 throws NamingException { 545 throw new OperationNotSupportedException (); 546 } 547 548 549 566 public DirContext createSubcontext(String name, Attributes attrs) 567 throws NamingException { 568 throw new OperationNotSupportedException (); 569 } 570 571 572 585 public DirContext getSchema(String name) 586 throws NamingException { 587 throw new OperationNotSupportedException (); 588 } 589 590 591 602 public DirContext getSchemaClassDefinition(String name) 603 throws NamingException { 604 throw new OperationNotSupportedException (); 605 } 606 607 608 625 public NamingEnumeration search(String name, Attributes matchingAttributes, 626 String [] attributesToReturn) 627 throws NamingException { 628 throw new OperationNotSupportedException (); 629 } 630 631 632 647 public NamingEnumeration search(String name, Attributes matchingAttributes) 648 throws NamingException { 649 throw new OperationNotSupportedException (); 650 } 651 652 653 672 public NamingEnumeration search(String name, String filter, 673 SearchControls cons) 674 throws NamingException { 675 throw new OperationNotSupportedException (); 676 } 677 678 679 703 public NamingEnumeration search(String name, String filterExpr, 704 Object [] filterArgs, SearchControls cons) 705 throws NamingException { 706 throw new OperationNotSupportedException (); 707 } 708 709 710 712 713 716 protected String normalize(ZipEntry entry) { 717 718 String result = "/" + entry.getName(); 719 if (entry.isDirectory()) { 720 result = result.substring(0, result.length() - 1); 721 } 722 return result; 723 724 } 725 726 727 730 protected void loadEntries() { 731 732 try { 733 734 Enumeration entryList = base.entries(); 735 entries = new Entry ("/", new ZipEntry ("/")); 736 737 while (entryList.hasMoreElements()) { 738 739 ZipEntry entry = (ZipEntry ) entryList.nextElement(); 740 String name = normalize(entry); 741 int pos = name.lastIndexOf('/'); 742 int currentPos = -1; 746 int lastPos = 0; 747 while ((currentPos = name.indexOf('/', lastPos)) != -1) { 748 Name parentName = new CompositeName (name.substring(0, lastPos)); 749 Name childName = new CompositeName (name.substring(0, currentPos)); 750 String entryName = name.substring(lastPos, currentPos); 751 Entry parent = treeLookup(parentName); 754 Entry child = treeLookup(childName); 755 if (child == null) { 756 String zipName = name.substring(1, currentPos) + "/"; 761 child = new Entry (entryName, new ZipEntry (zipName)); 762 if (parent != null) 763 parent.addChild(child); 764 } 765 lastPos = currentPos + 1; 767 } 768 String entryName = name.substring(pos + 1, name.length()); 769 Name compositeName = new CompositeName (name.substring(0, pos)); 770 Entry parent = treeLookup(compositeName); 771 Entry child = new Entry (entryName, entry); 772 if (parent != null) 773 parent.addChild(child); 774 775 } 776 777 } catch (Exception e) { 778 } 779 780 } 781 782 783 786 protected Entry treeLookup(Name name) { 787 if (name.isEmpty()) 788 return entries; 789 Entry currentEntry = entries; 790 for (int i = 0; i < name.size(); i++) { 791 if (name.get(i).length() == 0) 792 continue; 793 currentEntry = currentEntry.getChild(name.get(i)); 794 if (currentEntry == null) 795 return null; 796 } 797 return currentEntry; 798 } 799 800 801 804 protected ArrayList list(Entry entry) { 805 806 ArrayList entries = new ArrayList (); 807 Entry [] children = entry.getChildren(); 808 Arrays.sort(children); 809 NamingEntry namingEntry = null; 810 811 for (int i = 0; i < children.length; i++) { 812 ZipEntry current = children[i].getEntry(); 813 Object object = null; 814 if (current.isDirectory()) { 815 object = new WARDirContext(base, children[i]); 816 } else { 817 object = new WARResource(current); 818 } 819 namingEntry = new NamingEntry 820 (children[i].getName(), object, NamingEntry.ENTRY); 821 entries.add(namingEntry); 822 } 823 824 return entries; 825 826 } 827 828 829 831 832 835 protected class Entry implements Comparable { 836 837 838 840 841 public Entry(String name, ZipEntry entry) { 842 this.name = name; 843 this.entry = entry; 844 } 845 846 847 849 850 protected String name = null; 851 852 853 protected ZipEntry entry = null; 854 855 856 protected Entry children[] = new Entry [0]; 857 858 859 861 862 public int compareTo(Object o) { 863 if (!(o instanceof Entry )) 864 return (+1); 865 return (name.compareTo(((Entry ) o).getName())); 866 } 867 868 public ZipEntry getEntry() { 869 return entry; 870 } 871 872 873 public String getName() { 874 return name; 875 } 876 877 878 public void addChild(Entry entry) { 879 Entry [] newChildren = new Entry [children.length + 1]; 880 for (int i = 0; i < children.length; i++) 881 newChildren[i] = children[i]; 882 newChildren[children.length] = entry; 883 children = newChildren; 884 } 885 886 887 public Entry [] getChildren() { 888 return children; 889 } 890 891 892 public Entry getChild(String name) { 893 for (int i = 0; i < children.length; i++) { 894 if (children[i].name.equals(name)) { 895 return children[i]; 896 } 897 } 898 return null; 899 } 900 901 902 } 903 904 905 907 908 912 protected class WARResource extends Resource { 913 914 915 917 918 public WARResource(ZipEntry entry) { 919 this.entry = entry; 920 } 921 922 923 925 926 protected ZipEntry entry; 927 928 929 931 932 937 public InputStream streamContent() 938 throws IOException { 939 try { 940 if (binaryContent == null) { 941 inputStream = base.getInputStream(entry); 942 } 943 } catch (ZipException e) { 944 throw new IOException (e.getMessage()); 945 } 946 return super.streamContent(); 947 } 948 949 950 } 951 952 953 } 954 955 | Popular Tags |