org.osgi.framework.ServiceListener Java Examples

The following examples show how to use org.osgi.framework.ServiceListener. 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: Test6.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public boolean runTest() {
  ServiceListener[] listeners = new ServiceListener[NUM_LISTENERS];
  for (int i = 0; i < NUM_LISTENERS; i++) {
    listeners[i] = new MyServiceListener();
    bc.addServiceListener(listeners[i]);
  }
  
  Control control = (Control)tracker.getService();
  control.register();
  control.unregister();
  
  for (int i = 0; i < NUM_LISTENERS; i++) {
    bc.removeServiceListener(listeners[i]);
  }
  
  return true;
}
 
Example #2
Source File: MetadataStateDescriptionFragmentProviderTest.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    initMocks(this);

    when(bundleContext.getService(same(managedProviderRef))).thenReturn(managedProvider);

    when(item.getName()).thenReturn(ITEM_NAME);

    metadataRegistry = new MetadataRegistryImpl();

    metadataRegistry.setManagedProvider(managedProvider);
    metadataRegistry.activate(bundleContext);

    ArgumentCaptor<ServiceListener> captor = ArgumentCaptor.forClass(ServiceListener.class);
    verify(bundleContext).addServiceListener(captor.capture(), any());
    providerTracker = captor.getValue();
    providerTracker.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, managedProviderRef));

    stateDescriptionFragmentProvider = new MetadataStateDescriptionFragmentProvider(metadataRegistry,
            new HashMap<>());
}
 
Example #3
Source File: MetadataCommandDescriptionProviderTest.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    initMocks(this);

    when(bundleContext.getService(same(managedProviderRef))).thenReturn(managedProvider);

    when(item.getName()).thenReturn(ITEM_NAME);

    metadataRegistry = new MetadataRegistryImpl();

    metadataRegistry.setManagedProvider(managedProvider);
    metadataRegistry.activate(bundleContext);

    ArgumentCaptor<ServiceListener> captor = ArgumentCaptor.forClass(ServiceListener.class);
    verify(bundleContext).addServiceListener(captor.capture(), any());
    providerTracker = captor.getValue();
    providerTracker.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, managedProviderRef));

    commandDescriptionProvider = new MetadataCommandDescriptionProvider(metadataRegistry, new HashMap<>());
}
 
Example #4
Source File: MetadataRegistryImplTest.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    initMocks(this);

    when(bundleContext.getService(same(managedProviderRef))).thenReturn(managedProvider);

    when(item.getName()).thenReturn(ITEM_NAME);

    registry = new MetadataRegistryImpl();

    registry.setManagedProvider(managedProvider);
    registry.activate(bundleContext);

    ArgumentCaptor<ServiceListener> captor = ArgumentCaptor.forClass(ServiceListener.class);
    verify(bundleContext).addServiceListener(captor.capture(), any());
    providerTracker = captor.getValue();
    providerTracker.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, managedProviderRef));

}
 
Example #5
Source File: MetadataRegistryImplTest.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    initMocks(this);

    when(bundleContext.getService(same(managedProviderRef))).thenReturn(managedProvider);

    when(item.getName()).thenReturn(ITEM_NAME);

    registry = new MetadataRegistryImpl();

    registry.setManagedProvider(managedProvider);
    registry.activate(bundleContext);

    ArgumentCaptor<ServiceListener> captor = ArgumentCaptor.forClass(ServiceListener.class);
    verify(bundleContext).addServiceListener(captor.capture(), any());
    providerTracker = captor.getValue();
    providerTracker.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, managedProviderRef));
}
 
Example #6
Source File: Activator.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public void start(BundleContext context) throws Exception {
context.addServiceListener(new ServiceListener() {
			
	@Override
	public void serviceChanged(ServiceEvent event) {
		if (event.getType() == ServiceEvent.REGISTERED){
	    	component = new Component();
	    	
	    	Server server = new Server(Protocol.HTTP, 8182);
	    	component.getServers().add(server);
	    	component.setLogService(new LogService(false));
	    	
	    	final Application app = new ApiApplication();
	    	component.getDefaultHost().attachDefault(app);
	    	try {
	    		component.start();
	    	} catch (Exception e) {
	    		e.printStackTrace();
	    	}
		}
	}
}, "(objectclass=" + ApiStartServiceToken.class.getName() +")");
  }
 
