1 64 package com.jcorporate.expresso.kernel.digester; 65 66 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 67 import org.apache.commons.digester.Digester; 68 import org.apache.log4j.Logger; 69 import org.xml.sax.SAXException ; 70 71 import javax.xml.parsers.FactoryConfigurationError ; 72 import javax.xml.parsers.ParserConfigurationException ; 73 import java.io.File ; 74 import java.io.FileInputStream ; 75 import java.io.InputStream ; 76 import java.net.URL ; 77 78 79 84 85 public class ExpressoServicesConfig implements java.io.Serializable { 86 87 92 private static final int MAX_NESTING = 20; 93 94 95 98 private Logger log = Logger.getLogger(ExpressoServicesConfig.class); 99 100 103 private String fileName = null; 104 105 108 private URL fileURL = null; 109 110 113 private ComponentConfig root; 114 115 118 SaxParserConfigurer saxParserConfig; 119 120 121 124 public ExpressoServicesConfig() { 125 saxParserConfig = new SaxParserConfigurer(); 126 } 127 128 public static ExpressoServicesConfig newBlankContainer() { 129 ExpressoServicesConfig esc = new ExpressoServicesConfig(); 130 esc.initBlankContainer(); 131 return esc; 132 } 133 134 public void initBlankContainer() { 135 root = new ComponentConfig(); 136 } 137 138 142 public void loadExpressoServices() { 143 try { 144 javax.xml.parsers.SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance(); 145 javax.xml.parsers.SAXParser sp = spf.newSAXParser(); 146 147 Digester digester = new Digester(sp); 148 149 150 URL url = this.getClass().getResource("/com/jcorporate/expresso/kernel/expresso-services_5_1.dtd"); 151 if (url != null) { 152 digester.register("-//Jcorporate Ltd//DTD Expresso Services Configuration 5.1//EN", 153 url.toString()); 154 } else { 155 throw new IllegalArgumentException ("Unable to locate " + 156 "expresso-services_5_1.dtd in component package"); 157 } 158 159 160 setDigesterRules(digester); 161 162 InputStream is; 163 164 if (fileName != null) { 165 File f = new File (fileName); 166 is = new FileInputStream (f); 167 } else if (fileURL != null) { 168 is = fileURL.openStream(); 169 } else { 170 throw new IllegalArgumentException ("Must set the filename to parse before loading Expresso Services"); 171 } 172 173 digester.parse(is); 174 } catch (FactoryConfigurationError ex) { 175 log.error("Fatal error trying to find a suitable Digester compatible parser.", ex); 176 } catch (SAXException ex) { 177 log.error("Fatal error trying to digest expresso-services.xml file", ex); 178 } catch (ParserConfigurationException ex) { 179 log.error("Fatal error trying to find a suitable Digester compatible parser.", ex); 180 } catch (java.io.IOException ex) { 181 log.error("Fatal IO error parsing input.", ex); 182 } 183 184 } 185 186 191 public void setExpressoServicesFile(String fileName) { 192 if (log.isDebugEnabled()) { 193 log.debug("Setting up to load Expresso Services file from: " + fileName); 194 } 195 this.fileName = fileName; 196 } 197 198 203 public String getExpressoServicesFile() { 204 return this.fileName; 205 } 206 207 212 public void setExpressoServicesFile(URL url) { 213 if (log.isDebugEnabled()) { 214 log.debug("Setting up to load Expresso Services file from: " + url.toString()); 215 } 216 this.fileURL = url; 217 } 218 219 225 public URL getExpressoServicesFileURL() { 226 return this.fileURL; 227 } 228 229 230 238 protected void setDigesterRules(Digester digester) { 239 root = new ComponentConfig(); 240 digester.push(root); 241 digester.addSetProperties("expresso-services"); 242 digester.addCallMethod("expresso-services/property", "addProperty", 2); 243 digester.addCallParam("expresso-services/property", 0, "name"); 244 digester.addCallParam("expresso-services/property", 1, "value"); 245 246 digester.addCallMethod("expresso-services/mapped-property", "addMappedProperty", 3); 247 digester.addCallParam("expresso-services/mapped-property", 0, "name"); 248 digester.addCallParam("expresso-services/mapped-property", 1, "key"); 249 digester.addCallParam("expresso-services/mapped-property", 2, "value"); 250 251 digester.addCallMethod("expresso-services/indexed-property", "addMappedProperty", 3); 252 digester.addCallParam("expresso-services/indexed-property", 0, "name"); 253 digester.addCallParam("expresso-services/indexed-property", 1, "index"); 254 digester.addCallParam("expresso-services/indexed-property", 2, "value"); 255 256 257 for (int i = 0; i < MAX_NESTING; i++) { 258 String componentDepthString = getComponentDepthString(i); 259 digester.addObjectCreate("expresso-services/" + componentDepthString + "component", 260 com.jcorporate.expresso.kernel.digester.ComponentConfig.class); 261 digester.addSetProperties("expresso-services/" + componentDepthString + "component", 262 new String []{"name", "class-name"}, new String []{"name", "className"}); 263 digester.addCallMethod("expresso-services/" + componentDepthString + "component/property", "addProperty", 264 2); 265 digester.addCallParam("expresso-services/" + componentDepthString + "component/property", 0, "name"); 266 digester.addCallParam("expresso-services/" + componentDepthString + "component/property", 1, "value"); 267 268 digester.addCallMethod("expresso-services/" + componentDepthString + "component/mapped-property", 269 "addMappedProperty", 3); 270 digester.addCallParam("expresso-services/" + componentDepthString + "component/mapped-property", 0, "name"); 271 digester.addCallParam("expresso-services/" + componentDepthString + "component/mapped-property", 1, "key"); 272 digester.addCallParam("expresso-services/" + componentDepthString + "component/mapped-property", 2, 273 "value"); 274 275 digester.addCallMethod("expresso-services/" + componentDepthString + "component/indexed-property", 276 "addMappedProperty", 3); 277 digester.addCallParam("expresso-services/" + componentDepthString + "component/indexed-property", 0, 278 "name"); 279 digester.addCallParam("expresso-services/" + componentDepthString + "component/indexed-property", 1, 280 "index"); 281 digester.addCallParam("expresso-services/" + componentDepthString + "component/indexed-property", 2, 282 "value"); 283 284 digester.addSetNext("expresso-services/" + componentDepthString + "component", "addChildComponent"); 285 } 286 287 } 288 289 private String getComponentDepthString(int index) { 290 FastStringBuffer fsb = FastStringBuffer.getInstance(); 291 for (int i = 0; i < index; i++) { 292 fsb.append("component/"); 293 } 294 295 String returnValue = fsb.toString(); 296 fsb.release(); 297 298 return returnValue; 299 } 300 301 307 public ComponentConfig getRootConfig() { 308 return root; 309 } 310 311 319 public boolean equals(Object parm1) { 320 if (parm1 == null) { 321 return false; 322 } 323 324 if (!(parm1 instanceof ExpressoServicesConfig)) { 325 return false; 326 } 327 328 ExpressoServicesConfig other = (ExpressoServicesConfig) parm1; 329 330 ComponentConfig root = getRootConfig(); 331 ComponentConfig otherRoot = other.getRootConfig(); 332 333 if ((root == null) ^ (otherRoot == null)) { 334 return false; 335 } 336 337 if (root == null && otherRoot == null) { 338 return true; 339 } 340 341 342 return root.equals(otherRoot); 343 } 344 345 352 public String getName() { 353 return root.getName(); 354 } 355 356 } | Popular Tags |