Search in sources :

Example 6 with ClassPath

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;
}
Also used : ClassPath(com.oracle.bedrock.runtime.java.ClassPath)

Example 7 with ClassPath

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));
}
Also used : ClassPath(com.oracle.bedrock.runtime.java.ClassPath) Mockito(org.mockito.Mockito) Test(org.junit.Test)

Example 8 with ClassPath

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));
}
Also used : ClassPath(com.oracle.bedrock.runtime.java.ClassPath) Test(org.junit.Test)

Example 9 with ClassPath

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ClassPath(com.oracle.bedrock.runtime.java.ClassPath)

Example 10 with 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));
}
Also used : ClassPath(com.oracle.bedrock.runtime.java.ClassPath) DummyClass(classloader.child.DummyClass) Test(org.junit.Test)

Aggregations

ClassPath (com.oracle.bedrock.runtime.java.ClassPath)29 Test (org.junit.Test)21 File (java.io.File)12 TestClasses (com.oracle.bedrock.testsupport.junit.options.TestClasses)10 AbstractJUnit4Test (com.oracle.bedrock.testsupport.junit.AbstractJUnit4Test)9 JUnit3Test (com.oracle.bedrock.testsupport.junit.JUnit3Test)9 JUnit4Test (com.oracle.bedrock.testsupport.junit.JUnit4Test)9 RunWithAnnotatedTest (com.oracle.bedrock.testsupport.junit.RunWithAnnotatedTest)9 OptionsByType (com.oracle.bedrock.OptionsByType)4 LocalPlatform (com.oracle.bedrock.runtime.LocalPlatform)4 MetaClass (com.oracle.bedrock.runtime.MetaClass)4 JavaAgent (com.oracle.bedrock.runtime.java.options.JavaAgent)3 DummyClass (classloader.child.DummyClass)2 AvailablePortIterator (com.oracle.bedrock.runtime.network.AvailablePortIterator)2 JUnit3Suite (com.oracle.bedrock.testsupport.junit.JUnit3Suite)2 Capture (com.oracle.bedrock.util.Capture)2 LinkedHashSet (java.util.LinkedHashSet)2 DummyParentLoadedClass (classloader.parent.DummyParentLoadedClass)1 JavaApplication (com.oracle.bedrock.runtime.java.JavaApplication)1 ContainerClassLoader (com.oracle.bedrock.runtime.java.container.ContainerClassLoader)1