Java Code Examples for org.osgi.framework.BundleContext#createFilter()

The following examples show how to use org.osgi.framework.BundleContext#createFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Activator.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (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();
}
 
Example 2
Source File: Activator.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (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 open office converter service
	String positiveFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()),
			new EqFilter(Converter.ATTR_TYPE, OpenOffice2Xliff.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION,
					Converter.DIRECTION_POSITIVE)).toString();
	positiveTracker = new ServiceTracker(context, context.createFilter(positiveFilter),
			new OpenOfficePositiveCustomizer());
	positiveTracker.open();

	String reverseFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()),
			new EqFilter(Converter.ATTR_TYPE, Xliff2OpenOffice.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION,
					Converter.DIRECTION_REVERSE)).toString();
	reverseTracker = new ServiceTracker(context, context.createFilter(reverseFilter),
			new OpenOfficeReverseCustomize());
	reverseTracker.open();
}
 
Example 3
Source File: Activator.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (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();
}
 
Example 4
Source File: Activator.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (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();

}
 
Example 5
Source File: OSGiServiceCapabilityTracker.java    From carbon-kernel with Apache License 2.0 6 votes vote down vote up
/**
 * Returns and instance of {@link Filter}.
 *
 * @param capabilityNameList all the required capability services.
 * @return LDAP like filter
 */
private Filter getORFilter(List<String> capabilityNameList) {
    StringBuilder orFilterBuilder = new StringBuilder();
    orFilterBuilder.append("(|");

    for (String service : capabilityNameList) {
        orFilterBuilder.append("(").append(OBJECT_CLASS).append("=").append(service).append(")");
    }

    orFilterBuilder.append(")");

    BundleContext bundleContext = DataHolder.getInstance().getBundleContext();
    try {
        return bundleContext.createFilter(orFilterBuilder.toString());
    } catch (InvalidSyntaxException e) {
        throw new StartOrderResolverException("Error occurred while creating the service filter", e);
    }
}
 
Example 6
Source File: Activator.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (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();

}
 
Example 7
Source File: Activator.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (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 open office converter service
	String positiveFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()),
			new EqFilter(Converter.ATTR_TYPE, OpenOffice2Xliff.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION,
					Converter.DIRECTION_POSITIVE)).toString();
	positiveTracker = new ServiceTracker(context, context.createFilter(positiveFilter),
			new OpenOfficePositiveCustomizer());
	positiveTracker.open();

	String reverseFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()),
			new EqFilter(Converter.ATTR_TYPE, Xliff2OpenOffice.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION,
					Converter.DIRECTION_REVERSE)).toString();
	reverseTracker = new ServiceTracker(context, context.createFilter(reverseFilter),
			new OpenOfficeReverseCustomize());
	reverseTracker.open();
}
 
Example 8
Source File: Activator.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (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();
}
 
Example 9
Source File: Activator.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * (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();

}
 
Example 10
Source File: ServiceTracker.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a {@code ServiceTracker} on the specified class name.
 * 
 * <p>
 * Services registered under the specified class name will be tracked by
 * this {@code ServiceTracker}.
 * 
 * @param context The {@code BundleContext} against which the tracking is
 *        done.
 * @param clazz The class name of the services to be tracked.
 * @param customizer The customizer object to call when services are added,
 *        modified, or removed in this {@code ServiceTracker}. If customizer
 *        is {@code null}, then this {@code ServiceTracker} will be used as
 *        the {@code ServiceTrackerCustomizer} and this
 *        {@code ServiceTracker} will call the
 *        {@code ServiceTrackerCustomizer} methods on itself.
 */
public ServiceTracker(final BundleContext context, final String clazz, final ServiceTrackerCustomizer<S, T> customizer) {
	this.context = context;
	this.trackReference = null;
	this.trackClass = clazz;
	this.customizer = (customizer == null) ? this : customizer;
	// we call clazz.toString to verify clazz is non-null!
	this.listenerFilter = "(" + Constants.OBJECTCLASS + "=" + clazz.toString() + ")";
	try {
		this.filter = context.createFilter(listenerFilter);
	} catch (InvalidSyntaxException e) {
		/*
		 * we could only get this exception if the clazz argument was
		 * malformed
		 */
		IllegalArgumentException iae = new IllegalArgumentException("unexpected InvalidSyntaxException: " + e.getMessage());
		iae.initCause(e);
		throw iae;
	}
}
 
Example 11
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void start(BundleContext bc)
    throws Exception
{
  this.bc = bc;

  isLog = bc.createFilter(LOG_FILTER);
  isDevice = bc.createFilter(DEVICE_FILTER);
  isDriver = bc.createFilter(DRIVER_FILTER);
  isSelector = bc.createFilter(SELECTOR_FILTER);
  isLocator = bc.createFilter(LOCATOR_FILTER);

  startService(LOG_FILTER);

  start();

  bc.addFrameworkListener(this);
  final Bundle b = bc.getBundle(0);
  if (b.getState() == Bundle.ACTIVE) {
    activate();
  } else {
    info("Passive start");
  }
}
 
