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.toolkit;
025:
026: import jacareto.system.Environment;
027: import jacareto.system.Language;
028: import jacareto.system.Module;
029: import jacareto.system.Modules;
030: import jacareto.toolkit.io.FileUtilities;
031:
032: import org.apache.log4j.Logger;
033:
034: import java.io.File;
035: import java.io.IOException;
036:
037: import java.net.URL;
038: import java.net.URLClassLoader;
039:
040: import java.util.Collection;
041: import java.util.Iterator;
042: import java.util.Vector;
043:
044: /**
045: * A class loader for loading classes during run-time.
046: *
047: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
048: * @version 1.01
049: */
050: public class JacaretoClassLoader extends URLClassLoader {
051: /*protected Class findClass (String className) throws ClassNotFoundException {
052: System.out.println (className);
053: return super.findClass (className);
054: }*/
055:
056: /** The env. */
057: private Environment env;
058:
059: /** The logger. */
060: private Logger logger;
061:
062: /** The language instance. */
063: private Language language;
064:
065: /** The added paths. */
066: private Vector addedPaths;
067:
068: /** The listeners. */
069: private Collection listeners;
070:
071: /** The identifier. */
072: private String identifier;
073:
074: /**
075: * Creates a new Jacareto class loader.
076: *
077: * @param env the environment
078: * @param identifier a string naming the class loader
079: */
080: public JacaretoClassLoader(Environment env, String identifier) {
081: super (new URL[0], ClassLoader.getSystemClassLoader());
082: this .identifier = identifier;
083: this .env = env;
084: this .logger = env.getLogger();
085: this .language = env.getLanguage();
086: addedPaths = new Vector(5, 5);
087: listeners = new Vector(5, 5);
088:
089: if (StringToolkit.isDefined(identifier)) {
090: ClassLoader parent = getParent();
091:
092: if ((parent != null)
093: && (parent instanceof JacaretoSystemClassLoader)) {
094: ((JacaretoSystemClassLoader) parent)
095: .registerChild(this );
096: }
097: }
098: }
099:
100: /**
101: * Creates a new class loader without identifier. The class loader will not be registered at
102: * the Jacareto System Class Loader
103: *
104: * @param env the environment
105: */
106: public JacaretoClassLoader(Environment env) {
107: this (env, null);
108: }
109:
110: /**
111: * Adds all elements contained in a path entry, separated by a ";"
112: *
113: * @param paths the paths, separated by a semicolon
114: */
115: public void addPaths(String paths) {
116: String[] entries = FileUtilities.getEntries(paths);
117: boolean pathsAdded = false;
118:
119: if (entries != null) {
120: for (int i = 0; i < entries.length; i++) {
121: String path = entries[i];
122:
123: try {
124: File pathToAdd = new File(path).getCanonicalFile();
125: path = pathToAdd.getAbsolutePath();
126:
127: if (!pathToAdd.exists()) {
128: logger
129: .error(language
130: .getString("Toolkit.JacaretoClassLoader.Error.FileNotFound")
131: + " " + path);
132: } else if (!addedPaths.contains(pathToAdd)) {
133: logger
134: .debug(language
135: .getString("Toolkit.JacaretoClassLoader.Msg.Adding")
136: + " " + path);
137:
138: if (!addedPaths.contains(pathToAdd)) {
139: addURL(pathToAdd.toURL());
140: addedPaths.add(pathToAdd);
141: pathsAdded = true;
142: }
143: }
144: } catch (IOException iex) {
145: logger
146: .error(language
147: .getString("Toolkit.JacaretoClassLoader.Error.Path")
148: + " " + path);
149: }
150: }
151: }
152:
153: if (pathsAdded) {
154: fireJacaretoClassLoaderEvent(new JacaretoClassLoaderEvent(
155: this , JacaretoClassLoaderEvent.PATHS_ADDED, paths));
156: }
157: }
158:
159: /**
160: * Adds the jar files of the modules to the paths.
161: *
162: * @param modules the modules to add
163: */
164: public void addModules(Modules modules) {
165: Iterator it = modules.iterator();
166:
167: while (it.hasNext()) {
168: addPaths(((Module) it.next()).getAbsolutePath());
169: }
170: }
171:
172: /**
173: * Returns an iterator on the added paths.
174: *
175: * @return the instance which iterates all additional paths
176: */
177: public Iterator getAddedPathsIterator() {
178: return addedPaths.iterator();
179: }
180:
181: /*
182: * Returns the URL of a resource with the given name.
183: *
184: * @return the URL of the resource
185: */
186: /*public URL findResource(String name) {
187: System.out.println ("Searching for resource: " + name);
188: URL result = super.findResource (name);
189: System.out.println ("Result: " + result);
190: return result;
191: } */
192: /* Loads a class specified by its name.
193: */
194: /* public Class getClass (String classname) throws ClassNotFoundException {
195: logger.debug (language.getString ("JacaretoClassLoader.Msg.Loading") + " " + classname);
196: Class result = findClass (classname);
197: //Class result = super.loadClass (classname);
198: // Class result = Class.forName (classname, false, this);
199: return result;
200: } */
201:
202: /**
203: * Adds a listener for Jacareto class loader events.
204: *
205: * @param listener the listener to add
206: */
207: public void addJacaretoClassLoaderListener(
208: JacaretoClassLoaderListener listener) {
209: if (!listeners.contains(listener)) {
210: listeners.add(listener);
211: }
212: }
213:
214: /**
215: * Removes a listener for events.
216: *
217: * @param listener the listener to be removed
218: */
219: public void removeJacaretoClassLoaderListener(
220: JacaretoClassLoaderListener listener) {
221: if (listeners.contains(listener)) {
222: listeners.remove(listener);
223: }
224: }
225:
226: /**
227: * Fires a Jacareto class loader event.
228: *
229: * @param event the class loader event
230: */
231: protected void fireJacaretoClassLoaderEvent(
232: JacaretoClassLoaderEvent event) {
233: Iterator it = listeners.iterator();
234:
235: while (it.hasNext()) {
236: ((JacaretoClassLoaderListener) it.next())
237: .classLoaderChanged(event);
238: }
239: }
240:
241: /**
242: * Prints all package names to the logger with INFO level. This method can be used for
243: * debugging.
244: */
245: public void printPackages() {
246: Package[] packages = getPackages();
247:
248: for (int i = 0; i < packages.length; i++) {
249: logger.info(packages[i].getName());
250: }
251: }
252:
253: /**
254: * Returns the identifier.
255: *
256: * @return the identifier
257: */
258: public String getIdentifier() {
259: return identifier;
260: }
261: }
|