示例#1
0
 /** {@inheritDoc} */
 protected <P> void applyUpdate(
     UpdateContext updateContext, UpdateResultHandler<? super Void, P> resultHandler, P param) {
   try {
     final String loggerName = getLoggerName();
     final LoggerService service = new LoggerService(loggerName);
     service.setLevel(Level.parse(getLevelName()));
     final BatchBuilder batchBuilder = updateContext.getBatchBuilder();
     final BatchServiceBuilder<Logger> builder =
         batchBuilder.addService(LogServices.loggerName(loggerName), service);
     builder.setInitialMode(ServiceController.Mode.ACTIVE);
     builder.addListener(new UpdateResultHandler.ServiceStartListener<P>(resultHandler, param));
   } catch (Throwable t) {
     resultHandler.handleFailure(t, param);
     return;
   }
 }
 @Override
 public BatchServiceBuilder setInitialMode(ServiceController.Mode mode) {
   if (mode.equals(ServiceController.Mode.ON_DEMAND)) {
     ServerStartBatchBuilder.this.serverStartupListener.expectOnDemand(serviceName);
   } else {
     ServerStartBatchBuilder.this.serverStartupListener.unexpectOnDemand(serviceName);
   }
   delegate.setInitialMode(mode);
   return this;
 }
  @Override
  @SuppressWarnings({"rawtypes", "unchecked"})
  public ServiceState registerService(
      AbstractBundle bundleState, String[] clazzes, Object serviceValue, Dictionary properties) {
    if (clazzes == null || clazzes.length == 0)
      throw new IllegalArgumentException("Null service classes");

    // Immediately after registration of a {@link ListenerHook}, the ListenerHook.added() method
    // will be called
    // to provide the current collection of service listeners which had been added prior to the hook
    // being registered.
    Collection<ListenerInfo> listenerInfos = null;
    if (serviceValue instanceof ListenerHook)
      listenerInfos = eventsPlugin.getServiceListenerInfos(null);

    // A temporary association of the clazz and name
    Map<ServiceName, String> associations = new HashMap<ServiceName, String>();

    // Generate the service names
    long serviceId = getNextServiceId();
    ServiceName[] serviceNames = new ServiceName[clazzes.length];
    for (int i = 0; i < clazzes.length; i++) {
      if (clazzes[i] == null)
        throw new IllegalArgumentException("Null service class at index: " + i);

      String shortName = clazzes[i].substring(clazzes[i].lastIndexOf(".") + 1);
      serviceNames[i] =
          ServiceName.of(
              "jbosgi", bundleState.getSymbolicName(), shortName, new Long(serviceId).toString());
    }

    final ServiceState serviceState =
        new ServiceState(bundleState, serviceId, serviceNames, clazzes, serviceValue, properties);
    BatchBuilder batchBuilder = serviceContainer.batchBuilder();
    Service service =
        new Service() {
          @Override
          public Object getValue() throws IllegalStateException {
            // [TODO] for injection to work this needs to be the Object value
            return serviceState;
          }

          @Override
          public void start(StartContext context) throws StartException {}

          @Override
          public void stop(StopContext context) {}
        };

    log.debug("Register service: " + Arrays.asList(serviceNames));

    ServiceName rootServiceName = serviceNames[0];
    BatchServiceBuilder serviceBuilder = batchBuilder.addService(rootServiceName, service);
    associations.put(rootServiceName, clazzes[0]);

    // Set the startup mode
    serviceBuilder.setInitialMode(Mode.AUTOMATIC);

    // Add the service aliases
    for (int i = 1; i < serviceNames.length; i++) {
      ServiceName alias = serviceNames[i];
      associations.put(alias, clazzes[1]);
      serviceBuilder.addAliases(alias);
    }

    try {
      batchBuilder.install();

      // Register the name association. We do this here
      // in case anything went wrong during the install
      for (Entry<ServiceName, String> aux : associations.entrySet()) {
        bundleState.addRegisteredService(serviceState);
        registerNameAssociation(aux.getValue(), aux.getKey());
      }
    } catch (ServiceRegistryException ex) {
      log.error("Cannot register services: " + serviceNames, ex);
    }

    // Call the newly added ListenerHook.added() method
    if (serviceValue instanceof ListenerHook) {
      ListenerHook listenerHook = (ListenerHook) serviceValue;
      listenerHook.added(listenerInfos);
    }

    // This event is synchronously delivered after the service has been registered with the
    // Framework.
    eventsPlugin.fireServiceEvent(bundleState, ServiceEvent.REGISTERED, serviceState);

    return serviceState;
  }
 @Override
 public BatchServiceBuilder addListener(
     Collection<? extends ServiceListener<? super T>> collection) {
   delegate.addListener(collection);
   return this;
 }
 @Override
 public BatchServiceBuilder addListener(ServiceListener... serviceListeners) {
   delegate.addListener(serviceListeners);
   return this;
 }
 @Override
 public BatchServiceBuilder addInjectionValue(Injector target, Value value) {
   delegate.addInjection(target, value);
   return this;
 }
 @Override
 public BatchServiceBuilder addOptionalDependency(
     ServiceName dependency, Class type, Injector target) {
   delegate.addOptionalDependency(dependency, type, target);
   return this;
 }
 @Override
 public BatchServiceBuilder addDependency(ServiceName dependency, Injector<Object> target) {
   delegate.addDependency(dependency, target);
   return this;
 }
 @Override
 public BatchServiceBuilder addOptionalDependency(ServiceName dependency) {
   delegate.addOptionalDependency(dependency);
   return this;
 }
 @Override
 public BatchServiceBuilder addOptionalDependencies(Iterable<ServiceName> dependencies) {
   delegate.addOptionalDependencies(dependencies);
   return this;
 }
 @Override
 public BatchServiceBuilder addDependencies(ServiceName... dependencies) {
   delegate.addDependencies(dependencies);
   return this;
 }
 @Override
 public BatchServiceBuilder setLocation(Location location) {
   delegate.setLocation(location);
   return this;
 }
 @Override
 public BatchServiceBuilder addAliases(ServiceName... aliases) {
   delegate.addAliases(aliases);
   return this;
 }