GNDMS 0.6.0
RESTful verison of GNDMS
Public Member Functions | Package Functions | Private Member Functions | Private Attributes | Static Private Attributes

de.zib.gndms.infra.system.GNDMSystemDirectory Class Reference

ThingAMagic. More...

Inheritance diagram for de.zib.gndms.infra.system.GNDMSystemDirectory:
de.zib.gndms.infra.system.SystemDirectory de.zib.gndms.logic.model.gorfx.QuoteCalculatorProvider de.zib.gndms.logic.access.TaskActionProvider

List of all members.

Public Member Functions

< T > T waitForInstance (@NotNull Class< T > clazz,@NotNull String name)
 Waits until an instance on instances with the key name, has been registered and returns it.
synchronized void addInstance (@NotNull String name,@NotNull Object obj)
synchronized< T > T getInstance (@NotNull Class<?extends T > clazz,@NotNull String name)
 Returns the instance, which has been registered on instances with the name name.
synchronized void reloadConfiglets (final EntityManagerFactory emf)
 Creates a new EntityManager using emf.
String getSystemName ()
String getSystemTempDirName ()
 Returns the value set for the environment variable GNDMS_TMP.

Package Functions

void init ()
synchronized void shutdownConfiglets ()
 Shuts down all configlets stored in the configlets map.
public< T extends Configlet > T getConfiglet (final @NotNull Class< T > clazz, final @NotNull String name)
 Retrieves the configlet stored with the key name from configlets map, casts it to a T class and returns it.

Private Member Functions

void addInstance_ (final String name, final Object obj)
 Adds an instance to the instances map.
synchronized ConfigletState[] loadConfigletStates (final EntityManager emParam)
 Loads all ConfigletStates managed by a specific EntityManager into an array.
synchronized void createOrUpdateConfiglets (final ConfigletState[] statesParam)
 Iterates through the ConfigletState array and either updates the state of the corresponding Configlet, if already stored in the configlets map, or creates a new Configlet using createConfiglet(de.zib.gndms.model.common.ConfigletState) and stores it together with the name of the Configlet in the map.
synchronized Configlet createConfiglet (final ConfigletState configParam)
 Creates a Configlet out of a ConfigletState.
synchronized void shutdownOldConfiglets (final EntityManager emParam)
 Removes old configlets from the configlets map.

Private Attributes

final Logger logger = LoggerFactory.getLogger(GNDMSystemDirectory.class)
String systemName
final Map< String, Object > instances
 stores several instances needed for the GNDMSystem
final Map< String, Configlet > configlets = ( new MapMaker() ).makeMap()
final BoundInjector boundInjector = new BoundInjector()
GNDMSInjector injector

Static Private Attributes

static final int INITIAL_CAPACITY = 32
static final long INSTANCE_RETRIEVAL_INTERVAL = 250L

Detailed Description

ThingAMagic.

See also:
GNDMSystem
Author:
try ste fan pla nti kow zib
Version:
$Id$

User: stepn Date: 03.09.2008 Time: 16:50:06


Member Function Documentation

void de.zib.gndms.infra.system.GNDMSystemDirectory.addInstance_ ( final String  name,
final Object  obj 
) [private]

Adds an instance to the instances map.

The name which will be mapped to the instance must not be equal to the keywords "out","err","args","em" or "emg".

Parameters:
namethe name which is to be mapped to the specified instance
objthe instance to be associated with the specified name
                                                                   {
        if ("out".equals(name) || "err".equals(name) || "args".equals(name) || "em".equals(name)
            || "emg".equals(name))
            throw new IllegalArgumentException("Reserved instance name");

        if (instances.containsKey(name))
            throw new IllegalStateException("Name clash in instance registration: " + name);
        else
            instances.put(name, obj);

        logger.debug("adding: " + name + '\'');
    }
synchronized Configlet de.zib.gndms.infra.system.GNDMSystemDirectory.createConfiglet ( final ConfigletState  configParam) [private]

Creates a Configlet out of a ConfigletState.

The created instance uses logger as its Logger object. The name and state of the new Configlet is taken from configParam.

Parameters:
configParamA ConfigletState to be converted to a Configlet
Returns:
a Configlet out of a ConfigletState
                                                                                         {
                try {
                        final Class<? extends Configlet> clazz = Class.forName(configParam.getClassName()).asSubclass(Configlet.class);
                        final Configlet instance = clazz.newInstance();
                        instance.init(logger, configParam.getName(), configParam.getState());
                        return instance;
                }
                catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                }
                catch (InstantiationException e) {
                        throw new RuntimeException(e);
                }
                catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                }
        }
synchronized void de.zib.gndms.infra.system.GNDMSystemDirectory.createOrUpdateConfiglets ( final ConfigletState[]  statesParam) [private]

Iterates through the ConfigletState array and either updates the state of the corresponding Configlet, if already stored in the configlets map, or creates a new Configlet using createConfiglet(de.zib.gndms.model.common.ConfigletState) and stores it together with the name of the Configlet in the map.

Parameters:
statesParaman array containing several ConfigletStates to be stored in the configlets map
                                                                                               {
                for (ConfigletState configletState : statesParam) {
                        final String name = configletState.getName();
                        if (configlets.containsKey(name)) {
                                configlets.get(name).update(configletState.getState());
                        }
                        else {
                                final Configlet configlet = createConfiglet(configletState);
                                configlets.put(name, configlet);
                        }
                }
        }
