Search in sources :

Example 1 with RemoteServiceAdminListener

use of org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener in project ecf by eclipse.

the class AbstractRemoteServiceRegisterTest method testRemoteServiceAdminListener.

public void testRemoteServiceAdminListener() throws Exception {
    RemoteServiceAdminListener listener = createRemoteServiceAdminListener();
    ServiceRegistration listenerReg = getContext().registerService(RemoteServiceAdminListener.class.getName(), listener, null);
    Properties props = getServiceProperties();
    registration = registerDefaultService(props);
    // Wait a while
    Thread.sleep(REGISTER_WAIT);
    assertTrue(remoteServiceAdminEvents.size() > 0);
    assertTrue(containsEventType(RemoteServiceAdminEvent.EXPORT_REGISTRATION));
    // Now bring down
    registration.unregister();
    registration = null;
    // Wait a while
    Thread.sleep(REGISTER_WAIT);
    assertTrue(remoteServiceAdminEvents.size() > 2);
    assertTrue(containsEventType(RemoteServiceAdminEvent.EXPORT_UNREGISTRATION));
    // finally unregister the listenerReg
    listenerReg.unregister();
}
Also used : RemoteServiceAdminListener(org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener) Properties(java.util.Properties) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with RemoteServiceAdminListener

use of org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener in project ecf by eclipse.

the class AbstractRemoteServiceRegisterTest method testRegisterAndUpdateProperties.

