Example usage for javax.xml.stream XMLStreamWriter close

List of usage examples for javax.xml.stream XMLStreamWriter close

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter close.

Prototype

public void close() throws XMLStreamException;

Source Link

Document

Close this writer and free any resources associated with the writer.

Usage

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213.java

@Test(groups = { "wso2.greg" }, description = "change registry.xml and restart server")
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.integration_user })
public void editRegistryXML() throws Exception {

    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    File srcFile = new File(registryXmlPath);
    try {// ww w.j a  v  a  2  s .c o  m
        OMElement handlerConfig = AXIOMUtil.stringToOM("<property name=\"useOriginalSchema\">true</property>");
        OMElement registryXML = getRegistryXmlOmElement();

        OMElement om1;
        for (Iterator iterator = registryXML.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {
                om1 = om;
                om1.addChild(handlerConfig);
                registryXML.addChild(om1);
                registryXML.build();
                break;
            }

        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e);

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e);

    } catch (IOException e) {
        throw new IOException("IO exception" + e);

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

    restartServer();

    EnvironmentBuilder builder = new EnvironmentBuilder().greg(userId);
    ManageEnvironment environment = builder.build();

    //reinitialize environment after server restart
    resourceAdminServiceClient = new ResourceAdminServiceClient(environment.getGreg().getBackEndUrl(),
            environment.getGreg().getSessionCookie());
    serverAdminClient = new ServerAdminClient(environment.getGreg().getProductVariables().getBackendUrl(),
            environment.getGreg().getSessionCookie());
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213.java

@AfterClass(groups = { "wso2.greg" })
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.integration_user })
public void deleteSchema() throws Exception {
    resourceAdminServiceClient.deleteResource(schemaPath);
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    try {/*from   w w w  .  ja va  2  s  .  c  o m*/
        File srcFile = new File(registryXmlPath);
        OMElement element = getRegistryXmlOmElement();
        OMElement omElement = null;

        for (Iterator iterator = element.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {

                omElement = om;
                for (Iterator it = omElement.getChildrenWithName(new QName("property")); it.hasNext();) {
                    OMElement omRemove = (OMElement) it.next();
                    if (omRemove.getAttribute(new QName("name")).getAttributeValue()
                            .equals("useOriginalSchema")) {
                        it.remove();
                        element.build();
                        break;
                    }
                }
                break;
            }
        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        element.serialize(writer);
        restartServer();

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }

    }
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213TestCase.java

@Test(groups = { "wso2.greg" }, description = "change registry.xml and restart server")
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
public void editRegistryXML() throws Exception {

    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    File srcFile = new File(registryXmlPath);
    try {/*w  w w  .j  a  va 2 s.c o  m*/
        OMElement handlerConfig = AXIOMUtil.stringToOM("<property name=\"useOriginalSchema\">true</property>");
        OMElement registryXML = getRegistryXmlOmElement();

        OMElement om1;
        for (Iterator iterator = registryXML.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {
                om1 = om;
                om1.addChild(handlerConfig);
                registryXML.addChild(om1);
                registryXML.build();
                break;
            }

        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e);

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e);

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

    restartServer();

    String sessionCookie = getSessionCookie();

    //reinitialize environment after server restart
    resourceAdminServiceClient = new ResourceAdminServiceClient(backendURL, sessionCookie);
    serverAdminClient = new ServerAdminClient(backendURL, sessionCookie);
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213TestCase.java

@AfterClass(groups = { "wso2.greg" })
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
public void deleteSchema() throws Exception {
    resourceAdminServiceClient.deleteResource(schemaPath);
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    try {/*from   w w w  . j  a  va 2  s.  c o  m*/
        File srcFile = new File(registryXmlPath);
        OMElement element = getRegistryXmlOmElement();
        OMElement omElement = null;

        for (Iterator iterator = element.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {

                omElement = om;
                for (Iterator it = omElement.getChildrenWithName(new QName("property")); it.hasNext();) {
                    OMElement omRemove = (OMElement) it.next();
                    if (omRemove.getAttribute(new QName("name")).getAttributeValue()
                            .equals("useOriginalSchema")) {
                        it.remove();
                        element.build();
                        break;
                    }
                }
                break;
            }
        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        element.serialize(writer);
        restartServer();

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }

    }
}

From source file:org.wso2.carbon.registry.jira2.issues.test2.Carbon0102TestCase.java

/**
 * Update the registry XML file with tasks configurations
 *
 * @throws IOException//  w w w  .  j  a v  a2 s .c  o  m
 * @throws XMLStreamException
 */
public void updateRegistryXMLConfigurations() throws IOException, XMLStreamException {

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    File srcFile = new File(registryXmlPath);
    try {
        OMElement handlerConfig = AXIOMUtil.stringToOM(
                "\n<task name=\"MediationTask\" class=\"org.wso2.carbon.governance.platform.extensions.mediation."
                        + "MediationArtifactPopulatorTask\">\n"
                        + "            <trigger cron=\"1/10 * * * * ?\"/>\n"
                        + "            <property key=\"userName\" value=\"admin\" />\n"
                        + "            <property key=\"password\" value=\"admin\" />\n"
                        + "            <property key=\"serverUrl\" value=\"https://localhost:9444/services/\"/>\n"
                        + "            <property key=\"proxyArtifactKey\" value=\"proxy\" />\n"
                        + "            <property key=\"sequenceArtifactKey\" value=\"sequence\" />\n"
                        + "            <property key=\"endpointArtifactKey\" value=\"endpoint\" />\n"
                        + "</task>");

        OMElement registryXML = getRegistryXmlOmElement();
        Iterator iterator = registryXML.getChildrenWithName(new QName("tasks"));
        OMElement om = (OMElement) iterator.next();
        om.addChild(handlerConfig);
        registryXML.addChild(om);
        registryXML.build();

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e);

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e);

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

}

From source file:org.wso2.carbon.registry.jira2.issues.test2.Carbon0102TestCase.java

/**
 * Remove the changes from the registry xml.
 * @throws IOException//from w  ww . ja va 2s.co m
 * @throws XMLStreamException
 */
public void renewRegistryXML() throws IOException, XMLStreamException {

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    File srcFile = new File(registryXmlPath);
    try {

        OMElement registryXML = getRegistryXmlOmElement();
        Iterator iterator = registryXML.getChildrenWithName(new QName("tasks"));
        OMElement ele = (OMElement) iterator.next();
        Iterator childIterator = ele.getChildrenWithName(new QName("task"));
        childIterator.remove();
        registryXML.build();
        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e);

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e);

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

}

