@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 testLoadClassInOsgiCore() throws Exception { Class<?> clazz = BasicEntity.class; String classname = clazz.getName(); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = getBundle(mgmt, "org.apache.brooklyn.core"); Entity entity = createSimpleEntity(bundle.getLocation(), clazz); ClassLoaderUtils cluMgmt = new ClassLoaderUtils(getClass(), mgmt); ClassLoaderUtils cluClass = new ClassLoaderUtils(clazz); ClassLoaderUtils cluNone = new ClassLoaderUtils(getClass()); ClassLoaderUtils cluEntity = new ClassLoaderUtils(getClass(), entity); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluNone, cluEntity); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluNone, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluNone, cluEntity); assertLoadSucceeds(bundle.getSymbolicName() + ":" + bundle.getVersion() + ":" + classname, clazz, cluMgmt, cluClass, cluNone, cluEntity); }
Entity entity = createSimpleEntity(bundleUrl, clazz);
@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 testLoadJustOneClassInOsgiWhiteList() 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 cluEntity = new ClassLoaderUtils(getClass(), entity); BundledName resource = new BundledName(classname).toResource(); BundledName bn = new BundledName(resource.bundle, resource.version, "/" + resource.name); Asserts.assertSize(cluEntity.getResources(bn.toString()), 1); }