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

org.uqbar.commons.utils.ApplicationContext Maven / Gradle / Ivy

package org.uqbar.commons.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * El contexto de la aplicación.
 */
public class ApplicationContext {
	private static ApplicationContext instance = new ApplicationContext();
	private Map singletons = new HashMap();

	// ********************************************************
	// ** Instance management
	// ********************************************************

	public static ApplicationContext getInstance() {
		return instance;
	}

	/**
	 * Prepara un ApplicationContext a partir de una configuración. El {@link ApplicationContext} creado se
	 * guarda y será obtenido a partir de ahora cada vez que se invoque a {@link #getInstance()}.
	 * 
	 * @param configuration La configuración que se desea utilizar.
	 * @return El {@link ApplicationContext} creado.
	 */
	public static ApplicationContext create(ApplicationContextConfiguration configuration) {
		instance = new ApplicationContext();

		configuration.configure(instance);

		return instance;
	}

	// ********************************************************
	// ** Singletons
	// ********************************************************

	public  T getSingleton(Object key) {
		return this.internalGetSingleton(key, //
			"No existe un singleton registrado bajo la key: " + key);
	}

	public  void configureSingleton(Object key, T singleton) {
		this.singletons.put(key, singleton);
	}

	// ********************************************************
	// ** Homes
	// ********************************************************

	public  Home getHome(Class type) {
		return this.internalGetSingleton(type, //
			"No existe una home registrada para la clase: " + type.getSimpleName());
	}

	public  void configureHome(Class type, Home home) {
		this.singletons.put(type, home);
	}

	public  void configureHome(Class type) {
		this.configureHome(type, new Home(type));
	}

	// ********************************************************
	// ** Internal
	// ********************************************************

	@SuppressWarnings("unchecked")
	private  T internalGetSingleton(Object key, String message) {
		T singleton = (T) this.singletons.get(key);

		if (singleton == null) {
			throw new RuntimeException(message);
		}

		return singleton;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy