001: /******************************************************************************
002: * Configuration.java
003: * ****************************************************************************/package org.openlaszlo.server;
004:
005: import java.io.*;
006: import java.util.*;
007:
008: import org.openlaszlo.utils.ChainedException;
009:
010: import org.apache.log4j.Logger;
011: import org.apache.regexp.RE;
012: import org.apache.regexp.RESyntaxException;
013: import org.jdom.*;
014: import org.jdom.input.SAXBuilder;
015: import org.jdom.filter.ElementFilter;
016:
017: /**
018: * Configuration contains global server configuration information.
019: * It reads an array of options from an xml file.
020: *
021: * @author Eric Bloch
022: * @version 1.0
023: */
024: public class Configuration {
025:
026: Map mOptions = new Hashtable();
027: Map mAppPaths = new Hashtable();
028: Map mAppPatterns = new Hashtable();
029: ArrayList mAppRegexps = new ArrayList();
030: Map mAppOptions = new Hashtable(); // options set in application LZX
031: Map mDebugRegexps = new Hashtable();
032:
033: private static Logger mLogger = Logger
034: .getLogger(Configuration.class);
035:
036: /**
037: * Constructs a new configuration
038: */
039: public Configuration() {
040: try {
041: mLogger.debug("building configuration");
042: SAXBuilder builder = new SAXBuilder();
043:
044: String fileName = LPS.getConfigDirectory() + File.separator
045: + "lps.xml";
046: Document doc = builder.build(new File(fileName));
047: Element root = doc.getRootElement();
048: List elts = root.getContent(new ElementFilter());
049: ListIterator iter = elts.listIterator();
050:
051: while (iter.hasNext()) {
052: Element elt = (Element) iter.next();
053:
054: // Check for global default options.
055: if (elt.getName().equals("option")) {
056: String name = elt.getAttributeValue("name");
057: if (name != null && !name.equals("")) {
058: Option option = new Option(elt);
059: mOptions.put(name, option);
060: }
061: }
062:
063: // Check for application specific options
064: if (elt.getName().equals("application")) {
065: String path = elt.getAttributeValue("path");
066: String pattern = elt.getAttributeValue("pattern");
067:
068: if (path != null && !path.equals("")
069: && pattern != null && !pattern.equals("")) {
070: String msg =
071: /* (non-Javadoc)
072: * @i18n.test
073: * @org-mes="Can't have attributes path (" + p[0] + ")" + " and pattern (" + p[1] + ") " + " defined in an application element together."
074: */
075: org.openlaszlo.i18n.LaszloMessages.getMessage(
076: Configuration.class.getName(),
077: "051019-80", new Object[] { path,
078: pattern });
079:
080: mLogger.debug(
081: /* (non-Javadoc)
082: * @i18n.test
083: * @org-mes="Exception reading configuration: " + p[0]
084: */
085: org.openlaszlo.i18n.LaszloMessages.getMessage(
086: Configuration.class.getName(),
087: "051019-88", new Object[] { msg }));
088: throw new ChainedException(msg);
089: }
090:
091: if (path != null && !path.equals("")) {
092: Hashtable appOpts = getOptions(elt, "option",
093: "name");
094: if (appOpts.size() != 0)
095: mAppPaths.put(path, appOpts);
096: }
097:
098: if (pattern != null && !pattern.equals("")) {
099: Hashtable appOpts = getOptions(elt, "option",
100: "name");
101: if (appOpts.size() != 0) {
102: RE re = new RE(pattern);
103: mDebugRegexps.put(re, pattern);
104: mAppRegexps.add(re);
105: mAppPatterns.put(re, appOpts);
106: }
107: }
108: }
109: }
110: } catch (RESyntaxException e) {
111: mLogger.error(
112: /* (non-Javadoc)
113: * @i18n.test
114: * @org-mes="RE exception reading configuration " + p[0]
115: */
116: org.openlaszlo.i18n.LaszloMessages.getMessage(
117: Configuration.class.getName(), "051019-116",
118: new Object[] { e.getMessage() }));
119: throw new ChainedException(e.getMessage());
120: } catch (JDOMException e) {
121: mLogger.error(
122: /* (non-Javadoc)
123: * @i18n.test
124: * @org-mes="jdom exception reading configuration " + p[0]
125: */
126: org.openlaszlo.i18n.LaszloMessages.getMessage(
127: Configuration.class.getName(), "051019-126",
128: new Object[] { e.getMessage() }));
129: throw new ChainedException(e.getMessage());
130: } catch (IOException e) {
131: mLogger.error("io exception reading configuration "
132: + e.getMessage());
133: throw new ChainedException(e.getMessage());
134: }
135: }
136:
137: /**
138: * @param app element
139: * @param tagname tag name
140: * @param optname option name
141: * @return hashtable of options
142: */
143: public static Hashtable getOptions(Element app, String tagname,
144: String optname) {
145: Hashtable options = new Hashtable();
146: List elts = app.getContent(new ElementFilter());
147: ListIterator iter = elts.listIterator();
148: while (iter.hasNext()) {
149: Element elt = (Element) iter.next();
150: if (elt.getName().equals(tagname)) {
151: String name = elt.getAttributeValue(optname);
152: if (name != null && !name.equals("")) {
153: addOptionNamed(options, elt, name);
154: }
155: }
156: }
157: return options;
158: }
159:
160: public String getPattern(RE re) {
161: return (String) mDebugRegexps.get(re);
162: }
163:
164: public static void addOption(Map options, Element elt) {
165: String name = elt.getName();
166: Option option = (Option) options.get(name);
167: if (option == null) {
168: option = new Option(elt);
169: options.put(name, option);
170: } else {
171: option.addElement(elt);
172: }
173: }
174:
175: public static void addOptionNamed(Map options, Element elt,
176: String name) {
177: Option option = (Option) options.get(name);
178: if (option == null) {
179: option = new Option(elt);
180: options.put(name, option);
181: } else {
182: option.addElement(elt);
183: }
184: }
185:
186: /**
187: * @param path application path
188: * @return table of application options
189: */
190: public Map getApplicationOptions(String path) {
191: return (Map) mAppOptions.get(path);
192: }
193:
194: /**
195: * @param path application path
196: * @param opts application option hashtable from LZX
197: */
198: public void setApplicationOptions(String path, Map opts) {
199: mAppOptions.put(path, opts);
200: }
201:
202: /**
203: * @return true if the option is allowed for given value of the
204: * given key
205: */
206: public boolean optionAllows(String key, String value) {
207: return optionAllows(key, value, true);
208: }
209:
210: /**
211: * @param allow if true, an undefined option means that it is
212: * allowed, else it is denied.
213: * @return true if the option is allowed for given value of the
214: * given key
215: */
216: public boolean optionAllows(String key, String value, boolean allow) {
217: Option opt = (Option) mOptions.get(key);
218: if (opt != null) {
219: return opt.allows(value, allow);
220: } else {
221: mLogger.debug(
222: /* (non-Javadoc)
223: * @i18n.test
224: * @org-mes="No option for " + p[0] + "; is allowed? " + p[1]
225: */
226: org.openlaszlo.i18n.LaszloMessages.getMessage(
227: Configuration.class.getName(), "051019-215",
228: new Object[] { key, new Boolean(allow) }));
229: return allow;
230: }
231: }
232:
233: /**
234: * @param path application path relative to webapp.
235: * @return true if the option is allowed for given value of the
236: * given key
237: */
238: public boolean optionAllows(String path, String key, String value) {
239: return optionAllows(path, key, value, true);
240: }
241:
242: /**
243: * @param path application path relative to webapp.
244: * @param allow if true, an undefined option means that it is
245: * allowed, else it is denied.
246: * @return true if the option is allowed for given value of the
247: * given key
248: */
249: public boolean optionAllows(String path, String key, String value,
250: boolean allow) {
251: Option opt;
252: Map appOptions;
253:
254: // Check LZX configured application options.
255: appOptions = (Map) mAppOptions.get(path);
256: if (appOptions != null) {
257: opt = (Option) appOptions.get(key);
258: if (opt != null) {
259: return opt.allows(value, allow);
260: }
261: }
262:
263: // Check for server configured application option.
264: appOptions = (Map) mAppPaths.get(path);
265: if (appOptions != null) {
266: opt = (Option) appOptions.get(key);
267: if (opt != null) {
268: return opt.allows(value, allow);
269: }
270: }
271:
272: // Check regexp patterns
273: if (!mAppRegexps.isEmpty()) {
274: ListIterator iter = mAppRegexps.listIterator();
275: while (iter.hasNext()) {
276: RE re = (RE) iter.next();
277: if (re.match(path)) {
278: appOptions = (Map) mAppPatterns.get(re);
279: if (appOptions != null) {
280: opt = (Option) appOptions.get(key);
281: if (opt != null) {
282: return opt.allows(value, allow);
283: }
284: }
285: }
286: }
287: }
288:
289: // Check for global option.
290: return optionAllows(key, value, allow);
291: }
292: }
|