Java 类org.osgi.framework.ServiceEvent 实例源码

项目:neoscada    文件:ServiceDiscoverer.java   
@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;
    }
}
项目:gemini.blueprint    文件:MockBundleContextTest.java   
public void testRemoveServiceListener() throws Exception {
    ServiceListener listener = new ServiceListener() {
        public void serviceChanged(ServiceEvent event) {
        }
    };

    Set listeners = mock.getServiceListeners();

    mock.removeServiceListener(null);

    assertEquals(0, listeners.size());

    mock.removeServiceListener(listener);
    assertEquals(0, listeners.size());

    mock.addServiceListener(listener);
    assertEquals(1, listeners.size());

    mock.removeServiceListener(listener);
    assertEquals(0, listeners.size());
}
项目:gemini.blueprint    文件:ListListenerAdapterTest.java   
@SuppressWarnings("rawtypes")
public ServiceRegistration registerService(String[] clazzes, final Object service, Dictionary properties) {
    MockServiceRegistration reg = new MockServiceRegistration(properties);

    MockServiceReference ref = new MockServiceReference(getBundle(), properties, reg, clazzes) {

        @Override
        public Object getProperty(String key) {
            if (SERVICE_PROPERTY.equals(key)) {
                return service;
            } else {
                return super.getProperty(key);
            }
        }

    };
    ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, ref);

    for (Iterator iter = serviceListeners.iterator(); iter.hasNext();) {
        ServiceListener listener = (ServiceListener) iter.next();
        listener.serviceChanged(event);
    }

    return reg;
}
项目:gemini.blueprint    文件:OsgiServiceCollectionProxyFactoryBeanTest.java   
public void testServiceReferenceMemberType() throws Exception {
    serviceFactoryBean.setMemberType(MemberType.SERVICE_REFERENCE);
    serviceFactoryBean.setInterfaces(new Class<?>[] { Runnable.class });
    serviceFactoryBean.afterPropertiesSet();

    Collection col = (Collection) serviceFactoryBean.getObject();

    assertFalse(col.isEmpty());
    assertSame(ref, col.iterator().next());

    Set listeners = bundleContext.getServiceListeners();
    ServiceListener list = (ServiceListener) listeners.iterator().next();
    ServiceReference ref2 = new MockServiceReference();
    list.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref2));

    assertEquals(2, col.size());
    Iterator iter = col.iterator();
    iter.next();
    assertSame(ref2, iter.next());
}
项目:gemini.blueprint    文件:OsgiServiceDynamicInterceptorTest.java   
public void testInvocationTimeoutWhenServiceNA() throws Throwable {
    // service n/a

    Object target = new Object();
    Method m = target.getClass().getDeclaredMethod("hashCode", null);

    MethodInvocation invocation = new MockMethodInvocation(m);
    createInterceptor(new MockFilter(nullFilter));
    ServiceEvent event = new ServiceEvent(ServiceEvent.UNREGISTERING, reference);
    listener.serviceChanged(event);
    interceptor.getRetryTemplate().reset(3000);
    long now = System.currentTimeMillis();
    try {
        interceptor.invoke(invocation);
        fail("should have thrown exception");
    }
    catch (ServiceUnavailableException ex) {
        // expected
    }

    // service is up
    interceptor.getRetryTemplate().reset(1);

    assertTrue("Call did not block for 3000ms, actually blocked for " + (System.currentTimeMillis() - now) + "ms",
        (System.currentTimeMillis() - now) >= 3000);
}
项目:gemini.blueprint    文件:OsgiServiceDynamicInterceptorListenerTest.java   
public void testUnbind() {
    interceptor.afterPropertiesSet();

    assertEquals(1, SimpleTargetSourceLifecycleListener.BIND);
    assertEquals(0, SimpleTargetSourceLifecycleListener.UNBIND);

    ServiceListener sl = (ServiceListener) bundleContext.getServiceListeners().iterator().next();

    // save old ref and invalidate it so new services are not found
    ServiceReference oldRef = refs[0];
    refs = null;

    sl.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, oldRef));

    assertEquals(1, SimpleTargetSourceLifecycleListener.BIND);
    assertEquals(1, SimpleTargetSourceLifecycleListener.UNBIND);
}
项目:gemini.blueprint    文件:OsgiServiceDynamicInterceptorListenerTest.java   
public void testStickinessWhenABetterServiceIsAvailable() throws Exception {
    interceptor.setSticky(true);
    interceptor.afterPropertiesSet();

    ServiceListener sl = (ServiceListener) bundleContext.getServiceListeners().iterator().next();

    Dictionary props = new Hashtable();
    // increase service ranking
    props.put(Constants.SERVICE_RANKING, 10);

    ServiceReference ref = new MockServiceReference(null, props, null);
    ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, ref);

    assertEquals(1, SimpleTargetSourceLifecycleListener.BIND);
    assertEquals(0, SimpleTargetSourceLifecycleListener.UNBIND);

    sl.serviceChanged(event);

    assertEquals("the proxy is not sticky", 1, SimpleTargetSourceLifecycleListener.BIND);
    assertEquals(0, SimpleTargetSourceLifecycleListener.UNBIND);
}
项目:gemini.blueprint    文件:OsgiServiceDynamicInterceptorListenerTest.java   
public void testStickinessWhenServiceGoesDown() throws Exception {
    interceptor.setSticky(true);
    interceptor.afterPropertiesSet();

    ServiceListener sl = (ServiceListener) bundleContext.getServiceListeners().iterator().next();

    Dictionary props = new Hashtable();
    // increase service ranking
    props.put(Constants.SERVICE_RANKING, 10);

    ServiceReference higherRankingRef = new MockServiceReference(null, props, null);
    refs = new ServiceReference[] { new MockServiceReference(), higherRankingRef };

    assertTrue(Arrays.equals(bundleContext.getServiceReferences((String)null, null), refs));

    assertEquals(1, SimpleTargetSourceLifecycleListener.BIND);
    assertEquals(0, SimpleTargetSourceLifecycleListener.UNBIND);

    sl.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, refs[0]));

    assertEquals(2, SimpleTargetSourceLifecycleListener.BIND);
    assertEquals(0, SimpleTargetSourceLifecycleListener.UNBIND);

    assertSame("incorrect backing reference selected", higherRankingRef, ((ServiceReferenceProxy) interceptor
            .getServiceReference()).getTargetServiceReference());
}
项目:gemini.blueprint    文件:OsgiListenerUtilsTest.java   
/**
 * Test method for
 * {@link org.eclipse.gemini.blueprint.util.OsgiListenerUtils#addServiceListener(org.osgi.framework.BundleContext, org.osgi.framework.ServiceListener, java.lang.String)}.
 */
