/** * Returns <code>true</code> if the specified file is in the extension's classloader path. * @param file file whose presence in the extensions path will be checked. * @return <code>true</code> if the specified file is in the extension's classloader path, <code>false</code> otherwise. */ public static boolean isInExtensionsPath(AbstractFile file) {return loader.contains(file);}
/** * Adds the specified file to the extension's classpath. * @param file file to add to the classpath. */ public static void addToClassPath(AbstractFile file) {loader.addFile(file);}
/** * Tries to find and load the specified class. * @param name fully qualified name of the class to load. * @return the requested <code>Class</code> if found, <code>null</code> otherwise. * @throws ClassNotFoundException if the requested class was not found. */ @Override protected Class<?> findClass(String name) throws ClassNotFoundException { AbstractFile file; // File containing the class' bytecode. // Tries to locate the specified class and, if found, load it. if((file = findResourceAsFile(name.replace('.', '/') + ".class")) != null) { try {return loadClass(name, file);} catch(Exception e) {} } throw new ClassNotFoundException(name); } }
/** * Returns an input stream on the requested resource. * @param name name of the resource to open. * @return an input stream on the requested resource, <code>null</code> if not found. */ @Override public InputStream getResourceAsStream(String name) { AbstractFile file; // File representing the resource. InputStream in; // Input stream on the resource. // Tries the parent first, to respect the delegation model. if((in = getParent().getResourceAsStream(name)) != null) return in; // Tries to locate the resource in the extended classpath if it wasn't found // in the parent. if((file = findResourceAsFile(name)) != null) { try {return file.getInputStream();} catch(Exception e) {} } // Couldn't find the resource. return null; }
/** * Explores the content of the specified file and looks for classes that match the specified class filter. * <p> * This is a convenience method and is strictly equivalent to calling {@link #find(AbstractFile,ClassFilter,ClassLoader)} * with a class loader argument initialized with the following code: * <pre> * AbstractFileClassLoader loader; * * loader = new AbstractFileClassLoader(); * loader.addFile(browsable); * </pre> * </p> * @param browsable file in which to look for classes. * @param classFilter how to decide which classes should be kept. * @return a vector containing all the classes that were found and matched <code>classFilter</code>. * @throws IOException if an error occurs while exploring <code>browsable</code>. */ public List<Class<?>> find(AbstractFile browsable, ClassFilter classFilter) throws IOException { AbstractFileClassLoader classLoader; // Default class loader. // Initializes the default class loader. classLoader = new AbstractFileClassLoader(); classLoader.addFile(browsable); // Explores browsable. return find(browsable, classFilter, classLoader); } }
/** * Returns the absolute path of the requested library. * @param name name of the library to load. * @return the absolute path of the requested library if found, <code>null</code> otheriwse. */ @Override protected String findLibrary(String name) { AbstractFile file; // Path of the requested library. // Tries to find the requested library. if((file = findResourceAsFile(name)) == null) return null; // Retrieves its absolute path. return file.getAbsolutePath(); }
/** * Loads and returns the class defined by the specified name and path. * @param name name of the class to load. * @param file file containing the class' bytecode. * @return the class defined by the specified name and path. * @throws IOException if an error occurs. */ private Class<?> loadClass(String name, AbstractFile file) throws IOException { byte[] buffer; // Buffer for the class' bytecode. int offset; // Current offset in buffer. InputStream in; // Stream on the class' bytecode. // Initialisation. buffer = new byte[(int)file.getSize()]; offset = 0; in = null; try { // Loads the content of file in buffer. in = file.getInputStream(); while(offset != buffer.length) offset += in.read(buffer, offset, buffer.length - offset); // Loads the class. return defineClass(name, buffer, 0, buffer.length); } // Frees resources. finally { if(in != null) in.close(); } }
/** * Tries to find the requested resource. * @param name name of the resource to locate. * @return the URL of the requested resource if found, <code>null</code> otherwise. */ @Override protected URL findResource(String name) { AbstractFile file; // Path to the requested resource. // Tries to find the resource. if((file = findResourceAsFile(name)) == null) return null; // Tries to retrieve an URL on the resource. try {return file.getJavaNetURL();} catch(Exception e) {return null;} }
/** * Adds the specified <code>file</code> to the class loader's classpath. * <p> * Note that the file will <b>not</b> be added if it's already in the classpath. * </p> * @param file file to add the class loader's classpath. * @throws IllegalArgumentException if <code>file</code> is not browsable. */ public void addFile(AbstractFile file) { // Makes sure the specified file is browsable. if(!file.isBrowsable()) throw new IllegalArgumentException(); // Only adds the file if it's not already there. if(!contains(file)) files.add(file); }