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

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

The newest version!
package org.uqbar.commons.applicationContext;

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

import org.uqbar.commons.model.Entity;
import org.uqbar.commons.model.Repo;

/**
 * Clase Singleton que permite almacenar y recuperar "well known instances", como
 * repositorios y cualquier otro objeto singleton (componentes de envío de mails,
 * simulación de impresoras, etc.). 

* * Implementa un Patrón arquitectural llamado Service Locator * *
* * ApplicationContext is a Singleton class that allows to retrieve special * or "well known" instances, like repositories or any other singleton objects. * It implements a Service Locator Architecture Pattern * */ 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); } // ******************************************************** // ** Repos // ******************************************************** public Repo getRepo(Class type) { return this.internalGetSingleton(type, // "No existe un repo registrado para la clase: " + type.getSimpleName()); } public void configureRepo(Class type, Repo repo) { this.singletons.put(type, repo); } // ******************************************************** // ** 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 - 2025 Weber Informatics LLC | Privacy Policy