public void testAddServiceListenerBundleContextServiceListenerString() {
    final List refs = new ArrayList();

    ServiceListener list = new ServiceListener() {

        public void serviceChanged(ServiceEvent event) {
            if (ServiceEvent.REGISTERED == event.getType())
                refs.add(event.getSource());
        }
    };

    OsgiListenerUtils.addServiceListener(bundleContext, list, (String) null);

    assertFalse(refs.isEmpty());
    assertEquals(3, refs.size());
    assertSame(ref1, refs.get(0));
    assertSame(ref2, refs.get(1));
    assertSame(ref3, refs.get(2));
}
项目:gemini.blueprint    文件:OsgiListenerUtilsTest.java   
/**
 * Test method for
 * {@link org.eclipse.gemini.blueprint.util.OsgiListenerUtils#addSingleServiceListener(org.osgi.framework.BundleContext, org.osgi.framework.ServiceListener, java.lang.String)}.
 */
public void testAddSingleServiceListenerBundleContextServiceListenerString() {
    final List refs = new ArrayList();

    ServiceListener listener = new ServiceListener() {

        public void serviceChanged(ServiceEvent event) {
            if (ServiceEvent.REGISTERED == event.getType())
                refs.add(event.getSource());
        }
    };

    OsgiListenerUtils.addSingleServiceListener(bundleContext, listener, (String) null);

    assertFalse(refs.isEmpty());
    assertEquals(1, refs.size());
    assertSame(ref1, refs.get(0));
}
项目:aries-rsa    文件:TopologyManagerExportTest.java   
@Test
public void testExportExisting() throws Exception {
    IMocksControl c = EasyMock.createControl();
    RemoteServiceAdmin rsa = c.createMock(RemoteServiceAdmin.class);
    final EndpointListenerNotifier mockEpListenerNotifier = c.createMock(EndpointListenerNotifier.class);
    final ServiceReference sref = createUserService(c);
    expectServiceExported(c, rsa, mockEpListenerNotifier, sref, createEndpoint());
    c.replay();

    EndpointRepository endpointRepo = new EndpointRepository();
    endpointRepo.setNotifier(mockEpListenerNotifier);
    ExportPolicy policy = new DefaultExportPolicy();
    TopologyManagerExport exportManager = new TopologyManagerExport(endpointRepo, syncExecutor(), policy);
    exportManager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, sref));
    exportManager.add(rsa);
    c.verify();
}
项目:aries-rsa    文件:TopologyManagerExportTest.java   
@Test
public void testExportExistingMultipleInterfaces() throws Exception {
    IMocksControl c = EasyMock.createControl();
    RemoteServiceAdmin rsa = c.createMock(RemoteServiceAdmin.class);
    final EndpointListenerNotifier mockEpListenerNotifier = c.createMock(EndpointListenerNotifier.class);
    List<String> exportedInterfaces = Arrays.asList("a.b.C","foo.Bar");
    final ServiceReference sref = createUserService(c, exportedInterfaces);
    expectServiceExported(c, rsa, mockEpListenerNotifier, sref, createEndpoint());
    c.replay();

    EndpointRepository endpointRepo = new EndpointRepository();
    endpointRepo.setNotifier(mockEpListenerNotifier);
    ExportPolicy policy = new DefaultExportPolicy();
    TopologyManagerExport exportManager = new TopologyManagerExport(endpointRepo, syncExecutor(), policy);
    exportManager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, sref));
    exportManager.add(rsa);
    c.verify();
}
项目:aries-rsa    文件:TopologyManagerExportTest.java   
@Test
public void testExportExistingNoExportedInterfaces() throws Exception {
    IMocksControl c = EasyMock.createControl();
    RemoteServiceAdmin rsa = c.createMock(RemoteServiceAdmin.class);
    final EndpointListenerNotifier mockEpListenerNotifier = c.createMock(EndpointListenerNotifier.class);
    String exportedInterfaces = "";
    final ServiceReference sref = createUserService(c, exportedInterfaces);
    c.replay();

    EndpointRepository endpointRepo = new EndpointRepository();
    endpointRepo.setNotifier(mockEpListenerNotifier);
    ExportPolicy policy = new DefaultExportPolicy();
    TopologyManagerExport exportManager = new TopologyManagerExport(endpointRepo, syncExecutor(), policy);
    exportManager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, sref));
    exportManager.add(rsa);
    c.verify();
}
项目:aries-rsa    文件:RemoteServiceAdminCore.java   
public RemoteServiceAdminCore(BundleContext context, BundleContext apiContext, DistributionProvider provider) {
    this.bctx = context;
    this.apictx = apiContext;
    this.eventProducer = new EventProducer(bctx);
    this.provider = provider;
    // listen for exported services being unregistered so we can close the export
    this.exportedServiceListener = new ServiceListener() {
        public void serviceChanged(ServiceEvent event) {
            if (event.getType() == ServiceEvent.UNREGISTERING) {
                removeServiceExports(event.getServiceReference());
            }
        }
    };
    try {
        String filter = "(" + RemoteConstants.SERVICE_EXPORTED_INTERFACES + "=*)";
        context.addServiceListener(exportedServiceListener, filter);
    } catch (InvalidSyntaxException ise) {
        throw new RuntimeException(ise); // can never happen
    }
}
项目:aspecio    文件:ServiceWeavingManager.java   
@Override
public void serviceChanged(ServiceEvent event) {
    if (closed) {
        return;
    }

    ServiceReference<?> sr = event.getServiceReference();

    switch (event.getType()) {
    case ServiceEvent.REGISTERED:
        onServiceRegistration(sr);
        break;

    case ServiceEvent.MODIFIED:
        onServiceUpdate(sr);
        break;

    case ServiceEvent.MODIFIED_ENDMATCH:
    case ServiceEvent.UNREGISTERING:
        onServiceDeparture(sr);
        break;
    }
}
项目:aspecio    文件:AspectInterceptorManager.java   
@Override
public void serviceChanged(ServiceEvent event) {
    if (closed) {
        return;
    }

    ServiceReference<?> sr = event.getServiceReference();

    switch (event.getType()) {
    case ServiceEvent.REGISTERED:
        onServiceRegistration(sr);
        break;

    case ServiceEvent.MODIFIED:
        onServiceUpdate(sr);
        break;

    case ServiceEvent.MODIFIED_ENDMATCH:
    case ServiceEvent.UNREGISTERING:
        onServiceDeparture(sr);
        break;
    }
}
项目:Camel    文件:BlueprintCamelContext.java   
@Override
public void serviceChanged(ServiceEvent event) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Service {} changed to {}", event, event.getType());
    }
    // look for blueprint container to be registered, and then we can start the CamelContext
    if (event.getType() == ServiceEvent.REGISTERED
            && event.getServiceReference().isAssignableTo(bundleContext.getBundle(), "org.osgi.service.blueprint.container.BlueprintContainer")
            && bundleContext.getBundle().equals(event.getServiceReference().getBundle())) {
        try {
            maybeStart();
        } catch (Exception e) {
            LOG.error("Error occurred during starting Camel: " + this + " due " + e.getMessage(), e);
        }
    }
}
项目:ApkLauncher    文件:Activator.java   
/**
 * Implements ServiceListener.serviceChanged().
 * Prints the details of any service event from the framework.
 * @param event the fired service event.
**/
public void serviceChanged(ServiceEvent event)
{
    String[] objectClass = (String[])
        event.getServiceReference().getProperty("objectClass");

    if (event.getType() == ServiceEvent.REGISTERED)
    {
        System.out.println(
            "Ex1: Service of type " + objectClass[0] + " registered.");
    }
    else if (event.getType() == ServiceEvent.UNREGISTERING)
    {
        System.out.println(
            "Ex1: Service of type " + objectClass[0] + " unregistered.");
    }
    else if (event.getType() == ServiceEvent.MODIFIED)
    {
        System.out.println(
            "Ex1: Service of type " + objectClass[0] + " modified.");
    }
}
项目:motech    文件:EntitiesBundleMonitor.java   
/**
 * Receives notification that the context of the entities bundle has had a lifecycle change and
 * checks it current status.
 *
 * @param event The {@code ServiceEvent} object.
 */
