Search in sources :

Example 1 with BlueprintContainerImpl

use of org.apache.aries.blueprint.container.BlueprintContainerImpl in project aries by apache.

the class BlueprintContextListener method contextDestroyed.

public void contextDestroyed(ServletContextEvent event) {
    ServletContext servletContext = event.getServletContext();
    Object container = servletContext.getAttribute(CONTAINER_ATTRIBUTE);
    if (container instanceof BlueprintContainerImpl) {
        BlueprintContainerImpl blueprint = (BlueprintContainerImpl) container;
        blueprint.destroy();
    }
}
Also used : BlueprintContainerImpl(org.apache.aries.blueprint.container.BlueprintContainerImpl) ServletContext(javax.servlet.ServletContext)

Example 2 with BlueprintContainerImpl

use of org.apache.aries.blueprint.container.BlueprintContainerImpl in project aries by apache.

the class BlueprintContextListener method contextInitialized.

public void contextInitialized(ServletContextEvent event) {
    ServletContext servletContext = event.getServletContext();
    String location = servletContext.getInitParameter(CONTEXT_LOCATION);
    if (location == null) {
        location = DEFAULT_CONTEXT_LOCATION;
    }
    List<URL> resourcePaths = new ArrayList<URL>();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {
        Enumeration<URL> resources = classLoader.getResources(location);
        while (resources.hasMoreElements()) {
            resourcePaths.add(resources.nextElement());
        }
        servletContext.log("Loading Blueprint contexts " + resourcePaths);
        Map<String, String> properties = new HashMap<String, String>();
        String propLocations = servletContext.getInitParameter(PROPERTIES);
        if (propLocations != null) {
            for (String propLoc : propLocations.split(",")) {
                Enumeration<URL> propUrl = classLoader.getResources(propLoc);
                while (propUrl.hasMoreElements()) {
                    URL url = propUrl.nextElement();
                    InputStream is = url.openStream();
                    try {
                        Properties props = new Properties();
                        props.load(is);
                        Enumeration names = props.propertyNames();
                        while (names.hasMoreElements()) {
                            String key = names.nextElement().toString();
                            properties.put(key, props.getProperty(key));
                        }
                    } finally {
                        is.close();
                    }
                }
            }
        }
        NamespaceHandlerSet nsHandlerSet = getNamespaceHandlerSet(servletContext, classLoader);
        BlueprintContainerImpl container = new BlueprintContainerImpl(classLoader, resourcePaths, properties, nsHandlerSet, true);
        servletContext.setAttribute(CONTAINER_ATTRIBUTE, container);
    } catch (Exception e) {
        servletContext.log("Failed to startup blueprint container. " + e, e);
    }
}
Also used : NamespaceHandlerSet(org.apache.aries.blueprint.parser.NamespaceHandlerSet) SimpleNamespaceHandlerSet(org.apache.aries.blueprint.container.SimpleNamespaceHandlerSet) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Properties(java.util.Properties) URL(java.net.URL) IOException(java.io.IOException) BlueprintContainerImpl(org.apache.aries.blueprint.container.BlueprintContainerImpl) ServletContext(javax.servlet.ServletContext)

Example 3 with BlueprintContainerImpl

use of org.apache.aries.blueprint.container.BlueprintContainerImpl in project aries by apache.

the class BlueprintContainerTest method testPlaceholders.

@Test
public void testPlaceholders() throws Exception {
    URL url1 = getClass().getClassLoader().getResource("test.xml");
    URL url2 = getClass().getClassLoader().getResource("test2.xml");
    BlueprintContainerImpl container = new BlueprintContainerImpl(getClass().getClassLoader(), Arrays.asList(url1, url2));
    Foo foo = (Foo) container.getComponentInstance("foo");
    System.out.println(foo);
    assertNotNull(foo);
    assertEquals(5, foo.getA());
    assertEquals(1, foo.getB());
    container.destroy();
}
Also used : BlueprintContainerImpl(org.apache.aries.blueprint.container.BlueprintContainerImpl) Foo(org.apache.aries.blueprint.sample.Foo) URL(java.net.URL) Test(org.junit.Test)

Example 4 with BlueprintContainerImpl

use of org.apache.aries.blueprint.container.BlueprintContainerImpl in project aries by apache.

the class BlueprintContainerTest method testSimple.

@Test
public void testSimple() throws Exception {
    URL url = getClass().getClassLoader().getResource("test.xml");
    BlueprintContainerImpl container = new BlueprintContainerImpl(getClass().getClassLoader(), Arrays.asList(url));
    Foo foo = (Foo) container.getComponentInstance("foo");
    System.out.println(foo);
    assertNotNull(foo);
    assertEquals(5, foo.getA());
    assertEquals(1, foo.getB());
    container.destroy();
}
Also used : BlueprintContainerImpl(org.apache.aries.blueprint.container.BlueprintContainerImpl) Foo(org.apache.aries.blueprint.sample.Foo) URL(java.net.URL) Test(org.junit.Test)

Example 5 with BlueprintContainerImpl

use of org.apache.aries.blueprint.container.BlueprintContainerImpl in project aries by apache.

the class BlueprintContainerTest method main.

public static void main(String[] args) throws Exception {
    URL url = BlueprintContainerTest.class.getClassLoader().getResource("test.xml");
    BlueprintContainerImpl container = new BlueprintContainerImpl(BlueprintContainerTest.class.getClassLoader(), Arrays.asList(url));
    System.out.println(container.getComponentInstance("foo"));
    container.destroy();
}
Also used : BlueprintContainerImpl(org.apache.aries.blueprint.container.BlueprintContainerImpl) URL(java.net.URL)

Aggregations

BlueprintContainerImpl (org.apache.aries.blueprint.container.BlueprintContainerImpl)5 URL (java.net.URL)4 ServletContext (javax.servlet.ServletContext)2 Foo (org.apache.aries.blueprint.sample.Foo)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 SimpleNamespaceHandlerSet (org.apache.aries.blueprint.container.SimpleNamespaceHandlerSet)1 NamespaceHandlerSet (org.apache.aries.blueprint.parser.NamespaceHandlerSet)1