1 22 package org.objectweb.petals.jbi.management.systemstate; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 31 import javax.xml.XMLConstants ; 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import javax.xml.transform.Source ; 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.TransformerFactory ; 37 import javax.xml.transform.dom.DOMSource ; 38 import javax.xml.transform.stream.StreamResult ; 39 import javax.xml.transform.stream.StreamSource ; 40 import javax.xml.validation.Schema ; 41 import javax.xml.validation.SchemaFactory ; 42 import javax.xml.validation.Validator ; 43 44 import org.objectweb.petals.tools.jbicommon.util.XMLUtil; 45 import org.objectweb.petals.util.SystemUtil; 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.Element ; 48 import org.w3c.dom.Node ; 49 import org.w3c.dom.NodeList ; 50 import org.xml.sax.SAXException ; 51 52 60 public class StateStorage { 61 public static final String ARCHIVE_PATH_ATTRIBUTE = "archive-path"; 62 63 public static final String COMPONENT_ELEMENT = "component"; 64 65 public static final String COMPONENTS_ELEMENT = "components"; 66 67 public static final String INSTALL_PATH_ATTRIBUTE = "install-path"; 68 69 public static final String INSTALL_STATE_ATTRIBUTE = "install-state"; 70 71 public static final String LIFECYCLE_STATE_ATTRIBUTE = "lifecycle-state"; 72 73 public static final String NAME_ATTRIBUTE = "name"; 74 75 public static final String SERVICE_ASSEMBLIES_ELEMENT = "service-assemblies"; 76 77 public static final String SERVICE_ASSEMBLY_ELEMENT = "service-assembly"; 78 79 public static final String SHARE_LIBRARY_ELEMENT = "shared-library"; 80 81 public static final String SHARED_LIBRARIES_ELEMENT = "shared-libraries"; 82 83 public static final String SYSTEM_STATE_ELEMENT = "system-state"; 84 85 protected static final String NS = "http://org.objectweb.petals/xml/ns/system-state"; 86 87 protected static final String SCHEMA_FILE_PATH = "schema" + File.separator 88 + "system-state.xsd"; 89 90 protected static final String STATE_FILE_PATH = "conf" + File.separator 91 + "system-state.xml"; 92 93 protected Document document; 94 95 protected File file; 96 97 protected File schemaFile; 98 99 public StateStorage() { 100 File installPath = SystemUtil.getPetalsInstallDirectory(); 101 102 if (installPath != null) { 103 file = new File (installPath.getAbsolutePath(), STATE_FILE_PATH); 104 105 schemaFile = new File (installPath.getAbsolutePath(), 106 SCHEMA_FILE_PATH); 107 } 108 } 109 110 public ComponentState createComponentStateHolder(String componentName, 111 String installURL, String zipURL, String installState, 112 String lifecycleState) throws Exception { 113 Node e = createComponentNode(componentName, installURL, zipURL, 114 installState, lifecycleState); 115 116 getComponentsNode().appendChild(e); 117 118 saveSystemState(); 119 120 return createComponent(e); 121 } 122 123 129 public ServiceAssemblyState createServiceAssemblyStateHolder(String saName, 130 String installURL, String zipURL, String lifecycleState) 131 throws Exception { 132 Node e = createServiceAssemblyNode(saName, installURL, zipURL, 133 lifecycleState); 134 135 getServiceAssembliesNode().appendChild(e); 136 137 saveSystemState(); 138 139 return createServiceAssembly(e); 140 } 141 142 public SharedLibraryState createSharedLibraryStateHolder(String slName, 143 String installURL, String zipURL) throws Exception { 144 Node e = createSharedLibraryNode(slName, installURL, zipURL); 145 146 getSharedLibrariesNode().appendChild(e); 147 148 saveSystemState(); 149 150 return createSharedLibrary(e); 151 } 152 153 public ComponentState deleteComponentStateHolder(String componentName) 154 throws Exception { 155 Node n = getComponentNode(componentName); 156 157 deleteStateAndSave(getComponentsNode(), n); 158 159 return createComponent(n); 160 } 161 162 public ServiceAssemblyState deleteServiceAssemblyStateHolder(String saName) 163 throws Exception { 164 Node n = getServiceAssemblyNode(saName); 165 166 deleteStateAndSave(getServiceAssembliesNode(), n); 167 168 return createServiceAssembly(n); 169 } 170 171 177 public SharedLibraryState deleteSharedLibraryStateHolder(String slName) 178 throws Exception { 179 Node n = getSharedLibraryNode(slName); 180 181 deleteStateAndSave(getSharedLibrariesNode(), n); 182 183 return createSharedLibrary(n); 184 } 185 186 195 public void init() throws Exception { 196 loadStateStorageDocument(); 197 validateSystemStateXml(); 198 } 199 200 public List <ComponentState> retrieveAllComponentStates() { 201 NodeList elements = getComponentsNode().getChildNodes(); 202 203 List <ComponentState> list = new ArrayList <ComponentState>(); 204 205 for (int i = 0; i < elements.getLength(); i++) { 206 list.add(createComponent(elements.item(i))); 207 } 208 return list; 209 } 210 211 public List <ServiceAssemblyState> retrieveAllServiceAssemblyStates() { 212 NodeList elements = getServiceAssembliesNode().getChildNodes(); 213 214 List <ServiceAssemblyState> list = new ArrayList <ServiceAssemblyState>(); 215 216 for (int i = 0; i < elements.getLength(); i++) { 217 list.add(createServiceAssembly(elements.item(i))); 218 } 219 return list; 220 } 221 222 public List <SharedLibraryState> retrieveAllSharedLibraryStates() { 223 NodeList elements = getSharedLibrariesNode().getChildNodes(); 224 225 List <SharedLibraryState> list = new ArrayList <SharedLibraryState>(); 226 227 for (int i = 0; i < elements.getLength(); i++) { 228 list.add(createSharedLibrary(elements.item(i))); 229 } 230 return list; 231 } 232 233 public void updateComponentInstallationState(String componentName, 234 String installState) throws Exception { 235 Node n = getComponentNode(componentName); 236 237 updateStateAndSave(n, INSTALL_STATE_ATTRIBUTE, installState); 238 } 239 240 public void updateComponentLifeCycleState(String componentName, 241 String lifecycleState) throws Exception { 242 Node n = getComponentNode(componentName); 243 244 updateStateAndSave(n, LIFECYCLE_STATE_ATTRIBUTE, lifecycleState); 245 } 246 247 public void updateServiceAssemblyState(String saName, String lifecycleState) 248 throws Exception { 249 Node n = getServiceAssemblyNode(saName); 250 251 updateStateAndSave(n, LIFECYCLE_STATE_ATTRIBUTE, lifecycleState); 252 } 253 254 261 public void validateSystemStateXml() throws SAXException , IOException { 262 SchemaFactory factory = SchemaFactory 264 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 265 266 Source schemaSource = new StreamSource (schemaFile); 268 Schema schema = factory.newSchema(schemaSource); 269 270 Validator validator = schema.newValidator(); 272 273 validator.validate(new StreamSource (file)); 275 } 276 277 283 protected ComponentState createComponent(Node node) { 284 String name = XMLUtil.getAttributeValue(node, NAME_ATTRIBUTE); 285 String iURL = XMLUtil.getAttributeValue(node, INSTALL_PATH_ATTRIBUTE); 286 String aURL = XMLUtil.getAttributeValue(node, ARCHIVE_PATH_ATTRIBUTE); 287 String iSte = XMLUtil.getAttributeValue(node, INSTALL_STATE_ATTRIBUTE); 288 String lSte = XMLUtil 289 .getAttributeValue(node, LIFECYCLE_STATE_ATTRIBUTE); 290 291 return new ComponentState(name, iURL, aURL, iSte, lSte); 292 } 293 294 305 protected Node createComponentNode(String componentName, String installURL, 306 String zipURL, String installState, String lifecycleState) 307 throws Exception { 308 Node element = XMLUtil.createNode(document, COMPONENT_ELEMENT, 309 NAME_ATTRIBUTE, componentName, INSTALL_PATH_ATTRIBUTE, 310 installURL, ARCHIVE_PATH_ATTRIBUTE, zipURL, 311 INSTALL_STATE_ATTRIBUTE, installState, 312 LIFECYCLE_STATE_ATTRIBUTE, lifecycleState); 313 314 return element; 315 } 316 317 323 protected ServiceAssemblyState createServiceAssembly(Node node) { 324 String name = XMLUtil.getAttributeValue(node, NAME_ATTRIBUTE); 325 String iURL = XMLUtil.getAttributeValue(node, INSTALL_PATH_ATTRIBUTE); 326 String aURL = XMLUtil.getAttributeValue(node, ARCHIVE_PATH_ATTRIBUTE); 327 String lSte = XMLUtil 328 .getAttributeValue(node, LIFECYCLE_STATE_ATTRIBUTE); 329 330 return new ServiceAssemblyState(name, iURL, aURL, lSte); 331 } 332 333 343 protected Node createServiceAssemblyNode(String saName, String installURL, 344 String zipURL, String lifecycleState) throws Exception { 345 Node element = XMLUtil.createNode(document, SERVICE_ASSEMBLY_ELEMENT, 346 NAME_ATTRIBUTE, saName, INSTALL_PATH_ATTRIBUTE, installURL, 347 ARCHIVE_PATH_ATTRIBUTE, zipURL, LIFECYCLE_STATE_ATTRIBUTE, 348 lifecycleState); 349 350 return element; 351 } 352 353 359 protected SharedLibraryState createSharedLibrary(Node node) { 360 String name = XMLUtil.getAttributeValue(node, NAME_ATTRIBUTE); 361 String iURL = XMLUtil.getAttributeValue(node, INSTALL_PATH_ATTRIBUTE); 362 String aURL = XMLUtil.getAttributeValue(node, ARCHIVE_PATH_ATTRIBUTE); 363 364 return new SharedLibraryState(name, iURL, aURL); 365 } 366 367 376 protected Node createSharedLibraryNode(String slName, String installURL, 377 String zipURL) throws Exception { 378 379 Node element = XMLUtil.createNode(document, SHARE_LIBRARY_ELEMENT, 380 NAME_ATTRIBUTE, slName, INSTALL_PATH_ATTRIBUTE, installURL, 381 ARCHIVE_PATH_ATTRIBUTE, zipURL); 382 383 return element; 384 } 385 386 392 400 protected void deleteStateAndSave(Node parent, Node child) throws Exception { 401 parent.removeChild(child); 402 403 saveSystemState(); 404 } 405 406 protected Node getComponentNode(String name) { 407 return getNode(COMPONENT_ELEMENT, name); 408 } 409 410 protected Node getComponentsNode() { 411 return XMLUtil.getNode(document, "*/" + COMPONENTS_ELEMENT); 412 } 413 414 protected Node getNode(String elementType, String name) { 415 return XMLUtil.getNode(document, "//*/" + elementType + "[@" 416 + NAME_ATTRIBUTE + "=\"" + name + "\"]"); 417 } 418 419 protected Node getServiceAssembliesNode() { 420 return XMLUtil.getNode(document, "*/" + SERVICE_ASSEMBLIES_ELEMENT); 421 } 422 423 protected Node getServiceAssemblyNode(String name) { 424 return getNode(SERVICE_ASSEMBLY_ELEMENT, name); 425 } 426 427 protected Node getSharedLibrariesNode() { 428 return XMLUtil.getNode(document, "*/" + SHARED_LIBRARIES_ELEMENT); 429 } 430 431 protected Node getSharedLibraryNode(String name) { 432 return getNode(SHARE_LIBRARY_ELEMENT, name); 433 } 434 435 440 protected void initializeDocument() { 441 NodeList children = document.getChildNodes(); 442 443 for (int i = 0; i < children.getLength(); i++) { 444 document.removeChild(children.item(i)); 445 } 446 447 449 Element s = document.createElement(SYSTEM_STATE_ELEMENT); 450 Node c = document.createElement(COMPONENTS_ELEMENT); 451 Node sl = document.createElement(SHARED_LIBRARIES_ELEMENT); 452 Node sa = document.createElement(SERVICE_ASSEMBLIES_ELEMENT); 453 454 s.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, StateStorage.NS); 455 document.appendChild(s); 456 s.appendChild(c); 457 s.appendChild(sl); 458 s.appendChild(sa); 459 } 460 461 465 protected void loadStateStorageDocument() throws Exception { 466 if (!file.exists()) { 468 file.createNewFile(); 469 } 470 471 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory 472 .newInstance(); 473 474 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 475 476 if ((new FileInputStream (file)).available() == 0) { 478 document = docBuilder.newDocument(); 479 initializeDocument(); 480 saveSystemState(); 481 } else { 482 document = docBuilder.parse(file); 483 } 484 485 } 486 487 492 protected void saveSystemState() throws Exception { 493 Transformer transformer = TransformerFactory.newInstance() 494 .newTransformer(); 495 496 FileWriter fileWriter = new FileWriter (file); 497 transformer.transform(new DOMSource (document), new StreamResult ( 498 fileWriter)); 499 } 500 501 510 protected void updateStateAndSave(Node node, String att, String value) 511 throws Exception { 512 node.getAttributes().getNamedItem(att).setNodeValue(value); 513 514 saveSystemState(); 515 } 516 } 517 | Popular Tags |