Search in sources :

Example 41 with Filter

use of org.osgi.framework.Filter in project opennms by OpenNMS.

the class DynamicallyTrackedProcessor method process.

@Override
public void process(final Exchange exchange) throws Exception {
    if (m_tracker == null) {
        String filterString = String.format("(&(%s=%s)(name=%s))", Constants.OBJECTCLASS, Processor.class.getName(), m_processorName);
        Filter filter = m_context.createFilter(filterString);
        LOG.info("Starting tracker with filter: {}", filterString);
        m_tracker = new ServiceTracker<Object, Processor>(m_context, filter, null);
        m_tracker.open();
    }
    // Grab the first service that meets our criteria
    Processor processor = m_tracker.getService();
    // Fail if no process is defined
    if (processor == null) {
        throw new Exception("No suitable processer service was found with name: " + m_processorName);
    }
    processor.process(exchange);
}
Also used : Processor(org.apache.camel.Processor) Filter(org.osgi.framework.Filter)

Example 42 with Filter

use of org.osgi.framework.Filter in project opennms by OpenNMS.

the class DynamicallyTrackedProcessor method process.

@Override
public void process(final Exchange exchange) throws Exception {
    if (m_tracker == null) {
        String filterString = String.format("(&(%s=%s)(name=%s)(%s=true))", Constants.OBJECTCLASS, Processor.class.getName(), m_processorName, m_processorKey);
        Filter filter = m_context.createFilter(filterString);
        LOG.info("Starting tracker with filter: {}", filterString);
        m_tracker = new ServiceTracker<Object, Processor>(m_context, filter, null);
        m_tracker.open();
    }
    try {
        // Grab the first service that meets our criteria
        Processor processor = m_tracker.getService();
        // Fail if no process is defined
        if (processor == null) {
            throw new RuntimeException("No suitable processer was found.");
        }
        LOG.debug("Processing exchange with: {}", processor.getClass());
        processor.process(exchange);
    } catch (Throwable e) {
        LOG.warn("Message dispatch failed: " + e.getMessage(), e);
        exchange.setException(e);
    }
}
Also used : Processor(org.apache.camel.Processor) Filter(org.osgi.framework.Filter)

Example 43 with Filter

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

the class BundleFrameworkConfigurationFactoryImpl method createBundleFrameworkConfig.

public BundleFrameworkConfiguration createBundleFrameworkConfig(String frameworkId, BundleContext parentCtx, AriesApplication app) {
    BundleFrameworkConfiguration config = null;
    DeploymentMetadata metadata = app.getDeploymentMetadata();
    /**
     * Set up framework config properties
     */
    Properties frameworkConfig = new Properties();
    // Problems occur if the parent framework has osgi.console set because the child framework
    // will also attempt to listen on the same port which will cause port clashs. Setting this
    // to null essentially turns the console off.
    frameworkConfig.put("osgi.console", "none");
    String flowedSystemPackages = EquinoxFrameworkUtils.calculateSystemPackagesToFlow(EquinoxFrameworkUtils.getSystemExtraPkgs(parentCtx), metadata.getImportPackage());
    frameworkConfig.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, flowedSystemPackages);
    /**
     * Set up BundleManifest for the framework bundle
     */
    Properties frameworkBundleManifest = new Properties();
    frameworkBundleManifest.put(Constants.BUNDLE_SYMBOLICNAME, metadata.getApplicationSymbolicName());
    frameworkBundleManifest.put(Constants.BUNDLE_VERSION, metadata.getApplicationVersion().toString());
    /**
     * Set up Import-Package header for framework manifest
     */
    // Extract the import packages and remove anything we already have available in the current framework
    Collection<Content> imports = EquinoxFrameworkUtils.calculateImports(metadata.getImportPackage(), EquinoxFrameworkUtils.getExportPackages(parentCtx));
    if (imports != null && !imports.isEmpty()) {
        StringBuffer buffer = new StringBuffer();
        for (Content i : imports) buffer.append(EquinoxFrameworkUtils.contentToString(i) + ",");
        frameworkBundleManifest.put(Constants.IMPORT_PACKAGE, buffer.substring(0, buffer.length() - 1));
    }
    /**
     * Set up CompositeServiceFilter-Import header for framework manifest
     */
    StringBuilder serviceImportFilter = new StringBuilder();
    String txRegsitryImport = "(" + Constants.OBJECTCLASS + "=" + EquinoxFrameworkConstants.TRANSACTION_REGISTRY_BUNDLE + ")";
    Collection<Filter> deployedServiceImports = metadata.getDeployedServiceImport();
    //if there are more services than the txRegistry import a OR group is required for the Filter
    if (deployedServiceImports.size() > 0) {
        serviceImportFilter.append("(|");
    }
    for (Filter importFilter : metadata.getDeployedServiceImport()) {
        serviceImportFilter.append(importFilter.toString());
    }
    serviceImportFilter.append(txRegsitryImport);
    //close the OR group if needed
    if (deployedServiceImports.size() > 0) {
        serviceImportFilter.append(")");
    }
    frameworkBundleManifest.put(EquinoxFrameworkConstants.COMPOSITE_SERVICE_FILTER_IMPORT, serviceImportFilter.toString());
    config = new BundleFrameworkConfigurationImpl(frameworkId, frameworkConfig, frameworkBundleManifest);
    return config;
}
Also used : DeploymentMetadata(org.apache.aries.application.DeploymentMetadata) Filter(org.osgi.framework.Filter) Content(org.apache.aries.application.Content) Properties(java.util.Properties) BundleFrameworkConfiguration(org.apache.aries.application.management.spi.framework.BundleFrameworkConfiguration)

Example 44 with Filter

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

the class Helper method getOsgiService.

public static <T> T getOsgiService(BundleContext bundleContext, Class<T> type, String filter, long timeout) {
    ServiceTracker tracker = null;
    try {
        String flt;
        if (filter != null) {
            if (filter.startsWith("(")) {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
            } else {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
            }
        } else {
            flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bundleContext, osgiFilter, null);
        tracker.open(true);
        // Note that the tracker is not closed to keep the reference
        // This is buggy, as the service reference may change i think
        Object svc = tracker.waitForService(timeout);
        if (svc == null) {
            Dictionary<?, ?> dic = bundleContext.getBundle().getHeaders();
            System.err.println("Test bundle headers: " + explode(dic));
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
                System.err.println("ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return type.cast(svc);
    } 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) ServiceReference(org.osgi.framework.ServiceReference)

Example 45 with Filter

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

the class Helper method getOsgiServiceReference.

public static <T> ServiceReference getOsgiServiceReference(BundleContext bundleContext, Class<T> type, String filter, long timeout) {
    ServiceTracker tracker = null;
    try {
        String flt;
        if (filter != null) {
            if (filter.startsWith("(")) {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
            } else {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
            }
        } else {
            flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bundleContext, osgiFilter, null);
        tracker.open(true);
        // Note that the tracker is not closed to keep the reference
        // This is buggy, as the service reference may change i think
        Object svc = tracker.waitForService(timeout);
        if (svc == null) {
            Dictionary<?, ?> dic = bundleContext.getBundle().getHeaders();
            System.err.println("Test bundle headers: " + explode(dic));
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
                System.err.println("ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return tracker.getServiceReference();
    } 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) ServiceReference(org.osgi.framework.ServiceReference)

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