001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.system;
025:
026: import jacareto.toolkit.ZipToolkit;
027:
028: import java.io.File;
029: import java.io.FileFilter;
030: import java.io.IOException;
031: import java.io.InputStream;
032:
033: import java.util.Hashtable;
034: import java.util.Iterator;
035: import java.util.Properties;
036: import java.util.Vector;
037: import java.net.*;
038:
039: /**
040: * The list of all modules Jacareto knows.
041: *
042: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
043: * @version 1.01
044: */
045: public class Modules extends EnvironmentMember {
046: /** The vector of modules. */
047: private Vector modules;
048:
049: /**
050: * Creates a new modules instance.
051: *
052: * @param env the Environment
053: * @param dir the directory where to search for the modules stored as jar-files
054: */
055: public Modules(Environment env, String dir) {
056: super (env);
057: modules = new Vector(5, 5);
058: loadModules(dir);
059: }
060:
061: /**
062: * Loads all modules contained in the given directory.
063: *
064: * @param dir DOCUMENT ME!
065: */
066: public void loadModules(String dir) {
067: File moduleDir = new File(dir);
068:
069: if (!moduleDir.exists()) {
070: moduleDir.mkdir();
071: }
072:
073: File[] moduleFiles = moduleDir.listFiles(new FileFilter() {
074: public boolean accept(File pathname) {
075: return pathname.getName().endsWith(".jar");
076: }
077: });
078:
079: for (int i = 0; i < moduleFiles.length; i++) {
080: try {
081:
082: InputStream input = ZipToolkit.getInputStream(
083: moduleFiles[i].getAbsolutePath(),
084: "customization/module.xml");
085: Customization moduleCustom = new Customization(input);
086: Customization langCustom = null;
087:
088: // language-dependent file
089: input = ZipToolkit
090: .getInputStream(moduleFiles[i]
091: .getAbsolutePath(), "lang/module_"
092: + getLanguage().getIdentifier()
093: + ".properties");
094:
095: if (input == null) {
096: // default file (without language id)
097: input = ZipToolkit.getInputStream(moduleFiles[i]
098: .getAbsolutePath(),
099: "lang/module.properties");
100: }
101:
102: if (input != null) {
103: Properties properties = new Properties();
104: properties.load(input);
105: langCustom = new Customization(properties);
106: } else {
107: // older module versions
108: input = ZipToolkit.getInputStream(moduleFiles[i]
109: .getAbsolutePath(),
110: "lang/english/module.xml");
111: langCustom = new Customization(input);
112: }
113:
114: Language moduleLang = new Language(langCustom, logger);
115: Hashtable attributes = moduleCustom.getAttributes();
116: String moduleName = (String) attributes.get("name");
117: String moduleAuthor = (String) attributes.get("author");
118: String moduleVersion = (String) attributes
119: .get("version");
120: addModule(new Module(env, moduleName, moduleAuthor,
121: moduleVersion, moduleCustom, moduleLang,
122: moduleFiles[i].getAbsolutePath()));
123:
124: } catch (IOException ioex) {
125: logger.error(ioex);
126: }
127: }
128: }
129:
130: /**
131: * Add the content of the customization instances of the modules to the default customization.
132: */
133: public void addCustomizations() {
134: addCustomizations(customization);
135: }
136:
137: /**
138: * Add the content of the customization instances of the modules to the given customization.
139: *
140: * @param customization DOCUMENT ME!
141: */
142: public void addCustomizations(Customization customization) {
143: Iterator it = iterator();
144:
145: while (it.hasNext()) {
146: customization.addContent(((Module) it.next())
147: .getCustomization());
148: }
149: }
150:
151: /**
152: * Adds a module.
153: *
154: * @param module DOCUMENT ME!
155: */
156: private void addModule(Module module) {
157: modules.add(module);
158:
159: //customization.addContent (module.getCustomization());
160: language.getMapping().addContent(
161: module.getLanguage().getMapping());
162: }
163:
164: /**
165: * Returns all modules.
166: *
167: * @return DOCUMENT ME!
168: */
169: public Vector getModules() {
170: return modules;
171: }
172:
173: /**
174: * Returns an iterator on all modules.
175: *
176: * @return DOCUMENT ME!
177: */
178: public Iterator iterator() {
179: return modules.iterator();
180: }
181:
182: /**
183: * Returns a string representation.
184: *
185: * @return DOCUMENT ME!
186: */
187: public String toString() {
188: String result = "Modules: ";
189: Iterator it = modules.iterator();
190:
191: while (it.hasNext()) {
192: result += (((Module) it.next()).getName() + " ");
193: }
194:
195: return result;
196: }
197: }
|