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);
}
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;
}
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();
}
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;
}
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);
}
Aggregations