1 10 package org.jgap.util; 11 12 import java.io.*; 13 import java.util.*; 14 import java.util.jar.*; 15 16 28 public class PluginDiscoverer { 29 30 private final static String CVS_REVISION = "$Revision: 1.8 $"; 31 32 private static final boolean DEBUG = false; 33 34 private List m_classpathFolders; 36 37 private List m_classpathJars; 39 40 private String m_jarFile; 41 42 49 public PluginDiscoverer() { 50 init(); 51 String classpath = System.getProperty("java.class.path"); 52 StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator); 53 while (st.hasMoreTokens()) { 54 String item = st.nextToken(); 55 File f = new File(item); 56 if (item.toLowerCase().endsWith(".jar") && f.isFile()) { 57 m_classpathJars.add(item); 58 } 59 else if (f.isDirectory()) { 60 m_classpathFolders.add(item); 61 } 62 } 63 } 64 65 72 public PluginDiscoverer(String a_jarFile) { 73 init(); 74 m_jarFile = a_jarFile; 75 m_classpathJars.add(m_jarFile); 76 } 77 78 private void init() { 79 m_classpathFolders = new Vector(); 80 m_classpathJars = new Vector(); 81 } 82 83 92 private String checkIfClassMatches(String a_jarFilename, final Class a_interfaceClass, 93 String a_testClass) { 94 if (a_testClass.toLowerCase().endsWith(".class")) { 96 a_testClass = a_testClass.substring(0, a_testClass.length() - 6); 97 } 98 a_testClass = a_testClass.replace('\\', '.').replace('/', '.'); 100 while (a_testClass.startsWith(".")) { 102 a_testClass = a_testClass.substring(1); 103 } 104 if (a_testClass.indexOf('$') != -1) { 105 return null; 107 } 108 try { 109 ClassLoader cl; 110 if (a_jarFilename == null) { 111 cl = getClass().getClassLoader(); 112 } 113 else { 114 cl = new JarClassLoader(a_jarFilename); 115 } 116 Class testClassObj = Class.forName(a_testClass, false, 117 cl); 118 if (a_interfaceClass.isAssignableFrom(testClassObj)) { 119 if (testClassObj.isInterface()) { 120 return null; 122 } 123 if ( (testClassObj.getModifiers() & java.lang.reflect.Modifier.ABSTRACT) 124 > 0) { 125 return null; 127 } 128 return a_testClass; 129 } 130 } catch (UnsatisfiedLinkError ule) { 131 if (DEBUG) { 132 System.out.println("Unsatisfied link error for class: " + a_testClass); 133 } 134 } catch (IllegalAccessError e) { 135 if (DEBUG) { 136 System.out.println("Unable to load class: " + a_testClass); 137 } 138 } catch (ClassNotFoundException cnfe) { 139 if (DEBUG) { 140 System.out.println("Class not found: " + a_testClass); 141 } 142 } catch (NoClassDefFoundError nex) { 143 if (DEBUG) { 144 System.out.println("No class definition found: " + a_testClass); 145 } 146 } 147 return null; 148 } 149 150 161 public List findImplementingClasses(final String a_fullInterfaceName) 162 throws ClassNotFoundException { 163 Class interfaceToLookFor = Class.forName(a_fullInterfaceName); 164 return findImplementingClasses(interfaceToLookFor); 165 } 166 167 175 public List findImplementingClasses(final Class a_intrface) { 176 List result = new Vector(); 177 String s = null; 179 try { 180 File f = new File("."); 182 s = f.getCanonicalPath(); 183 s = FileKit.getConformPath(s, true); 184 } catch (IOException iex) { 185 throw new RuntimeException ("Unable to determine current directory",iex); 186 } 187 Iterator i = m_classpathJars.iterator(); 188 while (i.hasNext()) { 189 String filename = (String ) i.next(); 190 filename = FileKit.getConformPath(filename, true); 191 if (filename.startsWith(s)) { 195 try { 196 JarFile jar = new JarFile(filename); 197 Enumeration item = jar.entries(); 198 while (item.hasMoreElements()) { 199 JarEntry entry = (JarEntry) item.nextElement(); 200 String name = entry.getName(); 201 if (name.toLowerCase().endsWith(".class")) { 202 String classname = checkIfClassMatches(filename, a_intrface, name); 203 if (classname != null) { 204 result.add(classname); 205 } 206 } 207 } 208 } catch (IOException e) { 209 System.out.println("Unable to open jar " + filename); 210 } 211 } 212 } 213 i = m_classpathFolders.iterator(); 215 while (i.hasNext()) { 216 String folder = (String ) i.next(); 217 System.err.println(folder); 218 findImplementingClasses0(a_intrface, result, folder, ""); 219 } 220 return result; 221 } 222 223 234 private void findImplementingClasses0(final Class a_intrface, 235 final List a_result, 236 final String a_base, 237 final String a_path) { 238 a_result.addAll(findImplementingClasses(a_intrface, a_base, a_path)); 239 File f = new File(a_base + File.separator + a_path); 240 if (!f.isDirectory()) { 241 return; 242 } 243 File[] matches = f.listFiles(new DirectoryFilter()); 252 for (int i = 0; i < matches.length; i++) { 253 String folder = a_path + File.separator + matches[i].getName(); 254 findImplementingClasses0(a_intrface, a_result, a_base, folder); 255 } 256 } 257 258 267 public List findImplementingClasses(final Class a_intrface, 268 final String a_base, 269 final String a_path) { 270 List result = new Vector(); 271 File f = new File(a_base + File.separator + a_path); 272 if (!f.isDirectory()) { 273 return result; 274 } 275 File[] matches = f.listFiles(new ClassFilter()); 276 for (int i = 0; i < matches.length; i++) { 277 String classname = a_path + File.separator + matches[i].getName(); 278 classname = checkIfClassMatches(null, a_intrface, classname); 279 if (classname != null) { 280 result.add(classname); 281 } 282 } 283 return result; 284 } 285 286 289 public class ClassFilter 290 implements FilenameFilter { 291 public boolean accept(final File a_dir, final String a_name) { 292 return (a_name != null && a_name.toLowerCase().endsWith(".class")); 293 } 294 } 295 298 public class DirectoryFilter 299 implements FilenameFilter { 300 public boolean accept(final File a_dir, final String a_name) { 301 return (a_dir != null 302 && new File(a_dir.getPath() 303 + File.separator + a_name).isDirectory()); 304 } 305 } 306 314 public static void main(String [] args) 315 throws Exception { 316 PluginDiscoverer discoverer = new PluginDiscoverer(); 317 List plugins = discoverer.findImplementingClasses( 321 "org.jgap.INaturalSelector"); 322 System.out.println(); 323 int size = plugins.size(); 324 System.out.println("" + size + " plugin" 325 + (size == 1 ? "" : "s") + " discovered" 326 + (size == 0 ? "" : ":")); 327 for (int i = 0; i < size; i++) { 328 System.out.println(plugins.get(i)); 329 } 330 System.exit(0); 342 } 343 } 344 | Popular Tags |