org.osgi.framework.ServiceEvent Java Examples

The following examples show how to use org.osgi.framework.ServiceEvent. 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: 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 #2
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 #3
Source File: CMCommands.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void serviceChanged(ServiceEvent event)
{
  switch (event.getType()) {
  case ServiceEvent.REGISTERED:
    @SuppressWarnings("unchecked")
    final ServiceReference<ConfigurationAdmin> sr =
      (ServiceReference<ConfigurationAdmin>) event.getServiceReference();
    if (refCA != sr) {
      refCA = sr;
    }
    break;
  case ServiceEvent.MODIFIED:
    break;
  case ServiceEvent.UNREGISTERING:
    if (refCA != null) {
      refCA = null;
    }
    break;
  default:
    break;
  }
}
 
Example #4
Source File: LogReaderDispatcher.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void open()
{
  final String filter = "(objectclass=" + LogReaderService.class.getName() + ")";

  try {
    final ServiceReference<?>[] srl =
      bc.getServiceReferences((String) null, filter);
    for (int i = 0; srl != null && i < srl.length; i++) {

      serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, srl[i]));
    }
    bc.addServiceListener(this, filter);
  } catch (final Exception e) {
    e.printStackTrace();
  }
}
 
Example #5
Source File: ComponentServiceListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void serviceChanged(ServiceEvent event)
{
  ServiceReference<?> sr = event.getServiceReference();
  for (String cn : (String [])sr.getProperty(Constants.OBJECTCLASS)) {
    Set<ReferenceListener> rls = serviceListeners.get(cn);
    if (rls != null) {
      for (ReferenceListener rl : rls) {
        rl.serviceEvent(sr, event);
      }
    }
  }
  List<Runnable> postrun = afterServiceEvent.get(event);
  if (postrun != null) {
    for (Runnable r : postrun) {
      r.run();
    }
  }
}
 
Example #6
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 #7
Source File: ServiceListenerTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void runTest() throws Throwable {
  boolean teststatus = true;
  int cnt = 1;

  teststatus = runStartStopTest( "FRAMEsl05A", cnt, buA,
                                 new int[]{
                                   ServiceEvent.REGISTERED,
                                   ServiceEvent.UNREGISTERING,
                                 },
                                 new int[]{
                                   ServiceEvent.REGISTERED,
                                   ServiceEvent.UNREGISTERING,
                                 } );

  if (teststatus == true) {
    out.println("### ServiceListenerTestsuite :FRAMEsl05A:PASS");
  }
  else {
    fail("### ServiceListenerTestsuite :FRAMEsl05A:FAIL");
  }
}
 
Example #8
Source File: ServiceListenerTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
boolean checkEvents(int[] eventTypes)
{
  if (events.size() != eventTypes.length) {
    dumpEvents(eventTypes);
    return false;
  }

  for (int i=0; i<eventTypes.length; i++) {
    ServiceEvent evt = (ServiceEvent) events.get(i);
    if (eventTypes[i] != evt.getType() ) {
      dumpEvents(eventTypes);
      return false;
    }
  }
  return true;
}
 
Example #9
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 #10
Source File: ServiceDiscoverer.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public synchronized void serviceChanged ( final ServiceEvent event )
{
    switch ( event.getType () )
    {
        case ServiceEvent.REGISTERED:
            addReference ( event.getServiceReference () );
            break;
        case ServiceEvent.MODIFIED:
            update ();
            break;
        case ServiceEvent.UNREGISTERING:
            removeReference ( event.getServiceReference () );
            break;
    }
}
 
Example #11
Source File: LogFrameworkListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * The service event callback method inserts all service events into the
 * log.
 * 
 * Event of types REGISTERED, UNREGISTERED are assigned the log
 * level info.
 * 
 * Events of type MODIFIED are assigned the log level DEBUG.
 * 
 * @param se
 *            the service event that has occurred.
 */
public void serviceChanged(ServiceEvent se) {
    ServiceReference<?> sr = se.getServiceReference();
    Bundle bundle = sr.getBundle();
    String msg = null;
    int level = LogService.LOG_INFO;
    switch (se.getType()) {
    case ServiceEvent.REGISTERED:
        msg = "ServiceEvent REGISTERED";
        break;
    case ServiceEvent.UNREGISTERING:
        msg = "ServiceEvent UNREGISTERING";
        break;
    case ServiceEvent.MODIFIED:
        msg = "ServiceEvent MODIFIED";
        level = LogService.LOG_DEBUG;
        break;
    }
    lrsf.log(new LogEntryImpl(bundle, sr, level, msg));
}
 
