Java Code Examples for org.ehcache.spi.service.PluralService

Following code examples demonstrate how to use org.ehcache.spi.service.PluralServicefrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.spi.service.PluralServiceand various code implementation of this class.

  }

  private <T extends Service> T getServiceInternal(
      final Class<T> serviceType, final ServiceCreationConfiguration<T> config, final boolean shouldCreate) {
    if (serviceType.isAnnotationPresent(PluralService.class)) {
      throw new IllegalArgumentException(serviceType.getName() + " is marked as a PluralService");
    }
    final Collection<T> registeredServices = findServices(serviceType, config, shouldCreate);
    if (registeredServices.size() > 1) {
      throw new AssertionError("The non-PluralService type" + serviceType.getName()
          + " has more than one service registered");
    }
    return (registeredServices.isEmpty() ? null : registeredServices.iterator().next());
  } 


  public void addService(final Service service) {
    final Lock lock = runningLock.readLock();
    lock.lock();
    try {
      Set<Class<? extends Service>> serviceClazzes = new HashSet<Class<? extends Service>>();

      for (Class<?> i : getAllInterfaces(service.getClass())) {
        if (Service.class != i && Service.class.isAssignableFrom(i)) {

          @SuppressWarnings("unchecked")
          Class<? extends Service> serviceClass = (Class<? extends Service>) i;

          serviceClazzes.add(serviceClass);
        }
      }

      if (services.putIfAbsent(service.getClass(), Collections.singleton(service)) != null) {
        throw new IllegalStateException("Registration of duplicate service " + service.getClass());
      }

      
      for (Class<? extends Service> serviceClazz : serviceClazzes) {
        if (serviceClazz.isAnnotationPresent(PluralService.class)) {
          Set<Service> registeredServices = services.get(serviceClazz);
          if (registeredServices == null) {
            registeredServices = new LinkedHashSet<Service>();
            services.put(serviceClazz, registeredServices);
          }
          registeredServices.add(service);

        } else {
          if (services.putIfAbsent(serviceClazz, Collections.singleton(service)) != null) {
            final StringBuilder message = new StringBuilder("Duplicate service implementation(s) found for ")
                .append(service.getClass());
            for (Class<? extends Service> serviceClass : serviceClazzes) {
              if (!serviceClass.isAnnotationPresent(PluralService.class)) {
                final Service declaredService = services.get(serviceClass).iterator().next();
                if (declaredService != null) {
                  message
                      .append("\n\t\t- ")
                      .append(serviceClass)
                      .append(" already has ")
                      .append(declaredService.getClass());
                }
              }
            }
            throw new IllegalStateException(message.toString());
          }
        }
      }

      if (running.get()) {
        loadDependenciesOf(service.getClass());
        service.start(this);
      }
    } finally {
      lock.unlock();
    }
  } 

  }

  private <T extends Service> Collection<T> findServices(
      Class<T> serviceType, ServiceCreationConfiguration<T> config, boolean shouldCreate) {
    final Collection<T> registeredServices = getServicesOfTypeInternal(serviceType);
    if (shouldCreate && (registeredServices.isEmpty() || serviceType.isAnnotationPresent(PluralService.class))) {
      registeredServices.addAll(discoverServices(serviceType, config));
    }
    return registeredServices;
  } 

Advertisement
Javadoc
Indicates that a Service subtype is permitted to have more than one concrete implementation register

ed with a ServiceProvider.

Read More
Advertisement