@Override
public void serviceChanged(ServiceEvent event) {
    ServiceReference<?> reference = event.getServiceReference();

    if (isEntities(reference.getBundle())) {
        Object service = bundleContext.getService(reference);

        if (service instanceof ApplicationContext) {
            LOGGER.trace(
                    "The Entities Bundle Application Context Status: {}",
                    nullSafeToString(event)
            );

            synchronized (lock) {
                contextInitialized = event.getType() != ServiceEvent.UNREGISTERING;

                if (contextInitialized) {
                    LOGGER.info("The entities bundle context has been initialized");
                } else {
                    LOGGER.info("The entities bundle context has been unregistered");
                }
            }
        }
    }
}
项目:semweb4j    文件:RDF2GoActivator.java   
private void handleRegisteredEvent(ServiceEvent event) {
    ServiceReference ref = event.getServiceReference();
    ModelFactory factory = (ModelFactory)this.bc.getService(ref);
    String currClass = getCurrentFactoryClassName();
    String newClass = factory.getClass().getName();
    if(this.currentFactorySR == null) {
        log.debug("Registering " + factory.getClass().getName() + " as default ModelFactory");
        this.currentFactorySR = ref;
        RDF2Go.register(factory);
    } else if(this.defaultFactoryClassName != null
            && !currClass.equals(this.defaultFactoryClassName)
            && newClass.equals(this.defaultFactoryClassName)) {
        RDF2Go.register((ModelFactory)null);
        this.bc.ungetService(this.currentFactorySR);

        factory = (ModelFactory)this.bc.getService(ref);
        this.currentFactorySR = ref;
        RDF2Go.register(factory);
        log.debug("RDF2Go uses " + newClass + " as default ModelFactory");
    } else {
        this.bc.ungetService(ref);
    }
}
项目:pentaho-osgi-bundles    文件:PentahoCacheManagerFactoryTest.java   
@Test
public void testListeners() throws Exception {
  Future<PentahoCacheProvidingService> serviceFuture;

  factory.init();
  verify( bundleContext ).addServiceListener( serviceListenerCaptor.capture(), argThat( matchesFilter ) );

  serviceFuture = factory.getProviderService( MOCK_PID );
  assertThat( serviceFuture.isDone(), is( true ) );
  assertThat( serviceFuture.get( 1, TimeUnit.SECONDS ), is( providingService ) );

  ServiceListener serviceListener = serviceListenerCaptor.getValue();

  serviceListener.serviceChanged( new ServiceEvent( ServiceEvent.UNREGISTERING, serviceReference ) );

  serviceFuture = factory.getProviderService( MOCK_PID );
  assertThat( serviceFuture.isDone(), is( false ) );

  serviceListener.serviceChanged( new ServiceEvent( ServiceEvent.REGISTERED, serviceReference ) );

  serviceFuture = factory.getProviderService( MOCK_PID );
  assertThat( serviceFuture.isDone(), is( true ) );
  assertThat( serviceFuture.get( 1, TimeUnit.SECONDS ), is( providingService ) );
}
项目:mhus-osgi-tools    文件:BundleWatchImpl.java   
@Override
public void serviceChanged(ServiceEvent event) {

    if (!enabled) return;

    try {
        if (event.getType() != ServiceEvent.REGISTERED) return;
        Object service = context.getService(event.getServiceReference());
        if (service == null) return;
        if (!(service instanceof VaadinConfigurableResourceProviderAdmin)) return;

        log.info("Admin-Service registered");
        refreshAll();
    } catch (Throwable t) {
        t.printStackTrace(); //TODO log
    }
}
项目:openeos    文件:LiquibaseBundleTracker.java   
private void registerForServiceNotAvailable(final Bundle bundle, final String filterDataSource) throws InvalidSyntaxException {
    //serviceHideManager.hideService(filterDataSource);
    context.addServiceListener(new ServiceListener() {

        @Override
        public void serviceChanged(ServiceEvent event) {
            if (event.getType() == ServiceEvent.REGISTERED) {

                try {
                    checkAndRunLiquibase(bundle);
                } catch (Exception e) {
                    LOG.error(
                            MessageFormat.format("An error occured while processing bundle {0} for liquibase extender.",
                                    bundle.getSymbolicName()), e);
                }
                context.removeServiceListener(this);
                //serviceHideManager.showService(filterDataSource);
            }
        }
    }, filterDataSource);
}
项目:tigase-server    文件:Activator.java   
@Override
public void serviceChanged(ServiceEvent event) {
    if (event.getType() == ServiceEvent.REGISTERED) {
        if (serviceReference == null) {
            serviceReference = event.getServiceReference();
            serviceManager = (ModulesManager) context.getService(serviceReference);
            registerAddons();
        }
    } else if (event.getType() == ServiceEvent.UNREGISTERING) {
        if (serviceReference == event.getServiceReference()) {
            unregisterAddons();
            context.ungetService(serviceReference);
            serviceManager = null;
            serviceReference = null;
        }
    }
}
项目:IoT-Gateway    文件:EventManager.java   
/**
 * Handle notifications of new/removed GenericDeviceEventListeners
 *
 * @param event
 */
