Search in sources :

Example 1 with IndentingXMLStreamWriter

use of org.jvnet.hk2.config.IndentingXMLStreamWriter 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 2 with IndentingXMLStreamWriter

use of org.jvnet.hk2.config.IndentingXMLStreamWriter 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)

Example 3 with IndentingXMLStreamWriter

use of org.jvnet.hk2.config.IndentingXMLStreamWriter 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)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)3 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)3 IndentingXMLStreamWriter (org.jvnet.hk2.config.IndentingXMLStreamWriter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)2 BufferedOutputStream (java.io.BufferedOutputStream)1 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 Lock (java.util.concurrent.locks.Lock)1 ManagedFile (org.glassfish.common.util.admin.ManagedFile)1 ConfigurationPersistence (org.glassfish.config.support.ConfigurationPersistence)1 Test (org.junit.Test)1 Dom (org.jvnet.hk2.config.Dom)1 DomDocument (org.jvnet.hk2.config.DomDocument)1 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)1 TransactionListener (org.jvnet.hk2.config.TransactionListener)1 Transactions (org.jvnet.hk2.config.Transactions)1