Search in sources :

Example 1 with NetworkChannelFactory

use of ch.ethz.iks.r_osgi.channels.NetworkChannelFactory in project ecf by eclipse.

the class RemoteOSGiServiceImpl method getNetworkChannelFactory.

private NetworkChannelFactory getNetworkChannelFactory(final String protocol) throws RemoteOSGiException {
    try {
        final Filter filter = RemoteOSGiActivator.getActivator().getContext().createFilter(// $NON-NLS-1$
        "(" + NetworkChannelFactory.PROTOCOL_PROPERTY + "=" + // $NON-NLS-1$
        protocol + // $NON-NLS-1$
        ")");
        final ServiceReference[] refs = networkChannelFactoryTracker.getServiceReferences();
        if (refs != null) {
            for (int i = 0; i < refs.length; i++) {
                if (filter.match(refs[i])) {
                    return (NetworkChannelFactory) networkChannelFactoryTracker.getService(refs[i]);
                }
            }
        }
        throw new RemoteOSGiException(// $NON-NLS-1$
        "No NetworkChannelFactory for " + protocol + // $NON-NLS-1$
        " found.");
    } catch (final InvalidSyntaxException e) {
        // does not happen
        e.printStackTrace();
        return null;
    }
}
Also used : NetworkChannelFactory(ch.ethz.iks.r_osgi.channels.NetworkChannelFactory) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint) ServiceReference(org.osgi.framework.ServiceReference) RemoteServiceReference(ch.ethz.iks.r_osgi.RemoteServiceReference)

Example 2 with NetworkChannelFactory

use of ch.ethz.iks.r_osgi.channels.NetworkChannelFactory in project ecf by eclipse.

the class RemoteOSGiServiceImpl method connect.

/**
 * connect to a remote OSGi host.
 *
 * @param uri
 *            the uri of the remote OSGi peer.
 * @return the array of service urls of services offered by the remote peer.
 * @throws RemoteOSGiException
 *             in case of errors.
 * @throws IOException
 *             in case of IO problems.
 * @since 0.6
 */
public RemoteServiceReference[] connect(final URI uri) throws RemoteOSGiException, IOException {
    final URI endpoint = URI.create(getChannelURI(uri));
    final ChannelEndpointImpl test = (ChannelEndpointImpl) channels.get(endpoint.toString());
    if (test != null) {
        test.usageCounter++;
        return test.getAllRemoteReferences(null);
    }
    final ChannelEndpointImpl channel;
    final String protocol = endpoint.getScheme();
    final NetworkChannelFactory factory = getNetworkChannelFactory(protocol);
    channel = new ChannelEndpointImpl(factory, endpoint);
    return channel.sendLease(getServices(endpoint.getScheme()), getTopics());
}
Also used : NetworkChannelFactory(ch.ethz.iks.r_osgi.channels.NetworkChannelFactory) URI(ch.ethz.iks.r_osgi.URI)

Example 3 with NetworkChannelFactory

use of ch.ethz.iks.r_osgi.channels.NetworkChannelFactory in project ecf by eclipse.

the class RemoteOSGiServiceImpl method cleanup.

/**
 * the method is called when the R-OSGi bundle is about to be stopped.
 * removes all registered proxy bundles.
 */
void cleanup() {
    final ChannelEndpoint[] c = (ChannelEndpoint[]) channels.values().toArray(new ChannelEndpoint[channels.size()]);
    channels.clear();
    for (int i = 0; i < c.length; i++) {
        c[i].dispose();
    }
    final Object[] factories = networkChannelFactoryTracker.getServices();
    if (factories != null) {
        for (int i = 0; i < factories.length; i++) {
            try {
                ((NetworkChannelFactory) factories[i]).deactivate(this);
            } catch (final IOException ioe) {
                if (log != null) {
                    log.log(LogService.LOG_ERROR, ioe.getMessage(), ioe);
                }
            }
        }
    }
    eventAdminTracker.close();
    remoteServiceTracker.close();
    serviceDiscoveryHandlerTracker.close();
    remoteServiceListenerTracker.close();
    networkChannelFactoryTracker.close();
}
Also used : NetworkChannelFactory(ch.ethz.iks.r_osgi.channels.NetworkChannelFactory) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint) IOException(java.io.IOException) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint)