@Override
public void serviceChanged(ServiceEvent event) {
    ServiceReference reference = event.getServiceReference();
    GDEventListener listener = (GDEventListener) context.getService(reference);
    switch (event.getType()) {
        case ServiceEvent.REGISTERED:
            Object filter = reference.getProperty(GENERICDEVICE_FILTER);
            listeners.put(listener, getFilter(filter));
            break;
        case ServiceEvent.MODIFIED:
            break;
        case ServiceEvent.UNREGISTERING:
            listeners.remove(listener);
            break;
    }
}
项目:Ptoceti    文件:Activator.java   
/**
 * Called by the framework for initialisation when the Activator class is loaded.
 * The method first get a service reference on the osgi logging service, used for
 * logging whithin the bundle.
 *
 * If the method cannot get a reference to the logging service, a NullPointerException is thrown.
 * @param context
 * @throws BundleException
 */
public void start(BundleContext context) throws Exception {
    // TODO Auto-generated method stub
    Activator.bc = context;

    // we construct a listener to detect if the log service appear or disapear.
    String filter = "(objectclass=" + logServiceName + ")";
    ServiceListener logServiceListener = new LogServiceListener();
    try {
        bc.addServiceListener( logServiceListener, filter);
        // in case the service is already registered, we send a REGISTERED event to its listener.
        ServiceReference srLog = bc.getServiceReference( logServiceName );
        if( srLog != null ) {
            logServiceListener.serviceChanged(new ServiceEvent( ServiceEvent.REGISTERED, srLog ));
        }
    } catch ( InvalidSyntaxException e ) {
        throw new BundleException("Error in filter string while registering LogServiceListener." + e.toString());
    }

    piService = new PiService();
}
项目:Ptoceti    文件:Activator.java   
public void serviceChanged(ServiceEvent event) {
    ServiceReference sr = event.getServiceReference();
    switch (event.getType()) {
    case ServiceEvent.REGISTERED: {
        httpHandler.setHttpService((HttpService) Activator.bc.getService(sr));
        Activator.log(LogService.LOG_INFO,
                "Getting instance of service: "
                        + HttpService.class.getName() + "," + Constants.SERVICE_PID + "=" + (String) sr.getProperty(Constants.SERVICE_PID)
                        + " from " + sr.getBundle().getSymbolicName());
    }
        break;
    case ServiceEvent.UNREGISTERING: {
        Activator.log(LogService.LOG_INFO, "Releasing service: "
                + HttpService.class.getName() + ","
                + Constants.SERVICE_PID + "="
                + (String) sr.getProperty(Constants.SERVICE_PID));

        // httpService.unregister(obixServletPath);
        httpHandler.setHttpService(null);
    }
        break;
    }
}
项目:Ptoceti    文件:SensorNode.java   
/**
 * Unique method of the ServiceListener interface. The framework invoke this method when
 * a event has been posted. Since we registered this listener for registered and unregistered
 * event from the sensor node driver service, we will receive notification of theses here.
 *
 */