Example #7
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * remove a service listener.
 * 
 * @param listener
 *            the service listener.
 * @see org.osgi.framework.BundleContext#removeServiceListener(org.osgi.framework.ServiceListener)
 * 
 */
public void removeServiceListener(final ServiceListener listener) {
	checkValid();

	final ServiceListenerEntry entry;
	synchronized (bundle.registeredServiceListeners) {
		entry = getRegisteredServiceListener(listener);
		if (entry == null) {
			return;
		}
		entry.removed = true;
		serviceListeners.remove(entry);
		bundle.registeredServiceListeners.remove(entry);
		if (bundle.registeredServiceListeners.isEmpty()) {
			bundle.registeredServiceListeners = null;
		}
	}

	informListenerHooks(serviceListenerHooks,
			new ServiceListenerEntry[] { entry }, false);
}
 
Example #8
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * add a service listener.
 * 
 * @param listener
 *            the service listener.
 * @param filterExpr
 *            the filter String.
 * @throws InvalidSyntaxException
 *             if the filter string is invalid.
 * @see org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener,
 *      java.lang.String)
 * 
 */
public void addServiceListener(final ServiceListener listener,
		final String filterExpr) throws InvalidSyntaxException {
	checkValid();

	final ServiceListenerEntry entry = new ServiceListenerEntry(bundle,
			listener, filterExpr);

	if (bundle.registeredServiceListeners == null) {
		bundle.registeredServiceListeners = new ArrayList<ServiceListenerEntry>(
				1);
	}

	synchronized (bundle.registeredServiceListeners) {
		final ServiceListenerEntry existing = getRegisteredServiceListener(
				listener);
		if (existing != null) {
			removeServiceListener(listener);
		}
		bundle.registeredServiceListeners.add(entry);
		serviceListeners.add(entry);
	}

	informListenerHooks(serviceListenerHooks,
			new ServiceListenerEntry[] { entry }, true);
}
 
Example #9
Source File: ServiceListenerState.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add a new service listener. If an old one exists, and it has the
 * same owning bundle, the old listener is removed first.
 *
 * @param bc The bundle context adding this listener.
 * @param listener The service listener to add.
 * @param filter An LDAP filter string to check when a service is modified.
 * @exception org.osgi.framework.InvalidSyntaxException
 * If the filter is not a correct LDAP expression.
 */
synchronized void add(BundleContextImpl bc,
                      ServiceListener listener,
                      String filter)
    throws InvalidSyntaxException
{
  final ServiceListenerEntry sle = new ServiceListenerEntry(bc, listener, filter);
  if (serviceSet.contains(sle)) {
    remove(bc, listener);
  }
  serviceSet.add(sle);
  listeners.fwCtx.serviceHooks.handleServiceListenerReg(sle);
  checkSimple(sle);
}
 
Example #10
Source File: ServiceListenerState.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Remove a service listener.
 *
 * @param bc The bundle context removing this listener.
 * @param listener The service listener to remove.
 */
synchronized void remove(BundleContextImpl bc, ServiceListener listener) {
  for (final Iterator<ServiceListenerEntry> it = serviceSet.iterator(); it.hasNext();) {
    final ServiceListenerEntry sle = it.next();
    if (sle.bc == bc && sle.listener == listener) {
      sle.setRemoved(true);
      listeners.fwCtx.serviceHooks.handleServiceListenerUnreg(sle);
      removeFromCache(sle);
      it.remove();
      break;
    }
  }
}
 
Example #11
Source File: BundleContextImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add a service listener.
 *
 * @see org.osgi.framework.BundleContext#addServiceListener
 */
public void addServiceListener(ServiceListener listener) {
  checkValid();
  try {
    bundle.fwCtx.listeners.addServiceListener(this, listener, null);
  } catch (final InvalidSyntaxException neverHappens) { }
}
 
