Search in sources :

Example 16 with Filter

use of org.osgi.framework.Filter in project aries by apache.

the class AbstractTest method addServiceExportPolicy.

protected void addServiceExportPolicy(Class<?> clazz, ScopeUpdate scopeUpdate) throws InvalidSyntaxException {
    Filter filter = bundleContext.createFilter('(' + Constants.OBJECTCLASS + '=' + clazz.getName() + ')');
    SharePolicy policy = new SharePolicy(SharePolicy.TYPE_EXPORT, "scope.share.service", filter);
    Map<String, List<SharePolicy>> policyMap = scopeUpdate.getSharePolicies(SharePolicy.TYPE_EXPORT);
    List<SharePolicy> policies = policyMap.get("scope.share.service");
    if (policies == null) {
        policies = new ArrayList<SharePolicy>();
        policyMap.put("scope.share.service", policies);
    }
    policies.add(policy);
}
Also used : Filter(org.osgi.framework.Filter) SharePolicy(org.apache.aries.subsystem.scope.SharePolicy) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with Filter

use of org.osgi.framework.Filter in project aries by apache.

the class AbstractTest method addPackageImportPolicy.

protected void addPackageImportPolicy(String packageName, ScopeUpdate scopeUpdate) throws InvalidSyntaxException {
    Filter filter = bundleContext.createFilter("(osgi.wiring.package=" + packageName + ')');
    SharePolicy policy = new SharePolicy(SharePolicy.TYPE_IMPORT, "osgi.wiring.package", filter);
    Map<String, List<SharePolicy>> policyMap = scopeUpdate.getSharePolicies(SharePolicy.TYPE_IMPORT);
    List<SharePolicy> policies = policyMap.get("osgi.wiring.package");
    if (policies == null) {
        policies = new ArrayList<SharePolicy>();
        policyMap.put("osgi.wiring.package", policies);
    }
    policies.add(policy);
}
Also used : Filter(org.osgi.framework.Filter) SharePolicy(org.apache.aries.subsystem.scope.SharePolicy) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with Filter

use of org.osgi.framework.Filter in project aries by apache.

the class RequirementImpl method isSatisfied.

@SuppressWarnings("unchecked")
public boolean isSatisfied(Capability cap) {
    logger.debug(LOG_ENTRY, "isSatisfied", cap);
    boolean result = false;
    String name = getName();
    if (name.equals(cap.getName())) {
        String filterToCreate = getFilter();
        try {
            Filter f = FrameworkUtil.createFilter(FilterUtils.removeMandatoryFilterToken(filterToCreate));
            Hashtable<String, Object> hash = new Hashtable<String, Object>();
            Map<String, String> props = cap.getPropertiesAsMap();
            if ((props != null) && (!!!props.isEmpty())) {
                for (Map.Entry<String, String> propertyPair : props.entrySet()) {
                    hash.put(propertyPair.getKey(), propertyPair.getValue());
                }
            }
            result = f.match(hash);
        } catch (InvalidSyntaxException e) {
            logger.error(e.getMessage());
        }
    }
    logger.debug(LOG_EXIT, "isSatisfied", result);
    return result;
}
Also used : Filter(org.osgi.framework.Filter) Hashtable(java.util.Hashtable) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Map(java.util.Map)

Example 19 with Filter

use of org.osgi.framework.Filter in project aries by apache.

the class AriesRepositoryGenerator method getOsgiService.

private Object getOsgiService(BundleContext bc, String className) {
    ServiceTracker tracker = null;
    try {
        String flt = "(" + Constants.OBJECTCLASS + "=" + className + ")";
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bc, osgiFilter, null);
        tracker.open();
        // add tracker to the list of trackers we close at tear down
        srs.add(tracker);
        Object x = tracker.waitForService(DEFAULT_TIMEOUT);
        if (x == null) {
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return x;
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter", e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 20 with Filter

use of org.osgi.framework.Filter in project aries by apache.

the class ConfigurationTests method testNamedConfiguration.

@SuppressWarnings({ "unchecked", "serial" })
public void testNamedConfiguration() throws Exception {
    Bundle tb3Bundle = installBundle("tb3.jar");
    Configuration configurationA = null, configurationB = null;
    try {
        configurationA = configurationAdmin.getConfiguration("configA", "?");
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put("ports", new int[] { 12, 4567 });
        configurationA.update(properties);
        configurationB = configurationAdmin.getConfiguration("configB", "?");
        properties = new Hashtable<>();
        properties.put("color", "green");
        properties.put("ports", new int[] { 80 });
        configurationB.update(properties);
        Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
        ServiceTracker<CdiContainer, CdiContainer> st = new ServiceTracker<>(bundleContext, filter, null);
        st.open();
        CdiContainer container = st.waitForService(timeout);
        assertNotNull(container);
        int t = st.getTrackingCount();
        BeanManager beanManager = container.getBeanManager();
        Set<Bean<?>> beans = beanManager.getBeans("configB");
        assertNotNull(beans);
        Bean<? extends Object> bean = beanManager.resolve(beans);
        CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
        Map<String, Object> config = (Map<String, Object>) beanManager.getReference(bean, new TypeLiteral<Map<String, Object>>() {
        }.getType(), ctx);
        assertNotNull(config);
        assertEquals("green", config.get("color"));
        assertArrayEquals(new int[] { 80 }, (int[]) config.get("ports"));
        configurationA.delete();
        while (t == st.getTrackingCount()) {
            Thread.sleep(10);
        }
        assertTrue(st.isEmpty());
        st.close();
        filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=" + CdiEvent.Type.WAITING_FOR_CONFIGURATIONS + "))");
        st = new ServiceTracker<>(bundleContext, filter, null);
        st.open();
        assertFalse(st.isEmpty());
    } finally {
        if (configurationB != null) {
            try {
                configurationB.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        tb3Bundle.uninstall();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Bean(javax.enterprise.inject.spi.Bean) Filter(org.osgi.framework.Filter) BeanManager(javax.enterprise.inject.spi.BeanManager) Map(java.util.Map) CdiContainer(org.osgi.service.cdi.CdiContainer)

Aggregations

Filter (org.osgi.framework.Filter)81 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)31 ServiceTracker (org.osgi.util.tracker.ServiceTracker)29 ArrayList (java.util.ArrayList)22 ServiceReference (org.osgi.framework.ServiceReference)20 Hashtable (java.util.Hashtable)18 Test (org.junit.Test)14 BundleContext (org.osgi.framework.BundleContext)13 List (java.util.List)12 Bundle (org.osgi.framework.Bundle)11 Dictionary (java.util.Dictionary)9 SharePolicy (org.apache.aries.subsystem.scope.SharePolicy)9 Configuration (org.osgi.service.cm.Configuration)9 HashMap (java.util.HashMap)7 Map (java.util.Map)6 IOException (java.io.IOException)5 URL (java.net.URL)5 InstallInfo (org.apache.aries.subsystem.scope.InstallInfo)5 ScopeUpdate (org.apache.aries.subsystem.scope.ScopeUpdate)5 Iterator (java.util.Iterator)4