public void serviceChanged(ServiceEvent event) {
    ServiceReference sr = event.getServiceReference();
    switch(event.getType()) {
        case ServiceEvent.REGISTERED: {
            sensorNodeDriver = (SensorNodeDriver) Activator.bc.getService(sr);
            poolThread = new Thread(this);
            poolThread.start();
            Activator.log( LogService.LOG_INFO, "Getting instance of service: " + (String) sr.getProperty( Constants.SERVICE_PID)
                + ", " + Constants.SERVICE_ID + "=" + ((Long)sr.getProperty(Constants.SERVICE_ID)).toString() );
        }
        break;
        case ServiceEvent.UNREGISTERING: {
            Activator.log( LogService.LOG_INFO, "Releasing instance of service: " + (String) sr.getProperty( Constants.SERVICE_PID)
                + ", " + Constants.SERVICE_ID + "=" + ((Long)sr.getProperty(Constants.SERVICE_ID)).toString());
            sensorNodeDriver = null;
            poolThread.interrupt();
        }
        break;
    }

}
项目:Ptoceti    文件:ModbusDeviceImpl.java   
public ModbusDriverCommunicator( String modbusPort, int modbusId, int  modbusPoolingRateS ) {
    this.id = modbusId;
    this.portName = modbusPort;
    this.poolingTimeMill = ((long)modbusPoolingRateS);

    // We first need to get a reference to the modbus driver service. We try to get this reference dynamically
    // by constructing a listener that will detect when the modbus driver service appear or disapear.
    String filter = "(&(objectclass=" + modbusDriverServiceName + ")"
        + "(" + ModbusDriver.MODBUS_DRIVER_SERVICE_PORT + "=" + modbusPort + "))";

    try {
        Activator.bc.addServiceListener( this, filter);
        // in case the service is already registered, we send a REGISTERED event to its listener.
        ServiceReference srModbus[] = Activator.bc.getServiceReferences( modbusDriverServiceName, filter );
        if( srModbus != null ) {
            this.serviceChanged(new ServiceEvent( ServiceEvent.REGISTERED, srModbus[0] ));
        }
    } catch ( InvalidSyntaxException e ) {
        // We known there shouldn't be an exception thrown here since we made the filter string.
    }
}
项目:Ptoceti    文件:ModbusDeviceImpl.java   
/**
 * Unique method of the ServiceListener interface. The framework invoke this method when
 * a event has been posted. Since we registered this listener for registered and unregistered
 * event from the modbus driver service, we will receive notification of theses here.
 *
 */