Example #12
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void serviceChanged(ServiceEvent event) { 
  switch (event.getType()) { 
  case ServiceEvent.REGISTERED: 
    log("ServiceEvent.REGISTERED"); 
    dateService = (DateService) Activator.bc.getService(event 
                                                         .getServiceReference()); 
    startUsingService(); 
    break; 
  case ServiceEvent.MODIFIED: 
    log("ServiceEvent.MODIFIED received"); 
    stopUsingService(); 
    dateService = (DateService) Activator.bc.getService(event 
                                                    .getServiceReference()); 
    startUsingService(); 
    break; 
  case ServiceEvent.UNREGISTERING: 
    log("ServiceEvent.UNREGISTERING"); 
    stopUsingService(); 
    break; 
  } 
}
 
Example #13
Source File: ServiceRestartCountCalculatorTest.java    From sling-whiteboard with Apache License 2.0 6 votes vote down vote up
@Test
public void unknownServiceUnregistrationsAreTracked() {
    HashMap<String, Object> props = new HashMap<>();
    props.put(Constants.OBJECTCLASS, new String[] { "foo", "bar" });
    DummyServiceReference<Object> sr1 = new DummyServiceReference<>(props);
    
    HashMap<String, Object> props2 = new HashMap<>();
    props2.put(Constants.OBJECTCLASS, new String[] { "foo"} );
    DummyServiceReference<Object> sr2 = new DummyServiceReference<>(props2);
    
    ServiceRestartCountCalculator srcc = new ServiceRestartCountCalculator();
    srcc.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, sr1));
    srcc.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, sr1));
    
    srcc.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, sr2));
    srcc.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, sr2));
    
    assertThat(srcc.getRegistrations().size(), equalTo(0));
    Map<String, Integer> unidentifiedRegistrations = srcc.getUnidentifiedRegistrationsByClassName();
    assertThat(unidentifiedRegistrations.size(), equalTo(2));
    assertThat(unidentifiedRegistrations.get("foo"), equalTo(2));
    assertThat(unidentifiedRegistrations.get("bar"), equalTo(1));
}
 
Example #14
Source File: ServiceListenerTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
String toStringEventType(int eventType)
{
  String res = String.valueOf(eventType);

  switch (eventType) {
  case ServiceEvent.REGISTERED:    res +=" (REGISTERED)    "; break;
  case ServiceEvent.MODIFIED:      res +=" (MODIFIED)      "; break;
  case ServiceEvent.UNREGISTERING: res +=" (UNREGISTERING) "; break;
  default:                         res +=" (?)             ";
  }
  return res;
}
 
Example #15
Source File: UIBundleDeployer.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
public ServletServiceListener() {
    try {
        ServiceReference[] servletSRs = bundleContext.getServiceReferences((String)null,
                "(objectClass=" + Servlet.class.getName() + ")");

        if (servletSRs != null) {
            for (ServiceReference sr : servletSRs) {
                registerServletFromSR(sr, ServiceEvent.REGISTERED);
            }
        }
    } catch (Exception e) {
        log.error("Failed to obtain registerd services. Invalid filter Syntax.", e);
    }
}
 
Example #16
Source File: SecurePermissionOps.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
void callServiceChanged(final FrameworkContext fwCtx,
                        final Collection<ServiceListenerEntry> receivers,
                        final ServiceEvent evt,
                        final Set<ServiceListenerEntry> matchBefore)
{
  AccessController.doPrivileged(new PrivilegedAction<Object>() {
    public Object run()
    {
      fwCtx.listeners.serviceChanged(receivers, evt, matchBefore);
      return null;
    }
  });
}
 
Example #17
Source File: RegListenThread.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public synchronized void serviceChanged(ServiceEvent se) {
  ServiceReference srlocal = se.getServiceReference();
  try {
    Thread.sleep(500); // sleep for longer time that the registering thread sleeps
                       // as we need to be hanging here to create 
                       // a lock for the registering thread
  }
  catch (Exception ex) {
    out.println("### Frame test bundle :FRAME210A exception, in RegListenThread");
    ex.printStackTrace(out);
    status = false;
  }

  bc.getService(srlocal);
}
 
Example #18
Source File: ServiceListenerTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void dumpEvents(int[] eventTypes)
{
  int max = events.size()> eventTypes.length
    ? events.size() : eventTypes.length;
  out.println("Expected event type --  Actual event");
  for (int i=0; i<max; i++) {
    ServiceEvent evt
      = i<events.size() ? (ServiceEvent) events.get(i) : null;
    out.println(" "
                +(i<eventTypes.length
                  ? toStringEventType(eventTypes[i]) : "- NONE - ")
                +" -- "
                +toString(evt) );
  }
}
 
