Author: jflesch
Date: 2006-06-15 20:03:59 +0000 (Thu, 15 Jun 2006)
New Revision: 9213
Modified:
trunk/apps/Thaw/build.xml
trunk/apps/Thaw/readme.txt
trunk/apps/Thaw/src/thaw/core/Config.java
trunk/apps/Thaw/src/thaw/core/Core.java
Log:
Can now save and load config
Modified: trunk/apps/Thaw/build.xml
===================================================================
--- trunk/apps/Thaw/build.xml 2006-06-15 18:05:35 UTC (rev 9212)
+++ trunk/apps/Thaw/build.xml 2006-06-15 20:03:59 UTC (rev 9213)
@@ -15,7 +15,7 @@
<target name="compile">
<mkdir dir="${bin.dir}" />
- <javac srcdir="${src.dir}" destdir="${bin.dir}" debug="false"
optimize="true">
+ <javac srcdir="${src.dir}" destdir="${bin.dir}" debug="true"
optimize="true">
<compilerarg value="-Xlint" />
@@ -47,7 +47,10 @@
</manifest>
<fileset dir="${bin.dir}"/>
- <fileset file="${hsqldb.location}"/>
+
+ <!-- hsqldb is not required for the moment -->
+ <!-- <fileset file="${hsqldb.location}"/> -->
+
<fileset file="gpl.txt" />
</jar>
Modified: trunk/apps/Thaw/readme.txt
===================================================================
--- trunk/apps/Thaw/readme.txt 2006-06-15 18:05:35 UTC (rev 9212)
+++ trunk/apps/Thaw/readme.txt 2006-06-15 20:03:59 UTC (rev 9213)
@@ -9,13 +9,18 @@
COMPILATION
===========
+<!-- forget what follow, Hsqldb is not required for the moment -->
+<!-- it will be when I will start to deal with indexes -->
+<!--
In order to compile Thaw, you need to obtain the latest version of hsqldb.jar.
Here is a link to the current (06/11/2006) version of hsqldb:
http://switch.dl.sourceforge.net/sourceforge/hsqldb/hsqldb_1_8_0_4.zip
Extract the zip, and copy "hsqldb/lib/hsqldb.jar" to "Thaw/lib".
+-->
+
To compile:
$ ant
Modified: trunk/apps/Thaw/src/thaw/core/Config.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/Config.java 2006-06-15 18:05:35 UTC (rev
9212)
+++ trunk/apps/Thaw/src/thaw/core/Config.java 2006-06-15 20:03:59 UTC (rev
9213)
@@ -3,8 +3,26 @@
import java.util.HashMap;
import java.util.Vector;
import java.io.File;
+import java.util.Set;
+import java.util.Iterator;
+/* XML */
+import org.w3c.dom.Document;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+import org.w3c.dom.DOMImplementation;
+import javax.xml.transform.stream.StreamResult;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.OutputKeys;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+
/**
* This class the thaw config.
*
@@ -104,8 +122,61 @@
}
- /* TODO */
+ Document xmlDoc = null;
+ DocumentBuilderFactory xmlFactory = null;
+ DocumentBuilder xmlBuilder = null;
+ //DOMImplementation impl = null;
+
+ Element rootEl = null;
+
+ xmlFactory = DocumentBuilderFactory.newInstance();
+ try {
+ xmlBuilder = xmlFactory.newDocumentBuilder();
+ } catch(javax.xml.parsers.ParserConfigurationException e) {
+ Logger.warning(this, "Unable to load config because:
"+e);
+ return false;
+ }
+
+ try {
+ xmlDoc = xmlBuilder.parse(configFile);
+ } catch(org.xml.sax.SAXException e) {
+ Logger.warning(this, "Unable to load config because:
"+e);
+ return false;
+ } catch(java.io.IOException e) {
+ Logger.warning(this, "Unable to load config because:
"+e);
+ return false;
+ }
+
+ rootEl = xmlDoc.getDocumentElement();
+
+
+ NodeList params = rootEl.getElementsByTagName("param");
+
+ for(int i = 0;i < params.getLength(); i++) {
+ Element paramEl;
+ Node paramNode = params.item(i);
+
+ if(paramNode != null && paramNode.getNodeType() ==
Node.ELEMENT_NODE) {
+ paramEl = (Element)paramNode;
+ parameters.put(paramEl.getAttribute("name"),
paramEl.getAttribute("value"));
+ }
+ }
+
+ NodeList plugins = rootEl.getElementsByTagName("plugin");
+
+ for(int i = 0;i < params.getLength(); i++) {
+
+ Element pluginEl;
+ Node pluginNode = plugins.item(i);
+
+ if(pluginNode != null && pluginNode.getNodeType() ==
Node.ELEMENT_NODE) {
+ pluginEl = (Element)pluginNode;
+ pluginNames.add(pluginEl.getAttribute("name"));
+ }
+ }
+
+
return true;
}
@@ -115,19 +186,101 @@
* @return true if success, else false.
*/
public boolean saveConfig() {
+ StreamResult configOut;
+
if(configFile == null) {
Logger.error(this, "saveConfig(): No file specified !");
return false;
}
- if(!configFile.exists() || !configFile.canWrite()) {
- Logger.warning(this, "Unable to write config file
'"+configFile.getPath()+"'");
+ try {
+ if( (!configFile.exists() &&
!configFile.createNewFile())
+ || !configFile.canWrite()) {
+ Logger.warning(this, "Unable to write config
file '"+configFile.getPath()+"' (can't write)");
+ return false;
+ }
+ } catch(java.io.IOException e) {
+ Logger.warning(this, "Error while checking perms to
save config: "+e);
+ }
+
+
+
+ configOut = new StreamResult(configFile);
+
+ Document xmlDoc = null;
+ DocumentBuilderFactory xmlFactory = null;
+ DocumentBuilder xmlBuilder = null;
+ DOMImplementation impl = null;
+
+ Element rootEl = null;
+
+ xmlFactory = DocumentBuilderFactory.newInstance();
+
+ try {
+ xmlBuilder = xmlFactory.newDocumentBuilder();
+ } catch(javax.xml.parsers.ParserConfigurationException e) {
+ Logger.error(this, "Unable to save configuration
because: "+e.toString());
return false;
}
+
+
+ impl = xmlBuilder.getDOMImplementation();
+ xmlDoc = impl.createDocument(null, "config", null);
- /* TODO */
+ rootEl = xmlDoc.getDocumentElement();
+
+ Iterator entries = parameters.keySet().iterator();
+
+ while(entries.hasNext()) {
+ String entry = (String)entries.next();
+ String value = (String)parameters.get(entry);
+
+ Element paramEl = xmlDoc.createElement("param");
+ paramEl.setAttribute("name", entry);
+ paramEl.setAttribute("value", value);
+
+ rootEl.appendChild(paramEl);
+ }
+
+ Iterator plugins = pluginNames.iterator();
+
+ while(plugins.hasNext()) {
+ String pluginName = (String)plugins.next();
+ Element pluginEl = xmlDoc.createElement("plugin");
+
+ pluginEl.setAttribute("name", pluginName);
+
+ rootEl.appendChild(pluginEl);
+ }
+
+
+ /* Serialization */
+ DOMSource domSource = new DOMSource(xmlDoc);
+ TransformerFactory transformFactory =
TransformerFactory.newInstance();
+
+ Transformer serializer;
+
+ try {
+ serializer = transformFactory.newTransformer();
+ } catch(javax.xml.transform.TransformerConfigurationException
e) {
+ Logger.error(this, "Unable to save configuration
because: "+e.toString());
+ return false;
+ }
+
+ serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-15");
+ serializer.setOutputProperty(OutputKeys.INDENT,"yes");
+
+ /* final step */
+ try {
+ serializer.transform(domSource, configOut);
+ } catch(javax.xml.transform.TransformerException e) {
+ Logger.error(this, "Unable to save configuration
because: "+e.toString());
+ return false;
+ }
+
+
return true;
}
Modified: trunk/apps/Thaw/src/thaw/core/Core.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/Core.java 2006-06-15 18:05:35 UTC (rev
9212)
+++ trunk/apps/Thaw/src/thaw/core/Core.java 2006-06-15 20:03:59 UTC (rev
9213)
@@ -95,6 +95,12 @@
public boolean initConfig() {
config = new Config();
config.loadConfig();
+
+ if(config.getValue("nodeAddress") == null)
+ config.setValue("nodeAddress", "127.0.0.1");
+
+ if(config.getValue("nodePort") == null)
+ config.setValue("nodePort", "9481");
return true;
}
@@ -136,6 +142,11 @@
Logger.info(this, "Stopping plugins ...");
pluginManager.stopPlugins();
+ Logger.info(this, "Saving configuration ...");
+ if(!config.saveConfig()) {
+ Logger.error(this, "Config was not saved correctly !");
+ }
+
Logger.info(this, "Exiting");
System.exit(0);
}