當前位置: 首頁>>代碼示例>>Java>>正文


Java ServiceListener類代碼示例

本文整理匯總了Java中org.osgi.framework.ServiceListener的典型用法代碼示例。如果您正苦於以下問題:Java ServiceListener類的具體用法?Java ServiceListener怎麽用?Java ServiceListener使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ServiceListener類屬於org.osgi.framework包,在下文中一共展示了ServiceListener類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testRemoveServiceListener

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
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());
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:22,代碼來源:MockBundleContextTest.java

示例2: registerService

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
@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;
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:26,代碼來源:ListListenerAdapterTest.java

示例3: testServiceReferenceMemberType

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
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());
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:21,代碼來源:OsgiServiceCollectionProxyFactoryBeanTest.java

示例4: testUnbind

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
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);
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:18,代碼來源:OsgiServiceDynamicInterceptorListenerTest.java

示例5: testStickinessWhenABetterServiceIsAvailable

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
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);
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:22,代碼來源:OsgiServiceDynamicInterceptorListenerTest.java

示例6: testStickinessWhenServiceGoesDown

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
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());
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:27,代碼來源:OsgiServiceDynamicInterceptorListenerTest.java

示例7: testAddServiceListenerBundleContextServiceListenerString

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
/**
 * 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));
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:24,代碼來源:OsgiListenerUtilsTest.java

示例8: testAddSingleServiceListenerBundleContextServiceListenerString

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
/**
 * 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));
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:22,代碼來源:OsgiListenerUtilsTest.java

示例9: RemoteServiceAdminCore

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
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
    }
}
 
開發者ID:apache,項目名稱:aries-rsa,代碼行數:21,代碼來源:RemoteServiceAdminCore.java

示例10: testListeners

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
@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 ) );
}
 
開發者ID:pentaho,項目名稱:pentaho-osgi-bundles,代碼行數:25,代碼來源:PentahoCacheManagerFactoryTest.java

示例11: registerForServiceNotAvailable

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
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);
}
 
開發者ID:frincon,項目名稱:openeos,代碼行數:22,代碼來源:LiquibaseBundleTracker.java

示例12: setup

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
@Before
public void setup() throws Exception {
    serviceReference = context.mock(ServiceReference.class, "serviceReference");
    bundleContext = context.mock(BundleContext.class);
    listener = context.mock(GDEventListener.class);
    eventManager = new EventManager();
    eventManager.setContext(bundleContext);

    timer = new Timer();
    timer.schedule(new ShutdownTask(), 3000);

    context.checking(new Expectations() {
        {
            //oneOf(bundleContext).createFilter(with(aNonNull(String.class)));
            allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(aNonNull(String.class)));
            allowing(bundleContext).removeServiceListener(with(any(ServiceListener.class)));
            allowing(bundleContext).getServiceReferences(with(any(Class.class)), with(any(String.class)));
            allowing(bundleContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
        }
    });

    registerDevice("zwave32");
    registerDevice("zwave31");
    registerDevice("dev");
}
 
開發者ID:Ericsson-LMF,項目名稱:IoT-Gateway,代碼行數:26,代碼來源:EventManagerTest.java

示例13: start

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
/**
 * 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();
}
 
開發者ID:lathil,項目名稱:Ptoceti,代碼行數:30,代碼來源:Activator.java

示例14: start

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
/**
    * 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();
   }
 
開發者ID:lathil,項目名稱:Ptoceti,代碼行數:36,代碼來源:Activator.java

示例15: testMandatoryServiceUnAvailableWhileWorking

import org.osgi.framework.ServiceListener; //導入依賴的package包/類
public void testMandatoryServiceUnAvailableWhileWorking() {
	serviceFactoryBean.setInterfaces(new Class[] { Serializable.class });
	serviceFactoryBean.afterPropertiesSet();

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

	assertFalse(col.isEmpty());
	Set listeners = bundleContext.getServiceListeners();

	ServiceListener list = (ServiceListener) listeners.iterator().next();
	list.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref));

	try {
		// disable filter
		col.isEmpty();
		fail("should have thrown exception");
	}
	catch (ServiceUnavailableException ex) {
		// expected
	}
}
 
開發者ID:BeamFoundry,項目名稱:spring-osgi,代碼行數:22,代碼來源:OsgiServiceCollectionProxyFactoryBeanTest.java


注:本文中的org.osgi.framework.ServiceListener類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。