use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.
the class AccountsServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BlueprintContainer container = (BlueprintContainer) getServletContext().getAttribute(BlueprintContextListener.CONTAINER_ATTRIBUTE);
StringBuilder sb = new StringBuilder();
sb.append("<html><body>");
List<Account> accounts = (List<Account>) container.getComponentInstance("accounts");
sb.append("<h2>Accounts</h2>");
sb.append("<ul>");
for (Account account : accounts) {
sb.append("<li>").append(account.getAccountNumber()).append("</li>");
}
sb.append("</ul>");
sb.append("<br/>");
Foo foo = (Foo) container.getComponentInstance("foo");
sb.append("<h2>Foo</h2>");
sb.append("<ul>");
sb.append("<li>").append("a = ").append(foo.getA()).append("</li>");
sb.append("<li>").append("b = ").append(foo.getB()).append("</li>");
sb.append("<li>").append("currency = ").append(foo.getCurrency()).append("</li>");
sb.append("<li>").append("date = ").append(foo.getDate()).append("</li>");
sb.append("</ul>");
sb.append("</body></html>");
resp.getWriter().write(sb.toString());
}
use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.
the class BlueprintMetadata method getComponentIdsByType.
/*
*
* type could be Bean, Service, serviceReference (non-Javadoc)
*
* @see org.apache.aries.jmx.blueprint.BlueprintMetadataMBean#getComponentIdsByType(long, java.lang.String)
*/
public String[] getComponentIdsByType(long containerServiceId, String type) throws IOException {
BlueprintContainer container = getBlueprintContainer(containerServiceId);
Collection<? extends ComponentMetadata> components;
if (type.equals(BlueprintMetadataMBean.SERVICE_METADATA)) {
components = container.getMetadata(ServiceMetadata.class);
} else if (type.equals(BlueprintMetadataMBean.BEAN_METADATA)) {
components = container.getMetadata(BeanMetadata.class);
} else if (type.equals(BlueprintMetadataMBean.SERVICE_REFERENCE_METADATA)) {
components = container.getMetadata(ServiceReferenceMetadata.class);
} else {
throw new IllegalArgumentException("Unrecognized component type: " + type);
}
String[] ids = new String[components.size()];
int i = 0;
for (ComponentMetadata component : components) {
// Here we ignore it.
if (null == component)
continue;
ids[i++] = component.getId();
}
return ids;
}
use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.
the class BlueprintMetadata method getBlueprintContainer.
private BlueprintContainer getBlueprintContainer(long containerServiceId) throws IOException {
String filter = "(" + Constants.SERVICE_ID + "=" + containerServiceId + ")";
ServiceReference[] serviceReferences = null;
try {
serviceReferences = bundleContext.getServiceReferences(BlueprintContainer.class.getName(), filter);
} catch (InvalidSyntaxException e) {
throw new IOException(e);
}
if (serviceReferences == null || serviceReferences.length < 1) {
throw new IOException("Invalid BlueprintContainer service id: " + containerServiceId);
}
return (BlueprintContainer) bundleContext.getService(serviceReferences[0]);
}
use of org.osgi.service.blueprint.container.BlueprintContainer in project pentaho-metaverse by pentaho.
the class MetaverseBeanUtilTest method testGet.
@Test
public void testGet() throws Exception {
MetaverseBeanUtil instance = MetaverseBeanUtil.getInstance();
BundleContext bundleContext = mock(BundleContext.class);
Bundle bundle = mock(Bundle.class);
when(bundleContext.getBundle()).thenReturn(bundle);
instance.setBundleContext(bundleContext);
assertNull(instance.get(BEAN_NAME));
ServiceReference serviceReference = mock(ServiceReference.class);
when(bundleContext.getServiceReferences(Mockito.any(Class.class), Mockito.anyString())).thenReturn(Collections.singletonList(serviceReference));
assertNull(instance.get(BEAN_NAME));
BlueprintContainer service = mock(BlueprintContainer.class);
when(bundleContext.getService(Mockito.any(ServiceReference.class))).thenReturn(service);
Object testObject = new Object();
when(service.getComponentInstance(BEAN_NAME)).thenReturn(testObject);
assertEquals(testObject, instance.get(BEAN_NAME));
when(bundleContext.getServiceReferences(Mockito.any(Class.class), Mockito.anyString())).thenThrow(InvalidSyntaxException.class);
assertNull(MetaverseBeanUtil.getInstance().get(BEAN_NAME));
}
use of org.osgi.service.blueprint.container.BlueprintContainer in project pentaho-metaverse by pentaho.
the class MetaverseUtilTest method testGetDocumentController.
@Test
public void testGetDocumentController() throws Exception {
IDocumentController documentController = MetaverseUtil.getDocumentController();
assertNotNull(documentController);
MetaverseUtil.documentController = null;
assertNull(MetaverseUtil.getDocumentController());
// Generate an exception
MetaverseBeanUtil instance = MetaverseBeanUtil.getInstance();
BundleContext bundleContext = mock(BundleContext.class);
Bundle bundle = mock(Bundle.class);
when(bundleContext.getBundle()).thenReturn(bundle);
instance.setBundleContext(bundleContext);
ServiceReference serviceReference = mock(ServiceReference.class);
BlueprintContainer service = mock(BlueprintContainer.class);
when(bundleContext.getService(Mockito.any(ServiceReference.class))).thenReturn(service);
when(service.getComponentInstance("IDocumentController")).thenReturn(documentController);
assertNull(MetaverseUtil.getDocumentController());
}
Aggregations