1 26 27 package org.objectweb.util.browser.plugin.java.reflect; 28 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 import java.util.Enumeration ; 32 import java.util.Vector ; 33 import java.util.jar.JarEntry ; 34 35 import org.objectweb.util.browser.api.Context; 36 import org.objectweb.util.browser.api.Entry; 37 import org.objectweb.util.browser.api.Wrapper; 38 import org.objectweb.util.browser.core.api.ContextContainer; 39 import org.objectweb.util.browser.core.naming.DefaultEntry; 40 import org.objectweb.util.browser.core.naming.DefaultName; 41 42 50 public class JarFileContext 51 implements Wrapper, Context { 52 53 59 protected JarFileStructure jarFile_; 60 61 67 73 80 protected String replaceAll(String original, String to_replace, String new_value) { 81 StringBuffer newString = new StringBuffer (); 82 int indexBegin = 0; 83 int index = original.indexOf(to_replace, indexBegin); 84 while (index != -1) { 85 newString.append(original.substring(indexBegin, index)); 86 newString.append(new_value); 87 indexBegin = index + to_replace.length(); 88 index = original.indexOf(to_replace, indexBegin); 89 } 90 newString.append(original.substring(indexBegin)); 91 return newString.toString(); 92 } 93 94 protected ContextContainer getContext(ContextContainer contextContainer, String packageName){ 95 if(packageName!=null && !packageName.equals("")) { 96 String currentPackage = null; 97 String otherPackage = null; 98 if(packageName.lastIndexOf(".")!=-1){ 99 currentPackage = packageName.substring(0,packageName.indexOf(".")); 100 otherPackage = packageName.substring(packageName.indexOf(".")+1); 101 } else { 102 currentPackage = packageName; 103 } 104 if(currentPackage!=null) { 105 ContextContainer cc = (ContextContainer)contextContainer.getLocalValue(currentPackage); 106 if(cc == null) { 107 cc = new PackageContextContainer(); 108 contextContainer.addEntry(currentPackage, cc); 109 } 110 if(otherPackage!=null) 111 return getContext(cc, otherPackage); 112 else 113 return cc; 114 } else 115 return contextContainer; 116 } 117 return contextContainer; 118 } 119 120 protected void addClass(ContextContainer contextContainer, Class theClass) { 121 String packageName = theClass.getPackage().getName(); 122 ContextContainer cc = getContext(contextContainer, packageName); 123 cc.addEntry(theClass.getName().substring(theClass.getName().lastIndexOf(".") + 1),theClass); 124 } 125 126 132 public Entry [] getEntries(){ 133 URLClassLoader classLoader = new URLClassLoader (new URL []{jarFile_.getJarURL()}); 134 ContextContainer cc = new PackageContextContainer(); 135 Vector elements = new Vector (); 136 Enumeration entries = jarFile_.getJarFile().entries(); 137 while(entries.hasMoreElements()) { 138 JarEntry entry = (JarEntry )entries.nextElement(); 139 if(!entry.isDirectory()){ 140 try { 141 Class c = Class.forName(replaceAll(entry.getName().substring(0, entry.getName().lastIndexOf('.')),"/","."),true,classLoader); 142 addClass(cc, c); 143 } catch (ClassNotFoundException e) { 145 } 147 } 148 } 149 Entry [] theEntries = cc.getEntries(); 150 for (int i = 0; i < theEntries.length; i++) { 151 elements.add(new DefaultEntry(theEntries[i].getValue(),new DefaultName(theEntries[i].getName().toString()),this)); 152 } 153 return (Entry [])elements.toArray(new Entry [0]); 154 } 155 156 162 public void setWrapped(Object object){ 163 jarFile_ = (JarFileStructure)object; 164 } 165 166 public Object getWrapped(){ 167 return jarFile_; 168 } 169 170 } 171 172 | Popular Tags |