KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > systemstate > StateStorage


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: StateStorage.java 154 27 avr. 2006 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.systemstate;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.xml.XMLConstants JavaDoc;
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import javax.xml.transform.Source JavaDoc;
35 import javax.xml.transform.Transformer JavaDoc;
36 import javax.xml.transform.TransformerFactory JavaDoc;
37 import javax.xml.transform.dom.DOMSource JavaDoc;
38 import javax.xml.transform.stream.StreamResult JavaDoc;
39 import javax.xml.transform.stream.StreamSource JavaDoc;
40 import javax.xml.validation.Schema JavaDoc;
41 import javax.xml.validation.SchemaFactory JavaDoc;
42 import javax.xml.validation.Validator JavaDoc;
43
44 import org.objectweb.petals.tools.jbicommon.util.XMLUtil;
45 import org.objectweb.petals.util.SystemUtil;
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.Element JavaDoc;
48 import org.w3c.dom.Node JavaDoc;
49 import org.w3c.dom.NodeList JavaDoc;
50 import org.xml.sax.SAXException JavaDoc;
51
52 /**
53  * Creates and reads state files for components, shared libs and service
54  * assemblies. State files are text files that keep the state of JBI entities to
55  * allow state retreval after Petals system shutdown or crash.
56  *
57  * @author Adrien LOUIS - EBM Websourcing
58  *
59  */

