Package org.nanocontainer

Examples of org.nanocontainer.NanoContainer


import java.net.MalformedURLException;

public class DefaultNanoContainerTestCase extends TestCase {

    public void testBasic() throws PicoRegistrationException, PicoInitializationException, ClassNotFoundException {
        NanoContainer nanoContainer = new DefaultNanoContainer();
        nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.DefaultWebServerConfig");
        nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.WebServer", "org.nanocontainer.testmodel.WebServerImpl");
    }
View Full Code Here


        nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.DefaultWebServerConfig");
        nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.WebServer", "org.nanocontainer.testmodel.WebServerImpl");
    }

    public void testProvision() throws PicoException, PicoInitializationException, ClassNotFoundException {
        NanoContainer nanoContainer = new DefaultNanoContainer();
        nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.DefaultWebServerConfig");
        nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.WebServerImpl");

        assertNotNull("WebServerImpl should exist", nanoContainer.getPico().getComponentInstance(WebServerImpl.class));
        assertTrue("WebServerImpl should exist", nanoContainer.getPico().getComponentInstance(WebServerImpl.class) instanceof WebServerImpl);
    }
View Full Code Here

        assertNotNull("WebServerImpl should exist", nanoContainer.getPico().getComponentInstance(WebServerImpl.class));
        assertTrue("WebServerImpl should exist", nanoContainer.getPico().getComponentInstance(WebServerImpl.class) instanceof WebServerImpl);
    }

    public void testNoGenerationRegistration() throws PicoRegistrationException, PicoIntrospectionException {
        NanoContainer nanoContainer = new DefaultNanoContainer();
        try {
            nanoContainer.registerComponentImplementation("Ping");
            fail("should have failed");
        } catch (ClassNotFoundException e) {
            // expected
        }
    }
View Full Code Here

            // expected
        }
    }

    public void testParametersCanBePassedInStringForm() throws ClassNotFoundException, PicoException, PicoInitializationException {
        NanoContainer nanoContainer = new DefaultNanoContainer();
        String className = ThingThatTakesParamsInConstructor.class.getName();

        nanoContainer.registerComponentImplementation("thing",
                className,
                new String[]{
                    "java.lang.String",
                    "java.lang.Integer"
                },
                new String[]{
                    "hello",
                    "22"
                });

        ThingThatTakesParamsInConstructor thing =
                (ThingThatTakesParamsInConstructor) nanoContainer.getPico().getComponentInstance("thing");
        assertNotNull("component not present", thing);
        assertEquals("hello22", thing.getValue());
    }
View Full Code Here

        assertNotNull("The testcomp.jar system property should point to java/nanocontainer/src/test-comp/TestComp.jar", testcompJarFileName);
        File testCompJar = new File(testcompJarFileName);

        // Set up parent
        NanoContainer parentContainer = new DefaultNanoContainer();
        parentContainer.addClassLoaderURL(testCompJar.toURL());
        parentContainer.registerComponentImplementation("parentTestComp", "TestComp");
        parentContainer.registerComponentImplementation("java.lang.StringBuffer");

        PicoContainer parentContainerAdapterPico = parentContainer.getPico();
        Object parentTestComp = parentContainerAdapterPico.getComponentInstance("parentTestComp");
        assertEquals("TestComp", parentTestComp.getClass().getName());

        // Set up child
        NanoContainer childContainer = new DefaultNanoContainer(parentContainer);
        File testCompJar2 = new File(testCompJar.getParentFile(), "TestComp2.jar");
        childContainer.addClassLoaderURL(testCompJar2.toURL());
        childContainer.registerComponentImplementation("childTestComp", "TestComp2");

        PicoContainer childContainerAdapterPico = childContainer.getPico();
        Object childTestComp = childContainerAdapterPico.getComponentInstance("childTestComp");

        assertEquals("TestComp2", childTestComp.getClass().getName());

        assertNotSame(parentTestComp, childTestComp);