Example #19
Source File: ServiceListenerTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
String toString(ServiceEvent evt)
{
  if (null==evt) return " - NONE - ";

  ServiceReference sr = evt.getServiceReference();
  String[] objectClasses = (String[]) sr.getProperty(Constants.OBJECTCLASS);
  String res = toStringEventType(evt.getType());

  for (int i=0; i<objectClasses.length; i++){
    if (i>0) res += ", ";
    res += objectClasses[i];
  }
  return res;
}
 
Example #20
Source File: ComponentConfiguration.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
boolean setAndTestLastReactivateEvent(ServiceEvent se)
{
  if (lastReactivateEvent != se) {
    lastReactivateEvent = se;
    return true;
  }
  return false;
}
 
Example #21
Source File: ServiceListenerTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void runTest() throws Throwable {
  int cnt = 1;

  assertTrue(runStartStopTest( "FRAMEsl10A", cnt, buA2,
                                 new int[]{
                                   ServiceEvent.REGISTERED,
                                   ServiceEvent.UNREGISTERING,
                                 },
                                 new int[]{
                                   ServiceEvent.REGISTERED,
                                   ServiceEvent.UNREGISTERING,
                                 }));
  out.println("### ServiceListenerTestsuite :FRAMEsl10A:PASS");
}
 
Example #22
Source File: ComponentServiceListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void addAfterServiceEvent(ServiceEvent se, Runnable r) {
  List<Runnable> ase = afterServiceEvent.get(se);
  if (ase == null) {
    ase = new ArrayList<Runnable>();
    afterServiceEvent.put(se, ase);
  }
  ase.add(r);
}
 
Example #23
Source File: DefaultSwingBundleDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void serviceChanged(ServiceEvent ev)
{
  if (!bAlive) {
    return;
  }

  if (bUpdateOnServiceChange) {
    if (Activator.desktop != null) {
      updateComponents(Activator.desktop.getSelectedBundles());
    }
  }

}
 
Example #24
Source File: DefaultSwingBundleDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void getAllServices()
{
  try {
    final ServiceReference<?>[] srl =
      Activator.getTargetBC_getServiceReferences();
    for (int i = 0; srl != null && i < srl.length; i++) {
      serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, srl[i]));
    }
  } catch (final Exception e) {
    e.printStackTrace();
  }
}
 
Example #25
Source File: JSoftGraphBundle.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void serviceChanged(ServiceEvent ev) {
  if(jmb.isAutoRefresh()) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          startFade();
        }
      });
  }
}
 
Example #26
Source File: LogReaderDispatcher.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void serviceChanged(ServiceEvent ev)
{
  @SuppressWarnings("unchecked")
  final
  ServiceReference<LogReaderService> sr =
    (ServiceReference<LogReaderService>) ev.getServiceReference();

  final LogReaderService lr =
    logReaders.containsKey(sr) ? logReaders.get(sr) : (LogReaderService) bc
        .getService(sr);

  if (null != lr) {
    switch (ev.getType()) {
    case ServiceEvent.REGISTERED:
      lr.addLogListener(this);
      logReaders.put(sr, lr);
      break;
    case ServiceEvent.MODIFIED:
      break;
    case ServiceEvent.UNREGISTERING:
      lr.removeLogListener(this);
      logReaders.remove(sr);
      bc.ungetService(sr);
      break;
    }
  }
}
 
Example #27
Source File: LogReaderDispatcher.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void close()
{
  bc.removeServiceListener(this);

  for (final ServiceReference<LogReaderService> sr : logReaders.keySet()) {
    serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, sr));
  }
  logReaders.clear();
}
 
Example #28
Source File: Command.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void serviceChanged(ServiceEvent e) {
    if (e.getServiceReference() == commandGroupRef) {
        synchronized (this) {
            // Wait for run command
        }
    }
}
 
Example #29
Source File: LogRef.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Service listener entry point. Releases the log service object if one has
 * been fetched.
 * 
 * @param evt
 *          Service event
 */
public void serviceChanged(ServiceEvent evt)
{
  if (evt.getServiceReference() == logSR
      && evt.getType() == ServiceEvent.UNREGISTERING) {
    ungetLogService();
  }
}
 
Example #30
Source File: UpgradeTaskProvider.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
protected void handleServiceChange ( final ServiceEvent event )
{
    logger.debug ( "service change - {} - {}", event.getType (), event.getServiceReference () );

    switch ( event.getType () )
    {
        case ServiceEvent.UNREGISTERING:
        case ServiceEvent.REGISTERED:
            refresh ();
            break;
    }
}