From source file:org.wso2.carbon.registry.scm.test.SvnTestCase.java

private void addScmConfiguration() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    try {//from  w w w.  ja v  a  2s  .com
        OMElement regConfig = getRegistryXmlOmElement();
        File checkOutDir = new File(getTempLocation());

        String scmConfig = "<scm>"
                + "        <connection checkOutURL=\"scm:svn:https://svn.wso2.org/repos/wso2/carbon/"
                + "platform/trunk/platform-integration/platform-automated-test-suite/"
                + "org.wso2.carbon.automation.test.repo/src/main/resources/artifacts/GREG/"
                + "policy\" workingDir=\"" + getTempLocation() + "\" mountPoint=\"/_system/"
                + "governance/policy\" checkInURL=\"\" readOnly=\"\" updateFrequency=\"1\">"
                + "                <username>anonymoususer</username>"
                + "                <password>anonymoususer123</password>" + "        </connection>"
                + "    </scm>";

        OMElement scmConfigOMElement = AXIOMUtil.stringToOM(scmConfig);
        scmConfigOMElement.build();
        regConfig.addChild(scmConfigOMElement);
        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        regConfig.serialize(writer);
        regConfig.build();
        isSCMConfigAdded = true;
        if (!checkOutDir.exists()) {
            checkOutDir.mkdir();
        }
        Thread.sleep(2000);
    } catch (Exception e) {
        log.error("registry.xml edit fails" + e.getMessage());
        throw new Exception("registry.xml edit fails" + e.getMessage());
    } finally {
        assert fileOutputStream != null;
        assert writer != null;
        writer.flush();
        writer.close();
        fileOutputStream.close();
    }
}

From source file:org.wso2.carbon.registry.scm.test.SvnTestCase.java