public void serviceChanged( ServiceEvent event ) {

        ServiceReference sr = event.getServiceReference();
        switch(event.getType()) {
            case ServiceEvent.REGISTERED: {
                modbusDr = (ModbusDriver) Activator.bc.getService(sr);
                myThread = new Thread(this);
                myThread.start();
                Activator.log( LogService.LOG_INFO, "Getting instance of service: " + (String) sr.getProperty( Constants.SERVICE_PID)
                    + ", " + Constants.SERVICE_ID + "=" + ((Long)sr.getProperty(Constants.SERVICE_ID)).toString() );
            }
            break;
            case ServiceEvent.UNREGISTERING: {
                Activator.log( LogService.LOG_INFO, "Releasing instance of service: " + (String) sr.getProperty( Constants.SERVICE_PID)
                    + ", " + Constants.SERVICE_ID + "=" + ((Long)sr.getProperty(Constants.SERVICE_ID)).toString());
                modbusDr = null;
                myThread.interrupt();
            }
            break;
        }
}
项目:Ptoceti    文件:WireAdminImpl.java   
/**
 * Receives notification that a service has had a life cycle change.
 *
 * We are interested in receiving notification of WireAdminListener services that register
 * an unregister to and from the framework. This allows us to build dynamically a list of
 * services to which to send WireAdminEvent(s) as they are produces by thi bundle.
 *
 * @param event The ServiceEvent describing the type of life cycle change.
 */