public void testRegisterAndUpdateProperties() throws Exception {
    remoteServiceAdminEvents.clear();
    // register RemoteServiceAdminListener
    ServiceRegistration listenerReg = getContext().registerService(RemoteServiceAdminListener.class.getName(), createRemoteServiceAdminListener(), null);
    // Create server container
    this.server = ContainerFactory.getDefault().createContainer(getServerContainerTypeName(), new Object[] { createServerID() });
    // Create props
    Properties props = getServiceProperties();
    // Put "1" for "testonekey"
    props.put("testonekey", "1");
    // Actually register with default service (IConcatService) as a remote service
    registration = registerDefaultService(props);
    ExportReference exportRef = null;
    Thread.sleep(15000);
    for (RemoteServiceAdminEvent e : remoteServiceAdminEvents) if (e.getType() == RemoteServiceAdminEvent.EXPORT_REGISTRATION)
        exportRef = e.getExportReference();
    assertTrue(exportRef != null);
    // remoteServiceAdminEvents should have the Export registration
    assertTrue(remoteServiceAdminEvents.size() > 0);
    // Get ExportReference
    EndpointDescription oldED = exportRef.getExportedEndpoint();
    assertNotNull(oldED);
    Map<String, ?> oldEDProperties = oldED.getProperties();
    assertNotNull(oldEDProperties);
    assertTrue("1".equals(oldEDProperties.get("testonekey")));
    assertTrue(oldEDProperties.get("testtwokey") == null);
    // Change testonekey value to "two" and set a new property "testtwokey" to "2"
    props.put("testonekey", "two");
    props.put("testtwokey", "2");
    // Set/update the properties on the registration
    this.registration.setProperties(props);
    // Now get new EndpointDescription and test that new properties have been changed in EndpointDescription
    EndpointDescription updatedED = exportRef.getExportedEndpoint();
    assertNotNull(updatedED);
    Map<String, ?> updatedEDProperties = updatedED.getProperties();
    assertNotNull(updatedEDProperties);
    assertTrue("two".equals(updatedEDProperties.get("testonekey")));
    assertTrue("2".equals(updatedEDProperties.get("testtwokey")));
    Thread.sleep(15000);
    listenerReg.unregister();
}
Also used : RemoteServiceAdminListener(org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener) ExportReference(org.osgi.service.remoteserviceadmin.ExportReference) EndpointDescription(org.osgi.service.remoteserviceadmin.EndpointDescription) Properties(java.util.Properties) RemoteServiceAdminEvent(org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 3 with RemoteServiceAdminListener

use of org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener in project ecf by eclipse.

the class RemoteServiceAdmin method getListeners.

private RemoteServiceAdminListener[] getListeners(EndpointPermission perm) {
    AccessController.doPrivileged(new PrivilegedAction<Object>() {

        public Object run() {
            synchronized (remoteServiceAdminListenerTrackerLock) {
                if (remoteServiceAdminListenerTracker == null) {
                    remoteServiceAdminListenerTracker = new ServiceTracker(getRSABundleContext(), RemoteServiceAdminListener.class.getName(), null);
                    remoteServiceAdminListenerTracker.open();
                }
                return null;
            }
        }
    });
    ServiceReference[] unfilteredRefs = remoteServiceAdminListenerTracker.getServiceReferences();
    if (unfilteredRefs == null)
        return null;
    // Filter by Bundle.hasPermission
    List<ServiceReference> filteredRefs = new ArrayList<ServiceReference>();
    for (ServiceReference ref : unfilteredRefs) if (perm == null || ref.getBundle().hasPermission(perm))
        filteredRefs.add(ref);
    List<RemoteServiceAdminListener> results = new ArrayList<RemoteServiceAdminListener>();
    for (final ServiceReference ref : filteredRefs) {
        RemoteServiceAdminListener l = AccessController.doPrivileged(new PrivilegedAction<RemoteServiceAdminListener>() {

            public RemoteServiceAdminListener run() {
                return (RemoteServiceAdminListener) remoteServiceAdminListenerTracker.getService(ref);
            }
        });
        if (l != null)
            results.add(l);
    }
    return results.toArray(new RemoteServiceAdminListener[results.size()]);
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) RemoteServiceAdminListener(org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) IRemoteServiceReference(org.eclipse.ecf.remoteservice.IRemoteServiceReference)

Example 4 with RemoteServiceAdminListener

use of org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener in project ecf by eclipse.

the class RemoteServiceAdmin method publishEvent.

private void publishEvent(final RemoteServiceAdminEvent event, EndpointDescription endpointDescription) {
    // send event synchronously to RemoteServiceAdminListeners
    EndpointPermission perm = new EndpointPermission(endpointDescription, Activator.getDefault().getFrameworkUUID(), EndpointPermission.READ);
    // notify synchronously all appropriate listeners (those with READ
    // permission)
    final RemoteServiceAdminListener[] listeners = getListeners(perm);
    if (listeners != null)
        for (int i = 0; i < listeners.length; i++) {
            final RemoteServiceAdminListener listener = listeners[i];
            SafeRunner.run(new ISafeRunnable() {

                public void handleException(Throwable exception) {
                    logError(// $NON-NLS-1$
                    "publishEvent", // $NON-NLS-1$
                    "Exeption in RemoteServiceAdminListener.remoteAdminEvent for listener=" + listener, exception);
                }

                public void run() throws Exception {
                    listener.remoteAdminEvent(event);
                }
            });
        }
    // Now also post the event asynchronously to EventAdmin
    postEvent(event, endpointDescription);
}
Also used : RemoteServiceAdminListener(org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) EndpointPermission(org.osgi.service.remoteserviceadmin.EndpointPermission)

Aggregations

RemoteServiceAdminListener (org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener)4 Properties (java.util.Properties)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ArrayList (java.util.ArrayList)1 ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)1 IRemoteServiceReference (org.eclipse.ecf.remoteservice.IRemoteServiceReference)1 ServiceReference (org.osgi.framework.ServiceReference)1 EndpointDescription (org.osgi.service.remoteserviceadmin.EndpointDescription)1 EndpointPermission (org.osgi.service.remoteserviceadmin.EndpointPermission)1 ExportReference (org.osgi.service.remoteserviceadmin.ExportReference)1 RemoteServiceAdminEvent (org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1