Example 12
Source File: ServiceTracker.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create a {@code ServiceTracker} on the specified class name.
 * 
 * <p>
 * Services registered under the specified class name will be tracked by
 * this {@code ServiceTracker}.
 * 
 * @param context The {@code BundleContext} against which the tracking is
 *        done.
 * @param clazz The class name of the services to be tracked.
 * @param customizer The customizer object to call when services are added,
 *        modified, or removed in this {@code ServiceTracker}. If customizer
 *        is {@code null}, then this {@code ServiceTracker} will be used as
 *        the {@code ServiceTrackerCustomizer} and this
 *        {@code ServiceTracker} will call the
 *        {@code ServiceTrackerCustomizer} methods on itself.
 */
public ServiceTracker(final BundleContext context, final String clazz, final ServiceTrackerCustomizer<S, T> customizer) {
	this.context = context;
	this.trackReference = null;
	this.trackClass = clazz;
	this.customizer = (customizer == null) ? this : customizer;
	// we call clazz.toString to verify clazz is non-null!
	this.listenerFilter = "(" + Constants.OBJECTCLASS + "=" + clazz.toString() + ")";
	try {
		this.filter = context.createFilter(listenerFilter);
	} catch (InvalidSyntaxException e) {
		/*
		 * we could only get this exception if the clazz argument was
		 * malformed
		 */
		IllegalArgumentException iae = new IllegalArgumentException("unexpected InvalidSyntaxException: " + e.getMessage());
		iae.initCause(e);
		throw iae;
	}
}
 
Example 13
Source File: CarbonContext.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Create filter from the bundle context adding class name and properties
 *
 * @param bundleContext BundleContext
 * @param clazz The type of the OSGi service
 * @param props attribute list that filter the service list
 * @return Filter
 * @throws InvalidSyntaxException
 */
protected Filter createFilter(BundleContext bundleContext, Class clazz, Hashtable<String, String> props)
        throws InvalidSyntaxException {
    StringBuilder buf = new StringBuilder();
    buf.append("(objectClass=" + clazz.getName() + ")");
    if (props != null && !props.isEmpty()) {
        buf.insert(0, "(&");
        for (Map.Entry<String, String> entry : props.entrySet()) {
            buf.append("(" + entry.getKey() + "=" + entry.getValue() + ")");
        }
        buf.append(")");
    }
    return bundleContext.createFilter(buf.toString());
}
 
Example 14
Source File: AbstractServerActivator.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void awaitService(BundleContext bundleContext, String filter, long timeout)
        throws InvalidSyntaxException, InterruptedException {
    Filter serviceFilter = bundleContext.createFilter(filter);
    ServiceTracker<Object, ?> tracker = new ServiceTracker<>(bundleContext, serviceFilter, null);
    tracker.open();
    Object service = tracker.waitForService(timeout);
    tracker.close();
    if (service == null) {
        throw new IllegalStateException("Expected service with filter " + filter + " was not found");
    }
}
 
Example 15
Source File: CertificateRepoProxyFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Filter createFilter(String filterSt, BundleContext context) {
    try {
        return context.createFilter(filterSt);
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter " + filterSt, e);
    }
}
 
Example 16
Source File: ServiceTracker.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Create a {@code ServiceTracker} on the specified {@code ServiceReference}
 * .
 * 
 * <p>
 * The service referenced by the specified {@code ServiceReference} will be
 * tracked by this {@code ServiceTracker}.
 * 
 * @param context The {@code BundleContext} against which the tracking is
 *        done.
 * @param reference The {@code ServiceReference} for the service to be
 *        tracked.
 * @param customizer The customizer object to call when services are added,
 *        modified, or removed in this {@code ServiceTracker}. If customizer
 *        is {@code null}, then this {@code ServiceTracker} will be used as
 *        the {@code ServiceTrackerCustomizer} and this
 *        {@code ServiceTracker} will call the
 *        {@code ServiceTrackerCustomizer} methods on itself.
 */
public ServiceTracker(final BundleContext context, final ServiceReference<S> reference, final ServiceTrackerCustomizer<S, T> customizer) {
	this.context = context;
	this.trackReference = reference;
	this.trackClass = null;
	this.customizer = (customizer == null) ? this : customizer;
	this.listenerFilter = "(" + Constants.SERVICE_ID + "=" + reference.getProperty(Constants.SERVICE_ID).toString() + ")";
	try {
		this.filter = context.createFilter(listenerFilter);
	} catch (InvalidSyntaxException e) {
		/*
		 * we could only get this exception if the ServiceReference was
		 * invalid
		 */
		IllegalArgumentException iae = new IllegalArgumentException("unexpected InvalidSyntaxException: " + e.getMessage());
		iae.initCause(e);
		throw iae;
	}
}
 
