1 26 27 package org.objectweb.util.explorer.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.explorer.api.Context; 36 import org.objectweb.util.explorer.api.Entry; 37 import org.objectweb.util.explorer.core.common.api.ContextContainer; 38 import org.objectweb.util.explorer.core.naming.lib.DefaultEntry; 39 40 41 49 public class JarFileContext 50 implements Context 51 { 52 53 59 65 71 78 protected String replaceAll(String original, String to_replace, String new_value) { 79 StringBuffer newString = new StringBuffer (); 80 int indexBegin = 0; 81 int index = original.indexOf(to_replace, indexBegin); 82 while (index != -1) { 83 newString.append(original.substring(indexBegin, index)); 84 newString.append(new_value); 85 indexBegin = index + to_replace.length(); 86 index = original.indexOf(to_replace, indexBegin); 87 } 88 newString.append(original.substring(indexBegin)); 89 return newString.toString(); 90 } 91 92 protected ContextContainer getContext(ContextContainer contextContainer, String packageName){ 93 if(packageName!=null && !packageName.equals("")) { 94 String currentPackage = null; 95 String otherPackage = null; 96 if(packageName.lastIndexOf(".")!=-1){ 97 currentPackage = packageName.substring(0,packageName.indexOf(".")); 98 otherPackage = packageName.substring(packageName.indexOf(".")+1); 99 } else { 100 currentPackage = packageName; 101 } 102 if(currentPackage!=null) { 103 Entry entry = contextContainer.getEntry(currentPackage); 104 ContextContainer cc = (entry!=null)?(ContextContainer)entry.getValue():null; 105 if(cc == null) { 106 cc = new PackageContextContainer(); 107 contextContainer.addEntry(currentPackage, cc); 108 } 109 if(otherPackage!=null) 110 return getContext(cc, otherPackage); 111 else 112 return cc; 113 } else 114 return contextContainer; 115 } 116 return contextContainer; 117 } 118 119 protected void addClass(ContextContainer contextContainer, Class theClass) { 120 String packageName = theClass.getPackage().getName(); 121 ContextContainer cc = getContext(contextContainer, packageName); 122 cc.addEntry(theClass.getName().substring(theClass.getName().lastIndexOf(".") + 1),theClass); 123 } 124 125 131 134 public Entry [] getEntries(Object object){ 135 JarFileStructure jarFile = (JarFileStructure)object; 136 URLClassLoader classLoader = new URLClassLoader (new URL []{jarFile.getJarURL()}); 137 ContextContainer cc = new PackageContextContainer(); 138 Vector elements = new Vector (); 139 Enumeration enumeration = jarFile.getJarFile().entries(); 140 while(enumeration.hasMoreElements()) { 141 JarEntry entry = (JarEntry )enumeration.nextElement(); 142 if(!entry.isDirectory()){ 143 try { 144 Class c = Class.forName(replaceAll(entry.getName().substring(0, entry.getName().lastIndexOf('.')),"/","."),true,classLoader); 145 addClass(cc, c); 146 } catch (ClassNotFoundException e) { 148 } 150 } 151 } 152 Entry [] entries = cc.getEntries(object); 153 for (int i = 0; i < entries.length; i++) { 154 elements.add(new DefaultEntry(entries[i].getName().toString(),entries[i].getValue())); 155 } 156 return (Entry [])elements.toArray(new Entry [0]); 157 } 158 159 } 160 | Popular Tags |