ClassLoaderUtils { public Class<?> loadClass(String name) throws ClassNotFoundException { Maybe<Class<?>> cls = load(name, ClassLoaderDispatcher.INSTANCE); if (cls.isPresent()) { return cls.get(); } else { throw new ClassNotFoundException("Class " + name + " not found on the application class path, nor in the bundle white list.", getReturnException(cls)); } } ClassLoaderUtils(Object callingObj, Entity entity);  ClassLoaderUtils(Object callingObj, @Nullable ManagementContext mgmt);  ClassLoaderUtils(Class<?> callingClass);  ClassLoaderUtils(@Nullable ClassLoader cl);  ClassLoaderUtils(@Nullable ClassLoader cl, @Nullable ManagementContext mgmt);  ClassLoaderUtils(Class<?> callingClass, Entity entity);  ClassLoaderUtils(Class<?> callingClass, @Nullable ManagementContext mgmt); Class<?> loadClass(String name); Class<?> loadClass(String symbolicName, @Nullable String version, String className); URL getResource(String name); Iterable<URL> getResources(String name); @Beta boolean isBundleWhiteListed(Bundle bundle); @Override String toString();  }
@Test public void testLoadClassInOsgi() throws Exception { String bundlePath = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH; String bundleUrl = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL; String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = installBundle(mgmt, bundleUrl); @SuppressWarnings("unchecked") Class<? extends Entity> clazz = (Class<? extends Entity>) bundle.loadClass(classname); Entity entity = createSimpleEntity(bundleUrl, clazz); System.setProperty(ClassLoaderUtils.WHITE_LIST_KEY, "do.not.match.whitelist:1.0.0"); ClassLoaderUtils cluMgmt = new ClassLoaderUtils(getClass(), mgmt); ClassLoaderUtils cluClass = new ClassLoaderUtils(clazz); ClassLoaderUtils cluEntity = new ClassLoaderUtils(getClass(), entity); assertLoadFails(classname, cluMgmt); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + bundle.getVersion()+":" + classname, clazz, cluMgmt, cluClass, cluEntity); }
@Test public void testVariousLoadersLoadClassInOsgiWhiteList() throws Exception { String bundlePath = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH; String bundleUrl = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL; String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = installBundle(mgmt, bundleUrl); Class<?> clazz = bundle.loadClass(classname); Entity entity = createSimpleEntity(bundleUrl, clazz); String whiteList = bundle.getSymbolicName()+":"+bundle.getVersion(); System.setProperty(ClassLoaderUtils.WHITE_LIST_KEY, whiteList); ClassLoaderUtils cluMgmt = new ClassLoaderUtils(getClass(), mgmt); ClassLoaderUtils cluClass = new ClassLoaderUtils(clazz); ClassLoaderUtils cluEntity = new ClassLoaderUtils(getClass(), entity); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluEntity); }
@Test public void testLoadBrooklynClass() throws Exception { mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); new ClassLoaderUtils(this, mgmt).loadClass( "org.apache.brooklyn.api", BrooklynVersion.get(), Entity.class.getName()); new ClassLoaderUtils(this, mgmt).loadClass( "org.apache.brooklyn.api", BrooklynVersion.getOsgiVersion(), Entity.class.getName()); try { new ClassLoaderUtils(this, mgmt).loadClass( "org.apache.brooklyn.api", "100.100.100-alpha-version_wth.tags", Entity.class.getName()); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { IllegalStateException nested = Exceptions.getFirstThrowableOfType(e, IllegalStateException.class); assertNotNull(nested); Asserts.expectedFailureContains(nested, "not found to load"); } try { new ClassLoaderUtils(this, mgmt).loadClass( "org.apache.brooklyn.api", "100.100.100-SNAPSHOT", Entity.class.getName()); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { IllegalStateException nested = Exceptions.getFirstThrowableOfType(e, IllegalStateException.class); assertNotNull(nested); Asserts.expectedFailureContains(nested, "not found to load"); } }