use of com.oracle.bedrock.runtime.java.ClassPath in project oracle-bedrock by coherence-community.
the class ContainerClassLoader method newInstance.
/**
* A helper method to instantiate a new {@link ContainerClassLoader}.
*
* @param applicationName the name of the application
* @param classPath the {@link ClassPath} of the application
* @param localProperties the local system properties for the {@link ClassLoader}
* @param systemProperties the System properties to use to get the default class path
* @param availablePorts the {@link AvailablePortIterator}
* @param redirectErrorStream should the error stream be redirected to stdout
* @param pipeBufferSizeBytes the size of the pipe buffer for I/O redirection
*
* @return a {@link ContainerClassLoader} for the application
*
* @throws Exception if some exception occurs
*/
@SuppressWarnings("ConstantConditions")
protected static ContainerClassLoader newInstance(String applicationName, ClassPath classPath, Properties localProperties, Properties systemProperties, AvailablePortIterator availablePorts, boolean redirectErrorStream, int pipeBufferSizeBytes) throws Exception {
if (classPath == null || classPath.isEmpty()) {
classPath = new ClassPath(systemProperties.getProperty(PROPERTY_JAVA_CLASS_PATH));
if (classPath == null) {
System.out.println("Classpath should not be null!");
}
}
// acquire the platform
Scope platformScope = Container.getPlatformScope();
// establish an MBeanServerBuilder
ContainerMBeanServerBuilder mBeanServerBuilder = new ContainerMBeanServerBuilder(LocalPlatform.get().getAvailablePorts());
// establish the Scope for the application
ContainerScope scope = new ContainerScope(applicationName, platformScope.getProperties(), availablePorts, mBeanServerBuilder, redirectErrorStream, pipeBufferSizeBytes);
// override the MBeanServerBuilder for the Scope
localProperties.put(ContainerMBeanServerBuilder.PROPERTY_JMX_MBEAN_SERVER_BUILDER, DelegatingMBeanServerBuilder.class.getCanonicalName());
// add local properties to the scope
scope.getProperties().putAll(localProperties);
ClassLoader parentLoader = ContainerClassLoader.class.getClassLoader();
ContainerClassLoader loader = new ContainerClassLoader(classPath, parentLoader, scope);
String excludedPackageList = localProperties.getProperty(PROPERTY_EXCLUDED_PACKAGES);
if (excludedPackageList != null && excludedPackageList.trim().length() > 0) {
String[] packages = excludedPackageList.split(",");
for (String pack : packages) {
if (pack.trim().length() > 0) {
loader.addPackageToLoadFromParent(pack.trim());
}
}
}
// the following packages must not be isolated (ie: loaded by the parent)
loader.addPackageToLoadFromParent("com.oracle.bedrock.runtime.java");
loader.addPackageToLoadFromParent("com.oracle.bedrock.runtime.java.container");
return loader;
}
use of com.oracle.bedrock.runtime.java.ClassPath in project oracle-bedrock by coherence-community.
the class ContainerClassLoaderTest method shouldLoadClassFromJarInCustomClassPath.
/**
* Ensure that the ContainerClassLoader can load a Class from a
* Jar.
*
* @throws Exception
*/
@Test
public void shouldLoadClassFromJarInCustomClassPath() throws Exception {
// NOTE: the Mockito class in the Mockito jar contains a LICENSE file
ClassPath classPath = ClassPath.ofResource("LICENSE");
ClassLoader loader = ContainerClassLoader.newInstance("Test", classPath, System.getProperties());
Class<?> result = loader.loadClass(Mockito.class.getCanonicalName());
assertThat(result.getClassLoader(), sameInstance(loader));
assertThat(result.getCanonicalName(), is(Mockito.class.getCanonicalName()));
assertThat(result.equals(Mockito.class), is(false));
}
use of com.oracle.bedrock.runtime.java.ClassPath in project oracle-bedrock by coherence-community.
the class ContainerClassLoaderTest method shouldUseDefaultClassPath.
/**
* Ensure that we can create ContainerClassLoader that uses
* the System ClassPath.
*
* @throws Exception
*/
@Test
public void shouldUseDefaultClassPath() throws Exception {
ClassPath systemClassPath = ClassPath.ofSystem();
ContainerClassLoader loader = ContainerClassLoader.newInstance("Test", null, System.getProperties());
ClassPath classPath = loader.getClassPath();
Assert.assertThat(classPath, is(systemClassPath));
}
use of com.oracle.bedrock.runtime.java.ClassPath in project oracle-bedrock by coherence-community.
the class JavaModules method compose.
@Override
public JavaModules compose(JavaModules other) {
Set<String> setAdd = new LinkedHashSet<>(this.modules);
Set<String> setExport = new LinkedHashSet<>(this.exports);
Set<String> setPatch = new LinkedHashSet<>(this.patches);
Set<String> setReads = new LinkedHashSet<>(this.reading);
Set<String> setOpens = new LinkedHashSet<>(this.opens);
setAdd.addAll(other.modules);
setExport.addAll(other.exports);
setPatch.addAll(other.patches);
setReads.addAll(other.reading);
setOpens.addAll(other.opens);
boolean isEnabled = this.enabled && other.enabled;
ClassPath classPath = new ClassPath(this.classPath, other.classPath);
return new JavaModules(isEnabled, setAdd, setExport, setPatch, setReads, setOpens, classPath);
}
use of com.oracle.bedrock.runtime.java.ClassPath in project oracle-bedrock by coherence-community.
the class ContainerClassLoaderTest method shouldLoadClassFromDirectoryInCustomClassPath.
/**
* Ensure that the ContainerClassLoader can load a Class from a
* custom ClassPath (based on a Java Class).
*
* @throws Exception
*/
@Test
public void shouldLoadClassFromDirectoryInCustomClassPath() throws Exception {
ClassPath classPath = ClassPath.ofClass(DummyClass.class);
ClassLoader loader = ContainerClassLoader.newInstance("Test", classPath, System.getProperties());
Class<?> result = loader.loadClass(DummyClass.class.getCanonicalName());
assertThat(result.getClassLoader(), sameInstance(loader));
assertThat(result.getCanonicalName(), is(DummyClass.class.getCanonicalName()));
assertThat(result.equals(DummyClass.class), is(false));
}
Aggregations