KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > configuration > ConfigStore


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.configuration;
20
21 import java.io.StringReader JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30
31 import org.openharmonise.commons.xml.*;
32 import org.openharmonise.localfilesystem.*;
33 import org.openharmonise.vfs.*;
34 import org.openharmonise.vfs.context.*;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
40
41
42 /**
43  * Store for all configuration options. Persists to XML virtual file.
44  *
45  * @author Matthew Large
46  * @version $Revision: 1.1 $
47  *
48  */

49 public class ConfigStore {
50
51     /**
52      * Instance of store, following singleton pattern.
53      */

54     private static ConfigStore m_instance = null;
55
56     /**
57      * Virtual file system to persist to.
58      */

59     private AbstractVirtualFileSystem m_vfs = null;
60
61     /**
62      * Virtual file system uri for Windows.
63      */

64     private static String JavaDoc URI_WINDOWS = "file:/C:/ContentManager/";
65
66     /**
67      * Virtual file system uri for MacOS X.
68      */

69     private static String JavaDoc URI_MAC = "???";
70
71     /**
72      * Virtual file system uri for Linux.
73      */

74     private static String JavaDoc URI_LINUX = "???";
75
76
77     /**
78      * Virtual file system uri.
79      */

80     private static String JavaDoc URI = "???";
81     
82     /**
83      * Full path to virtual file for persisted data.
84      */

85     private String JavaDoc m_sConfigFileURI = "/ContentManager/config.xml";
86
87     /**
88      * Configuration XML.
89      */

90     private Document JavaDoc m_xml = null;
91
92     /**
93      * Map of property name to value
94      */

95     private HashMap JavaDoc m_properties = new HashMap JavaDoc(11);
96
97     /**
98      *
99      */

100     private ConfigStore() {
101         super();
102         this.setup();
103     }
104
105     /**
106      * Returns the instance of the configuration store, following the
107      * singleton pattern.
108      *
109      * @return Instance of configuration store
110      */

111     public static ConfigStore getInstance() {
112         if (m_instance == null) {
113             m_instance = new ConfigStore();
114         }
115
116         return m_instance;
117     }
118
119     /**
120      * Configures the config store.
121      *
122      */

123     private void setup() {
124         try {
125             URI = ConfigStore.URI_WINDOWS;
126
127             m_vfs = new LocalFileSystem(new URI JavaDoc(URI));
128
129             this.populate();
130         } catch (URISyntaxException JavaDoc e) {
131             e.printStackTrace();
132         }
133     }
134     
135     /**
136      * Returns the virtual file system that Content Manager
137      * is to use for local persistence.
138      *
139      * @return Virtual file system
140      */

141     public AbstractVirtualFileSystem getApplicationFileSystem() {
142         return this.m_vfs;
143     }
144
145     /**
146      * Returns a list of all the property names.
147      *
148      * @return List of property names
149      */

150     public List JavaDoc getPropertyNames() {
151         ArrayList JavaDoc aRetn = new ArrayList JavaDoc(m_properties.keySet());
152         return aRetn;
153     }
154
155     /**
156      * Populates the config store from persisted data.
157      *
158      */

159     private void populate() {
160         this.getXML();
161         Element JavaDoc root = m_xml.getDocumentElement();
162
163     }
164
165     /**
166      * Sets list of values for a given property.
167      *
168      * @param sPropName Property name
169      * @param vVals List of values
170      */

171     public void setProperty(String JavaDoc sPropName, ArrayList JavaDoc vVals) {
172         if (m_properties.containsKey(sPropName)) {
173             m_properties.remove(sPropName);
174         }
175         m_properties.put(sPropName, vVals);
176         ContextEvent ce = new ContextEvent(ContextType.CONTEXT_SYSTEM_PROP_CHANGED, sPropName);
177         ContextHandler.getInstance().fireContextEvent(ce);
178     }
179
180     /**
181      * Sets a value for a given property.
182      *
183      * @param sPropName Property name
184      * @param sValue Value
185      */

186     public void setProperty(String JavaDoc sPropName, String JavaDoc sValue) {
187         ArrayList JavaDoc aVals = new ArrayList JavaDoc(1);
188         aVals.add(sValue);
189         this.setProperty(sPropName, aVals);
190         ContextEvent ce = new ContextEvent(ContextType.CONTEXT_SYSTEM_PROP_CHANGED, sPropName);
191         ContextHandler.getInstance().fireContextEvent(ce);
192     }
193
194     /**
195      * Returns a list of values for a given property.
196      *
197      * @param sPropName Property name
198      * @return List of values
199      */

200     public ArrayList JavaDoc getPropertyVals(String JavaDoc sPropName) {
201         if (m_properties.containsKey(sPropName)) {
202             return (ArrayList JavaDoc) ((ArrayList JavaDoc) m_properties.get(sPropName))
203                 .clone();
204         } else {
205             return null;
206         }
207     }
208
209     /**
210      * Returns a value for a given property. If the property has more than
211      * one value only the first is returned.
212      *
213      * @param sPropName Property name
214      * @return Value
215      */

216     public String JavaDoc getPropertyValue(String JavaDoc sPropName) {
217         if (m_properties.containsKey(sPropName)) {
218             return (String JavaDoc) ((ArrayList JavaDoc) m_properties.get(sPropName)).get(0);
219         } else {
220             return null;
221         }
222     }
223
224     /**
225      * Persists all data to the application virtual file system.
226      *
227      */

228     public void save() {
229         try {
230             m_xml =
231                 DocumentBuilderFactory
232                     .newInstance()
233                     .newDocumentBuilder()
234                     .newDocument();
235
236             Element JavaDoc rootEl = m_xml.createElement("Project");
237             m_xml.appendChild(rootEl);
238
239             Iterator JavaDoc itor = m_properties.keySet().iterator();
240             while (itor.hasNext()) {
241                 String JavaDoc sPropName = (String JavaDoc) itor.next();
242                 ArrayList JavaDoc vVals = (ArrayList JavaDoc) m_properties.get(sPropName);
243
244                 if (vVals.size() > 0) {
245                     Element JavaDoc propEl = m_xml.createElement("Property");
246                     propEl.setAttribute("name", sPropName);
247                     rootEl.appendChild(propEl);
248
249                     ArrayList JavaDoc vVals2 = (ArrayList JavaDoc) vVals.clone();
250                     for (int i = 0; i < vVals2.size(); i++) {
251                         String JavaDoc sValue = (String JavaDoc) vVals2.get(i);
252                         if (sValue != null) {
253                             Element JavaDoc valEl = m_xml.createElement("Value");
254                             propEl.appendChild(valEl);
255                             Text JavaDoc txt = m_xml.createTextNode(sValue);
256                             valEl.appendChild(txt);
257                         }
258                     }
259                 }
260             }
261
262             XMLPrettyPrint printer = new XMLPrettyPrint();
263             printer.setNamespaceAware(true);
264
265             VirtualFile projFile = this.m_vfs.getVirtualFile(this.m_sConfigFileURI).getResource();
266             String JavaDoc sXML = printer.printNode(m_xml.getDocumentElement());
267             projFile.setContent( sXML.getBytes());
268
269             this.m_vfs.synchroniseFile(projFile);
270
271         } catch (Exception JavaDoc e) {
272             e.printStackTrace(System.out);
273         }
274     }
275
276     /**
277      * Retrieves persisted data and populates the configuration store.
278      *
279      */

280     private void getXML() {
281
282         VirtualFile projFile = null;
283         projFile = this.m_vfs.getVirtualFile(this.m_sConfigFileURI).getResource();
284
285         if (projFile.exists()) {
286
287             try {
288                 m_xml =
289                     DocumentBuilderFactory
290                         .newInstance()
291                         .newDocumentBuilder()
292                         .parse(
293                         new org.xml.sax.InputSource JavaDoc(new StringReader JavaDoc( new String JavaDoc(projFile.getContent()) )));
294
295                 Element JavaDoc rootEl = m_xml.getDocumentElement();
296
297                 NodeList JavaDoc nlProps = rootEl.getChildNodes();
298
299                 for (int i = 0; i < nlProps.getLength(); i++) {
300                     Node JavaDoc node = nlProps.item(i);
301                     if (node.getNodeType() != Node.ELEMENT_NODE) {
302                         continue;
303                     } else {
304                         Element JavaDoc propEl = (Element JavaDoc) node;
305                         String JavaDoc sPropName = propEl.getAttribute("name");
306                         ArrayList JavaDoc vVals = new ArrayList JavaDoc();
307
308                         NodeList JavaDoc nlVals = propEl.getChildNodes();
309                         for (int j = 0; j < nlVals.getLength(); j++) {
310                             Node JavaDoc node2 = nlVals.item(j);
311                             if (node2.getNodeType() != Node.ELEMENT_NODE) {
312                                 continue;
313                             } else {
314                                 Element JavaDoc valEl = (Element JavaDoc) node2;
315                                 vVals.add(valEl.getFirstChild().getNodeValue());
316                             }
317                         }
318
319                         m_properties.put(sPropName, vVals);
320
321                     }
322                 }
323
324             } catch (Exception JavaDoc e) {
325                 e.printStackTrace(System.out);
326             }
327         } else {
328
329             try {
330                 m_xml =
331                     DocumentBuilderFactory
332                         .newInstance()
333                         .newDocumentBuilder()
334                         .newDocument();
335
336                 Element JavaDoc rootEl = m_xml.createElement("Project");
337                 m_xml.appendChild(rootEl);
338             } catch (Exception JavaDoc e) {
339                 e.printStackTrace(System.out);
340             }
341         }
342     }
343
344 }
345
Popular Tags