Example 4 with NetworkChannelFactory

use of ch.ethz.iks.r_osgi.channels.NetworkChannelFactory in project ecf by eclipse.

the class RemoteOSGiServiceImpl method setupTrackers.

private void setupTrackers(final BundleContext context) throws IOException {
    // initialize service trackers
    eventAdminTracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
    eventAdminTracker.open();
    if (eventAdminTracker.getTrackingCount() == 0 && log != null) {
        log.log(LogService.LOG_WARNING, // $NON-NLS-1$
        "NO EVENT ADMIN FOUND. REMOTE EVENT DELIVERY TEMPORARILY DISABLED.");
    }
    try {
        eventHandlerTracker = new ServiceTracker(context, context.createFilter(// $NON-NLS-1$ //$NON-NLS-2$
        "(&(" + Constants.OBJECTCLASS + "=" + EventHandler.class.getName() + // $NON-NLS-1$
        ")(|(!(" + R_OSGi_INTERNAL + // $NON-NLS-1$
        "=*))" + // https://bugs.eclipse.org/418740
        "(!(" + // $NON-NLS-1$
        EventConstants.EVENT_TOPIC + // $NON-NLS-1$
        "=org/osgi/service/remoteserviceadmin/*))))"), new ServiceTrackerCustomizer() {

            public Object addingService(final ServiceReference reference) {
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=326033
                Collection theTopics;
                Object topic = reference.getProperty(EventConstants.EVENT_TOPIC);
                if (topic instanceof String)
                    theTopics = Arrays.asList(new String[] { (String) topic });
                else
                    theTopics = Arrays.asList((String[]) topic);
                // Remove filtered topics
                theTopics = StringUtils.rightDifference(topicFilters, theTopics);
                final LeaseUpdateMessage lu = new LeaseUpdateMessage();
                lu.setType(LeaseUpdateMessage.TOPIC_UPDATE);
                // $NON-NLS-1$
                lu.setServiceID("");
                lu.setPayload(new Object[] { theTopics.toArray(new String[theTopics.size()]), null });
                updateLeases(lu);
                return theTopics;
            }

            public void modifiedService(final ServiceReference reference, final Object oldTopics) {
                final List oldTopicList = (List) oldTopics;
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=326033
                Collection newTopicList;
                Object topic = reference.getProperty(EventConstants.EVENT_TOPIC);
                if (topic instanceof String)
                    newTopicList = Arrays.asList(new String[] { (String) topic });
                else
                    newTopicList = Arrays.asList((String[]) topic);
                // Remove filtered topics
                newTopicList = StringUtils.rightDifference(topicFilters, newTopicList);
                final Collection removed = CollectionUtils.rightDifference(newTopicList, oldTopicList);
                final Collection added = CollectionUtils.leftDifference(newTopicList, oldTopicList);
                final String[] addedTopics = (String[]) added.toArray(new String[removed.size()]);
                final String[] removedTopics = (String[]) removed.toArray(addedTopics);
                oldTopicList.removeAll(removed);
                oldTopicList.addAll(added);
                final LeaseUpdateMessage lu = new LeaseUpdateMessage();
                lu.setType(LeaseUpdateMessage.TOPIC_UPDATE);
                // $NON-NLS-1$
                lu.setServiceID("");
                lu.setPayload(new Object[] { addedTopics, removedTopics });
                updateLeases(lu);
            }

            public void removedService(final ServiceReference reference, final Object oldTopics) {
                final List oldTopicsList = (List) oldTopics;
                final String[] removedTopics = (String[]) oldTopicsList.toArray(new String[oldTopicsList.size()]);
                final LeaseUpdateMessage lu = new LeaseUpdateMessage();
                lu.setType(LeaseUpdateMessage.TOPIC_UPDATE);
                // $NON-NLS-1$
                lu.setServiceID("");
                lu.setPayload(new Object[] { null, removedTopics });
                updateLeases(lu);
            }
        });
        eventHandlerTracker.open();
        if (DEBUG) {
            log.log(LogService.LOG_DEBUG, // $NON-NLS-1$
            "Local topic space " + Arrays.asList(getTopics()));
        }
        remoteServiceListenerTracker = new ServiceTracker(context, RemoteServiceListener.class.getName(), null);
        remoteServiceListenerTracker.open();
        serviceDiscoveryHandlerTracker = new ServiceTracker(context, ServiceDiscoveryHandler.class.getName(), new ServiceTrackerCustomizer() {

            public Object addingService(final ServiceReference reference) {
                // register all known services for discovery
                final ServiceDiscoveryHandler handler = (ServiceDiscoveryHandler) context.getService(reference);
                RemoteServiceRegistration[] regs = null;
                synchronized (serviceRegistrations) {
                    regs = (RemoteServiceRegistration[]) serviceRegistrations.values().toArray(new RemoteServiceRegistration[serviceRegistrations.size()]);
                }
                for (int i = 0; i < regs.length; i++) {
                    handler.registerService(regs[i].getReference(), regs[i].getProperties(), URI.create(// $NON-NLS-1$
                    "r-osgi://" + RemoteOSGiServiceImpl.MY_ADDRESS + // $NON-NLS-1$
                    ":" + RemoteOSGiServiceImpl.R_OSGI_PORT + // $NON-NLS-1$
                    "#" + regs[i].getServiceID()));
                }
                return handler;
            }

            public void modifiedService(final ServiceReference reference, final Object service) {
            }

            public void removedService(final ServiceReference reference, final Object service) {
            }
        });
        serviceDiscoveryHandlerTracker.open();
        remoteServiceTracker = new ServiceTracker(context, context.createFilter(// $NON-NLS-1$
        "(" + RemoteOSGiService.R_OSGi_REGISTRATION + "=*)"), new // $NON-NLS-1$
        ServiceTrackerCustomizer() {

            public Object addingService(final ServiceReference reference) {
                // FIXME: Surrogates have to be monitored
                // separately!!!
                final ServiceReference service = Arrays.asList((String[]) reference.getProperty(Constants.OBJECTCLASS)).contains(SurrogateRegistration.class.getName()) ? (ServiceReference) reference.getProperty(SurrogateRegistration.SERVICE_REFERENCE) : reference;
                try {
                    final RemoteServiceRegistration reg = new RemoteServiceRegistration(reference, service);
                    if (log != null) {
                        log.log(LogService.LOG_INFO, // $NON-NLS-1$
                        "REGISTERING " + reference + // $NON-NLS-1$
                        " AS PROXIED SERVICES");
                    }
                    synchronized (serviceRegistrations) {
                        serviceRegistrations.put(service, reg);
                    }
                    registerWithServiceDiscovery(reg);
                    final LeaseUpdateMessage lu = new LeaseUpdateMessage();
                    lu.setType(LeaseUpdateMessage.SERVICE_ADDED);
                    lu.setServiceID(String.valueOf(reg.getServiceID()));
                    lu.setPayload(new Object[] { reg.getInterfaceNames(), reg.getProperties() });
                    updateLeases(lu);
                    return service;
                } catch (final ClassNotFoundException e) {
                    e.printStackTrace();
                    throw new RemoteOSGiException("Cannot find class " + service, // $NON-NLS-1$
                    e);
                }
            }

            public void modifiedService(final ServiceReference reference, final Object service) {
                if (reference.getProperty(R_OSGi_REGISTRATION) == null) {
                    removedService(reference, service);
                    return;
                }
                final RemoteServiceRegistration reg = (RemoteServiceRegistration) serviceRegistrations.get(reference);
                registerWithServiceDiscovery(reg);
                final LeaseUpdateMessage lu = new LeaseUpdateMessage();
                lu.setType(LeaseUpdateMessage.SERVICE_MODIFIED);
                lu.setServiceID(String.valueOf(reg.getServiceID()));
                lu.setPayload(new Object[] { null, reg.getProperties() });
                updateLeases(lu);
            }

            public void removedService(final ServiceReference reference, final Object service) {
                final ServiceReference sref = Arrays.asList((String[]) reference.getProperty(Constants.OBJECTCLASS)).contains(SurrogateRegistration.class.getName()) ? (ServiceReference) reference.getProperty(SurrogateRegistration.SERVICE_REFERENCE) : reference;
                final RemoteServiceRegistration reg = (RemoteServiceRegistration) serviceRegistrations.remove(sref);
                unregisterFromServiceDiscovery(reg);
                final LeaseUpdateMessage lu = new LeaseUpdateMessage();
                lu.setType(LeaseUpdateMessage.SERVICE_REMOVED);
                lu.setServiceID(String.valueOf(reg.getServiceID()));
                lu.setPayload(new Object[] { null, null });
                updateLeases(lu);
            }
        });
        remoteServiceTracker.open(true);
        networkChannelFactoryTracker = new ServiceTracker(context, context.createFilter(// $NON-NLS-1$ //$NON-NLS-2$
        "(" + Constants.OBJECTCLASS + "=" + NetworkChannelFactory.class.getName() + ")"), new // $NON-NLS-1$
        ServiceTrackerCustomizer() {

            public Object addingService(final ServiceReference reference) {
                final NetworkChannelFactory factory = (NetworkChannelFactory) context.getService(reference);
                try {
                    factory.activate(RemoteOSGiServiceImpl.this);
                } catch (final IOException ioe) {
                    if (log != null) {
                        log.log(LogService.LOG_ERROR, ioe.getMessage(), ioe);
                    }
                }
                return factory;
            }

            public void modifiedService(final ServiceReference reference, final Object factory) {
            }

            public void removedService(final ServiceReference reference, final Object factory) {
            }
        });
        networkChannelFactoryTracker.open();
    } catch (final InvalidSyntaxException ise) {
        ise.printStackTrace();
    }
}
Also used : NetworkChannelFactory(ch.ethz.iks.r_osgi.channels.NetworkChannelFactory) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) IOException(java.io.IOException) LeaseUpdateMessage(ch.ethz.iks.r_osgi.messages.LeaseUpdateMessage) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint) ServiceReference(org.osgi.framework.ServiceReference) RemoteServiceReference(ch.ethz.iks.r_osgi.RemoteServiceReference) ServiceDiscoveryHandler(ch.ethz.iks.r_osgi.service_discovery.ServiceDiscoveryHandler) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Aggregations

NetworkChannelFactory (ch.ethz.iks.r_osgi.channels.NetworkChannelFactory)4 ChannelEndpoint (ch.ethz.iks.r_osgi.channels.ChannelEndpoint)3 RemoteOSGiException (ch.ethz.iks.r_osgi.RemoteOSGiException)2 RemoteServiceReference (ch.ethz.iks.r_osgi.RemoteServiceReference)2 IOException (java.io.IOException)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 ServiceReference (org.osgi.framework.ServiceReference)2 URI (ch.ethz.iks.r_osgi.URI)1 LeaseUpdateMessage (ch.ethz.iks.r_osgi.messages.LeaseUpdateMessage)1 ServiceDiscoveryHandler (ch.ethz.iks.r_osgi.service_discovery.ServiceDiscoveryHandler)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Filter (org.osgi.framework.Filter)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1