1 package org.enhydra.server.conf; 2 3 import java.io.FileNotFoundException ; 4 import java.io.FileOutputStream ; 5 import java.io.IOException ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Set ; 10 11 import org.apache.xerces.parsers.DOMParser; 12 import org.apache.xml.serialize.LineSeparator; 13 import org.apache.xml.serialize.Method; 14 import org.apache.xml.serialize.OutputFormat; 15 import org.apache.xml.serialize.XMLSerializer; 16 import org.w3c.dom.Attr ; 17 import org.w3c.dom.Document ; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.NamedNodeMap ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NodeList ; 22 import org.xml.sax.SAXException ; 23 24 32 public class EnhydraServerXML { 33 34 35 public Document document; 37 38 DOMParser parser; 40 41 private String xmlFileName; 43 44 private Node serverNode; 46 47 private String lineSep; 48 49 50 51 private EnhydraServerXML() { 52 } 53 54 public EnhydraServerXML(String fileName) { 55 this.xmlFileName = fileName; 56 57 try { 58 parser = new DOMParser(); 59 parser.parse(this.xmlFileName); 60 document = parser.getDocument(); 61 62 this.serverNode = this.getServerNode(); 63 this.lineSep = this.determineLineSeparator(); 64 65 } catch (SAXException e) { 66 e.printStackTrace(); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } 70 } 71 72 73 77 String determineLineSeparator() { 78 79 String os = System.getProperty("os.name"); 80 81 if(os.toLowerCase().indexOf("windows") != -1) 82 return LineSeparator.Windows; 83 if( (os.toLowerCase().indexOf("unix") != -1) || (os.toLowerCase().indexOf("linux") != -1) ) 84 return LineSeparator.Unix; 85 return LineSeparator.Web; 87 } 88 89 90 91 94 public void saveDocument() { 95 96 try { 97 98 FileOutputStream os = new FileOutputStream (this.xmlFileName); 99 OutputFormat of = new OutputFormat(); 100 101 of.setIndent(4); 103 104 of.setMethod(Method.XML); 105 of.setPreserveSpace(true); 106 107 XMLSerializer out = new XMLSerializer(os, of); 108 out.serialize(this.document); 109 110 } catch (FileNotFoundException e) { 111 e.printStackTrace(); 112 } catch (IOException e) { 113 e.printStackTrace(); 114 } 115 } 116 117 118 119 120 125 public HashMap [] getApplications() { 126 ArrayList hashArray = null; 127 try { 128 NodeList childs = this.serverNode.getChildNodes(); 129 hashArray = new ArrayList (childs.getLength()); 130 131 for(int i = 0; i < childs.getLength(); i++) { 132 if(childs.item(i).getNodeName().equalsIgnoreCase("Application")) { 133 HashMap hash = new HashMap (); 134 NamedNodeMap attributes = childs.item(i).getAttributes(); 135 for(int j = 0; j < attributes.getLength(); j++) 136 hash.put(attributes.item(j).getNodeName(), attributes.item(j).getNodeValue()); 137 hashArray.add(hash); 138 } 139 } 140 141 HashMap []hashes = new HashMap [hashArray.size()]; 142 for(int i = 0; i < hashArray.size(); i++) 143 hashes[i] = (HashMap )hashArray.get(i); 144 145 return hashes; 146 147 } catch(Exception e) { 148 return null; 149 } 150 } 151 152 156 public void addApplication(HashMap hash) { 157 try { 158 Element application = this.document.createElement("application"); 159 Set keys = hash.keySet(); 161 Iterator it = keys.iterator(); 162 while(it.hasNext()) { 163 String key = it.next().toString(); 164 Attr attr = this.document.createAttribute(key); 165 attr.setNodeValue(hash.get(key).toString()); 166 application.setAttributeNode(attr); 167 } 168 this.serverNode.appendChild(this.document.createTextNode(this.lineSep + "\t")); 170 this.serverNode.appendChild(application); 171 this.serverNode.appendChild(this.document.createTextNode(this.lineSep)); 172 } catch(Exception e) { 173 System.err.println("New application could not be added!"); 174 } 175 } 176 177 180 public void removeApplication(String contextPath) { 181 try { 182 Node application = this.findApplication(contextPath); 183 184 Node spaceBefore = null; 186 Node spaceAfter = null; 187 if(application.getPreviousSibling() != null && application.getPreviousSibling().getNodeType() == Node.TEXT_NODE) 188 spaceBefore = application.getPreviousSibling(); 189 if(application.getNextSibling() != null && application.getNextSibling().getNodeType() == Node.TEXT_NODE) 190 spaceAfter = application.getNextSibling(); 191 192 this.serverNode.removeChild(application); 194 195 if(spaceBefore != null) 197 this.serverNode.removeChild(spaceBefore); 198 if(spaceAfter != null) 199 this.serverNode.removeChild(spaceAfter); 200 } catch(Exception e) { 201 System.err.println("Application with context path \"" + contextPath + "\" could not be removed!"); 202 } 203 } 204 210 public HashMap getApplication(String contextPath){ 211 Node appNode = findApplication(contextPath); 212 if (appNode == null){ 213 return null; } 215 HashMap hash = new HashMap (); 216 NamedNodeMap attributes = appNode.getAttributes(); 217 for(int j = 0; j < attributes.getLength(); j++) 218 hash.put(attributes.item(j).getNodeName(), attributes.item(j).getNodeValue()); 219 return hash; 220 } 221 222 225 private Node getServerNode() { 226 Element root = this.document.getDocumentElement(); if(root.getNodeName().equalsIgnoreCase("server")) 228 return root; 229 230 System.err.println("Tag node Server cannot be found !"); 231 return null; 232 } 233 234 235 238 private Node findApplication(String contextPath) { 239 NodeList childs = this.serverNode.getChildNodes(); 240 for(int i = 0; i < childs.getLength(); i++) { 241 if(childs.item(i).getNodeName().equalsIgnoreCase("application")) { 242 if(childs.item(i).getAttributes().getNamedItem("contextPath").getNodeValue().equalsIgnoreCase(contextPath)) 243 return childs.item(i); 244 } 245 } 246 return null; 247 } 248 249 250 251 252 253 254 public static void main(String [] args) { 255 try { 256 EnhydraServerXML test = new EnhydraServerXML("EnhydraServer.xml"); 257 258 259 264 265 HashMap hash = new HashMap (); 266 hash.put("name", "zzz application"); 267 hash.put("contextPath", "cp"); 268 hash.put("connections", "2233, 4455"); 269 hash.put("running", "false"); 270 hash.put("URLPath", "C:/..."); 271 hash.put("description", "zzz app"); 272 test.addApplication(hash); 273 274 test.saveDocument(); 275 276 277 280 System.out.println("Happy end"); 281 } catch(Exception e) { 282 System.out.println("NOOOOOOOOOO"); 283 } 284 } 285 } 286 | Popular Tags |