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

Java Open Source » Testing » jacareto 
jacareto » jacareto » system » Modules.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.ZipToolkit;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;
import java.net.*;

/**
 * The list of all modules Jacareto knows.
 *
 * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.01
 */
public class Modules extends EnvironmentMember {
    /** The vector of modules. */
    private Vector modules;

    /**
     * Creates a new modules instance.
     *
     * @param env the Environment
     * @param dir the directory where to search for the modules stored as jar-files
     */
    public Modules (Environment env, String dir) {
        super(env);
        modules = new Vector(5, 5);
        loadModules (dir);
    }

    /**
     * Loads all modules contained in the given directory.
     *
     * @param dir DOCUMENT ME!
     */
    public void loadModules (String dir) {
        File moduleDir = new File(dir);

        if (! moduleDir.exists ()) {
            moduleDir.mkdir ();
        }

        File[] moduleFiles = moduleDir.listFiles (new FileFilter() {
                    public boolean accept (File pathname) {
                        return pathname.getName ().endsWith (".jar");
                    }
                });

        for (int i = 0; i < moduleFiles.length; i++) {
            try {

                   InputStream input = ZipToolkit.getInputStream (moduleFiles[i].getAbsolutePath (),
                        "customization/module.xml");
                   Customization moduleCustom = new Customization(input);
                   Customization langCustom = null;

                   // language-dependent file
                   input = ZipToolkit.getInputStream (moduleFiles[i].getAbsolutePath (),
                        "lang/module_" + getLanguage ().getIdentifier () + ".properties");

                   if (input == null) {
                       // default file (without language id)
                       input = ZipToolkit.getInputStream (moduleFiles[i].getAbsolutePath (),
                              "lang/module.properties");
                   }

                   if (input != null) {
                       Properties properties = new Properties();
                       properties.load (input);
                       langCustom = new Customization(properties);
                   } else {
                      // older module versions
                       input = ZipToolkit.getInputStream (moduleFiles[i].getAbsolutePath (),
                               "lang/english/module.xml");
                       langCustom = new Customization(input);
                   }

                   Language moduleLang = new Language(langCustom, logger);
                   Hashtable attributes = moduleCustom.getAttributes ();
                   String moduleName = (String) attributes.get ("name");
                   String moduleAuthor = (String) attributes.get ("author");
                   String moduleVersion = (String) attributes.get ("version");
                   addModule (new Module(env, moduleName, moduleAuthor, moduleVersion, moduleCustom,
                           moduleLang, moduleFiles[i].getAbsolutePath ()));
              
            } catch (IOException ioex) {
                logger.error (ioex);
            }
        }
    }

    /**
     * Add the content of the customization instances of the modules to the default customization.
     */
    public void addCustomizations () {
        addCustomizations (customization);
    }

    /**
     * Add the content of the customization instances of the modules to the given customization.
     *
     * @param customization DOCUMENT ME!
     */
    public void addCustomizations (Customization customization) {
        Iterator it = iterator ();

        while (it.hasNext ()) {
            customization.addContent (((Module) it.next ()).getCustomization ());
        }
    }

    /**
     * Adds a module.
     *
     * @param module DOCUMENT ME!
     */
    private void addModule (Module module) {
        modules.add (module);

        //customization.addContent (module.getCustomization());
        language.getMapping ().addContent (module.getLanguage ().getMapping ());
    }

    /**
     * Returns all modules.
     *
     * @return DOCUMENT ME!
     */
    public Vector getModules () {
        return modules;
    }

    /**
     * Returns an iterator on all modules.
     *
     * @return DOCUMENT ME!
     */
    public Iterator iterator () {
        return modules.iterator ();
    }

    /**
     * Returns a string representation.
     *
     * @return DOCUMENT ME!
     */
    public String toString () {
        String result = "Modules: ";
        Iterator it = modules.iterator ();

        while (it.hasNext ()) {
            result += (((Module) it.next ()).getName () + " ");
        }

        return result;
    }
}
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.