Example #1
0
  /**
   * @param serviceID
   * @return
   */
  static RemoteServiceRegistration getServiceRegistration(final String serviceID) {

    final String filter =
        "".equals(serviceID)
            ? null
            : '(' //$NON-NLS-1$
                + Constants.SERVICE_ID
                + "="
                + serviceID
                + ")"; //$NON-NLS-1$ //$NON-NLS-2$

    try {
      final ServiceReference[] refs =
          RemoteOSGiActivator.getActivator().getContext().getServiceReferences(null, filter);
      if (refs == null) {
        if (log != null) {
          log.log(LogService.LOG_WARNING, "COULD NOT FIND " + filter); // $NON-NLS-1$
        }
        return null;
      }
      return (RemoteServiceRegistration) serviceRegistrations.get(refs[0]);
    } catch (final InvalidSyntaxException e) {
      e.printStackTrace();
      return null;
    }
  }
Example #2
0
  protected Component getContent(MPart mPart) {
    BundleContext context = getBundleContext();

    String id = getServiceID(mPart);

    try {
      String filter = String.format("(%s=%s)", IOutlineViewProvider.PROPERTY, id);
      Collection<ServiceReference<IOutlineViewProvider>> services =
          context.getServiceReferences(IOutlineViewProvider.class, filter);
      if (!services.isEmpty()) {
        ServiceReference<IOutlineViewProvider> ref = services.iterator().next();
        IOutlineViewProvider service = context.getService(ref);
        if (service != null) {
          try {
            return service.getOutlineContent(mPart);
          } finally {
            getBundleContext().ungetService(ref);
          }
        }
      }
    } catch (InvalidSyntaxException e) {
      e.printStackTrace();
    }

    return null;
  }
Example #3
0
  private NetworkChannelFactory getNetworkChannelFactory(final String protocol)
      throws RemoteOSGiException {
    try {
      final Filter filter =
          RemoteOSGiActivator.getActivator()
              .getContext()
              .createFilter(
                  "(" //$NON-NLS-1$
                      + NetworkChannelFactory.PROTOCOL_PROPERTY
                      + "="
                      + protocol //$NON-NLS-1$
                      + ")"); //$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(
          "No NetworkChannelFactory for " //$NON-NLS-1$
              + protocol
              + " found."); //$NON-NLS-1$

    } catch (final InvalidSyntaxException e) {
      // does not happen
      e.printStackTrace();
      return null;
    }
  }
 private void scanForExistingHandlers() {
   try {
     final ServiceReference<?>[] allServiceReferences =
         this.bundleContext.getAllServiceReferences("org.vertx.java.core.Handler", null);
     if (allServiceReferences != null) {
       for (final ServiceReference<?> serviceReference : allServiceReferences) {
         registerHandlerIfNecessary(serviceReference);
       }
     }
   } catch (final InvalidSyntaxException e) {
     e.printStackTrace();
   }
 }
Example #5
0
 /** mapping rule: LDAP filter --> output value the more specific filters should be given first. */
 public void initialize(BundleContext ctx, String[][] mappingRules) {
   filters = new Filter[mappingRules.length];
   outputStrings = new String[mappingRules.length];
   for (int i = 0; i < mappingRules.length; i++) {
     try {
       filters[i] = ctx.createFilter(mappingRules[i][0]);
       outputStrings[i] = mappingRules[i][1];
     } catch (InvalidSyntaxException e) {
       // TODO Neeed to process this
       e.printStackTrace();
     }
   }
 }
 @SuppressWarnings("rawtypes")
 private Set<ServiceReference<?>> findHandlerRefs(final int portNumber, final String filter) {
   final Set<ServiceReference<?>> result = new HashSet<ServiceReference<?>>();
   try {
     final Collection<ServiceReference<Handler>> refs =
         this.bundleContext.getServiceReferences(Handler.class, filter);
     for (final ServiceReference<Handler> ref : refs) {
       if (portNumber == getHandlerPortNumber(ref)) {
         result.add(ref);
       }
     }
   } catch (final InvalidSyntaxException e) {
     // Should never happen
     e.printStackTrace();
   }
   return result;
 }
Example #7
0
 /**
  * @param ref the <code>RemoteServiceReference</code> to the service.
  * @return the service reference of the service (or service proxy) or <code>null</code> if the
  *     service is not (yet) present.
  * @see
  *     ch.ethz.iks.r_osgi.RemoteOSGiService#getFetchedServiceReference(ch.ethz.iks.r_osgi.RemoteServiceReference)
  * @category RemoteOSGiService
  * @since 0.6
  */
 private ServiceReference getFetchedServiceReference(final RemoteServiceReference ref) {
   try {
     final ServiceReference[] refs =
         RemoteOSGiActivator.getActivator()
             .getContext()
             .getServiceReferences(
                 ref.getServiceInterfaces()[0],
                 "(" //$NON-NLS-1$
                     + SERVICE_URI
                     + "="
                     + ref.getURI()
                     + ")"); //$NON-NLS-1$ //$NON-NLS-2$
     if (refs != null) {
       return refs[0];
     }
   } catch (final InvalidSyntaxException doesNotHappen) {
     doesNotHappen.printStackTrace();
   }
   return null;
 }
