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.JacaretoSystemClassLoader;
027:
028: import org.apache.log4j.Logger;
029:
030: import java.io.IOException;
031:
032: /**
033: * An <code>Environment</code> object is an instance which bundles objects used by all other
034: * classes in the Jacareto framework. These objects are:
035: *
036: * <ul>
037: * <li>
038: * A customization object for all customization elements.
039: * </li>
040: * <li>
041: * A logger object for logging and error messages.
042: * </li>
043: * <li>
044: * A language object for language dependend texts.
045: * </li>
046: * <li>
047: * A files object which stores necessary files and directories.
048: * </li>
049: * </ul>
050: *
051: * Classes contained in a Jacareto system should have access to the environment instance of their
052: * system, so they can get the customization, the logger for logging messages and the language
053: * object for getting a text string. There is more than one way to provide the environment object
054: * to a class:
055: *
056: * <ul>
057: * <li>
058: * The class may extend the class {@link EnvironmentMember}. This superclass contains a constructor
059: * which gets an environment as argument and a method {@link
060: * EnvironmentMember#setEnvironment(Environment)}.
061: * </li>
062: * <li>
063: * If the class cannot extend <code>EnvironmentMember</code> because it already extends another
064: * class, then it should get the environment in its constructor, or it should have a method
065: * <code>setEnvironment(Environment)</code> or anything like that.
066: * </li>
067: * </ul>
068: *
069: * But why must all classes contained in a Jacareto system have a reference to the environment
070: * instance? Why does the environment not provide static methods to get the logger or the
071: * language? There is one reason why static methods are not used: It is our goal that one Jacareto
072: * system (like CleverPHL) can handle another Jacareto system (like Picorder) as target
073: * application. Those two systems have two different environments, but they run in the same Java
074: * virtual machine. Therefore they need two distinct access mechanisms to their environment
075: * instances.
076: *
077: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
078: * @version 1.00
079: */
080: public class Environment {
081: /** The logger instance. */
082: protected Logger logger;
083:
084: /** The customization instance. */
085: protected Customization customization;
086:
087: /** The language instance. */
088: protected Language language;
089:
090: /** The files instance. */
091: protected Files files;
092:
093: /** The modules instance. */
094: protected Modules modules;
095:
096: /** The Jacareto System Class Loader. */
097: protected JacaretoSystemClassLoader systemClassLoader;
098:
099: /**
100: * Creates a new env object. This method constructs the customization instance and the language
101: * instance from given xml filenames and the logger from a given logger name.
102: *
103: * @param customFile The filename of the customization file
104: * @param loggerName The name of the logger. if this value is <code>null</code>, the root
105: * logger will be taken
106: * @param langFile The filename of the language definition file
107: *
108: * @throws EnvironmentException if the env cannot be created
109: */
110: public Environment(String customFile, String loggerName,
111: String langFile) throws EnvironmentException {
112: // Sets the logger
113: if (loggerName == null) {
114: setLogger(Logger.getRootLogger());
115: } else {
116: setLogger(Logger.getLogger(loggerName));
117: }
118:
119: try {
120: setCustomization(new Customization(customFile));
121: } catch (IOException i) {
122: throw new EnvironmentException(
123: "Cannot read customization file!" + i);
124: }
125:
126: try {
127: setLanguage(new Language(new Customization(langFile),
128: logger));
129: } catch (IOException i) {
130: throw new EnvironmentException("Cannot read language file!"
131: + i);
132: }
133:
134: initJacaretoSystemClassLoader();
135: }
136:
137: /**
138: * Creates a new env object with the specified values.
139: *
140: * @param customization the customization instance.
141: * @param logger the logger instance.
142: * @param language the language instance
143: * @param files the files instance
144: */
145: public Environment(Customization customization, Logger logger,
146: Language language, Files files) {
147: setLogger(logger);
148: setCustomization(customization);
149: setLanguage(language);
150:
151: if (files != null) {
152: setFiles(files);
153: }
154:
155: initJacaretoSystemClassLoader();
156: }
157:
158: /**
159: * Creates a new env object with the specified values (and no Files instance).
160: *
161: * @param customization the customization instance.
162: * @param logger the logger instance.
163: * @param language the language instance
164: */
165: public Environment(Customization customization, Logger logger,
166: Language language) {
167: this (customization, logger, language, null);
168: }
169:
170: /**
171: * Creates a new env object with an empty customization object, the root logger and an empty
172: * language object.
173: */
174: public Environment() {
175: this (new Customization(), Logger.getRootLogger(),
176: new Language());
177: }
178:
179: /**
180: * Creates a new env object with the attributes of the given env object.
181: *
182: * @param env the environment instance whose attributes will be taken
183: */
184: public Environment(Environment env) {
185: this (env.getCustomization(), env.getLogger(), env.getLanguage());
186: }
187:
188: /**
189: * Sets the logger object.
190: *
191: * @param logger the new logger instance.
192: */
193: public void setLogger(Logger logger) {
194: this .logger = logger;
195: }
196:
197: /**
198: * Returns the logger object.
199: *
200: * @return DOCUMENT ME!
201: */
202: public Logger getLogger() {
203: return logger;
204: }
205:
206: /**
207: * Sets the modules
208: *
209: * @param modules the modules
210: */
211: public void setModules(Modules modules) {
212: this .modules = modules;
213: }
214:
215: /**
216: * Returns the modules.
217: *
218: * @return the modules
219: */
220: public Modules getModules() {
221: return modules;
222: }
223:
224: /**
225: * Sets the customization object.
226: *
227: * @param customization the new customization instance.
228: */
229: public void setCustomization(Customization customization) {
230: this .customization = customization;
231: }
232:
233: /**
234: * Returns the customization object.
235: *
236: * @return DOCUMENT ME!
237: */
238: public Customization getCustomization() {
239: return customization;
240: }
241:
242: /**
243: * Sets the files object.
244: *
245: * @param files the new files instance.
246: */
247: public void setFiles(Files files) {
248: this .files = files;
249: }
250:
251: /**
252: * Returns the files object.
253: *
254: * @return DOCUMENT ME!
255: */
256: public Files getFiles() {
257: return files;
258: }
259:
260: /**
261: * Sets the language object.
262: *
263: * @param language the new language instance.
264: */
265: public void setLanguage(Language language) {
266: this .language = language;
267: }
268:
269: /**
270: * Returns the language object.
271: *
272: * @return DOCUMENT ME!
273: */
274: public Language getLanguage() {
275: return language;
276: }
277:
278: /**
279: * Inits the Jacareto System Class Loader.
280: */
281: private void initJacaretoSystemClassLoader() {
282: ClassLoader systemClassLoader = ClassLoader
283: .getSystemClassLoader();
284:
285: if (systemClassLoader instanceof JacaretoSystemClassLoader) {
286: this .systemClassLoader = (JacaretoSystemClassLoader) systemClassLoader;
287: this .systemClassLoader.init(this );
288: } else {
289: if ((logger != null) && (language != null)) {
290: logger.warn(language
291: .getString("System.ClassLoader.NotJacareto"));
292: } else {
293: System.out
294: .println("Warning: System class loader is not the Jacareto system class loader.");
295: }
296: }
297: }
298:
299: /**
300: * Returns the Jacareto System Class Loader.
301: *
302: * @return the class loader
303: */
304: public JacaretoSystemClassLoader getJacaretoSystemClassLoader() {
305: return systemClassLoader;
306: }
307: }
|