Example #12
Source File: BundleContextImpl.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void addServiceListener(ServiceListener serviceListener, String str) throws InvalidSyntaxException {
    checkValid();
    ServiceListenerEntry serviceListenerEntry = new ServiceListenerEntry(serviceListener, str);
    if (this.bundle.registeredServiceListeners == null) {
        this.bundle.registeredServiceListeners = new ArrayList();
    }
    if (isServiceListenerRegistered(serviceListener)) {
        Framework.serviceListeners.remove(serviceListenerEntry);
    } else {
        this.bundle.registeredServiceListeners.add(serviceListener);
    }
    Framework.serviceListeners.add(serviceListenerEntry);
}
 
Example #13
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * add a service listener.
 * 
 * @param listener
 *            the service listener.
 * @see org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener)
 * 
 */
public void addServiceListener(final ServiceListener listener) {
	checkValid();
	try {
		addServiceListener(listener, null);
	} catch (final InvalidSyntaxException e) {
		// does not happen
	}
}
 
Example #14
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Determine if given service listener has been registered.
 * 
 * @param listener
 * @return <code>true</code> if the listener is registered.
 */
private ServiceListenerEntry getRegisteredServiceListener(
		final ServiceListener listener) {
	final ServiceListenerEntry[] listeners = bundle.registeredServiceListeners
			.toArray(
					new ServiceListenerEntry[bundle.registeredServiceListeners
							.size()]);
	for (int i = 0; i < listeners.length; i++) {
		if (listeners[i].bundle == bundle
				&& listeners[i].listener == listener) {
			return listeners[i];
		}
	}
	return null;
}
 
Example #15
Source File: BundleContextImpl.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
private boolean isServiceListenerRegistered(ServiceListener serviceListener) {
    ServiceListener[] serviceListenerArr = (ServiceListener[]) this.bundle.registeredServiceListeners.toArray(new ServiceListener[this.bundle.registeredServiceListeners.size()]);
    for (ServiceListener serviceListener2 : serviceListenerArr) {
        if (serviceListener2 == serviceListener) {
            return true;
        }
    }
    return false;
}
 
Example #16
Source File: BundleContextImpl.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void addServiceListener(ServiceListener serviceListener) {
    checkValid();
    try {
        addServiceListener(serviceListener, null);
    } catch (InvalidSyntaxException e) {
    }
}
 
Example #17
Source File: BundleContextImpl.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void removeServiceListener(ServiceListener serviceListener) {
    checkValid();
    try {
        Framework.serviceListeners.remove(new ServiceListenerEntry(serviceListener, null));
        this.bundle.registeredServiceListeners.remove(serviceListener);
        if (this.bundle.registeredServiceListeners.isEmpty()) {
            this.bundle.registeredServiceListeners = null;
        }
    } catch (InvalidSyntaxException e) {
    }
}
 
Example #18
Source File: Netbinox.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void addServiceListener(ServiceListener sl, String string) throws InvalidSyntaxException {
    delegate.addServiceListener(sl, string);
}
 
Example #19
Source File: Netbinox.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void addServiceListener(ServiceListener sl) {
    delegate.addServiceListener(sl);
}
 
Example #20
Source File: Framework.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
ServiceListenerEntry(ServiceListener serviceListener, String str) throws InvalidSyntaxException {
    this.listener = serviceListener;
    this.filter = str == null ? null : RFC1960Filter.fromString(str);
}
 
Example #21
Source File: MockBundleContext.java    From incubator-tamaya with Apache License 2.0 4 votes vote down vote up
@Override
public void addServiceListener(ServiceListener sl, String string) throws InvalidSyntaxException {
    throw new UnsupportedOperationException("Not supported (MockBundleContext addServiceListener)");
}
 
