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