KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > server > Configuration


1 /******************************************************************************
2  * Configuration.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.server;
11
12 import java.io.*;
13 import java.util.*;
14
15 import org.openlaszlo.utils.ChainedException;
16
17 import org.apache.log4j.Logger;
18 import org.apache.regexp.RE;
19 import org.apache.regexp.RESyntaxException;
20 import org.jdom.*;
21 import org.jdom.input.SAXBuilder;
22 import org.jdom.filter.ElementFilter;
23
24 /**
25  * Configuration contains global server configuration information.
26  * It reads an array of options from an xml file.
27  *
28  * @author Eric Bloch
29  * @version 1.0
30  */

31 public class Configuration {
32
33     Map mOptions = new Hashtable();
34     Map mAppPaths = new Hashtable();
35     Map mAppPatterns = new Hashtable();
36     ArrayList mAppRegexps = new ArrayList();
37     Map mAppOptions = new Hashtable(); // options set in application LZX
38

39     private static Logger mLogger = Logger.getLogger(Configuration.class);
40
41     /**
42      * Constructs a new configuration
43      */

44     public Configuration()
45     {
46         try {
47             mLogger.debug("building configuration");
48             SAXBuilder builder = new SAXBuilder();
49     
50             String JavaDoc fileName = LPS.getConfigDirectory() + File.separator + "lps.xml";
51             Document doc = builder.build(new File(fileName));
52             Element root = doc.getRootElement();
53             List elts = root.getContent(new ElementFilter());
54             ListIterator iter = elts.listIterator();
55
56             while(iter.hasNext()) {
57                 Element elt = (Element)iter.next();
58
59                 // Check for global default options.
60
if (elt.getName().equals("option")) {
61                     String JavaDoc name = elt.getAttributeValue("name");
62                     if (name != null && !name.equals("")) {
63                         Option option = new Option(elt);
64                         mOptions.put(name, option);
65                     }
66                 }
67
68                 // Check for application specific options
69
if (elt.getName().equals("application")) {
70                     String JavaDoc path = elt.getAttributeValue("path");
71                     String JavaDoc pattern = elt.getAttributeValue("pattern");
72
73                     if (path != null && ! path.equals("") &&
74                         pattern != null && ! pattern.equals("")) {
75                         String JavaDoc msg =
76                             "Can't have attributes path (" + path + ")" +
77                             " and pattern (" + pattern + ") " +
78                             " defined in an application element together.";
79                         mLogger.debug("Exception reading configuration: " + msg);
80                         throw new ChainedException(msg);
81                     }
82
83                     if (path != null && !path.equals("")) {
84                         Hashtable appOpts = getOptions(elt, "option", "name");
85                         if (appOpts.size() != 0)
86                             mAppPaths.put(path, appOpts);
87                     }
88
89                     if (pattern != null && !pattern.equals("")) {
90                         Hashtable appOpts = getOptions(elt, "option", "name");
91                         if (appOpts.size() != 0) {
92                             RE re = new RE(pattern);
93                             mAppRegexps.add(re);
94                             mAppPatterns.put(re, appOpts);
95                         }
96                     }
97                 }
98             }
99         } catch (RESyntaxException e) {
100             mLogger.error("RE exception reading configuration " + e.getMessage());
101             throw new ChainedException(e.getMessage());
102         } catch (JDOMException e) {
103             mLogger.error("jdom exception reading configuration " + e.getMessage());
104             throw new ChainedException(e.getMessage());
105         }
106     }
107
108
109     /**
110      * @param app element
111      * @param tagname tag name
112      * @param optname option name
113      * @return hashtable of options
114      */

115     public static Hashtable getOptions(Element app, String JavaDoc tagname, String JavaDoc optname)
116     {
117         Hashtable options = new Hashtable();
118         List elts = app.getContent(new ElementFilter());
119         ListIterator iter = elts.listIterator();
120         while (iter.hasNext()) {
121             Element elt = (Element)iter.next();
122             if (elt.getName().equals(tagname)) {
123                 String JavaDoc name = elt.getAttributeValue(optname);
124                 if (name != null && !name.equals("")) {
125                     addOption(options, elt);
126                 }
127             }
128         }
129         return options;
130     }
131
132     public static void addOption(Map options, Element elt) {
133         String JavaDoc name = elt.getName();
134         Option option = (Option)options.get(name);
135         if (option == null) {
136             option = new Option(elt);
137             options.put(name, option);
138         } else {
139             option.addElement(elt);
140         }
141     }
142     
143     /**
144      * @param path application path - .lzo extensions treated like .lzx
145      * @return table of application options
146      */

147     public Map getApplicationOptions(String JavaDoc path) {
148         if (path.endsWith(".lzo")) {
149             path = path.substring(0, path.length()-1) + 'x';
150         }
151         return (Map)mAppOptions.get(path);
152     }
153
154     /**
155      * @param path application path - .lzo extensions treated like .lzx
156      * @param opts application option hashtable from LZX
157      */

158     public void setApplicationOptions(String JavaDoc path, Map opts) {
159         if (path.endsWith(".lzo")) {
160             path = path.substring(0, path.length()-1) + 'x';
161         }
162         mAppOptions.put(path, opts);
163     }
164
165
166     /**
167      * @return true if the option is allowed for given value of the
168      * given key
169      */

170     public boolean optionAllows(String JavaDoc key, String JavaDoc value) {
171         return optionAllows(key, value, true);
172     }
173
174     /**
175      * @param allow if true, an undefined option means that it is
176      * allowed, else it is denied.
177      * @return true if the option is allowed for given value of the
178      * given key
179      */

180     public boolean optionAllows(String JavaDoc key, String JavaDoc value, boolean allow) {
181         Option opt = (Option)mOptions.get(key);
182         if (opt != null) {
183             return opt.allows(value, allow);
184         } else {
185             mLogger.debug("No option for " + key +
186                           "; is allowed? " + allow);
187             return allow;
188         }
189     }
190
191
192     /**
193      * @param path application path relative to webapp.
194      * @return true if the option is allowed for given value of the
195      * given key
196      */

197     public boolean optionAllows(String JavaDoc path, String JavaDoc key, String JavaDoc value) {
198         return optionAllows(path, key, value, true);
199     }
200
201     /**
202      * @param path application path relative to webapp.
203      * @param allow if true, an undefined option means that it is
204      * allowed, else it is denied.
205      * @return true if the option is allowed for given value of the
206      * given key
207      */

208     public boolean optionAllows(String JavaDoc path, String JavaDoc key, String JavaDoc value,
209                                 boolean allow) {
210         Option opt;
211         Map appOptions;
212
213         // Check LZX configured application options.
214
appOptions = (Map)mAppOptions.get(path);
215         if (appOptions != null) {
216             opt = (Option)appOptions.get(key);
217             if (opt != null) {
218                 return opt.allows(value, allow);
219             }
220         }
221
222         // Check for server configured application option.
223
appOptions = (Map)mAppPaths.get(path);
224         if (appOptions != null) {
225             opt = (Option)appOptions.get(key);
226             if (opt != null)
227                 return opt.allows(value, allow);
228         }
229
230         // Check regexp patterns
231
if (! mAppRegexps.isEmpty()) {
232             ListIterator iter = mAppRegexps.listIterator();
233             while (iter.hasNext()) {
234                 RE re = (RE)iter.next();
235                 if (re.match(path)) {
236                     appOptions = (Map)mAppPatterns.get(re);
237                     if (appOptions != null) {
238                         opt = (Option)appOptions.get(key);
239                         if (opt != null) {
240                             return opt.allows(value, allow);
241                         }
242                     }
243                 }
244             }
245         }
246
247         // Check for global option.
248
return optionAllows(key, value, allow);
249     }
250 }
251
Popular Tags