use of javax.xml.stream.XMLStreamWriter in project Payara by payara.
the class ConfigPersistence method test.
@Test
public void test() throws TransactionFailure {
final DomDocument document = getDocument(getHabitat());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.reset();
final ConfigurationPersistence testPersistence = new ConfigurationPersistence() {
public void save(DomDocument doc) throws IOException, XMLStreamException {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(baos);
doc.writeTo(new IndentingXMLStreamWriter(writer));
writer.close();
}
};
TransactionListener testListener = new TransactionListener() {
public void transactionCommited(List<PropertyChangeEvent> changes) {
try {
testPersistence.save(document);
} catch (IOException e) {
// To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
} catch (XMLStreamException e) {
// To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
}
}
public void unprocessedTransactedEvents(List<UnprocessedChangeEvents> changes) {
}
};
Transactions transactions = getHabitat().getService(Transactions.class);
try {
transactions.addTransactionsListener(testListener);
doTest();
} catch (TransactionFailure f) {
f.printStackTrace();
throw f;
} finally {
transactions.waitForDrain();
transactions.removeTransactionsListener(testListener);
}
// now check if we persisted correctly...
final String resultingXml = baos.toString();
logger.fine(resultingXml);
assertTrue("assertResult from " + getClass().getName() + " was false from " + resultingXml, assertResult(resultingXml));
}
use of javax.xml.stream.XMLStreamWriter in project Payara by payara.
the class DeepCopyTest method save.
public OutputStream save(DomDocument doc) throws IOException, XMLStreamException {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
outStream.reset();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(outStream);
doc.writeTo(new IndentingXMLStreamWriter(writer));
writer.close();
return outStream;
}
use of javax.xml.stream.XMLStreamWriter in project Payara by payara.
the class ConfigModularityUtils method serializeConfigBean.
public String serializeConfigBean(ConfigBeanProxy configBean) {
if (configBean == null)
return null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = null;
IndentingXMLStreamWriter indentingXMLStreamWriter = null;
String s = null;
try {
writer = xmlFactory.createXMLStreamWriter(new BufferedOutputStream(bos));
indentingXMLStreamWriter = new IndentingXMLStreamWriter(writer);
Dom configBeanDom = Dom.unwrap(configBean);
configBeanDom.writeTo(configBeanDom.model.getTagName(), indentingXMLStreamWriter);
indentingXMLStreamWriter.flush();
s = bos.toString();
} catch (XMLStreamException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
return null;
} finally {
try {
if (bos != null)
bos.close();
if (writer != null)
writer.close();
if (indentingXMLStreamWriter != null)
indentingXMLStreamWriter.close();
} catch (IOException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
} catch (XMLStreamException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
}
}
return s;
}
use of javax.xml.stream.XMLStreamWriter in project Payara by payara.
the class MarshallingUtils method getXmlForProperties.
public static String getXmlForProperties(List<Map<String, String>> properties) {
try {
String xml = null;
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
StringWriter sw = new StringWriter();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw);
writer.writeStartDocument("UTF-8", "1.0");
writer.writeStartElement("list");
for (Map<String, String> property : properties) {
writer.writeStartElement("map");
for (Map.Entry<String, String> entry : property.entrySet()) {
writer.writeStartElement("entry");
writer.writeAttribute("key", entry.getKey());
writer.writeAttribute("value", entry.getValue());
writer.writeEndElement();
}
writer.writeEndElement();
}
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
writer.close();
return sw.toString();
} catch (XMLStreamException ex) {
LogHelper.log(RestClientLogging.logger, Level.SEVERE, RestClientLogging.REST_CLIENT_XML_STREAM_ERROR, ex);
throw new RuntimeException(ex);
}
}
use of javax.xml.stream.XMLStreamWriter in project Payara by payara.
the class DomainXmlPersistence method save.
@Override
public void save(DomDocument doc) throws IOException {
if (modularityUtils.isIgnorePersisting() && !modularityUtils.isCommandInvocation()) {
if (skippedDoc != null) {
assert (doc == skippedDoc);
}
skippedDoc = doc;
return;
}
File destination = getDestination();
if (destination == null) {
String msg = localStrings.getLocalString("NoLocation", "domain.xml cannot be persisted, null destination");
logger.severe(msg);
throw new IOException(msg);
}
Lock writeLock = null;
try {
try {
writeLock = accessWrite();
} catch (TimeoutException e) {
String msg = localStrings.getLocalString("Timeout", "Timed out when waiting for write lock on configuration file");
logger.log(Level.SEVERE, msg);
throw new IOException(msg, e);
}
// get a temporary file
File f = File.createTempFile("domain", ".xml", destination.getParentFile());
if (!f.exists()) {
throw new IOException(localStrings.getLocalString("NoTmpFile", "Cannot create temporary file when saving domain.xml"));
}
// write to the temporary file
XMLStreamWriter writer = null;
OutputStream fos = getOutputStream(f);
try {
writer = xmlFactory.createXMLStreamWriter(new BufferedOutputStream(fos));
IndentingXMLStreamWriter indentingXMLStreamWriter = new IndentingXMLStreamWriter(writer);
doc.writeTo(indentingXMLStreamWriter);
indentingXMLStreamWriter.close();
} catch (XMLStreamException e) {
String msg = localStrings.getLocalString("TmpFileNotSaved", "Configuration could not be saved to temporary file");
logger.log(Level.SEVERE, msg, e);
throw new IOException(e.getMessage(), e);
// return after calling finally clause, because since temp file couldn't be saved,
// renaming should not be attempted
} finally {
if (writer != null) {
try {
writer.close();
} catch (XMLStreamException e) {
logger.log(Level.SEVERE, localStrings.getLocalString("CloseFailed", "Cannot close configuration writer stream"), e);
throw new IOException(e.getMessage(), e);
}
}
fos.close();
}
// backup the current file
File backup = new File(env.getConfigDirPath(), "domain.xml.bak");
if (destination.exists() && backup.exists() && !backup.delete()) {
String msg = localStrings.getLocalString("BackupDeleteFailed", "Could not delete previous backup file at {0}", backup.getAbsolutePath());
logger.severe(msg);
throw new IOException(msg);
}
if (destination.exists() && !FileUtils.renameFile(destination, backup)) {
String msg = localStrings.getLocalString("TmpRenameFailed", "Could not rename {0} to {1}", destination.getAbsolutePath(), backup.getAbsolutePath());
logger.severe(msg);
throw new IOException(msg);
}
// save the temp file to domain.xml
if (!FileUtils.renameFile(f, destination)) {
String msg = localStrings.getLocalString("TmpRenameFailed", "Could not rename {0} to {1}", f.getAbsolutePath(), destination.getAbsolutePath());
// try to rename backup to domain.xml (so that at least something is there)
if (!FileUtils.renameFile(backup, destination)) {
msg += "\n" + localStrings.getLocalString("RenameFailed", "Could not rename backup to {0}", destination.getAbsolutePath());
}
logger.severe(msg);
throw new IOException(msg);
}
} catch (IOException e) {
logger.log(Level.SEVERE, localStrings.getLocalString("ioexception", "IOException while saving the configuration, changes not persisted"), e);
throw e;
} finally {
if (writeLock != null) {
writeLock.unlock();
}
}
skippedDoc = null;
saved(destination);
}
Aggregations