Search in sources :

Example 96 with XMLStreamWriter

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));
}
Also used : TransactionListener(org.jvnet.hk2.config.TransactionListener) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) ConfigurationPersistence(org.glassfish.config.support.ConfigurationPersistence) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DomDocument(org.jvnet.hk2.config.DomDocument) Transactions(org.jvnet.hk2.config.Transactions) XMLStreamException(javax.xml.stream.XMLStreamException) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) List(java.util.List) Test(org.junit.Test)

Example 97 with XMLStreamWriter

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;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 98 with XMLStreamWriter

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;
}
Also used : IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Dom(org.jvnet.hk2.config.Dom) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 99 with XMLStreamWriter

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);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 100 with XMLStreamWriter

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);
}
Also used : IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ManagedFile(org.glassfish.common.util.admin.ManagedFile) Lock(java.util.concurrent.locks.Lock) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)209 XMLStreamException (javax.xml.stream.XMLStreamException)84 StringWriter (java.io.StringWriter)47 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)40 XMLStreamReader (javax.xml.stream.XMLStreamReader)32 QName (javax.xml.namespace.QName)26 Document (org.w3c.dom.Document)26 Fault (org.apache.cxf.interceptor.Fault)25 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StreamSource (javax.xml.transform.stream.StreamSource)17 StringReader (java.io.StringReader)16 JAXBException (javax.xml.bind.JAXBException)14 DOMSource (javax.xml.transform.dom.DOMSource)14 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)14 Message (org.apache.cxf.message.Message)13 Element (org.w3c.dom.Element)11