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.filter;
025:
026: import jacareto.system.Customization;
027: import jacareto.system.Environment;
028: import jacareto.system.Language;
029: import jacareto.toolkit.EnhancedHashtable;
030:
031: import org.apache.log4j.Logger;
032:
033: import java.lang.reflect.Constructor;
034: import java.lang.reflect.InvocationTargetException;
035:
036: import java.util.Enumeration;
037:
038: import javax.swing.DefaultListModel;
039:
040: /**
041: * The list of filters.
042: *
043: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
044: * @version 1.0
045: */
046: public class FilterList extends DefaultListModel {
047: /** The env. */
048: private Environment env;
049:
050: /**
051: * Creates a new filter list with the filters contained in the customization map given by the
052: * customization key and the given class loader which should load the filter classes.
053: *
054: * @param env the environment
055: * @param customizationKey the customization key
056: * @param classLoader the classLoader
057: */
058: public FilterList(Environment env, String customizationKey,
059: ClassLoader classLoader) {
060: this .env = env;
061:
062: if (customizationKey != null) {
063: loadFilters(customizationKey, classLoader);
064: }
065: }
066:
067: /**
068: * Creates a new filter list with the filters contained in the customization map given by the
069: * customization key and the default class loader
070: *
071: * @param env the environment
072: * @param customizationKey the customization key
073: */
074: public FilterList(Environment env, String customizationKey) {
075: this (env, customizationKey, null);
076: }
077:
078: /**
079: * Creates an empty filter list.
080: *
081: * @param env the environment
082: */
083: public FilterList(Environment env) {
084: this (env, null);
085: }
086:
087: /**
088: * Loads the filters from the customization with the default class loader
089: *
090: * @param customizationKey DOCUMENT ME!
091: */
092: private void loadFilters(String customizationKey) {
093: loadClassFilters(customizationKey + ".ClassFilters");
094: loadNonClassFilters(customizationKey + ".NonClassFilters");
095: }
096:
097: /**
098: * Loads the filters from the customization with the specified class loader.
099: *
100: * @param customizationKey DOCUMENT ME!
101: * @param classLoader DOCUMENT ME!
102: */
103: private void loadFilters(String customizationKey,
104: ClassLoader classLoader) {
105: loadClassFilters(customizationKey + ".ClassFilters",
106: classLoader);
107: loadNonClassFilters(customizationKey + ".NonClassFilters",
108: classLoader);
109: }
110:
111: /**
112: * Adds an element sorted.
113: *
114: * @param element DOCUMENT ME!
115: */
116: public void addSorted(Object element) {
117: boolean inserted = false;
118:
119: for (int i = 0; i < size(); i++) {
120: if (getElementAt(i).toString()
121: .compareTo(element.toString()) >= 0) {
122: insertElementAt(element, i);
123: inserted = true;
124:
125: break;
126: }
127: }
128:
129: if (!inserted) {
130: addElement(element);
131: }
132: }
133:
134: /**
135: * Loads the class filters from the customization with the default class loader.
136: *
137: * @param customizationKey DOCUMENT ME!
138: */
139: private void loadClassFilters(String customizationKey) {
140: loadClassFilters(customizationKey, null);
141: }
142:
143: /**
144: * Loads the class filters from the customization with the given class loader
145: *
146: * @param customizationKey DOCUMENT ME!
147: * @param classLoader DOCUMENT ME!
148: */
149: private void loadClassFilters(String customizationKey,
150: ClassLoader classLoader) {
151: Logger logger = env.getLogger();
152: Language language = env.getLanguage();
153: Customization customization = env.getCustomization();
154:
155: if (classLoader == null) {
156: classLoader = ClassLoader.getSystemClassLoader();
157: }
158:
159: try {
160: // some reflection instances
161: Class classFilterClass = Class.forName(
162: "jacareto.filter.ClassFilter", true, classLoader);
163: Class[] parameterTypes = new Class[3];
164: parameterTypes[0] = Class.forName(
165: "jacareto.system.Environment", true, classLoader);
166: parameterTypes[1] = Class.forName("java.lang.String", true,
167: classLoader);
168: parameterTypes[2] = Class.forName("java.lang.ClassLoader",
169: true, classLoader);
170:
171: Object[] parameters = new Object[3];
172: Constructor filterConstructor = classFilterClass
173: .getConstructor(parameterTypes);
174:
175: parameters[0] = env;
176: parameters[2] = classLoader;
177:
178: // load the map of class filters
179: EnhancedHashtable classFiltersMap = customization.getMap(
180: customizationKey, new EnhancedHashtable());
181: Enumeration enumeration = classFiltersMap.keys();
182:
183: while (enumeration.hasMoreElements()) {
184: try {
185: parameters[1] = (String) enumeration.nextElement();
186: logger
187: .debug(language
188: .getString("Filters.FilterList.Msg.LoadingClassFilter")
189: + " " + parameters[1]);
190:
191: Object filterToAdd = filterConstructor
192: .newInstance(parameters);
193:
194: if (filterToAdd != null) {
195: addSorted(filterToAdd);
196: }
197: } catch (InstantiationException inst) {
198: logger
199: .debug(
200: language
201: .getString("Filters.FilterList.Error.Construction"),
202: inst);
203: } catch (IllegalAccessException ill) {
204: logger
205: .debug(
206: language
207: .getString("Filters.FilterList.Error.Construction"),
208: ill);
209: } catch (IllegalArgumentException ila) {
210: logger
211: .debug(
212: language
213: .getString("Filters.FilterList.Error.Construction"),
214: ila);
215: } catch (InvocationTargetException inv) {
216: logger
217: .debug(
218: language
219: .getString("Filters.FilterList.Error.Construction"),
220: inv);
221: }
222: }
223: } catch (ClassNotFoundException c) {
224: logger
225: .error(
226: language
227: .getString("Filters.FilterList.Error.MissingClass"),
228: c);
229: } catch (NoSuchMethodException n) {
230: logger
231: .error(
232: language
233: .getString("Filters.FilterList.Error.MissingConstructor"),
234: n);
235: }
236: }
237:
238: /**
239: * Loads the filters from the customization which are no classfilters with the default class
240: * loader.
241: *
242: * @param customizationKey DOCUMENT ME!
243: */
244: private void loadNonClassFilters(String customizationKey) {
245: loadNonClassFilters(customizationKey, null);
246: }
247:
248: /**
249: * Loads the filters from the customization which are no classfilters with the given class
250: * loader.
251: *
252: * @param customizationKey DOCUMENT ME!
253: * @param classLoader DOCUMENT ME!
254: */
255: private void loadNonClassFilters(String customizationKey,
256: ClassLoader classLoader) {
257: Logger logger = env.getLogger();
258: Language language = env.getLanguage();
259: Customization customization = env.getCustomization();
260:
261: if (classLoader == null) {
262: classLoader = ClassLoader.getSystemClassLoader();
263: }
264:
265: try {
266: // some reflection instances
267: Class filterClass = Class.forName("jacareto.filter.Filter",
268: true, classLoader);
269: Class[] parameterTypes = new Class[1];
270: parameterTypes[0] = Class.forName(
271: "jacareto.system.Environment", true, classLoader);
272:
273: Object[] parameters = new Object[1];
274: parameters[0] = env;
275:
276: // load the map of filters
277: EnhancedHashtable filtersMap = customization.getMap(
278: customizationKey, new EnhancedHashtable());
279: Enumeration enumeration = filtersMap.keys();
280:
281: while (enumeration.hasMoreElements()) {
282: try {
283: String mapFilterClassName = (String) enumeration
284: .nextElement();
285: Class mapFilterClass = Class.forName(
286: mapFilterClassName, true, classLoader);
287:
288: if (!filterClass.isAssignableFrom(mapFilterClass)) {
289: throw new InstantiationException();
290: }
291:
292: Constructor filterConstructor = mapFilterClass
293: .getConstructor(parameterTypes);
294: logger
295: .debug(language
296: .getString("Filters.FilterList.Msg.Loading")
297: + " " + mapFilterClassName);
298: addSorted(filterConstructor.newInstance(parameters));
299: } catch (InstantiationException inst) {
300: logger
301: .error(
302: language
303: .getString("Filters.FilterList.Error.Construction"),
304: inst);
305: } catch (IllegalAccessException ill) {
306: logger
307: .error(
308: language
309: .getString("Filters.FilterList.Error.Construction"),
310: ill);
311: } catch (IllegalArgumentException ila) {
312: logger
313: .error(
314: language
315: .getString("Filters.FilterList.Error.Construction"),
316: ila);
317: } catch (InvocationTargetException inv) {
318: logger
319: .error(
320: language
321: .getString("Filters.FilterList.Error.Construction"),
322: inv);
323: }
324: }
325: } catch (ClassNotFoundException c) {
326: logger
327: .error(
328: language
329: .getString("Filters.FilterList.Error.MissingClass"),
330: c);
331: } catch (NoSuchMethodException n) {
332: logger
333: .error(
334: language
335: .getString("Filters.FilterList.Error.MissingConstructor"),
336: n);
337: }
338: }
339: }
|