Environment.java :  » Testing » jacareto » jacareto » system » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » system » Environment.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.system;


import jacareto.toolkit.JacaretoSystemClassLoader;

import org.apache.log4j.Logger;

import java.io.IOException;

/**
 * An <code>Environment</code> object is an instance which bundles objects used by all other
 * classes in the Jacareto framework. These objects are:
 * 
 * <ul>
 * <li>
 * A customization object for all customization elements.
 * </li>
 * <li>
 * A logger object for logging and error messages.
 * </li>
 * <li>
 * A language object for language dependend texts.
 * </li>
 * <li>
 * A files object which stores necessary files and directories.
 * </li>
 * </ul>
 * 
 * Classes contained in a Jacareto system should have access to the environment instance of their
 * system, so they can get the customization, the logger for logging messages and the language
 * object for getting a text string. There is more than one way to provide the environment object
 * to a class:
 * 
 * <ul>
 * <li>
 * The class may extend the class {@link EnvironmentMember}. This superclass contains a constructor
 * which gets an environment as argument and a method {@link
 * EnvironmentMember#setEnvironment(Environment)}.
 * </li>
 * <li>
 * If the class cannot extend <code>EnvironmentMember</code> because it already extends another
 * class, then it should get the environment in its constructor, or it should have a method
 * <code>setEnvironment(Environment)</code> or anything like that.
 * </li>
 * </ul>
 * 
 * But why must all classes contained in a Jacareto system have a reference to the environment
 * instance? Why does the environment not provide static methods to get the logger or the
 * language? There is one reason why static methods are not used: It is our goal that one Jacareto
 * system (like CleverPHL) can handle another Jacareto system (like Picorder) as target
 * application. Those two systems have two different environments, but they run in the same Java
 * virtual machine. Therefore they need two distinct access mechanisms to their environment
 * instances.
 *
 * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.00
 */
public class Environment {
    /** The logger instance. */
    protected Logger logger;

    /** The customization instance. */
    protected Customization customization;

    /** The language instance. */
    protected Language language;

    /** The files instance. */
    protected Files files;

    /** The modules instance. */
    protected Modules modules;

    /** The Jacareto System Class Loader. */
    protected JacaretoSystemClassLoader systemClassLoader;

    /**
     * Creates a new env object. This method constructs the customization instance and the language
     * instance from given xml filenames and the logger from a given logger name.
     *
     * @param customFile The filename of the customization file
     * @param loggerName The name of the logger. if this value is <code>null</code>, the root
     *        logger will be taken
     * @param langFile The filename of the language definition file
     *
     * @throws EnvironmentException if the env cannot be created
     */
    public Environment (String customFile, String loggerName, String langFile)
        throws EnvironmentException {
        // Sets the logger
        if (loggerName == null) {
            setLogger (Logger.getRootLogger ());
        } else {
            setLogger (Logger.getLogger (loggerName));
        }

        try {
            setCustomization (new Customization(customFile));
        } catch (IOException i) {
            throw new EnvironmentException("Cannot read customization file!" + i);
        }

        try {
            setLanguage (new Language(new Customization(langFile), logger));
        } catch (IOException i) {
            throw new EnvironmentException("Cannot read language file!" + i);
        }

        initJacaretoSystemClassLoader ();
    }

    /**
     * Creates a new env object with the specified values.
     *
     * @param customization the customization instance.
     * @param logger the logger instance.
     * @param language the language instance
     * @param files the files instance
     */
    public Environment (Customization customization, Logger logger, Language language, Files files) {
        setLogger (logger);
        setCustomization (customization);
        setLanguage (language);

        if (files != null) {
            setFiles (files);
        }

        initJacaretoSystemClassLoader ();
    }

    /**
     * Creates a new env object with the specified values (and no Files instance).
     *
     * @param customization the customization instance.
     * @param logger the logger instance.
     * @param language the language instance
     */
    public Environment (Customization customization, Logger logger, Language language) {
        this(customization, logger, language, null);
    }

    /**
     * Creates a new env object with an empty customization object, the root logger and an empty
     * language object.
     */
    public Environment () {
        this(new Customization(), Logger.getRootLogger (), new Language());
    }

    /**
     * Creates a new env object with the attributes of the given env object.
     *
     * @param env the environment instance whose attributes will be taken
     */
    public Environment (Environment env) {
        this(env.getCustomization (), env.getLogger (), env.getLanguage ());
    }

    /**
     * Sets the logger object.
     *
     * @param logger the new logger instance.
     */
    public void setLogger (Logger logger) {
        this.logger = logger;
    }

    /**
     * Returns the logger object.
     *
     * @return DOCUMENT ME!
     */
    public Logger getLogger () {
        return logger;
    }

    /**
     * Sets the modules
     *
     * @param modules the modules
     */
    public void setModules (Modules modules) {
        this.modules = modules;
    }

    /**
     * Returns the modules.
     *
     * @return the modules
     */
    public Modules getModules () {
        return modules;
    }

    /**
     * Sets the customization object.
     *
     * @param customization the new customization instance.
     */
    public void setCustomization (Customization customization) {
        this.customization = customization;
    }

    /**
     * Returns the customization object.
     *
     * @return DOCUMENT ME!
     */
    public Customization getCustomization () {
        return customization;
    }

    /**
     * Sets the files object.
     *
     * @param files the new files instance.
     */
    public void setFiles (Files files) {
        this.files = files;
    }

    /**
     * Returns the files object.
     *
     * @return DOCUMENT ME!
     */
    public Files getFiles () {
        return files;
    }

    /**
     * Sets the language object.
     *
     * @param language the new language instance.
     */
    public void setLanguage (Language language) {
        this.language = language;
    }

    /**
     * Returns the language object.
     *
     * @return DOCUMENT ME!
     */
    public Language getLanguage () {
        return language;
    }

    /**
     * Inits the Jacareto System Class Loader.
     */
    private void initJacaretoSystemClassLoader () {
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader ();

        if (systemClassLoader instanceof JacaretoSystemClassLoader) {
            this.systemClassLoader = (JacaretoSystemClassLoader) systemClassLoader;
            this.systemClassLoader.init (this);
        } else {
            if ((logger != null) && (language != null)) {
                logger.warn (language.getString ("System.ClassLoader.NotJacareto"));
            } else {
                System.out.println (
                    "Warning: System class loader is not the Jacareto system class loader.");
            }
        }
    }

    /**
     * Returns the Jacareto System Class Loader.
     *
     * @return the class loader
     */
    public JacaretoSystemClassLoader getJacaretoSystemClassLoader () {
        return systemClassLoader;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.