41 package com.sun.jmx.examples.scandir.config;
43 import java.io.ByteArrayInputStream;
44 import java.io.ByteArrayOutputStream;
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;
70 "jmx:com.sun.jmx.examples.scandir.config";
74 private static final Logger
LOG =
109 final FileOutputStream out =
new FileOutputStream(f);
110 boolean failed =
true;
113 write(bean,out,
false);
120 if (failed ==
true) f.delete();
125 }
catch (JAXBException x) {
126 final IOException io =
127 new IOException(
"Failed to write SessionConfigBean to " +
141 final ByteArrayOutputStream baos =
new ByteArrayOutputStream();
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);
166 final Object clone =
copy(bean);
180 private static Object
copy(Object bean) {
182 final ByteArrayOutputStream baos =
new ByteArrayOutputStream();
184 m.marshal(bean, baos);
185 final ByteArrayInputStream bais =
186 new ByteArrayInputStream(baos.toByteArray());
188 }
catch (JAXBException x) {
189 final IllegalArgumentException iae =
190 new IllegalArgumentException(
"Failed to write SessionConfigBean: "+x,x);
207 final Object clone =
copy(bean);
218 final File f =
new File(
file);
220 throw new IOException(
"No such file: "+
file);
222 throw new IOException(
"Can't read file: "+
file);
225 }
catch (JAXBException x) {
226 final IOException io =
227 new IOException(
"Failed to read SessionConfigBean from " +
241 throws JAXBException {
258 throws JAXBException {
272 throws JAXBException {
285 private static void writeXml(Object bean, OutputStream os,
boolean fragment)
286 throws JAXBException {
288 if (fragment) m.setProperty(m.JAXB_FRAGMENT,Boolean.TRUE);
299 final Marshaller m =
getContext().createMarshaller();
300 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
309 private static synchronized JAXBContext
getContext() throws JAXBException {
335 final File f =
new File(basename+
".new");
336 if (!f.createNewFile())
337 throw new IOException(
"file "+f.getName()+
" already exists");
340 final OutputStream newStream =
new FileOutputStream(f);
343 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
344 newStream.write(decl.getBytes(
"UTF-8"));
349 }
catch (IOException x) {
358 private static File
commit(String basename, File tmpFile)
361 final String backupName = basename+
"~";
362 final File desired =
new File(basename);
363 final File backup =
new File(backupName);
365 if (desired.exists()) {
366 if (!desired.renameTo(
new File(backupName)))
367 throw new IOException(
"can't rename to "+backupName);
369 if (!tmpFile.renameTo(
new File(basename)))
370 throw new IOException(
"can't rename to "+basename);
371 }
catch (IOException x) {
375 return new File(basename);