Search in sources :

Example 1 with NamespaceHandlerSet

use of org.apache.aries.blueprint.parser.NamespaceHandlerSet in project aries by apache.

the class AbstractBlueprintTest method parse.

protected ComponentDefinitionRegistryImpl parse(String name) throws Exception {
    final URI extensionHandler = new URI("http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0");
    NamespaceHandlerSet handlers = new NamespaceHandlerSet() {

        public Set<URI> getNamespaces() {
            return null;
        }

        public NamespaceHandler getNamespaceHandler(URI namespace) {
            if (namespace.equals(extensionHandler)) {
                return new ExtNamespaceHandler();
            } else {
                return null;
            }
        }

        public void removeListener(NamespaceHandlerSet.Listener listener) {
        }

        public Schema getSchema() throws SAXException, IOException {
            return null;
        }

        public Schema getSchema(Map<String, String> locations) throws SAXException, IOException {
            return null;
        }

        public boolean isComplete() {
            return false;
        }

        public void addListener(NamespaceHandlerSet.Listener listener) {
        }

        public void destroy() {
        }
    };
    return parse(name, handlers);
}
Also used : NamespaceHandlerSet(org.apache.aries.blueprint.parser.NamespaceHandlerSet) URI(java.net.URI) ExtNamespaceHandler(org.apache.aries.blueprint.ext.impl.ExtNamespaceHandler) Map(java.util.Map)

Example 2 with NamespaceHandlerSet

use of org.apache.aries.blueprint.parser.NamespaceHandlerSet in project aries by apache.

the class BaseNameSpaceHandlerSetup method parseCDR.

protected ComponentDefinitionRegistry parseCDR(String name) throws Exception {
    Parser p = new Parser();
    URL bpxml = this.getClass().getResource(name);
    p.parse(Arrays.asList(bpxml));
    Set<URI> nsuris = p.getNamespaces();
    NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
    ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
    cdr.registerComponentDefinition(new PassThroughMetadataImpl("blueprintBundle", b));
    p.populate(nshandlers, cdr);
    return cdr;
}
Also used : NamespaceHandlerSet(org.apache.aries.blueprint.parser.NamespaceHandlerSet) ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) PassThroughMetadataImpl(org.apache.aries.blueprint.reflect.PassThroughMetadataImpl) URI(java.net.URI) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl) URL(java.net.URL) Parser(org.apache.aries.blueprint.parser.Parser)

Example 3 with NamespaceHandlerSet

use of org.apache.aries.blueprint.parser.NamespaceHandlerSet in project aries by apache.

the class BlueprintContainerImpl method init.

public void init(boolean validate) throws Exception {
    // Parse xml resources
    Parser parser = new Parser();
    parser.parse(getResources());
    // Check namespaces
    Set<URI> namespaces = parser.getNamespaces();
    // Create handler set
    NamespaceHandlerSet handlerSet = createNamespaceHandlerSet(namespaces);
    // Add predefined beans
    componentDefinitionRegistry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintContainer", this));
    if (validate) {
        // Validate
        parser.validate(handlerSet.getSchema());
    }
    // Populate
    parser.populate(handlerSet, componentDefinitionRegistry);
    // Create repository
    repository = new NoOsgiRecipeBuilder(this, tempRecipeIdSpace).createRepository();
    // Processors handling
    processTypeConverters();
    processProcessors();
    // Instantiate eager singletons
    instantiateEagerComponents();
}
Also used : NamespaceHandlerSet(org.apache.aries.blueprint.parser.NamespaceHandlerSet) PassThroughMetadataImpl(org.apache.aries.blueprint.reflect.PassThroughMetadataImpl) URI(java.net.URI) Parser(org.apache.aries.blueprint.parser.Parser)

Example 4 with NamespaceHandlerSet

use of org.apache.aries.blueprint.parser.NamespaceHandlerSet in project aries by apache.

the class ParserServiceImpl method validateAndPopulate.

private ComponentDefinitionRegistry validateAndPopulate(Parser parser, Bundle clientBundle, boolean validate) throws IOException, SAXException {
    Set<URI> nsuris = parser.getNamespaces();
    ComponentDefinitionRegistry cdr;
    NamespaceHandlerSet nshandlers = _namespaceHandlerRegistry.getNamespaceHandlers(nsuris, clientBundle);
    try {
        if (validate) {
            parser.validate(nshandlers.getSchema());
        }
        cdr = new ComponentDefinitionRegistryImpl();
        parser.populate(nshandlers, cdr);
    } finally {
        nshandlers.destroy();
    }
    return cdr;
}
Also used : NamespaceHandlerSet(org.apache.aries.blueprint.parser.NamespaceHandlerSet) ComponentDefinitionRegistry(org.apache.aries.blueprint.ComponentDefinitionRegistry) URI(java.net.URI) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl)