Example 17
Source File: ServiceTracker.java    From concierge with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Create a {@code ServiceTracker} on the specified {@code ServiceReference}
 * .
 * 
 * <p>
 * The service referenced by the specified {@code ServiceReference} will be
 * tracked by this {@code ServiceTracker}.
 * 
 * @param context The {@code BundleContext} against which the tracking is
 *        done.
 * @param reference The {@code ServiceReference} for the service to be
 *        tracked.
 * @param customizer The customizer object to call when services are added,
 *        modified, or removed in this {@code ServiceTracker}. If customizer
 *        is {@code null}, then this {@code ServiceTracker} will be used as
 *        the {@code ServiceTrackerCustomizer} and this
 *        {@code ServiceTracker} will call the
 *        {@code ServiceTrackerCustomizer} methods on itself.
 */
public ServiceTracker(final BundleContext context, final ServiceReference<S> reference, final ServiceTrackerCustomizer<S, T> customizer) {
	this.context = context;
	this.trackReference = reference;
	this.trackClass = null;
	this.customizer = (customizer == null) ? this : customizer;
	this.listenerFilter = "(" + Constants.SERVICE_ID + "=" + reference.getProperty(Constants.SERVICE_ID).toString() + ")";
	try {
		this.filter = context.createFilter(listenerFilter);
	} catch (InvalidSyntaxException e) {
		/*
		 * we could only get this exception if the ServiceReference was
		 * invalid
		 */
		IllegalArgumentException iae = new IllegalArgumentException("unexpected InvalidSyntaxException: " + e.getMessage());
		iae.initCause(e);
		throw iae;
	}
}
 
Example 18
Source File: StartupTimeCalculator.java    From sling-whiteboard with Apache License 2.0 4 votes vote down vote up
public StartupTimeCalculator(BundleContext ctx, BundleStartTimeCalculator bundleCalculator, ServiceRestartCountCalculator serviceCalculator) throws InvalidSyntaxException {
    executor = Executors.newScheduledThreadPool(1);
    try {
        String readinessDelay = ctx.getProperty(PROPERTY_READINESS_DELAY);
        additionalReadinessDelayMillis = Long.parseLong(readinessDelay);
    } catch ( NumberFormatException e) {
        Log.debug(getClass(), "Failed parsing readiness delay", e);
    }
    this.bundleCalculator = bundleCalculator;
    this.serviceCalculator = serviceCalculator;
    this.readyTracker = new ServiceTracker<>(ctx, 
            ctx.createFilter("(|(" + Constants.OBJECTCLASS+"=org.apache.felix.systemready.SystemReady)(&(" + Constants.OBJECTCLASS+ "=org.apache.felix.hc.api.condition.Healthy)(tag=systemalive)))"),
            new ServiceTrackerCustomizerAdapter<Object, Object>() {

                @Override
                public Object addingService(ServiceReference<Object> reference) {
                    if ( future == null ) 
                        future = calculate();
                    return ctx.getService(reference);
                }
                
                @Override
                public void removedService(ServiceReference<Object> reference, Object service) {
                    if ( future != null && !future.isDone() ) {
                        boolean cancelled = future.cancel(false);
                        if ( cancelled ) {
                            metricsSupplier = null;
                            future = null;
                        }
                    }
                }
            });
    this.readyTracker.open();
    
    this.listenersTracker = new ServiceTracker<>(ctx, StartupMetricsListener.class, new ServiceTrackerCustomizerAdapter<StartupMetricsListener, StartupMetricsListener>() {
        @Override
        public StartupMetricsListener addingService(ServiceReference<StartupMetricsListener> reference) {
            StartupMetricsListener service = ctx.getService(reference);
            // TODO - there is still a minor race condition, between the supplier being set and the registration of services
            // which can cause the listener to receive the event twice
            if ( metricsSupplier != null )
                service.onStartupComplete(metricsSupplier.get());
            return service;
        }
    });
    this.listenersTracker.open();
}
 
Example 19
Source File: DataSourceFactoryTracker.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public DataSourceFactoryTracker ( final BundleContext context, final String driver, final SingleServiceListener<DataSourceFactory> listener, final boolean isConnectionPool ) throws InvalidSyntaxException
{
    super ( context, context.createFilter ( "(&(objectClass=" + DataSourceFactory.class.getName () + ")(" + DataSourceFactory.OSGI_JDBC_DRIVER_CLASS + "=" + driver + ")(isConnectionPool=" + isConnectionPool + "))" ), listener );
    this.driver = driver;
}
 
Example 20
Source File: DataSourceFactoryTracker.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public DataSourceFactoryTracker ( final BundleContext context, final String driver, final SingleServiceListener<DataSourceFactory> listener ) throws InvalidSyntaxException
{
    super ( context, context.createFilter ( "(&(objectClass=" + DataSourceFactory.class.getName () + ")(" + DataSourceFactory.OSGI_JDBC_DRIVER_CLASS + "=" + driver + "))" ), listener );
    this.driver = driver;
}