public void setManagementContext(ManagementContext mgmt) { this.mgmt = checkNotNull(mgmt, "mgmt"); currentLoader.set(new ClassLoaderUtils(currentClassLoader, mgmt)); }
private void setCurrentClassLoader(ClassLoader classLoader) { currentClassLoader = checkNotNull(classLoader); currentLoader.set(new ClassLoaderUtils(currentClassLoader, mgmt)); }
@Override public Iterable<URL> getResources(String name) { return new ClassLoaderUtils(loader, mgmt).getResources(name); } }
private Class<? extends MachineLocation> getLocationClass(String osFamily) { try { if (osFamily != null) { String className = OS_TO_MACHINE_LOCATION_TYPE.get(osFamily.toLowerCase(Locale.ENGLISH)); return new ClassLoaderUtils(this, managementContext).loadClass(className).asSubclass(MachineLocation.class); } } catch (ClassNotFoundException ex) {} return null; }
@Override public URL getResource(String name) { return new ClassLoaderUtils(loader, mgmt).getResource(name); }
private EntitySpec<?> buildSpec(String specTypName) { // TODO is this hardcoded list deprecated? If so log a warning. try { Class<?> specType = new ClassLoaderUtils(this.getClass()).loadClass(specTypName); return buildSpec(specType); } catch (ClassNotFoundException e) { throw new IllegalStateException("Unable to load hardcoded catalog type " + specTypName, e); } }
@Override public Class apply(final String input) { try { //return Class.forName(input); return new ClassLoaderUtils(this.getClass()).loadClass(input); } catch (ClassNotFoundException e) { throw Exceptions.propagate(e); } } });
@SuppressWarnings({ "unchecked", "rawtypes" }) private void loadExtension() { Map<String, String> extensions = getConfig(LocationConfigKeys.EXTENSIONS); if (extensions != null) { for (Map.Entry<String, String> extension: extensions.entrySet()) { try { Class<?> extensionClassType = new ClassLoaderUtils(this, getManagementContext()).loadClass(extension.getKey()); if (!hasExtension(extensionClassType)) { Object extensionClass = new ClassLoaderUtils(this, getManagementContext()).loadClass(extension.getValue()).newInstance(); addExtension((Class)extensionClassType, extensionClass); } } catch (Exception e) { LOG.error("Location extension can not be loaded (rethrowing): {} {} {}", new Object[] {extension.getKey(), extension.getValue(), e}); throw Exceptions.propagate(e); } } } }
@SuppressWarnings("unchecked") public static Map<String, Sensor<?>> getDefinedSensors(String entityTypeName) { try { return getDefinedSensors((Class<? extends Entity>) new ClassLoaderUtils(BrooklynTypes.class).loadClass(entityTypeName)); } catch (ClassNotFoundException e) { throw Exceptions.propagate(e); } }
@SuppressWarnings("unchecked") public static Map<String, ConfigKey<?>> getDefinedConfigKeys(String brooklynTypeName) { try { return getDefinedConfigKeys((Class<? extends BrooklynObject>) new ClassLoaderUtils(BrooklynTypes.class).loadClass(brooklynTypeName)); } catch (ClassNotFoundException e) { throw Exceptions.propagate(e); } }
@Test public void testDebug() throws ClassNotFoundException { System.out.println(BasicSpecParameter.fromClass(mgmt, new ClassLoaderUtils(this.getClass()).loadClass("org.apache.brooklyn.entity.stock.BasicApplication"))); }
protected Class<?> loadClass(String jType) throws ClassNotFoundException { try { return reflections.loadClass(jType); } catch (Exception e) { Exceptions.propagateIfFatal(e); } return new ClassLoaderUtils(reflections.getClassLoader(), managementContext).loadClass(jType); }
@Test public void testLoadClassNotInOsgi() throws Exception { ClassLoaderUtils clu = new ClassLoaderUtils(getClass()); assertLoadSucceeds(clu, getClass().getName(), getClass()); assertLoadSucceeds(clu, Entity.class.getName(), Entity.class); assertLoadSucceeds(clu, AbstractEntity.class.getName(), AbstractEntity.class); assertLoadFails(clu, "org.apache.brooklyn.this.name.does.not.Exist"); }
@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); }
@Test public void testLoadClassInOsgiApi() throws Exception { Class<?> clazz = Entity.class; String classname = clazz.getName(); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = getBundle(mgmt, "org.apache.brooklyn.api"); ClassLoaderUtils cluMgmt = new ClassLoaderUtils(getClass(), mgmt); ClassLoaderUtils cluClass = new ClassLoaderUtils(clazz); ClassLoaderUtils cluNone = new ClassLoaderUtils(getClass()); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluNone); assertLoadSucceeds(classname, clazz, cluMgmt, cluClass, cluNone); assertLoadSucceeds(bundle.getSymbolicName() + ":" + classname, clazz, cluMgmt, cluClass, cluNone); assertLoadSucceeds(bundle.getSymbolicName() + ":" + bundle.getVersion() + ":" + classname, clazz, cluMgmt, cluClass, cluNone); }
@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); }
@Override @SuppressWarnings({ "rawtypes", "unchecked" }) public Maybe<Class<?>> tryLoadClass(String className) { String mappedClassName = DeserializingClassRenamesProvider.INSTANCE.findMappedName(className); Maybe<Class<?>> cls = tryLoadClass0(mappedClassName); if (cls.isPresent()) { return cls; } try { return (Maybe) Maybe.of(new ClassLoaderUtils(loader, mgmt).loadClass(mappedClassName)); } catch (Exception e) { Exceptions.propagateIfFatal(e); // return original error return cls; } }
@Test public void testIsBundleWhiteListed() throws Exception { mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); ClassLoaderUtils clu = new ClassLoaderUtils(getClass(), mgmt); assertTrue(clu.isBundleWhiteListed(getBundle(mgmt, "org.apache.brooklyn.core"))); assertTrue(clu.isBundleWhiteListed(getBundle(mgmt, "org.apache.brooklyn.api"))); assertFalse(clu.isBundleWhiteListed(getBundle(mgmt, "com.google.guava"))); }
/** * When two guava versions installed, want us to load from the *brooklyn* version rather than * a newer version that happens to be in Karaf. */ @Test(groups={"Integration"}) public void testLoadsFromRightGuavaVersion() throws Exception { mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); ClassLoaderUtils clu = new ClassLoaderUtils(getClass(), mgmt); String bundleUrl = MavenRetriever.localUrl(MavenArtifact.fromCoordinate("com.google.guava:guava:jar:18.0")); Bundle bundle = installBundle(mgmt, bundleUrl); String bundleName = bundle.getSymbolicName(); String classname = bundleName + ":" + ImmutableList.class.getName(); assertLoadSucceeds(clu, classname, ImmutableList.class); }
@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); }