Example 5 with NamespaceHandlerSet

use of org.apache.aries.blueprint.parser.NamespaceHandlerSet in project aries by apache.

the class LifecyclePolicyTest method testStatic.

@Test
public void testStatic() throws Exception {
    final ReferenceMetadataImpl ref = new ReferenceMetadataImpl();
    ref.setId("ref");
    ref.setRuntimeInterface(TestItf.class);
    ref.setLifecycle(ExtendedReferenceMetadata.LIFECYCLE_STATIC);
    final BeanMetadataImpl bean1 = new BeanMetadataImpl();
    bean1.setId("bean1");
    bean1.setRuntimeClass(Bean1.class);
    bean1.setInitMethod("init");
    bean1.setDestroyMethod("destroy");
    bean1.addProperty("itf", new RefMetadataImpl("ref"));
    final BeanMetadataImpl bean2 = new BeanMetadataImpl();
    bean2.setId("bean2");
    bean2.setRuntimeClass(Bean2.class);
    bean2.setInitMethod("init");
    bean2.setDestroyMethod("destroy");
    bean2.addProperty("bean1", new RefMetadataImpl("bean1"));
    Bundle bundle = EasyMock.createMock(Bundle.class);
    BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
    Bundle extenderBundle = EasyMock.createMock(Bundle.class);
    BundleContext extenderBundleContext = EasyMock.createMock(BundleContext.class);
    BlueprintListener eventDispatcher = EasyMock.createMock(BlueprintListener.class);
    NamespaceHandlerRegistry namespaceHandlerRegistry = EasyMock.createMock(NamespaceHandlerRegistry.class);
    ProxyManager proxyManager = EasyMock.createMock(ProxyManager.class);
    NamespaceHandlerSet namespaceHandlerSet = EasyMock.createMock(NamespaceHandlerSet.class);
    TestItf itf = EasyMock.createMock(TestItf.class);
    ServiceRegistration registration = EasyMock.createMock(ServiceRegistration.class);
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
    List<URL> pathList = new ArrayList<URL>();
    Set<URI> namespaces = new HashSet<URI>();
    BlueprintContainerImpl container = new BlueprintContainerImpl(bundle, bundleContext, extenderBundle, eventDispatcher, namespaceHandlerRegistry, executorService, timer, pathList, proxyManager, namespaces) {

        private boolean repoCreated = false;

        @Override
        public BlueprintRepository getRepository() {
            if (!repoCreated) {
                getComponentDefinitionRegistry().registerComponentDefinition(ref);
                getComponentDefinitionRegistry().registerComponentDefinition(bean1);
                getComponentDefinitionRegistry().registerComponentDefinition(bean2);
                repoCreated = true;
            }
            return super.getRepository();
        }
    };
    ServiceReference svcRef1 = EasyMock.createMock(ServiceReference.class);
    EasyMock.expect(bundle.getSymbolicName()).andReturn("bundleSymbolicName").anyTimes();
    EasyMock.expect(bundle.getVersion()).andReturn(Version.emptyVersion).anyTimes();
    EasyMock.expect(bundle.getState()).andReturn(Bundle.ACTIVE).anyTimes();
    EasyMock.expect(bundle.getBundleContext()).andReturn(bundleContext).anyTimes();
    Hashtable<String, String> headers = new Hashtable<String, String>();
    headers.put(Constants.BUNDLE_SYMBOLICNAME, "bundleSymbolicName;blueprint.aries.xml-validation:=false");
    EasyMock.expect(bundle.getHeaders()).andReturn(headers).anyTimes();
    eventDispatcher.blueprintEvent(EasyMock.<BlueprintEvent>anyObject());
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(namespaceHandlerRegistry.getNamespaceHandlers(namespaces, bundle)).andReturn(namespaceHandlerSet).anyTimes();
    EasyMock.expect(namespaceHandlerSet.getNamespaces()).andReturn(namespaces).anyTimes();
    namespaceHandlerSet.addListener(container);
    EasyMock.expectLastCall();
    EasyMock.expect(bundleContext.getProperty(BlueprintConstants.XML_VALIDATION_PROPERTY)).andReturn(null);
    Properties props = new Properties();
    props.put("osgi.blueprint.container.version", Version.emptyVersion);
    props.put("osgi.blueprint.container.symbolicname", "bundleSymbolicName");
    EasyMock.expect(bundleContext.registerService(EasyMock.aryEq(new String[] { BlueprintContainer.class.getName() }), EasyMock.same(container), EasyMock.eq((Dictionary) props))).andReturn(registration);
    bundleContext.addServiceListener(EasyMock.<org.osgi.framework.ServiceListener>anyObject(), EasyMock.<String>anyObject());
    EasyMock.expectLastCall();
    EasyMock.expect(bundleContext.getServiceReferences((String) null, "(objectClass=" + TestItf.class.getName() + ")")).andReturn(new ServiceReference[] { svcRef1 });
    EasyMock.expect(bundleContext.getService(svcRef1)).andReturn(itf);
    EasyMock.expect(bundle.loadClass("java.lang.Object")).andReturn((Class) Object.class).anyTimes();
    EasyMock.replay(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
    container.run();
    ReferenceRecipe recipe = (ReferenceRecipe) container.getRepository().getRecipe("ref");
    recipe.start(container);
    Bean2 bean2i = (Bean2) container.getRepository().create("bean2");
    Assert.assertNotNull(bean2i);
    Assert.assertEquals(1, Bean2.initialized);
    Assert.assertEquals(0, Bean2.destroyed);
    EasyMock.verify(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
    // 
    // Unregister the service
    // 
    // Given the lifecycle is 'static', this should cause the Bean1 and Bean2
    // to be destroyed
    // 
    EasyMock.reset(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
    EasyMock.expect(bundle.getSymbolicName()).andReturn("bundleSymbolicName").anyTimes();
    EasyMock.expect(bundle.getVersion()).andReturn(Version.emptyVersion).anyTimes();
    EasyMock.expect(bundleContext.ungetService(svcRef1)).andReturn(false);
    EasyMock.replay(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
    recipe.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, svcRef1));
    Assert.assertEquals(1, Bean2.initialized);
    Assert.assertEquals(1, Bean2.destroyed);
    EasyMock.verify(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
    // 
    // Re-register the service
    // 
    // Given the lifecycle is 'static', this should cause the Bean1 and Bean2
    // to be recreated
    // 
    EasyMock.reset(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
    EasyMock.expect(bundle.getSymbolicName()).andReturn("bundleSymbolicName").anyTimes();
    EasyMock.expect(bundle.getVersion()).andReturn(Version.emptyVersion).anyTimes();
    EasyMock.expect(bundleContext.getService(svcRef1)).andReturn(itf);
    EasyMock.expect(bundle.loadClass("java.lang.Object")).andReturn((Class) Object.class).anyTimes();
    EasyMock.replay(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
    recipe.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, svcRef1));
    Assert.assertEquals(2, Bean2.initialized);
    Assert.assertEquals(1, Bean2.destroyed);
    EasyMock.verify(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
}
Also used : NamespaceHandlerSet(org.apache.aries.blueprint.parser.NamespaceHandlerSet) Dictionary(java.util.Dictionary) BlueprintListener(org.osgi.service.blueprint.container.BlueprintListener) ProxyManager(org.apache.aries.proxy.ProxyManager) ArrayList(java.util.ArrayList) Properties(java.util.Properties) URI(java.net.URI) URL(java.net.URL) ReferenceMetadataImpl(org.apache.aries.blueprint.reflect.ReferenceMetadataImpl) ServiceEvent(org.osgi.framework.ServiceEvent) ServiceRegistration(org.osgi.framework.ServiceRegistration) HashSet(java.util.HashSet) BeanMetadataImpl(org.apache.aries.blueprint.reflect.BeanMetadataImpl) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) RefMetadataImpl(org.apache.aries.blueprint.reflect.RefMetadataImpl) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

NamespaceHandlerSet (org.apache.aries.blueprint.parser.NamespaceHandlerSet)8 URI (java.net.URI)6 URL (java.net.URL)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2 ComponentDefinitionRegistry (org.apache.aries.blueprint.ComponentDefinitionRegistry)2 BlueprintContainerImpl (org.apache.aries.blueprint.container.BlueprintContainerImpl)2 SimpleNamespaceHandlerSet (org.apache.aries.blueprint.container.SimpleNamespaceHandlerSet)2 ComponentDefinitionRegistryImpl (org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl)2 Parser (org.apache.aries.blueprint.parser.Parser)2 PassThroughMetadataImpl (org.apache.aries.blueprint.reflect.PassThroughMetadataImpl)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Dictionary (java.util.Dictionary)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1