Source code: com/dghda/kent/SimpleProvider.java
1 /* Copyright (C) 2001 Duane Griffin <duanegriffin@users.sourceforge.net>
2 This file is part of Kent.
3
4 Kent is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 Kent is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with Kent; see the file COPYING. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19
20 package com.dghda.kent;
21
22 import java.io.*;
23 import java.util.*;
24
25 import com.dghda.module.*;
26
27 /** A class which loads modules from classes specified in a config file. */
28 public class SimpleProvider implements ModuleProvider {
29 public static final String SIMPLE_PROVIDER_CONFIG_FILE_PROPERTY = "com.dghda.kent.SimpleProvider.ConfigFile";
30 public static final String DEFAULT_SIMPLE_PROVIDER_CONFIG_FILE = "/WEB-INF/SimpleProvider.cfg";
31
32 /** Create a new simple provider for the given report engine. */
33 public SimpleProvider (ReportEngine engine) {
34 m_Engine = engine;
35 String config = m_Engine.getEnvironment().getProperty (SIMPLE_PROVIDER_CONFIG_FILE_PROPERTY);
36 if (config == null)
37 config = DEFAULT_SIMPLE_PROVIDER_CONFIG_FILE;
38 try {
39 readConfig (config, m_Engine.getResource (config));
40 } catch (IOException exc) {
41 m_Engine.getEnvironment().log ("An error occurred reading configuration file " + config + " for the simple module provider", exc);
42 }
43 }
44
45 /**
46 An iterator over all modules provided.
47 The iterator returns instances of the Module interface.
48 */
49 public Iterator getModuleIterator() {
50 Vector result = new Vector (m_Modules.values());
51 return result.iterator();
52 }
53
54 /**
55 Returns the module instance with the given ID.
56 @returns null if the module isn't provided.
57 */
58 public Module getModule (String id) {
59 return (Module) m_Modules.get (id);
60 }
61
62 /**
63 Reads module classes from the given configuration file.
64 The configuration file consists of comments (lines starting with a #), and module classes.
65 Specified classes are loaded and instantiated, if possible.
66 */
67 protected void readConfig (String file, InputStream input) throws IOException {
68 InputStreamReader inputReader = new InputStreamReader (input);
69 BufferedReader reader = new BufferedReader (inputReader);
70 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
71 line.trim();
72
73 // Check for blank lines or comments
74 if (line.equals ("") || line.startsWith ("#"))
75 continue;
76
77 try {
78
79 // Attempt to load the specified module
80 Class moduleClass = Class.forName (line);
81 final Object [] constructorArgs = new Object[1];
82 constructorArgs[0] = m_Engine;
83
84 // Attempt to create an instance of the specified module
85 java.lang.reflect.Constructor constructor = moduleClass.getConstructor (CONSTRUCTOR_ARG_TYPES);
86 Module module = (Module) constructor.newInstance (constructorArgs);
87
88 // Add the module to the collection
89 m_Modules.put (module.getID(), module);
90 m_Engine.getEnvironment().log ("Added module " + module.getID() + " to simple provider");
91 } catch (Exception exc) {
92 m_Engine.getEnvironment().log ("An error occurred attempting to load and create the module class " + line + " specified in the simple module provider config file " + file, exc);
93 }
94 }
95 }
96
97 private ReportEngine m_Engine;
98 private TreeMap m_Modules = new TreeMap();
99 private static final Class [] CONSTRUCTOR_ARG_TYPES = {ReportEngine.class};
100 }