1 4 5 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 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(); 39 private static Logger mLogger = Logger.getLogger(Configuration.class); 40 41 44 public Configuration() 45 { 46 try { 47 mLogger.debug("building configuration"); 48 SAXBuilder builder = new SAXBuilder(); 49 50 String 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 if (elt.getName().equals("option")) { 61 String 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 if (elt.getName().equals("application")) { 70 String path = elt.getAttributeValue("path"); 71 String pattern = elt.getAttributeValue("pattern"); 72 73 if (path != null && ! path.equals("") && 74 pattern != null && ! pattern.equals("")) { 75 String 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 115 public static Hashtable getOptions(Element app, String tagname, String 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 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 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 147 public Map getApplicationOptions(String 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 158 public void setApplicationOptions(String 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 170 public boolean optionAllows(String key, String value) { 171 return optionAllows(key, value, true); 172 } 173 174 180 public boolean optionAllows(String key, String 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 197 public boolean optionAllows(String path, String key, String value) { 198 return optionAllows(path, key, value, true); 199 } 200 201 208 public boolean optionAllows(String path, String key, String value, 209 boolean allow) { 210 Option opt; 211 Map appOptions; 212 213 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 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 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 return optionAllows(key, value, allow); 249 } 250 } 251 | Popular Tags |