1 64 65 package com.jcorporate.expresso.kernel.management; 66 67 import com.jcorporate.expresso.kernel.ComponentBase; 68 import com.jcorporate.expresso.kernel.ComponentLifecycle; 69 import com.jcorporate.expresso.kernel.Configuration; 70 import com.jcorporate.expresso.kernel.RootContainerInterface; 71 import com.jcorporate.expresso.kernel.digester.ComponentConfig; 72 import com.jcorporate.expresso.kernel.digester.ExpressoServicesConfig; 73 import com.jcorporate.expresso.kernel.exception.ConfigurationException; 74 import com.jcorporate.expresso.kernel.exception.ExpressoRuntimeException; 75 import com.jcorporate.expresso.kernel.util.ClassLocator; 76 import org.w3c.dom.Document ; 77 import org.w3c.dom.Element ; 78 79 import javax.xml.parsers.DocumentBuilderFactory ; 80 import javax.xml.parsers.FactoryConfigurationError ; 81 import javax.xml.parsers.ParserConfigurationException ; 82 import java.io.BufferedOutputStream ; 83 import java.io.File ; 84 import java.io.FileOutputStream ; 85 import java.io.IOException ; 86 import java.io.OutputStream ; 87 import java.util.Iterator ; 88 import java.util.Map ; 89 90 91 102 103 public class DefaultServiceWriter extends ComponentBase implements ServiceWriter, ComponentLifecycle { 104 DOMWriter domWriter = null; 105 String domWriterClass = null; 106 107 110 public DefaultServiceWriter() { 111 super(); 112 } 113 114 120 public DOMWriter getDOMWriter() { 121 return domWriter; 122 } 123 124 133 public void writeServicesFile(RootContainerInterface rootContainer, String location) throws ExpressoRuntimeException { 134 try { 135 File f = new File (location); 136 if (f == null) { 137 throw new ExpressoRuntimeException("Unable to open file " + location + " for writing"); 138 } 139 140 f.createNewFile(); 141 OutputStream os = new BufferedOutputStream (new FileOutputStream (f)); 142 this.writeServicesFile(rootContainer, os); 143 os.flush(); 144 os.close(); 145 } catch (IOException ex) { 146 throw new ExpressoRuntimeException("I/O error writing config file", ex); 147 } 148 } 149 150 158 public void writeServicesFile(RootContainerInterface rootContainer, OutputStream os) throws ExpressoRuntimeException { 159 Document d = buildDOMTree(rootContainer.getExpressoServicesConfig()); 160 this.getDOMWriter().saveDocument(os, d); 161 } 162 163 164 172 protected Document buildDOMTree(ExpressoServicesConfig configRoot) throws ExpressoRuntimeException { 173 try { 174 Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 175 Element root = d.createElement("expresso-services"); 176 ComponentConfig c = configRoot.getRootConfig(); 177 d.appendChild(root); 178 processComponentProperties(c, root, d); 179 180 for (Iterator i = c.getChildComponents().iterator(); i.hasNext();) { 181 ComponentConfig oneChild = (ComponentConfig) i.next(); 182 processComponent(oneChild, root, d); 183 } 184 185 return d; 186 } catch (ParserConfigurationException ex) { 187 throw new ExpressoRuntimeException("DOM Parser Configuration Error saving file", ex); 188 } catch (FactoryConfigurationError ex) { 189 throw new ExpressoRuntimeException("DOM Document Factory Configuration Error saving file", ex); 190 } 191 } 192 193 201 protected void processComponent(ComponentConfig config, Element parent, Document dom) { 202 Element thisComponent = dom.createElement("component"); 203 thisComponent.setAttribute("name", config.getName()); 204 thisComponent.setAttribute("class-name", config.getClassName()); 205 processComponentProperties(config, thisComponent, dom); 206 parent.appendChild(thisComponent); 207 208 for (Iterator i = config.getChildComponents().iterator(); i.hasNext();) { 209 ComponentConfig oneChild = (ComponentConfig) i.next(); 210 processComponent(oneChild, thisComponent, dom); 211 } 212 } 213 214 221 protected void processComponentProperties(ComponentConfig config, Element parent, Document dom) { 222 for (Iterator i = config.getProperties().keySet().iterator(); i.hasNext();) { 226 String propName = (String ) i.next(); 227 String propValue = config.getProperty(propName); 228 Element property = dom.createElement("property"); 229 property.setAttribute("name", propName); 230 property.setAttribute("value", propValue); 231 parent.appendChild(property); 232 } 233 234 Map allMappedProperties = (Map ) config.getAllMappedProperties(); 238 for (Iterator i = allMappedProperties.keySet().iterator(); i.hasNext();) { 239 String propertyName = (String ) i.next(); 240 Map subMap = (Map ) allMappedProperties.get(propertyName); 241 for (Iterator j = subMap.keySet().iterator(); j.hasNext();) { 242 String propertyKey = (String ) j.next(); 243 String propertyValue = (String ) subMap.get(propertyKey); 244 Element property = dom.createElement("mapped-property"); 245 property.setAttribute("name", propertyName); 246 property.setAttribute("key", propertyKey); 247 property.setAttribute("value", propertyValue); 248 parent.appendChild(property); 249 } 250 } 251 252 Map allIndexedProperties = (Map ) config.getAllIndexedProperties(); 256 for (Iterator i = allIndexedProperties.keySet().iterator(); i.hasNext();) { 257 String propertyName = (String ) i.next(); 258 Map subMap = (Map ) allMappedProperties.get(propertyName); 259 for (Iterator j = subMap.keySet().iterator(); j.hasNext();) { 260 Integer propertyIndex = (Integer ) j.next(); 261 String propertyValue = (String ) subMap.get(propertyIndex); 262 Element property = dom.createElement("indexed-property"); 263 property.setAttribute("name", propertyName); 264 property.setAttribute("index", propertyIndex.toString()); 265 property.setAttribute("value", propertyValue); 266 parent.appendChild(property); 267 } 268 } 269 } 270 271 public void initialize() { 272 273 } 274 275 283 public void configure(Configuration newConfig) throws ConfigurationException { 284 String temp = (String ) newConfig.get("domWriterClass"); 285 try { 286 setDomWriterClass(temp); 287 } catch (ExpressoRuntimeException ex) { 288 throw new ConfigurationException("Invalid paramter 'serviceWriterClass'=" 289 + temp, ex.getNested()); 290 } 291 } 292 293 302 public void reconfigure(Configuration newConfig) throws ConfigurationException { 303 String rollBackClass = this.getDomWriterClass(); 304 try { 305 configure(newConfig); 306 } catch (ConfigurationException ex) { 307 try { 308 this.setDomWriterClass(rollBackClass); 309 } catch (ExpressoRuntimeException ex1) { 310 throw new ConfigurationException("Error rolling back configuration", ex1); 311 } 312 313 throw new ConfigurationException("Error setting configuration", ex); 314 } 315 } 316 317 321 public void destroy() { 322 domWriter = null; 323 domWriterClass = null; 324 } 325 326 331 public String getDomWriterClass() { 332 return domWriterClass; 333 } 334 335 340 public void setDomWriterClass(String domWriterClass) throws ExpressoRuntimeException { 341 try { 342 DOMWriter tmp = (DOMWriter) (ClassLocator.loadClass(domWriterClass).newInstance()); 343 this.domWriterClass = domWriterClass; 344 domWriter = tmp; 345 } catch (Exception ex) { 346 throw new ExpressoRuntimeException("Error setting DOM Writer Class", ex); 347 } 348 } 349 } | Popular Tags |