public void serviceChanged( ServiceEvent event ) {

    ServiceReference sr = event.getServiceReference();
    switch(event.getType()) {
        case ServiceEvent.REGISTERED: {
                synchronized( wireAdminEventListeners ){
                    if( !wireAdminEventListeners.containsKey( sr)) {
                        wireAdminEventListeners.put( sr,(WireAdminListener) Activator.bc.getService( sr ));
                    }
                }
            }
            break;
        case ServiceEvent.UNREGISTERING: {
                synchronized( wireAdminEventListeners ){
                    if( wireAdminEventListeners.containsKey( sr )) {
                        Activator.bc.ungetService( sr );
                        wireAdminEventListeners.remove( sr );
                    }
                }
            }
            break;
        }
}
项目:Ptoceti    文件:Activator.java   
/**
    * Called by the framework for initialisation when the Activator class is
    * loaded. The method first get a service reference on the osgi logging
    * service, used for logging whithin the bundle.
    * 
    * If the method cannot get a reference to the logging service, a
    * NullPointerException is thrown.
    * 
    * @param context
    * @throws BundleException
    */
   public void start(BundleContext context) throws BundleException {

Activator.bc = context;

// we construct a listener to detect if the log service appear or
// disapear.
String logFilter = "(objectclass=" + logServiceName + ")";
ServiceListener logServiceListener = new LogServiceListener();
try {
    bc.addServiceListener(logServiceListener, logFilter);
    // in case the service is already registered, we send a REGISTERED
    // event to its listener.
    ServiceReference srLog = bc.getServiceReference(logServiceName);
    if (srLog != null) {
    logServiceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, srLog));
    }
} catch (InvalidSyntaxException e) {
    throw new BundleException("Error in filter string while registering LogServiceListener." + e.toString());
}

log(LogService.LOG_INFO, "Starting version " + bc.getBundle().getHeaders().get("Bundle-Version"));

influxDbFactory.start();
   }
项目:Ptoceti    文件:Activator.java   
/**
 * Unique method of the ServiceListener interface.
 * 
 */
public void serviceChanged(ServiceEvent event) {

    ServiceReference sr = event.getServiceReference();
    switch (event.getType()) {
    case ServiceEvent.REGISTERED: {
    logSer = (LogService) bc.getService(sr);

    }
    break;
    case ServiceEvent.UNREGISTERING: {
    logSer = null;
    }
    break;
    }
}
项目:Ptoceti    文件:ObixServiceImpl.java   
public void serviceChanged(ServiceEvent event) {
    ServiceReference sr = event.getServiceReference();
    switch (event.getType()) {
    case ServiceEvent.REGISTERED: {
    ObixDataHandler.getInstance().setDataDevice((JdbcDevice) Activator.bc.getService(sr));
    Activator.log(LogService.LOG_INFO, "Getting instance of service: " + JdbcDevice.class.getName() + ","
        + Constants.SERVICE_PID + "=" + (String) sr.getProperty(Constants.SERVICE_PID));
    startDatabase();
    }
    break;
    case ServiceEvent.UNREGISTERING: {
    Activator.log(LogService.LOG_INFO, "Releasing service: " + JdbcDevice.class.getName() + ","
        + Constants.SERVICE_PID + "=" + (String) sr.getProperty(Constants.SERVICE_PID));

    // httpService.unregister(obixServletPath);
    ObixDataHandler.getInstance().setDataDevice(null);
    databaseInitialised = false;
    }
    break;
    }
}
项目:Ptoceti    文件:ObixServiceImpl.java   
public void serviceChanged(ServiceEvent event) {
    ServiceReference sr = event.getServiceReference();
    switch (event.getType()) {
    case ServiceEvent.REGISTERED: {
    ObixTimeSeriesHandler.getInstance().setTimeSeriesService(
        (TimeSeriesService) Activator.bc.getService(sr));
    Activator.log(LogService.LOG_INFO, "Getting instance of service: " + TimeSeriesService.class.getName()
        + "," + Constants.SERVICE_PID + "=" + (String) sr.getProperty(Constants.SERVICE_PID));
    }
    break;
    case ServiceEvent.UNREGISTERING: {
    Activator.log(LogService.LOG_INFO, "Releasing service: " + TimeSeriesService.class.getName() + ","
        + Constants.SERVICE_PID + "=" + (String) sr.getProperty(Constants.SERVICE_PID));

    // httpService.unregister(obixServletPath);
    ObixTimeSeriesHandler.getInstance().setTimeSeriesService(null);
    }
    break;
    }
}
项目:Ptoceti    文件:Activator.java   
/**
 * Unique method of the ServiceListener interface.
 * 
 * @param event the service event
 *
 */