public<T extends Configlet> T de.zib.gndms.infra.system.GNDMSystemDirectory.getConfiglet ( final @NotNull Class< T >  clazz,
final @NotNull String  name 
) [package]

Retrieves the configlet stored with the key name from configlets map, casts it to a T class and returns it.

Parameters:
clazzthe class the Configlet belongs to
namethe name of the Configlet
<T>the class the instance will be casted to
Returns:
a casted configlet from the configlets map
                                                                                                               {
                return clazz.cast(configlets.get(name));
        }
synchronized<T> T de.zib.gndms.infra.system.GNDMSystemDirectory.getInstance ( @NotNull Class<?extends T >  clazz,
@NotNull String  name 
)

Returns the instance, which has been registered on instances with the name name.

The instace will be casted to the parameter T of clazz.

Parameters:
clazzthe class the instance belongs to
namethe name of the instance as denoted on the map instances
<T>the class the instance will be casted to
Returns:
an instance from the instances map
    {
        final Object obj = instances.get(name);
        if (obj == null)
            throw new
                  IllegalStateException("Null instance retrieved or invalid or unregistered name");
        return clazz.cast(obj);
    }
String de.zib.gndms.infra.system.GNDMSystemDirectory.getSystemTempDirName ( )

Returns the value set for the environment variable GNDMS_TMP.

If nothing denoted, it will return the value of the enviroment variable TMPDIR instead. If also not denoted, "/tmp" will be returned.

Returns:
the temp directory of the GNDMSystem according to environment variables
                                             {
                String tmp = System.getenv("GNDMS_TMP");
                tmp = tmp == null ?  ""  : tmp.trim();
                if (tmp.length() == 0) {
                        tmp = System.getenv("TMPDIR");
                        tmp = tmp == null ?  ""  : tmp.trim();
                }
                if (tmp.length() == 0) {
                        tmp = "/tmp";
                }
                return tmp;
        }
synchronized ConfigletState [] de.zib.gndms.infra.system.GNDMSystemDirectory.loadConfigletStates ( final EntityManager  emParam) [private]

Loads all ConfigletStates managed by a specific EntityManager into an array.

Performs the query "listAllConfiglets" on the database and returns an array containing the result.

Parameters:
emParaman EntityManager managing ConfigletStates
Returns:
an array containing all ConfigletStates of the database
                                                                                               {
                final ConfigletState[] states;
                emParam.getTransaction().begin();
                try {
                        Query query = emParam.createNamedQuery("listAllConfiglets");
                        final List<ConfigletState> list = (List<ConfigletState>) query.getResultList();
                        Object[] states_ = list.toArray();
                        states = new ConfigletState[states_.length];
                        for (int i = 0; i < states_.length; i++)
                                states[i] = (ConfigletState) states_[i];
                        return states;
                }
                finally {
                        if (emParam.getTransaction().isActive())
                                emParam.getTransaction().commit();
                }
        }
synchronized void de.zib.gndms.infra.system.GNDMSystemDirectory.reloadConfiglets ( final EntityManagerFactory  emf)

Creates a new EntityManager using emf.

Calls loadConfigletStates(javax.persistence.EntityManager) and createOrUpdateConfiglets(de.zib.gndms.model.common.ConfigletState[]) to load all configlets managed by this EntityManager and update the configlets map. Old Configlets will be removed and shutted down using shutdownConfiglets()

Parameters:
emfthe factory the EntityManager will be created of
                                                                                  {
                ConfigletState[] states;
                EntityManager em = emf.createEntityManager();
                try {
                        states = loadConfigletStates(em);
                        createOrUpdateConfiglets(states);
                        shutdownOldConfiglets(em);
                }
                finally { if (em.isOpen()) em.close(); }
        }
synchronized void de.zib.gndms.infra.system.GNDMSystemDirectory.shutdownOldConfiglets ( final EntityManager  emParam) [private]

Removes old configlets from the configlets map.

Checks for every Configlet in the map, if still exists in the database. If not, the map entry will be removed and shutdown() invoked on the old configlet entry.

Parameters:
emParaman EntityManager managing Configlets
                                                                                     {
                Set<String> set = configlets.keySet();
                Object[] keys = set.toArray();
                for (Object name : keys) {
                        emParam.getTransaction().begin();
                        try {
                                if (emParam.find(ConfigletState.class, name) == null) {
                                        Configlet let = configlets.get(name);
                                        configlets.remove(name);
                                        let.shutdown();
                                }
                        }
                        catch (RuntimeException e) {
                                logger.warn( "", e);
                        }
                        finally {
                                if (emParam.getTransaction().isActive())
                                        emParam.getTransaction().commit();
                        }
                }
        }
<T> T de.zib.gndms.infra.system.GNDMSystemDirectory.waitForInstance ( @NotNull Class< T >  clazz,
@NotNull String  name 
)

Waits until an instance on instances with the key name, has been registered and returns it.

Parameters:
clazzthe class the instance belongs to
namethe name of the instance as denoted on the map instances
<T>the class the instance will be casted to
Returns:
an instance from the instances map
                                                                                              {
        T instance;
        try { instance = getInstance(clazz, name); }
        catch (IllegalStateException e) { instance = null; }
        while (instance != null) {
            try {
                Thread.sleep(INSTANCE_RETRIEVAL_INTERVAL);
            }
            catch (InterruptedException e) {
                // intended
            }
            try { instance = getInstance(clazz, name); }
            catch (IllegalStateException e) { instance = null; }
        }
        return instance;
    }

The documentation for this class was generated from the following file:
 All Classes Namespaces Functions Variables