All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.terracotta.management.ServiceLocator Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/*
 * All content copyright (c) 2003-2012 Terracotta, Inc., except as may otherwise be noted in a separate copyright
 * notice. All rights reserved.
 */

package org.terracotta.management;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Implements a dynamic service locator, (see
 * http://martinfowler.com/articles/injection.html#UsingAServiceLocator)
 * 
 * The intended use case is the following :
 *   1) Initialization
 *   ServiceLocator locator = new ServiceLocator();
 *   ConfigServiceImpl configServiceImpl = new ConfigServiceImpl();
 *   AgentEntity agentEntity = new AgentEntity();
 *   locator.loadService(ConfigService.class, configServiceImpl).loadService(Serializable.class, agentEntity);
 *   ServiceLocator.load(locator);
 *   
 *   2) Use of the service locator in the same class loader
 *   ConfigService value = ServiceLocator.locate(ConfigService.class);
 * 
 * You can not load it more than once (unless you call unload()); you can not start locating before load() was called
 * 
 * 
 */
public class ServiceLocator {

  private static final AtomicReference, Object>> installedServices = new AtomicReference, Object>>();

  private final Map, Object> services = new HashMap, Object>();

  public final  ServiceLocator loadService(Class clazz, T implementation) {
    services.put(clazz, implementation);
    return this;
  }

  public static  T locate(Class typeToReturn) {
    Map, Object> m = installedServices.get();
    if (m == null) {
      throw new IllegalStateException("The service locator has not been initialized yet ! (through the load() method)");
    }
    else {
      return (T) m.get(typeToReturn);
    }
  }

  public static void load(ServiceLocator locator) {
    // no special need for an immutable map, since installedServices is only accessed for read
    Map, Object> services = Collections.unmodifiableMap(new HashMap, Object>(locator.services));
    if (!installedServices.compareAndSet(null, services)) {
      throw new IllegalStateException("The service locator has already been initialized (through the load() method)");
    }
  }

  public static void unload() {
    installedServices.set(null);
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy