@Test public void testPolicy() throws IllegalArgumentException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException { LoadedLibrary lib = LoadedJarLibrary .createFromJar("export/jars/i5.las2peer.classLoaders.evilService-1.0.jar"); ServiceClassLoader testee = new ServiceClassLoader(lib, null, new RestrictivePolicy()); Class<?> cl = testee.loadClass("i5.las2peer.classLoaders.evilService.EvilService", false); Method notEvil = cl.getDeclaredMethod("notEvil"); Method accessNode = cl.getDeclaredMethod("accessNode"); Method createThread = cl.getDeclaredMethod("createThread"); notEvil.invoke(null); try { accessNode.invoke(null); fail("NoClassDefFoundError expected"); } catch (InvocationTargetException e) { System.out.print(e.getTargetException()); if (!(e.getTargetException() instanceof NoClassDefFoundError)) { fail("NoClassDefFoundError expected"); } } try { createThread.invoke(null); fail("NoClassDefFoundError expected"); } catch (InvocationTargetException e) { if (!(e.getTargetException() instanceof NoClassDefFoundError)) { fail("NoClassDefFoundError expected"); } } }
@Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Logger.logLoading(this, name, null); Class<?> c = findLoadedClass(name); if (c == null && this.policy.canLoad(name)) { try { if (parent != null) { c = parent.loadClass(name); } else { c = getSystemClassLoader().loadClass(name); } } catch (ClassNotFoundException e) { } } if (c == null) { try { c = findClass(name); } catch (ClassNotFoundException e) { Logger.logLoading(this, name, false); throw e; } } if (resolve) { resolveClass(c); } Logger.logLoading(this, name, true); return c; }
ServiceClassLoader extends ClassLoader { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Logger.logLoading(this, name, null); Class<?> c = findLoadedClass(name); if (c == null && this.policy.canLoad(name)) { try { if (parent != null) { c = parent.loadClass(name); } else { c = getSystemClassLoader().loadClass(name); } } catch (ClassNotFoundException e) { } } if (c == null) { try { c = findClass(name); } catch (ClassNotFoundException e) { Logger.logLoading(this, name, false); throw e; } } if (resolve) { resolveClass(c); } Logger.logLoading(this, name, true); return c; } }
ServiceClassLoader extends ClassLoader { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Logger.logLoading(this, name, null); Class<?> c = findLoadedClass(name); if (c == null && this.policy.canLoad(name)) { try { if (parent != null) { c = parent.loadClass(name); } else { c = getSystemClassLoader().loadClass(name); } } catch (ClassNotFoundException e) { } } if (c == null) { try { c = findClass(name); } catch (ClassNotFoundException e) { Logger.logLoading(this, name, false); throw e; } } if (resolve) { resolveClass(c); } Logger.logLoading(this, name, true); return c; } ServiceClassLoader(LoadedLibrary lib, ClassLoader parent, ClassLoaderPolicy policy); }
ServiceClassLoader extends ClassLoader { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Logger.logLoading(this, name, null); Class<?> c = findLoadedClass(name); if (c == null && this.policy.canLoad(name)) { try { if (parent != null) { c = parent.loadClass(name); } else { c = getSystemClassLoader().loadClass(name); } } catch (ClassNotFoundException e) { } } if (c == null) { try { c = findClass(name); } catch (ClassNotFoundException e) { Logger.logLoading(this, name, false); throw e; } } if (resolve) { resolveClass(c); } Logger.logLoading(this, name, true); return c; } ServiceClassLoader(LoadedLibrary lib, ClassLoader parent, ClassLoaderPolicy policy); LoadedLibrary getLibrary(); @Override URL getResource(String resourceName); }
ServiceClassLoader extends ClassLoader { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Logger.logLoading(this, name, null); Class<?> c = findLoadedClass(name); if (c == null && this.policy.canLoad(name)) { try { if (parent != null) { c = parent.loadClass(name); } else { c = getSystemClassLoader().loadClass(name); } } catch (ClassNotFoundException e) { } } if (c == null) { try { c = findClass(name); } catch (ClassNotFoundException e) { Logger.logLoading(this, name, false); throw e; } } if (resolve) { resolveClass(c); } Logger.logLoading(this, name, true); return c; } ServiceClassLoader(LoadedLibrary lib, ClassLoader parent, ClassLoaderPolicy policy); LoadedLibrary getLibrary(); @Override URL getResource(String resourceName);  }