1 package org.columba.core.config; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 13 import org.columba.api.exception.StoreException; 14 import org.columba.api.shutdown.IShutdownManager; 15 import org.columba.core.io.DiskIO; 16 import org.columba.core.shutdown.ShutdownManager; 17 import org.jdom.Document; 18 import org.jdom.JDOMException; 19 import org.jdom.input.SAXBuilder; 20 import org.jdom.output.XMLOutputter; 21 22 public class XmlConfig { 23 24 private static final java.util.logging.Logger LOG = java.util.logging.Logger 25 .getLogger("org.columba.core.confignew"); 26 27 private String defaultConfig; 28 29 private File xmlFile; 30 31 private Document document; 32 33 41 public XmlConfig(final File xmlFile, final String defaultConfig) { 42 if (xmlFile == null) 43 throw new IllegalArgumentException ("xmlFile == null"); 44 45 this.defaultConfig = defaultConfig; 46 this.xmlFile = xmlFile; 47 48 final IShutdownManager shutdownManager = ShutdownManager.getInstance(); 51 52 shutdownManager.register(new Runnable () { 53 public void run() { 54 try { 55 transformModelToDocument(document); 57 58 File tmpFile = File.createTempFile("tag", ".backup"); 59 60 BufferedOutputStream out = new BufferedOutputStream ( 61 new FileOutputStream (tmpFile)); 62 63 saveDocument(document, out); 64 65 DiskIO.copyFile(tmpFile, xmlFile); 67 } catch (final Exception e) { 68 LOG.severe(e.getMessage()); 69 throw new StoreException( 70 "could not store configuration file at: " 71 + xmlFile.getAbsolutePath(), e); 72 } 73 } 74 }); 75 } 76 77 public Document load() throws StoreException { 78 if (!xmlFile.exists()) { 79 if (defaultConfig != null) { 80 try { 82 DiskIO.copyResource(defaultConfig + ".xml", xmlFile); 83 } catch (IOException e) { 84 LOG.severe(e.getMessage()); 85 throw new StoreException( 86 "could not create initial configuration file at: " 87 + xmlFile.getAbsolutePath(), e); 88 } 89 } else { 90 document = new Document(); 92 return document; 93 } 94 95 } 96 97 BufferedInputStream buf; 99 try { 100 buf = new BufferedInputStream (new FileInputStream (xmlFile)); 101 } catch (FileNotFoundException e) { 102 LOG.severe(e.getMessage()); 103 throw new StoreException("could not load configuration file at: " 104 + xmlFile.getAbsolutePath(), e); 105 } 106 107 document = retrieveDocument(buf); 108 109 return document; 110 } 111 112 115 private Document retrieveDocument(InputStream is) throws StoreException { 116 SAXBuilder builder = new SAXBuilder(); 117 builder.setIgnoringElementContentWhitespace(true); 118 Document doc = null; 119 try { 120 doc = builder.build(is); 121 } catch (JDOMException e) { 122 LOG.severe(e.getMessage()); 123 throw new StoreException("could not parse configuration file at: " 124 + xmlFile.getAbsolutePath(), e); 125 } catch (IOException e) { 126 LOG.severe(e.getMessage()); 127 throw new StoreException("could not parse configuration file at: " 128 + xmlFile.getAbsolutePath(), e); 129 } 130 return doc; 131 } 132 133 136 public void saveDocument(Document doc, OutputStream os) 137 throws StoreException { 138 try { 139 XMLOutputter outp = new XMLOutputter(" ", true); 140 141 outp.output(doc, os); 142 } catch (IOException e) { 143 LOG.severe(e.getMessage()); 144 throw new StoreException("could not store configuration file at: " 145 + xmlFile.getAbsolutePath(), e); 146 } 147 } 148 149 150 157 protected void transformModelToDocument(Document document) {} 158 } 159 | Popular Tags |