60 public class StateStorage {
61     public static final String JavaDoc ARCHIVE_PATH_ATTRIBUTE = "archive-path";
62
63     public static final String JavaDoc COMPONENT_ELEMENT = "component";
64
65     public static final String JavaDoc COMPONENTS_ELEMENT = "components";
66
67     public static final String JavaDoc INSTALL_PATH_ATTRIBUTE = "install-path";
68
69     public static final String JavaDoc INSTALL_STATE_ATTRIBUTE = "install-state";
70
71     public static final String JavaDoc LIFECYCLE_STATE_ATTRIBUTE = "lifecycle-state";
72
73     public static final String JavaDoc NAME_ATTRIBUTE = "name";
74
75     public static final String JavaDoc SERVICE_ASSEMBLIES_ELEMENT = "service-assemblies";
76
77     public static final String JavaDoc SERVICE_ASSEMBLY_ELEMENT = "service-assembly";
78
79     public static final String JavaDoc SHARE_LIBRARY_ELEMENT = "shared-library";
80
81     public static final String JavaDoc SHARED_LIBRARIES_ELEMENT = "shared-libraries";
82
83     public static final String JavaDoc SYSTEM_STATE_ELEMENT = "system-state";
84
85     protected static final String JavaDoc NS = "http://org.objectweb.petals/xml/ns/system-state";
86
87     protected static final String JavaDoc SCHEMA_FILE_PATH = "schema" + File.separator
88             + "system-state.xsd";
89
90     protected static final String JavaDoc STATE_FILE_PATH = "conf" + File.separator
91             + "system-state.xml";
92
93     protected Document JavaDoc document;
94
95     protected File JavaDoc file;
96
97     protected File JavaDoc schemaFile;
98
99     public StateStorage() {
100         File JavaDoc installPath = SystemUtil.getPetalsInstallDirectory();
101
102         if (installPath != null) {
103             file = new File JavaDoc(installPath.getAbsolutePath(), STATE_FILE_PATH);
104
105             schemaFile = new File JavaDoc(installPath.getAbsolutePath(),
106                     SCHEMA_FILE_PATH);
107         }
108     }
109
110     public ComponentState createComponentStateHolder(String JavaDoc componentName,
111             String JavaDoc installURL, String JavaDoc zipURL, String JavaDoc installState,
112             String JavaDoc lifecycleState) throws Exception JavaDoc {
113         Node JavaDoc e = createComponentNode(componentName, installURL, zipURL,
114                 installState, lifecycleState);
115
116         getComponentsNode().appendChild(e);
117
118         saveSystemState();
119
120         return createComponent(e);
121     }
122
123     // ///////////////////////////////////////////////////////////////////////
124
//
125
// Document <-> File management methods
126
//
127
// ///////////////////////////////////////////////////////////////////////
128

129     public ServiceAssemblyState createServiceAssemblyStateHolder(String JavaDoc saName,
130             String JavaDoc installURL, String JavaDoc zipURL, String JavaDoc lifecycleState)
131             throws Exception JavaDoc {
132         Node JavaDoc 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 JavaDoc slName,
143             String JavaDoc installURL, String JavaDoc zipURL) throws Exception JavaDoc {
144         Node JavaDoc 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 JavaDoc componentName)
154             throws Exception JavaDoc {
155         Node JavaDoc n = getComponentNode(componentName);
156
157         deleteStateAndSave(getComponentsNode(), n);
158
159         return createComponent(n);
160     }
161
162     public ServiceAssemblyState deleteServiceAssemblyStateHolder(String JavaDoc saName)
163             throws Exception JavaDoc {
164         Node JavaDoc n = getServiceAssemblyNode(saName);
165
166         deleteStateAndSave(getServiceAssembliesNode(), n);
167
168         return createServiceAssembly(n);
169     }
170
171     // ///////////////////////////////////////////////////////////////////////
172
//
173
// getNodeXXX methods
174
//
175
// ///////////////////////////////////////////////////////////////////////
176

177     public SharedLibraryState deleteSharedLibraryStateHolder(String JavaDoc slName)
178             throws Exception JavaDoc {
179         Node JavaDoc n = getSharedLibraryNode(slName);
180
181         deleteStateAndSave(getSharedLibrariesNode(), n);
182
183         return createSharedLibrary(n);
184     }
185
186     /**
187      * Initialize the StateStorage object. The recovery-file is created or read,
188      * then this object is setup with the information contained in the file.
189      *
190      * @throws IOException
191      * problems accessing the file
192      * @throws SAXException
193      * the structure of the xml-file is not valid.
194      */

195     public void init() throws Exception JavaDoc {
196         loadStateStorageDocument();
197         validateSystemStateXml();
198     }
199
200     public List JavaDoc<ComponentState> retrieveAllComponentStates() {
201         NodeList JavaDoc elements = getComponentsNode().getChildNodes();
202
203         List JavaDoc<ComponentState> list = new ArrayList JavaDoc<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 JavaDoc<ServiceAssemblyState> retrieveAllServiceAssemblyStates() {
212         NodeList JavaDoc elements = getServiceAssembliesNode().getChildNodes();
213
214         List JavaDoc<ServiceAssemblyState> list = new ArrayList JavaDoc<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 JavaDoc<SharedLibraryState> retrieveAllSharedLibraryStates() {
223         NodeList JavaDoc elements = getSharedLibrariesNode().getChildNodes();
224
225         List JavaDoc<SharedLibraryState> list = new ArrayList JavaDoc<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 JavaDoc componentName,
234             String JavaDoc installState) throws Exception JavaDoc {
235         Node JavaDoc n = getComponentNode(componentName);
236
237         updateStateAndSave(n, INSTALL_STATE_ATTRIBUTE, installState);
238     }
239
240     public void updateComponentLifeCycleState(String JavaDoc componentName,
241             String JavaDoc lifecycleState) throws Exception JavaDoc {
242         Node JavaDoc n = getComponentNode(componentName);
243
244         updateStateAndSave(n, LIFECYCLE_STATE_ATTRIBUTE, lifecycleState);
245     }
246
247     public void updateServiceAssemblyState(String JavaDoc saName, String JavaDoc lifecycleState)
248             throws Exception JavaDoc {
249         Node JavaDoc n = getServiceAssemblyNode(saName);
250
251         updateStateAndSave(n, LIFECYCLE_STATE_ATTRIBUTE, lifecycleState);
252     }
253
254     /**
255      *
256      * @param sourceURI
257      * @return
258      * @throws SAXException
259      * @throws IOException
260      */

261     public void validateSystemStateXml() throws SAXException JavaDoc, IOException JavaDoc {
262         // create a SchemaFactory capable of understanding WXS schemas
263
SchemaFactory JavaDoc factory = SchemaFactory
264                 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
265
266         // load a WXS schema, represented by a Schema instance
267
Source JavaDoc schemaSource = new StreamSource JavaDoc(schemaFile);
268         Schema JavaDoc schema = factory.newSchema(schemaSource);
269
270         // create a Validator instance which is be used to validate a document
271
Validator JavaDoc validator = schema.newValidator();
272
273         // validate
274
validator.validate(new StreamSource JavaDoc(file));
275     }
276
277     /**
278      * Create a ComponentState from the given node
279      *
280      * @param node
281      * @return a ComponentState , null if node is null
282      */

283     protected ComponentState createComponent(Node JavaDoc node) {
284         String JavaDoc name = XMLUtil.getAttributeValue(node, NAME_ATTRIBUTE);
285         String JavaDoc iURL = XMLUtil.getAttributeValue(node, INSTALL_PATH_ATTRIBUTE);
286         String JavaDoc aURL = XMLUtil.getAttributeValue(node, ARCHIVE_PATH_ATTRIBUTE);
287         String JavaDoc iSte = XMLUtil.getAttributeValue(node, INSTALL_STATE_ATTRIBUTE);
288         String JavaDoc lSte = XMLUtil
289                 .getAttributeValue(node, LIFECYCLE_STATE_ATTRIBUTE);
290
291         return new ComponentState(name, iURL, aURL, iSte, lSte);
292     }
293
294     /**
295      * create a Node representing a component state
296      *
297      * @param componentName
298      * @param installURL
299      * @param zipURL
300      * @param installState
301      * @param lifecycleState
302      * @return
303      * @throws Exception
304      */

305     protected Node JavaDoc createComponentNode(String JavaDoc componentName, String JavaDoc installURL,
306             String JavaDoc zipURL, String JavaDoc installState, String JavaDoc lifecycleState)
307             throws Exception JavaDoc {
308         Node JavaDoc 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     /**
318      * Create a ServiceAssemblyState from the given node
319      *
320      * @param node
321      * @return a ServiceAssemblyState , null if node is null
322      */

323     protected ServiceAssemblyState createServiceAssembly(Node JavaDoc node) {
324         String JavaDoc name = XMLUtil.getAttributeValue(node, NAME_ATTRIBUTE);
325         String JavaDoc iURL = XMLUtil.getAttributeValue(node, INSTALL_PATH_ATTRIBUTE);
326         String JavaDoc aURL = XMLUtil.getAttributeValue(node, ARCHIVE_PATH_ATTRIBUTE);
327         String JavaDoc lSte = XMLUtil
328                 .getAttributeValue(node, LIFECYCLE_STATE_ATTRIBUTE);
329
330         return new ServiceAssemblyState(name, iURL, aURL, lSte);
331     }
332
333     /**
334      * create a Node representing a sa state
335      *
336      * @param saName
337      * @param installURL
338      * @param zipURL
339      * @param lifecycleState
340      * @return
341      * @throws Exception
342      */

343     protected Node JavaDoc createServiceAssemblyNode(String JavaDoc saName, String JavaDoc installURL,
344             String JavaDoc zipURL, String JavaDoc lifecycleState) throws Exception JavaDoc {
345         Node JavaDoc 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     /**
354      * Create a SharedLibraryState from the given node
355      *
356      * @param node
357      * @return a SharedLibraryState , null if node is null
358      */

359     protected SharedLibraryState createSharedLibrary(Node JavaDoc node) {
360         String JavaDoc name = XMLUtil.getAttributeValue(node, NAME_ATTRIBUTE);
361         String JavaDoc iURL = XMLUtil.getAttributeValue(node, INSTALL_PATH_ATTRIBUTE);
362         String JavaDoc aURL = XMLUtil.getAttributeValue(node, ARCHIVE_PATH_ATTRIBUTE);
363
364         return new SharedLibraryState(name, iURL, aURL);
365     }
366
367     /**
368      * create a Node representing a sl state
369      *
370      * @param slName
371      * @param installURL
372      * @param zipURL
373      * @return
374      * @throws Exception
375      */

376     protected Node JavaDoc createSharedLibraryNode(String JavaDoc slName, String JavaDoc installURL,
377             String JavaDoc zipURL) throws Exception JavaDoc {
378
379         Node JavaDoc 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     // ///////////////////////////////////////////////////////////////////////
387
//
388
// service methods
389
//
390
// ///////////////////////////////////////////////////////////////////////
391

392     /**
393      * delete the element, and save the Document
394      *
395      * @param parent
396      * @param child
397      * @throws Exception
398      * node not found, saving problem
399      */

400     protected void deleteStateAndSave(Node JavaDoc parent, Node JavaDoc child) throws Exception JavaDoc {
401         parent.removeChild(child);
402
403         saveSystemState();
404     }
405
406     protected Node JavaDoc getComponentNode(String JavaDoc name) {
407         return getNode(COMPONENT_ELEMENT, name);
408     }
409
410     protected Node JavaDoc getComponentsNode() {
411         return XMLUtil.getNode(document, "*/" + COMPONENTS_ELEMENT);
412     }
413
414     protected Node JavaDoc getNode(String JavaDoc elementType, String JavaDoc name) {
415         return XMLUtil.getNode(document, "//*/" + elementType + "[@"
416                 + NAME_ATTRIBUTE + "=\"" + name + "\"]");
417     }
418
419     protected Node JavaDoc getServiceAssembliesNode() {
420         return XMLUtil.getNode(document, "*/" + SERVICE_ASSEMBLIES_ELEMENT);
421     }
422
423     protected Node JavaDoc getServiceAssemblyNode(String JavaDoc name) {
424         return getNode(SERVICE_ASSEMBLY_ELEMENT, name);
425     }
426
427     protected Node JavaDoc getSharedLibrariesNode() {
428         return XMLUtil.getNode(document, "*/" + SHARED_LIBRARIES_ELEMENT);
429     }
430
431     protected Node JavaDoc getSharedLibraryNode(String JavaDoc name) {
432         return getNode(SHARE_LIBRARY_ELEMENT, name);
433     }
434
435     /**
436      * add to the empty Document the necessary elements. clean the document
437      * before if necessary
438      *
439      */

440     protected void initializeDocument() {
441         NodeList JavaDoc children = document.getChildNodes();
442
443         for (int i = 0; i < children.getLength(); i++) {
444             document.removeChild(children.item(i));
445         }
446
447         // document.setXmlVersion("1.0");
448

449         Element JavaDoc s = document.createElement(SYSTEM_STATE_ELEMENT);
450         Node JavaDoc c = document.createElement(COMPONENTS_ELEMENT);
451         Node JavaDoc sl = document.createElement(SHARED_LIBRARIES_ELEMENT);
452         Node JavaDoc 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     /**
462      * Load the Document from the systemstate file. if the file is empty, the
463      * document is initialized and saved
464      */

465     protected void loadStateStorageDocument() throws Exception JavaDoc {
466         // create file if necessary
467
if (!file.exists()) {
468             file.createNewFile();
469         }
470
471         DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory
472                 .newInstance();
473
474         DocumentBuilder JavaDoc docBuilder = docBuilderFactory.newDocumentBuilder();
475
476         // if file is empty, initialize it (test the length of the file)
477
if ((new FileInputStream JavaDoc(file)).available() == 0) {
478             document = docBuilder.newDocument();
479             initializeDocument();
480             saveSystemState();
481         } else {
482             document = docBuilder.parse(file);
483         }
484
485     }
486
487     /**
488      * Write the document representing the system state to the recovery-file
489      *
490      * @throws Exception
491      */

492     protected void saveSystemState() throws Exception JavaDoc {
493         Transformer JavaDoc transformer = TransformerFactory.newInstance()
494                 .newTransformer();
495
496         FileWriter JavaDoc fileWriter = new FileWriter JavaDoc(file);
497         transformer.transform(new DOMSource JavaDoc(document), new StreamResult JavaDoc(
498                 fileWriter));
499     }
500
501     /**
502      * update the value of the attribute of the node, and save the Document
503      *
504      * @param node
505      * @param att
506      * @param value
507      * @throws Exception
508      * node not found, saving problem
509      */

510     protected void updateStateAndSave(Node JavaDoc node, String JavaDoc att, String JavaDoc value)
511             throws Exception JavaDoc {
512         node.getAttributes().getNamedItem(att).setNodeValue(value);
513
514         saveSystemState();
515     }
516 }
517
Popular Tags