コード例 #1
0
  /**
   * Sets the context. By default, it also updates the context of already registered services.
   *
   * @param context The context.
   */
  public void setContext(Context context) {
    this.context = context;

    for (Service service : this) {
      service.setContext(context);
    }
  }
コード例 #2
0
  /**
   * Replaces or adds a service. The replacement is based on the service class.
   *
   * @param newService The new service to set.
   */
  public synchronized void set(Service newService) {
    List<Service> services = new CopyOnWriteArrayList<Service>();
    Service service;
    boolean replaced = false;

    for (int i = 0; (i < size()); i++) {
      service = get(i);

      if (service != null) {
        if (service.getClass().isAssignableFrom(newService.getClass())) {
          try {
            service.stop();
          } catch (Exception e) {
            Context.getCurrentLogger().log(Level.WARNING, "Unable to stop service replaced", e);
          }

          services.add(newService);
          replaced = true;
        } else {
          services.add(service);
        }
      }
    }

    if (!replaced) {
      services.add(newService);
    }

    set(services);
  }
コード例 #3
0
  /**
   * Returns a service matching a given service class.
   *
   * @param <T> The service type.
   * @param clazz The service class to match.
   * @return The matched service instance.
   */
  @SuppressWarnings("unchecked")
  public <T extends Service> T get(Class<T> clazz) {
    for (Service service : this) {
      if (clazz.isAssignableFrom(service.getClass())) {
        return (T) service;
      }
    }

    return null;
  }
コード例 #4
0
  @Override
  public boolean addAll(int index, Collection<? extends Service> services) {
    if (services != null) {
      for (Service service : services) {
        service.setContext(getContext());
      }
    }

    return super.addAll(index, services);
  }
コード例 #5
0
 @Override
 public synchronized void stop() throws Exception {
   eventBus.unregisterHandler(created);
   eventBus.unregisterHandler(deleted);
   eventBus.unregisterHandler(retrieved);
   eventBus.unregisterHandler(updated);
   super.stop();
   logger.log(Level.INFO, "Not Logging CRUD events");
 }
コード例 #6
0
 @Override
 public boolean add(Service service) {
   service.setContext(getContext());
   return super.add(service);
 }
コード例 #7
0
 @Override
 public void add(int index, Service service) {
   service.setContext(getContext());
   super.add(index, service);
 }
コード例 #8
0
 /**
  * Stops each service.
  *
  * @throws Exception
  */
 public void stop() throws Exception {
   for (Service service : this) {
     service.stop();
   }
 }