KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > config > VergeConfigMediator


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.config;
8
9
10 import java.net.MalformedURLException JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16
17 import javax.servlet.ServletContext JavaDoc;
18
19 import org.apache.log4j.Logger;
20 import org.jdom.Document;
21
22 import com.inversoft.config.ConfigFactoryRegistry;
23 import com.inversoft.config.ConfigMediator;
24 import com.inversoft.config.ConfigurationException;
25 import com.inversoft.config.ConfigFactory;
26 import com.inversoft.error.ErrorList;
27 import com.inversoft.util.StringTools;
28
29
30 /**
31  * <p>
32  * This class is the config mediator for the Inversoft
33  * Portal framework. This reads in a list of configuration
34  * files from the servlet context, creates URLs to these
35  * using the servlet context's getResource method and passes
36  * an array of URLs to the {@link ConfigMediator#mediate(Document[])}
37  * method of the {@link ConfigMediator} class.
38  * </p>
39  *
40  * <p>
41  * The name of the context parameter is vergeConfiguration.
42  * </p>
43  *
44  * <p>
45  * If there are no configuration files specified, this logs
46  * a warning, but does not fail.
47  * </p>
48  *
49  * @author Brian Pontarelli
50  * @since 2.0
51  * @version 2.0
52  */

53 public class VergeConfigMediator extends ConfigMediator {
54
55     /**
56      * This classes logger
57      */

58     private static final Logger logger = Logger.getLogger(VergeConfigMediator.class);
59
60     /**
61      * The bundle name that Verge stores its factory configuration in
62      */

63     public static final String JavaDoc VERGE_FACTORIES_BUNDLE =
64         "com.inversoft.verge.config.ConfigFactories";
65
66
67     /**
68      * Constructs a new <code>VergeConfigMediator</code> and adds the default
69      * list of {@link ConfigFactory} objects to the {@link ConfigFactoryRegistry}.
70      * This are retrieved from the ResourceBundle with the name given by the
71      * {@link #VERGE_FACTORIES_BUNDLE} constant in this class.
72      */

73     public VergeConfigMediator() {
74         ConfigFactoryRegistry.load(ResourceBundle.getBundle(VERGE_FACTORIES_BUNDLE));
75     }
76
77
78     /**
79      * Parses the Verge framework configuration files
80      *
81      * @param context The ServletContext which should contain a parameter that
82      * is a list of all the configuration files
83      * @throws ConfigurationException If there were any problems parsing or
84      * building the configuration
85      */

86     public void mediate(ServletContext JavaDoc context) throws ConfigurationException {
87
88         System.out.println("================== Initializing Verge configuration ==================");
89
90         // Get the config file name and setup the configuration repository
91
ErrorList errors = new ErrorList();
92         String JavaDoc configFiles = context.getInitParameter(VergeConfigConstants.CONTEXT_PARAM);
93         if (StringTools.isEmpty(configFiles)) {
94             logger.warn("No config file(s) specified");
95             return;
96         }
97
98         // Find each file, comma separated, and add it to the configuration
99
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(configFiles, " ,\t\n\r\f");
100         List JavaDoc urls = new ArrayList JavaDoc(st.countTokens());
101         String JavaDoc file;
102         URL JavaDoc url = null;
103
104         while(st.hasMoreTokens()) {
105             file = st.nextToken().trim();
106
107             try {
108                 logger.info("Parsing file: " + file);
109                 url = context.getResource(file);
110                 if (url == null) {
111                     logger.error("Configuration file: " + file + " does not exist");
112                     errors.addError("Configuration file: " + file + " does not exist");
113                     continue;
114                 }
115             } catch (MalformedURLException JavaDoc murle) {
116                 logger.error(murle.toString());
117                 errors.addError(murle.toString());
118             }
119
120             urls.add(url);
121         }
122
123         // If there were any valid files or resources found, mediate
124
if (urls.size() > 0) {
125             try {
126                 super.mediate((URL JavaDoc []) urls.toArray(new URL JavaDoc[urls.size()]));
127             } catch (ConfigurationException ce) {
128                 errors.addErrorList(ce.getErrors());
129             }
130         }
131
132         if (!errors.isEmpty()) {
133             throw new ConfigurationException(errors);
134         }
135
136         // Let the JVM know that it can garbage collect all those JDOM classes
137
System.gc();
138     }
139 }
Popular Tags