Example #8
0
 /**
  * @see ch.ethz.iks.r_osgi.RemoteOSGiService#getRemoteServiceReferences(ch.ethz.iks.r_osgi.URI,
  *     java.lang.String, org.osgi.framework.Filter)
  */
 public RemoteServiceReference[] getRemoteServiceReferences(
     final URI service, final String clazz, final Filter filter) {
   final String uri = getChannelURI(service);
   ChannelEndpointImpl channel = (ChannelEndpointImpl) channels.get(uri);
   if (channel == null) {
     try {
       connect(service);
       channel = (ChannelEndpointImpl) channels.get(uri);
     } catch (final IOException ioe) {
       throw new RemoteOSGiException("Cannot connect to " + uri); // $NON-NLS-1$
     }
   }
   if (clazz == null) {
     return channel.getAllRemoteReferences(null);
   }
   try {
     return channel.getAllRemoteReferences(
         RemoteOSGiActivator.getActivator()
             .getContext()
             .createFilter(
                 filter != null
                     ? "(&"
                         + filter
                         + "(" //$NON-NLS-1$ //$NON-NLS-2$
                         + Constants.OBJECTCLASS
                         + "="
                         + clazz
                         + "))"
                     : "(" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                         + Constants.OBJECTCLASS
                         + "="
                         + clazz
                         + ")")); //$NON-NLS-1$ //$NON-NLS-2$
   } catch (final InvalidSyntaxException ise) {
     ise.printStackTrace();
     return null;
   }
 }
Example #9
0
  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,
          "NO EVENT ADMIN FOUND. REMOTE EVENT DELIVERY TEMPORARILY DISABLED."); //$NON-NLS-1$
    }

    try {
      eventHandlerTracker =
          new ServiceTracker(
              context,
              context.createFilter(
                  "(&("
                      + Constants.OBJECTCLASS
                      + "=" //$NON-NLS-1$ //$NON-NLS-2$
                      + EventHandler.class.getName()
                      + ")(!(" //$NON-NLS-1$
                      + R_OSGi_INTERNAL
                      + "=*)))"), //$NON-NLS-1$
              new ServiceTrackerCustomizer() {

                public Object addingService(final ServiceReference reference) {
                  final String[] theTopics =
                      (String[]) reference.getProperty(EventConstants.EVENT_TOPIC);
                  final LeaseUpdateMessage lu = new LeaseUpdateMessage();
                  lu.setType(LeaseUpdateMessage.TOPIC_UPDATE);
                  lu.setServiceID(""); // $NON-NLS-1$
                  lu.setPayload(new Object[] {theTopics, null});
                  updateLeases(lu);

                  return Arrays.asList(theTopics);
                }

                public void modifiedService(
                    final ServiceReference reference, final Object oldTopics) {

                  final List oldTopicList = (List) oldTopics;
                  final List newTopicList =
                      Arrays.asList((String[]) reference.getProperty(EventConstants.EVENT_TOPIC));
                  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);
                  lu.setServiceID(""); // $NON-NLS-1$
                  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);
                  lu.setServiceID(""); // $NON-NLS-1$
                  lu.setPayload(new Object[] {null, removedTopics});
                  updateLeases(lu);
                }
              });
      eventHandlerTracker.open();

      if (DEBUG) {
        log.log(
            LogService.LOG_DEBUG,
            "Local topic space " //$NON-NLS-1$
                + 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);

                  final RemoteServiceRegistration[] 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(
                            "r-osgi://" //$NON-NLS-1$
                                + 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 ServiceTrackerCustomizer() { //$NON-NLS-1$

                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,
                          "REGISTERING " //$NON-NLS-1$
                              + reference
                              + " AS PROXIED SERVICES"); //$NON-NLS-1$
                    }

                    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, e); // $NON-NLS-1$
                  }
                }

                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);

                  unregisterFromServiceDiscovery(reg);
                  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();

      networkChannelFactoryTracker =
          new ServiceTracker(
              context,
              context.createFilter(
                  "("
                      + Constants.OBJECTCLASS
                      + "=" //$NON-NLS-1$ //$NON-NLS-2$
                      + NetworkChannelFactory.class.getName()
                      + ")"),
              new ServiceTrackerCustomizer() { //$NON-NLS-1$

                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();
    }
  }