public void serviceChanged( final ServiceEvent event ) {

        ServiceReference sr = event.getServiceReference();
        switch(event.getType()) {
            case ServiceEvent.REGISTERED: {
                logSer = (LogService) bc.getService(sr);
                registerService();
            }
            break;
            case ServiceEvent.UNREGISTERING: {
                logSer = null;
            }
            break;
        }
}
项目:Ptoceti    文件:MqttClientWrapper.java   
@Override
public void serviceChanged(ServiceEvent event) {
    ServiceReference sr = event.getServiceReference();
    switch(event.getType()) {
        case ServiceEvent.REGISTERED: {
            mqttMessageFormatter = (IMqttMessageFomatter) Activator.bc.getService(sr);
            Activator.log( LogService.LOG_INFO, "Getting instance of service: " + (String) sr.getProperty( Constants.SERVICE_PID)
                + ", " + Constants.SERVICE_ID + "=" + ((Long)sr.getProperty(Constants.SERVICE_ID)).toString() );
            start();
        }
        break;
        case ServiceEvent.UNREGISTERING: {
            Activator.log( LogService.LOG_INFO, "Releasing instance of service: " + (String) sr.getProperty( Constants.SERVICE_PID)
                + ", " + Constants.SERVICE_ID + "=" + ((Long)sr.getProperty(Constants.SERVICE_ID)).toString());
            mqttMessageFormatter = null;
            stop();
        }
        break;
    }

}
项目:Ptoceti    文件:Activator.java   
/**
 * Unique method of the ServiceListener interface.
 *
 */
public void serviceChanged( ServiceEvent event ) {

        ServiceReference sr = event.getServiceReference();
        switch(event.getType()) {
            case ServiceEvent.REGISTERED: {
                logSer = (LogService) bc.getService(sr);
                if( getDataSourceFactory() != null && logSer != null ){
                    registerService();
                }
            }
            break;
            case ServiceEvent.UNREGISTERING: {
                logSer = null;
            }
            break;
        }
}
项目:Ptoceti    文件:Activator.java   
@Override
public void serviceChanged(ServiceEvent event) {
    ServiceReference sr = event.getServiceReference();
    switch(event.getType()) {
        case ServiceEvent.REGISTERED: {
            dataSourceFactory = ((DataSourceFactory) bc.getService(sr));
            if( getDataSourceFactory() != null && logSer != null ){
                dataSourceDriverName = (String)sr.getProperty(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS);
                registerService();
            }
        }
        break;
        case ServiceEvent.UNREGISTERING: {
            dataSourceFactory = null;
            dataSourceDriverName = null;
        }
        break;
    }
}
项目:opennmszh    文件:OnmsOSGiBridgeActivator.java   
@Override
public void serviceChanged(final ServiceEvent serviceEvent) {
    switch(serviceEvent.getType()) {
        case ServiceEvent.REGISTERED:
            registerWithOnmsRegistry(serviceEvent.getServiceReference());
            break;
        case ServiceEvent.MODIFIED:
            registerWithOnmsRegistry(serviceEvent.getServiceReference());
            break;
        case ServiceEvent.MODIFIED_ENDMATCH:
            unregisterWithOnmsRegistry(serviceEvent.getServiceReference());
            break;
        case ServiceEvent.UNREGISTERING:
            unregisterWithOnmsRegistry(serviceEvent.getServiceReference());
            break;
    }
}