use of org.osgi.util.tracker.ServiceTracker in project translationstudio8 by heartsome.
the class Activator method start.
/**
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
* @param context
* @throws Exception
*/
public void start(BundleContext context) throws Exception {
plugin = this;
bundleContext = context;
// tracker the xml converter service
String positiveFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()), new EqFilter(Converter.ATTR_TYPE, Xml2Xliff.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION, Converter.DIRECTION_POSITIVE)).toString();
positiveTracker = new ServiceTracker(context, context.createFilter(positiveFilter), new XmlPositiveCustomizer());
positiveTracker.open();
String reverseFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()), new EqFilter(Converter.ATTR_TYPE, Xliff2Xml.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION, Converter.DIRECTION_REVERSE)).toString();
reverseTracker = new ServiceTracker(context, context.createFilter(reverseFilter), new XmlReverseCustomize());
reverseTracker.open();
}
use of org.osgi.util.tracker.ServiceTracker in project translationstudio8 by heartsome.
the class Activator method start.
/**
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
* @param context
* @throws Exception
*/
public void start(BundleContext context) throws Exception {
plugin = this;
bundleContext = context;
// tracker the rtf converter service
String positiveFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()), new EqFilter(Converter.ATTR_TYPE, Rtf2Xliff.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION, Converter.DIRECTION_POSITIVE)).toString();
positiveTracker = new ServiceTracker(context, context.createFilter(positiveFilter), new RtfPositiveCustomizer());
positiveTracker.open();
String reverseFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()), new EqFilter(Converter.ATTR_TYPE, Xliff2Rtf.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION, Converter.DIRECTION_REVERSE)).toString();
reverseTracker = new ServiceTracker(context, context.createFilter(reverseFilter), new RtfReverseCustomize());
reverseTracker.open();
}
use of org.osgi.util.tracker.ServiceTracker in project opennms by OpenNMS.
the class DispatcherWhiteboard method dispatch.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Consume(property = "endpointUri")
public void dispatch(final Object message) throws NoSuchMethodException, SecurityException {
LOG.debug("dispatch: {}", message);
if (m_tracker == null) {
m_tracker = new ServiceTracker(m_context, m_serviceClass, null);
m_tracker.open();
}
if (m_method == null) {
m_method = m_serviceClass.getMethod(m_methodName, m_messageClass);
}
try {
Object[] services = m_tracker.getServices();
if (services != null && services.length > 0) {
for (Object service : m_tracker.getServices()) {
m_method.invoke(service, message);
}
} else {
// in case there is no dispatcher registered, let the user know.
LOG.warn("No dispatcher for message found. ServiceClass: {}, ServiceMethod: {}", m_serviceClass, m_methodName);
}
} catch (Throwable e) {
// If anything goes wrong, log an error message
// TODO: Use a dead-letter channel?
LOG.warn("Message dispatch failed: " + e.getMessage(), e);
}
}
use of org.osgi.util.tracker.ServiceTracker in project karaf by apache.
the class KarafTestSupport method getOsgiService.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected <T> T getOsgiService(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 = type.cast(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);
}
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
System.err.println("Filtered ServiceReference: " + ref);
}
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.util.tracker.ServiceTracker in project karaf by apache.
the class SecuredCommandProcessorImpl method trackConverters.
private ServiceTracker<Converter, Converter> trackConverters(BundleContext context) {
return new ServiceTracker<Converter, Converter>(context, Converter.class.getName(), null) {
@Override
public Converter addingService(ServiceReference<Converter> reference) {
Converter converter = super.addingService(reference);
addConverter(converter);
return converter;
}
@Override
public void removedService(ServiceReference<Converter> reference, Converter service) {
removeConverter(service);
super.removedService(reference, service);
}
};
}
Aggregations