Search in sources :

Example 1 with ContextsConfig

use of org.apache.accumulo.start.classloader.vfs.ContextManager.ContextsConfig in project accumulo by apache.

the class ContextManagerTest method differentContexts.

@Test
public void differentContexts() throws Exception {
    ContextManager cm = new ContextManager(vfs, new ReloadingClassLoader() {

        @Override
        public ClassLoader getClassLoader() {
            return ClassLoader.getSystemClassLoader();
        }
    });
    cm.setContextConfig(new ContextsConfig() {

        @Override
        public ContextConfig getContextConfig(String context) {
            if (context.equals("CX1")) {
                return new ContextConfig(uri1, true);
            } else if (context.equals("CX2")) {
                return new ContextConfig(uri2, true);
            }
            return null;
        }
    });
    FileObject testDir = vfs.resolveFile(folder1.getRoot().toURI().toString());
    FileObject[] dirContents = testDir.getChildren();
    ClassLoader cl1 = cm.getClassLoader("CX1");
    FileObject[] files = ((VFSClassLoader) cl1).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents), files);
    FileObject testDir2 = vfs.resolveFile(folder2.getRoot().toURI().toString());
    FileObject[] dirContents2 = testDir2.getChildren();
    ClassLoader cl2 = cm.getClassLoader("CX2");
    FileObject[] files2 = ((VFSClassLoader) cl2).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents2), files2);
    Class<?> defaultContextClass = cl1.loadClass("test.HelloWorld");
    Object o1 = defaultContextClass.newInstance();
    Assert.assertEquals("Hello World!", o1.toString());
    Class<?> myContextClass = cl2.loadClass("test.HelloWorld");
    Object o2 = myContextClass.newInstance();
    Assert.assertEquals("Hello World!", o2.toString());
    Assert.assertFalse(defaultContextClass.equals(myContextClass));
    cm.removeUnusedContexts(new HashSet<>());
}
Also used : VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) ContextConfig(org.apache.accumulo.start.classloader.vfs.ContextManager.ContextConfig) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject) ContextsConfig(org.apache.accumulo.start.classloader.vfs.ContextManager.ContextsConfig) Test(org.junit.Test)

Example 2 with ContextsConfig

use of org.apache.accumulo.start.classloader.vfs.ContextManager.ContextsConfig in project accumulo by apache.

the class ContextManagerTest method testPostDelegation.

@Test
public void testPostDelegation() throws Exception {
    final VFSClassLoader parent = new VFSClassLoader(new FileObject[] { vfs.resolveFile(uri1) }, vfs);
    Class<?> pclass = parent.loadClass("test.HelloWorld");
    ContextManager cm = new ContextManager(vfs, new ReloadingClassLoader() {

        @Override
        public ClassLoader getClassLoader() {
            return parent;
        }
    });
    cm.setContextConfig(new ContextsConfig() {

        @Override
        public ContextConfig getContextConfig(String context) {
            if (context.equals("CX1")) {
                return new ContextConfig(uri2.toString(), true);
            } else if (context.equals("CX2")) {
                return new ContextConfig(uri2.toString(), false);
            }
            return null;
        }
    });
    Assert.assertTrue(cm.getClassLoader("CX1").loadClass("test.HelloWorld") == pclass);
    Assert.assertFalse(cm.getClassLoader("CX2").loadClass("test.HelloWorld") == pclass);
}
Also used : ContextConfig(org.apache.accumulo.start.classloader.vfs.ContextManager.ContextConfig) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) ContextsConfig(org.apache.accumulo.start.classloader.vfs.ContextManager.ContextsConfig) Test(org.junit.Test)

Aggregations

ContextConfig (org.apache.accumulo.start.classloader.vfs.ContextManager.ContextConfig)2 ContextsConfig (org.apache.accumulo.start.classloader.vfs.ContextManager.ContextsConfig)2 VFSClassLoader (org.apache.commons.vfs2.impl.VFSClassLoader)2 Test (org.junit.Test)2 FileObject (org.apache.commons.vfs2.FileObject)1