Example #22
Source File: ContentHandlerWrapper.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
ContentHandlerWrapper(FrameworkContext       framework,
	String                 mimetype) {

  this.framework = framework;
  this.mimetype  = mimetype;

  filter =
    "(&" +
    "(" + Constants.OBJECTCLASS + "=" +
    ContentHandler.class.getName() + ")" +
    "(" + URLConstants.URL_CONTENT_MIMETYPE + "=" + mimetype +
    ")" +
    ")";

  final ServiceListener serviceListener =
    new ServiceListener() {
      public void serviceChanged(ServiceEvent evt) {
        @SuppressWarnings("unchecked")
        final
        ServiceReference<ContentHandler> ref =
            (ServiceReference<ContentHandler>) evt.getServiceReference();

        switch (evt.getType()) {
        case ServiceEvent.MODIFIED:
          // fall through
        case ServiceEvent.REGISTERED:
          if (best == null) {
            updateBest();
            return ;
          }

          if (compare(best, ref) > 0) {
            best = ref;
          }
          break;
        case ServiceEvent.MODIFIED_ENDMATCH:
          // fall through
        case ServiceEvent.UNREGISTERING:
          if (best.equals(ref)) {
            best = null;
          }
        }
      }
    };

  try {
    framework.systemBundle.bundleContext.addServiceListener(serviceListener, filter);

  } catch (final Exception e) {
    throw new IllegalArgumentException("Could not register service listener for content handler: " + e);
  }

  if (framework.debug.url) {
    framework.debug.println("created wrapper for " + mimetype + ", filter=" + filter);
  }
}
 
Example #23
Source File: MockBundleContext.java    From incubator-tamaya with Apache License 2.0 4 votes vote down vote up
@Override
public void addServiceListener(ServiceListener sl) {
    throw new UnsupportedOperationException("Not supported (MockBundleContext addServiceListener)");
}
 
Example #24
Source File: NetigsoSelfQueryTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void addServiceListener(ServiceListener sl, String string) throws InvalidSyntaxException {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example #25
Source File: MockBundleContext.java    From incubator-tamaya with Apache License 2.0 4 votes vote down vote up
@Override
public void removeServiceListener(ServiceListener sl) {
    throw new UnsupportedOperationException("Not supported (MockBundleContext removeServiceListener)");
}
 
Example #26
Source File: BundleContextImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Add a service listener with a filter.
 *
 * @see org.osgi.framework.BundleContext#addServiceListener
 */
public void addServiceListener(ServiceListener listener, String filter)
  throws InvalidSyntaxException {
  checkValid();
  bundle.fwCtx.listeners.addServiceListener(this, listener, filter);
}
 
Example #27
Source File: ClassLoaderOsgiFramework.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public void removeServiceListener(ServiceListener listener) {
}
 
Example #28
Source File: ClassLoaderOsgiFramework.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public void addServiceListener(ServiceListener listener) {
}
 
Example #29
Source File: ClassLoaderOsgiFramework.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public void addServiceListener(ServiceListener listener, String filter) {
}
 
Example #30
Source File: Activator.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public void start(BundleContext context) throws Exception {
	
	System.err.println("Starting Admin bundle");
	
	context.addServiceListener(new ServiceListener() {
		
		@Override
		public void serviceChanged(ServiceEvent event) {
			System.err.println(event);
			if (event.getType() == ServiceEvent.REGISTERED){
				Application application = new AdminApplication();

				component = new Component();
				component.getServers().add(Protocol.HTTP, 8183);
				component.getClients().add(Protocol.FILE);

				boolean useAuth = Boolean.valueOf(Configuration.getInstance().getProperty("adminapi.use_authentication", "false"));
				
				if (useAuth) {
					String username = Configuration.getInstance().getProperty("adminapi.username", null);
					String password = Configuration.getInstance().getProperty("adminapi.password", null);
					
					ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "myRealm");
					MapVerifier verifier = new MapVerifier();
					verifier.getLocalSecrets().put(username, password.toCharArray());
					guard.setVerifier(verifier);
					guard.setNext(application);
					
					component.getDefaultHost().attachDefault(guard);
				} else {
					component.getDefaultHost().attachDefault(application);
				}
				
				try {
					component.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}, "(objectclass=" + ApiStartServiceToken.class.getName() +")");
}