Search in sources :

Example 16 with BlueprintContainer

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());
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) List(java.util.List)

Example 17 with BlueprintContainer

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;
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata)

Example 18 with BlueprintContainer

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]);
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 19 with BlueprintContainer

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));
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 20 with BlueprintContainer

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());
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) IDocumentController(org.pentaho.metaverse.api.IDocumentController) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

BlueprintContainer (org.osgi.service.blueprint.container.BlueprintContainer)29 Test (org.junit.Test)14 Bundle (org.osgi.framework.Bundle)11 ServiceReference (org.osgi.framework.ServiceReference)6 Helper.mvnBundle (org.apache.aries.blueprint.itests.Helper.mvnBundle)5 List (java.util.List)4 BundleContext (org.osgi.framework.BundleContext)4 DestroyTest (org.apache.aries.blueprint.sample.DestroyTest)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)3 URL (java.net.URL)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 Hashtable (java.util.Hashtable)2 Map (java.util.Map)2 BindingListener (org.apache.aries.blueprint.sample.BindingListener)2 Foo (org.apache.aries.blueprint.sample.Foo)2 InterfaceA (org.apache.aries.blueprint.sample.InterfaceA)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)2