private void removeScmConfiguration() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    try {//from   w  w  w .  j  a  v  a 2  s.com
        OMElement regConfig = RegistryConfiguratorTestCase.getRegistryXmlOmElement();
        regConfig.getFirstChildWithName(new QName("scm")).discard();
        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        regConfig.serialize(writer);
        regConfig.build();
        Thread.sleep(2000);
    } catch (Exception e) {
        log.error("registry.xml edit fails" + e.getMessage());
        throw new Exception("registry.xml edit fails" + e.getMessage());
    } finally {
        assert fileOutputStream != null;
        assert writer != null;
        writer.flush();
        writer.close();
        fileOutputStream.close();
    }
}

From source file:org.wso2.carbon.registry.synchronization.operation.CheckInCommand.java

private void restoreFromFileSystem(Registry registry, UserInputCallback callback)
        throws SynchronizationException {
    sentCount = 0;//from ww  w .  ja va2 s . c  o  m

    // we are doing the check-in through a temp file. (so assumed enough spaces are there)
    File tempFile = null;
    boolean deleteTempFileFailed = false;
    XMLStreamWriter xmlWriter = null;
    Writer writer = null;
    try {
        try {
            tempFile = File.createTempFile(SynchronizationConstants.DUMP_META_FILE_NAME,
                    SynchronizationConstants.META_FILE_EXTENSION);

            try {
                writer = new FileWriter(tempFile);
                // wrap the writer with an xml stream writer
                xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
                // prepare the dump xml
                xmlWriter.writeStartDocument();
                createMetaElement(xmlWriter, workingLocation, checkInPath, callback);
                xmlWriter.writeEndDocument();
            } finally {
                try {
                    if (xmlWriter != null) {
                        xmlWriter.close();
                    }
                } finally {
                    if (writer != null) {
                        writer.close();
                    }
                }
            }
        } catch (IOException e) {
            throw new SynchronizationException(MessageCode.ERROR_IN_CREATING_TEMP_FILE_FOR_DUMP, e);
        } catch (XMLStreamException e) {
            throw new SynchronizationException(MessageCode.ERROR_IN_CREATING_XML_STREAM_WRITER, e);
        }

        // do the restoring if the file system is actually updated
        if (updated) {
            try {
                Reader reader = null;
                try {
                    reader = new FileReader(tempFile);
                    registry.restore(checkInPath, reader);
                } finally {
                    if (reader != null) {
                        reader.close();
                    }
                }
            } catch (IOException e) {
                throw new SynchronizationException(MessageCode.ERROR_IN_READING_TEMP_FILE_OF_DUMP, e);
            } catch (RegistryException e) {
                throw new SynchronizationException(MessageCode.ERROR_IN_RESTORING, e, new String[] {
                        "path: " + checkInPath, "registry url: " + registryUrl, "username: " + username });
            }
        }
    } finally {
        if (tempFile != null) {
            // Our intention here is to delete the temporary file. We are not bothered whether
            // this operation fails.
            deleteTempFileFailed = !FileUtils.deleteQuietly(tempFile);
        }
    }
    if (deleteTempFileFailed) {
        throw new SynchronizationException(MessageCode.ERROR_IN_CLEANING_UP,
                new String[] { "file path: " + tempFile.getAbsolutePath() });
    }

    if (cleanRegistry && registryUrl == null) {
        Utils.cleanEmbeddedRegistry();
    }
}

From source file:org.wso2.carbon.repository.core.ResourceStorer.java

/**
 * Method to do a dump./*from  ww w .j a va  2  s  .  com*/
 *
 * @param _path   the path to obtain the dump from.
 * @param writer the writer used.
 *
 * @throws RepositoryException if the operation failed.
 */
public void dump(String _path, Writer writer) throws RepositoryException {
    String path = _path;

    if (!path.equals("/") && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }

    XMLStreamWriter xmlWriter = null;
    try {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        xmlWriter = xof.createXMLStreamWriter(writer);

        // we are not using xmlWriter.writeStartDocument and writeEndDocument to get rid of the
        // xml descriptor it put in every child node
        dumpRecursively(path, xmlWriter, writer);
    } catch (XMLStreamException e) {
        String msg = "Failed to serialize the dumped element at " + path + ".";
        log.error(msg);
        throw new RepositoryException(msg, e);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (XMLStreamException e) {
            }
        }
    }
}