View Full Code Here

    public static class AnotherFooComp {

    }

    public void testClassLoaderJugglingIsPossible() throws MalformedURLException, ClassNotFoundException {
        NanoContainer parentContainer = new DefaultNanoContainer();

        String testcompJarFileName = System.getProperty("testcomp.jar");
        // Paul's path to TestComp. PLEASE do not take out.
        //testcompJarFileName = "D:/OSS/PN/java/nanocontainer/src/test-comp/TestComp.jar";
        assertNotNull("The testcomp.jar system property should point to nano/reflection/src/test-comp/TestComp.jar", testcompJarFileName);
        File testCompJar = new File(testcompJarFileName);
        assertTrue(testCompJar.isFile());

        parentContainer.registerComponentImplementation("foo", "org.nanocontainer.testmodel.DefaultWebServerConfig");

        Object fooWebServerConfig = parentContainer.getPico().getComponentInstance("foo");
        assertEquals("org.nanocontainer.testmodel.DefaultWebServerConfig", fooWebServerConfig.getClass().getName());

        NanoContainer childContainer = new DefaultNanoContainer(parentContainer);
        childContainer.addClassLoaderURL(testCompJar.toURL());
        childContainer.registerComponentImplementation("bar", "TestComp");

        Object barTestComp = childContainer.getPico().getComponentInstance("bar");
        assertEquals("TestComp", barTestComp.getClass().getName());

        assertNotSame(fooWebServerConfig.getClass().getClassLoader(), barTestComp.getClass().getClassLoader());

        // This kludge is needed because IDEA, Eclipse and Maven have different numbers of
View Full Code Here

            }
        }
    }

    public void TODO_testSecurityManagerCanPreventOperations() throws MalformedURLException, ClassNotFoundException {
        NanoContainer parentContainer = new DefaultNanoContainer();

        String testcompJarFileName = System.getProperty("testcomp.jar");
        // Paul's path to TestComp. PLEASE do not take out.
        //testcompJarFileName = "D:/OSS/PN/java/nanocontainer/src/test-comp/TestComp.jar";
        assertNotNull("The testcomp.jar system property should point to nano/reflection/src/test-comp/TestComp.jar", testcompJarFileName);
        File testCompJar = new File(testcompJarFileName);
        assertTrue(testCompJar.isFile());

        parentContainer.registerComponentImplementation("foo", "org.nanocontainer.testmodel.DefaultWebServerConfig");

        Object fooWebServerConfig = parentContainer.getPico().getComponentInstance("foo");
        assertEquals("org.nanocontainer.testmodel.DefaultWebServerConfig", fooWebServerConfig.getClass().getName());

        NanoContainer childContainer = new DefaultNanoContainer(parentContainer);
        childContainer.addClassLoaderURL(testCompJar.toURL());
        //TODO childContainer.setPermission(some permission list, that includes the preventing of general file access);
        // Or shoud this be done in the ctor for DRCA ?
        // or should it a parameter in the addClassLoaderURL(..) method
        childContainer.registerComponentImplementation("bar", "org.nanocontainer.testmodel.FileSystemUsing");

        try {
            parentContainer.getPico().getComponentInstance("bar");
            fail("Should have barfed");
        } catch (java.security.AccessControlException e) {
View Full Code Here

      return (String[])tokens.toArray(new String[tokens.size()]);
  }
 
  private ContainerPopulator createContainerPopulator(Reader reader, MutablePicoContainer parent)
            throws ClassNotFoundException {
        NanoContainer nano = new DefaultNanoContainer(getClassLoader());
        Parameter[] parameters = new Parameter[] {
                new ConstantParameter(reader),
                new ConstantParameter(getClassLoader()) };
        nano.registerComponentImplementation(containerBuilderClassName,
                containerBuilderClassName, parameters);
        ContainerBuilder containerBuilder = (ContainerBuilder) nano
                .getPico().getComponentInstance(containerBuilderClassName);
        ObjectReference parentRef = new SimpleReference();
        parentRef.set(parent);
        containerBuilder.buildContainer(new SimpleReference(), parentRef, null, false);
        return (ContainerPopulator) containerBuilder;
View Full Code Here

    }

    private ContainerComposer createContainerComposer(ServletContext context) throws ClassNotFoundException{
        String containerComposerClassName = context.getInitParameter(CONTAINER_COMPOSER);
        // disposable container used to instantiate the ContainerComposer
        NanoContainer nanoContainer = new DefaultNanoContainer(Thread.currentThread().getContextClassLoader());
        String script = context.getInitParameter(CONTAINER_COMPOSER_CONFIGURATION);
        PicoContainer picoConfiguration = null;
        if ( script != null ){
            Reader scriptReader = new InputStreamReader(context.getResourceAsStream(script));
            String builderClassName = getBuilderClassName(script);
            ScriptedContainerBuilderFactory scriptedContainerBuilderFactory = new ScriptedContainerBuilderFactory(scriptReader, builderClassName, Thread.currentThread().getContextClassLoader());
            picoConfiguration = buildContainer(scriptedContainerBuilderFactory.getContainerBuilder());
        }
        ComponentAdapter componentAdapter = null;
        if ( picoConfiguration != null ){
            Parameter[] parameters = new Parameter[]{ new ConstantParameter(picoConfiguration) };
            componentAdapter = nanoContainer.registerComponentImplementation(containerComposerClassName, containerComposerClassName, parameters);
        } else {
            componentAdapter = nanoContainer.registerComponentImplementation(containerComposerClassName);           
        }
        return (ContainerComposer) componentAdapter.getComponentInstance(nanoContainer.getPico());       
    }
View Full Code Here

     * @return An instance of ContainerPopulator
     * @throws ClassNotFoundException
     */
    private ContainerPopulator createContainerPopulator(String containerBuilderClassName, Reader reader, ClassLoader classLoader)
            throws ClassNotFoundException {
        NanoContainer nano = new DefaultNanoContainer(classLoader);
        Parameter[] parameters = new Parameter[] {
                new ConstantParameter(reader),
                new ConstantParameter(classLoader) };
        nano.registerComponentImplementation(containerBuilderClassName,
                containerBuilderClassName, parameters);
        ContainerBuilder containerBuilder = (ContainerBuilder) nano.getPico()
                .getComponentInstance(containerBuilderClassName);
        //containerBuilder.buildContainer(new SimpleReference(), null, null,
        //        false);
        return (ContainerPopulator) containerBuilder;
    }
View Full Code Here

TOP

Related Classes of org.nanocontainer.NanoContainer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.