public static void main(String[] args) throws MalformedURLException, ClassNotFoundException { Policy.setPolicy(new PluginSecurityPolicy()); System.setSecurityManager(new SecurityManager()); PluginClassLoader urlCL = new PluginClassLoader(new File( "A Jar containing images/load.gif and SampleApp class").toURL()); Class<?> loadClass = urlCL.loadClass("net.sourceforge.marathon.examples.SampleApp"); System.out.println(loadClass); System.out.println("Loading the class using the class loader object. Resource = " + urlCL.getResource("images/load.gif")); System.out.println("Loading the class using the class loader object with absolute path. Resource = " + urlCL.getResource("/images/load.gif")); System.out.println("Loading the class using the class object. Resource = " + loadClass.getResource("images/load.gif")); }
/** * @see java.lang.Object#finalize() */ @Override protected void finalize() throws Throwable { super.finalize(); close(); }
/** * Constructor. * @param pluginArtifactFile plugin artifact * @throws IOException if an I/O error has occurred */ public PluginClassLoader(File pluginArtifactFile) throws IOException { super(); closed = false; this.pluginArtifactZip = new ZipFile(pluginArtifactFile); this.workDir = createWorkDir(pluginArtifactFile); indexPluginArtifact(); }
/** * @see java.lang.ClassLoader#findClass(java.lang.String) */ @Override protected Class<?> findClass(String name) throws ClassNotFoundException { InputStream inputStream = null; try { inputStream = findClassContent(name); if (inputStream == null) { return super.findClass(name); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(inputStream, baos); byte[] bytes = baos.toByteArray(); return defineClass(name, bytes, 0, bytes.length); } catch (IOException e) { throw new ClassNotFoundException(name, e); } finally { IOUtils.closeQuietly(inputStream); } }
Class<?> class1 = classLoader.loadClass(classname); return (Class<T>) class1; } else {
if (isSnapshot) { pluginCache.remove(coordinates); try { cachedPlugin.getLoader().close(); } catch (IOException e) { e.printStackTrace(); } } else { return cachedPlugin; throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e); //$NON-NLS-1$ URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH); if (specFile == null) { throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH)); //$NON-NLS-1$
/** * Reads the plugin into an object. This method will fail if the plugin is not valid. * This could happen if the file is not a java archive, or if the plugin spec file is * missing from the archive, etc. */ protected Plugin readPluginFile(PluginCoordinates coordinates, File pluginFile) throws Exception { try { PluginClassLoader pluginClassLoader = createPluginClassLoader(pluginFile); URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH); if (specFile == null) { throw new Exception(Messages.i18n.format("DefaultPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH)); //$NON-NLS-1$ } else { PluginSpec spec = PluginUtils.readPluginSpecFile(specFile); Plugin plugin = new Plugin(spec, coordinates, pluginClassLoader); // TODO use logger when available System.out.println("Read apiman plugin: " + spec); //$NON-NLS-1$ return plugin; } } catch (Exception e) { throw new Exception(Messages.i18n.format("DefaultPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e); //$NON-NLS-1$ } }
Class<?> class1 = classLoader.loadClass(classname); return (Class<T>) class1; } else {
/** * Reads the plugin into an object. This method will fail if the plugin is not valid. * This could happen if the file is not a java archive, or if the plugin spec file is * missing from the archive, etc. */ protected Plugin readPluginFile(PluginCoordinates coordinates, File pluginFile) throws Exception { try { PluginClassLoader pluginClassLoader = createPluginClassLoader(pluginFile); URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH); if (specFile == null) { throw new Exception(Messages.i18n.format("DefaultPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH)); //$NON-NLS-1$ } else { PluginSpec spec = PluginUtils.readPluginSpecFile(specFile); Plugin plugin = new Plugin(spec, coordinates, pluginClassLoader); // TODO use logger when available System.out.println("Read apiman plugin: " + spec); //$NON-NLS-1$ return plugin; } } catch (Exception e) { throw new Exception(Messages.i18n.format("DefaultPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e); //$NON-NLS-1$ } }
/** * @see java.lang.ClassLoader#findClass(java.lang.String) */ @Override protected Class<?> findClass(String name) throws ClassNotFoundException { InputStream inputStream = null; try { inputStream = findClassContent(name); if (inputStream == null) { return super.findClass(name); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(inputStream, baos); byte[] bytes = baos.toByteArray(); return defineClass(name, bytes, 0, bytes.length); } catch (IOException e) { throw new ClassNotFoundException(name, e); } finally { IOUtils.closeQuietly(inputStream); } }
Class<?> class1 = classLoader.loadClass(classname); return (Class<T>) class1; } else {
/** * @see java.lang.Object#finalize() */ @Override protected void finalize() throws Throwable { super.finalize(); close(); }
/** * Constructor. * @param pluginArtifactFile plugin artifact * @param parent parent classloader * @throws IOException if an I/O error has occurred */ public PluginClassLoader(File pluginArtifactFile, ClassLoader parent) throws IOException { super(parent); this.pluginArtifactZip = new ZipFile(pluginArtifactFile); this.workDir = createWorkDir(pluginArtifactFile); indexPluginArtifact(); }
Plugin plugin = pluginRegistry.loadPlugin(coordinates); PluginClassLoader classLoader = plugin.getLoader(); Class<?> class1 = classLoader.loadClass(classname); return createCustomComponent(componentType, class1, configProperties); } else {
try { result.getResult().getLoader().close(); } catch (IOException e) {} result = AsyncResultImpl.create(pluginCache.get(coordinates)); } else {
/** * Constructor. * @param pluginArtifactFile plugin artifact * @throws IOException if an I/O error has occurred */ public PluginClassLoader(File pluginArtifactFile) throws IOException { super(); closed = false; this.pluginArtifactZip = new ZipFile(pluginArtifactFile); this.workDir = createWorkDir(pluginArtifactFile); indexPluginArtifact(); }
@Override public void handle(IAsyncResult<Plugin> result) { if (result.isSuccess()) { IPolicy rval; Plugin plugin = result.getResult(); PluginClassLoader pluginClassLoader = plugin.getLoader(); ClassLoader oldCtxLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(pluginClassLoader); Class<?> c = pluginClassLoader.loadClass(classname); rval = (IPolicy) c.newInstance(); } catch (Exception e) { handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, e))); return; } finally { Thread.currentThread().setContextClassLoader(oldCtxLoader); } policyCache.put(policyImpl, rval); handler.handle(AsyncResultImpl.create(rval)); } else { handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, result.getError()))); } } });
try { result.getResult().getLoader().close(); } catch (IOException e) {} result = AsyncResultImpl.create(pluginCache.get(coordinates)); } else {
/** * Constructor. * @param pluginArtifactFile plugin artifact * @param parent parent classloader * @throws IOException if an I/O error has occurred */ public PluginClassLoader(File pluginArtifactFile, ClassLoader parent) throws IOException { super(parent); this.pluginArtifactZip = new ZipFile(pluginArtifactFile); this.workDir = createWorkDir(pluginArtifactFile); indexPluginArtifact(); }
@Override public void handle(IAsyncResult<Plugin> result) { if (result.isSuccess()) { IPolicy rval; Plugin plugin = result.getResult(); PluginClassLoader pluginClassLoader = plugin.getLoader(); ClassLoader oldCtxLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(pluginClassLoader); Class<?> c = pluginClassLoader.loadClass(classname); rval = (IPolicy) c.newInstance(); } catch (Exception e) { handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, e))); return; } finally { Thread.currentThread().setContextClassLoader(oldCtxLoader); } policyCache.put(policyImpl, rval); handler.handle(AsyncResultImpl.create(rval)); } else { handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, result.getError()))); } } });