1 40 41 package com.sun.jmx.examples.scandir.config; 42 43 import java.io.ByteArrayInputStream ; 44 import java.io.ByteArrayOutputStream ; 45 import java.io.File ; 46 import java.io.FileOutputStream ; 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.OutputStream ; 50 import java.util.logging.Logger ; 51 import javax.xml.bind.JAXBContext; 52 import javax.xml.bind.JAXBException; 53 import javax.xml.bind.Marshaller; 54 import javax.xml.bind.Unmarshaller; 55 56 62 public class XmlConfigUtils { 63 64 69 public static final String NAMESPACE = 70 "jmx:com.sun.jmx.examples.scandir.config"; 71 74 private static final Logger LOG = 75 Logger.getLogger(XmlConfigUtils.class.getName()); 76 77 private static JAXBContext context; 79 80 final String file; 83 84 89 public XmlConfigUtils(String file) { 90 this.file = file; 91 } 92 93 103 public synchronized void writeToFile(ScanManagerConfig bean) 104 throws IOException { 105 106 final File f = newXmlTmpFile(file); 108 try { 109 final FileOutputStream out = new FileOutputStream (f); 110 boolean failed = true; 111 try { 112 write(bean,out,false); 114 115 failed = false; 117 } finally { 118 out.close(); 119 if (failed == true) f.delete(); 121 } 122 123 commit(file,f); 125 } catch (JAXBException x) { 126 final IOException io = 127 new IOException ("Failed to write SessionConfigBean to " + 128 file+": "+x,x); 129 throw io; 130 } 131 } 132 133 139 public static String toString(Object bean) { 140 try { 141 final ByteArrayOutputStream baos = new ByteArrayOutputStream (); 142 final Marshaller m = createMarshaller(); 143 m.setProperty(m.JAXB_FRAGMENT,Boolean.TRUE); 144 m.marshal(bean, baos); 145 return baos.toString(); 146 } catch (JAXBException x) { 147 final IllegalArgumentException iae = 148 new IllegalArgumentException ( 149 "Failed to write SessionConfigBean: "+x,x); 150 throw iae; 151 } 152 } 153 154 165 public static ScanManagerConfig xmlClone(ScanManagerConfig bean) { 166 final Object clone = copy(bean); 167 return (ScanManagerConfig)clone; 168 } 169 170 180 private static Object copy(Object bean) { 181 try { 182 final ByteArrayOutputStream baos = new ByteArrayOutputStream (); 183 final Marshaller m = createMarshaller(); 184 m.marshal(bean, baos); 185 final ByteArrayInputStream bais = 186 new ByteArrayInputStream (baos.toByteArray()); 187 return createUnmarshaller().unmarshal(bais); 188 } catch (JAXBException x) { 189 final IllegalArgumentException iae = 190 new IllegalArgumentException ("Failed to write SessionConfigBean: "+x,x); 191 throw iae; 192 } 193 } 194 195 206 public static DirectoryScannerConfig xmlClone(DirectoryScannerConfig bean) { 207 final Object clone = copy(bean); 208 return (DirectoryScannerConfig)clone; 209 } 210 211 217 public synchronized ScanManagerConfig readFromFile() throws IOException { 218 final File f = new File (file); 219 if (!f.exists()) 220 throw new IOException ("No such file: "+file); 221 if (!f.canRead()) 222 throw new IOException ("Can't read file: "+file); 223 try { 224 return read(f); 225 } catch (JAXBException x) { 226 final IOException io = 227 new IOException ("Failed to read SessionConfigBean from " + 228 file+": "+x,x); 229 throw io; 230 } 231 } 232 233 240 public static ScanManagerConfig read(File f) 241 throws JAXBException { 242 final Unmarshaller u = createUnmarshaller(); 243 return (ScanManagerConfig) u.unmarshal(f); 244 245 } 246 247 256 public static void write(ScanManagerConfig bean, OutputStream os, 257 boolean fragment) 258 throws JAXBException { 259 writeXml((Object )bean,os,fragment); 260 } 261 262 271 public static void write(ResultRecord bean, OutputStream os, boolean fragment) 272 throws JAXBException { 273 writeXml((Object )bean,os,fragment); 274 } 275 276 285 private static void writeXml(Object bean, OutputStream os, boolean fragment) 286 throws JAXBException { 287 final Marshaller m = createMarshaller(); 288 if (fragment) m.setProperty(m.JAXB_FRAGMENT,Boolean.TRUE); 289 m.marshal(bean,os); 290 } 291 292 private static Unmarshaller createUnmarshaller() throws JAXBException { 294 return getContext().createUnmarshaller(); 295 } 296 297 private static Marshaller createMarshaller() throws JAXBException { 299 final Marshaller m = getContext().createMarshaller(); 300 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE); 301 return m; 302 } 303 304 private static synchronized JAXBContext getContext() throws JAXBException { 310 if (context == null) 311 context = JAXBContext.newInstance(ScanManagerConfig.class, 312 ResultRecord.class); 313 return context; 314 } 315 316 317 private static File newXmlTmpFile(String basename) throws IOException { 335 final File f = new File (basename+".new"); 336 if (!f.createNewFile()) 337 throw new IOException ("file "+f.getName()+" already exists"); 338 339 try { 340 final OutputStream newStream = new FileOutputStream (f); 341 try { 342 final String decl = 343 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; 344 newStream.write(decl.getBytes("UTF-8")); 345 newStream.flush(); 346 } finally { 347 newStream.close(); 348 } 349 } catch (IOException x) { 350 f.delete(); 351 throw x; 352 } 353 return f; 354 } 355 356 private static File commit(String basename, File tmpFile) 359 throws IOException { 360 try { 361 final String backupName = basename+"~"; 362 final File desired = new File (basename); 363 final File backup = new File (backupName); 364 backup.delete(); 365 if (desired.exists()) { 366 if (!desired.renameTo(new File (backupName))) 367 throw new IOException ("can't rename to "+backupName); 368 } 369 if (!tmpFile.renameTo(new File (basename))) 370 throw new IOException ("can't rename to "+basename); 371 } catch (IOException x) { 372 tmpFile.delete(); 373 throw x; 374 } 375 return new File (basename); 376 } 377 378 389 public static File createNewXmlFile(String basename) throws IOException { 390 return commit(basename,newXmlTmpFile(basename)); 391 } 392 393 } 394 | Popular Tags |