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); }
@Override public Iterable<URL> getResources(String name) { return new ClassLoaderUtils(loader, mgmt).getResources(name); } }
public void setManagementContext(ManagementContext mgmt) { this.mgmt = checkNotNull(mgmt, "mgmt"); currentLoader.set(new ClassLoaderUtils(currentClassLoader, mgmt)); }
private void assertLoadFails(ClassLoaderUtils clu, String bundledClassName) { BundledName className = new BundledName(bundledClassName); try { clu.loadClass(bundledClassName); Asserts.shouldHaveFailedPreviously("Using loader " + clu); } catch (ClassNotFoundException e) { Asserts.expectedFailureContains(e, bundledClassName, "not found on the application class path, nor in the bundle white list"); } BundledName resource = className.toResource(); String bundledResource = resource.toString(); assertNull(clu.getResource(bundledResource), resource + " is supposed to fail resource loading, but it was successful"); assertEquals(clu.getResources(bundledResource), ImmutableList.of(), resource + " is supposed to fail resource loading, but it was successful"); }
String className; if (looksLikeBundledClassName(name)) { String[] arr = name.split(CLASS_NAME_DELIMITER); if (arr.length > 3) { if (symbolicName != null && version != null) { return tryLoadFromBundle(dispatcher, symbolicName, version, className); cls = tryLoadFromBundle(dispatcher, symbolicName, version, className); if (cls.isPresent()) { return cls; Maybe<T> result = loadClass(name, dispatcher, className); return loadClass(name, dispatcher, className);
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { return currentLoader.get().loadClass(name); }
@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"))); }
protected <T> Maybe<T> tryLoadFromBundleWhiteList(LoaderDispatcher<T> dispatcher, String className) { Framework framework = getFramework(); if (framework == null) { return Maybe.absentNull(); } List<Bundle> bundles = Osgis.bundleFinder(framework) .satisfying(createBundleMatchingPredicate()) .findAll(); for (Bundle b : bundles) { Maybe<T> item = dispatcher.tryLoadFrom(b, className); if (item.isPresent()) { return item; } } return Maybe.absentNull(); }
protected <T> Maybe<T> tryLoadFromBundle(LoaderDispatcher<T> dispatcher, String symbolicName, String version, String name) { Framework framework = getFramework(); if (framework != null) { Maybe<Bundle> bundle = Osgis.bundleFinder(framework) .symbolicName(symbolicName) .version(version) .find(); if (bundle.isAbsent()) { throw new IllegalStateException("Bundle " + toBundleString(symbolicName, version) + " not found to load " + name); } return dispatcher.tryLoadFrom(bundle.get(), name); } else { log.warn("Request for bundle '"+symbolicName+"' "+(Strings.isNonBlank(version) ? "("+version+") " : "")+"will be ignored as no framework available; will look for '"+name+"' in plain old classpath"); return dispatcher.tryLoadFrom(classLoader, name); } }
@Beta public boolean isBundleWhiteListed(Bundle bundle) { WhiteListBundlePredicate p = createBundleMatchingPredicate(); return p.apply(bundle); }
private void setCurrentClassLoader(ClassLoader classLoader) { currentClassLoader = checkNotNull(classLoader); currentLoader.set(new ClassLoaderUtils(currentClassLoader, mgmt)); }
private void assertLoadSucceeds(ClassLoaderUtils clu, String bundledClassName, Class<?> expectedClass) throws ClassNotFoundException { BundledName className = new BundledName(bundledClassName); Class<?> cls = clu.loadClass(bundledClassName); assertEquals(cls.getName(), className.name); if (expectedClass != null) { assertEquals(cls, expectedClass); } ClassLoader cl = cls.getClassLoader(); BundledName resource = className.toResource(); String bundledResource = resource.toString(); URL resourceUrl = cl.getResource(resource.name); assertEquals(clu.getResource(bundledResource), resourceUrl); assertEquals(clu.getResources(bundledResource), ImmutableList.of(resourceUrl), "Loading with "+clu); BundledName rootResource = new BundledName(resource.bundle, resource.version, "/" + resource.name); String rootBundledResource = rootResource.toString(); assertEquals(clu.getResource(rootBundledResource), resourceUrl); assertEquals(clu.getResources(rootBundledResource), ImmutableList.of(resourceUrl)); }
@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); } } });
@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 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); }
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); } }
@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); }
@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); } }
@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); }