Example usage for javax.xml.stream XMLOutputFactory newInstance

List of usage examples for javax.xml.stream XMLOutputFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory newInstance.

Prototype

public static XMLOutputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

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

@Test(groups = {
        "wso2.greg" }, description = "Restore registry.xml to the previous state", dependsOnMethods = "deleteService")
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
public void restoreRegistryXML()
        throws XMLStreamException, IOException, InterruptedException, RegistryException {

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

    File srcFile = new File(registryXmlPath);
    try {/*from   w w w .  j  a  v a  2  s.co  m*/
        OMElement element = getRegistryXmlOmElement();
        element.getChildrenWithName(new QName("aspect")).remove();
        element.build();
        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        element.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 {
        writer.close();
        fileOutputStream.close();
    }

    String resourcePath = getTestArtifactLocation() + "artifacts" + File.separator + "GREG" + File.separator
            + "xml" + File.separator + "lifeCycleConfig.xml";

    Resource lifecycleConfig = governance.newResource();
    lifecycleConfig.setMediaType("application/xml");
    lifecycleConfig.setContentStream(new FileInputStream(resourcePath));
    governance.put("trunk/lifeCycleConfig.xml", lifecycleConfig);

}

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

@AfterClass(alwaysRun = true)
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
public void deleteAndRestore() throws Exception {

    String workList = "<workList serverURL=\"" + automationContext.getContextUrls().getBackEndUrl()
            + "\" remote=\"false\">\n" + "        <username>" + userNameWithoutDomain + "</username>\n"
            + "        <password>" + automationContext.getContextTenant().getContextUser().getPassword()
            + "</password>\n" + "        </workList>";

    ServiceManager serviceManager = new ServiceManager(governance);
    GovernanceUtils.loadGovernanceArtifacts((UserRegistry) governance);
    Service[] services = serviceManager.getAllServices();
    for (Service s : services) {
        if (s.getQName().getLocalPart().equals(serviceName)) {
            serviceManager.removeService(s.getId());
        }//from   w w  w.j a  va 2s.  c  om

    }

    if (governance.resourceExists("trunk/lifeCycleConfig.xml")) {
        governance.delete("trunk/lifeCycleConfig.xml");
    }
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    File srcFile = new File(registryXmlPath);
    try {
        OMElement element = getRegistryXmlOmElement();
        element.getChildrenWithName(new QName("aspect")).remove();

        if (!element.getChildrenWithName(new QName("workList")).hasNext()) {
            element.addChild(AXIOMUtil.stringToOM(workList));

        }

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

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

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

    } catch (IOException e) {
        throw new IOException("XML stream exception" + e.getMessage());

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

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

public void editRegistryXML() throws XMLStreamException, IOException, InterruptedException {
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    File srcFile = new File(registryXmlPath);
    try {//from   w ww  . j  a  va 2  s. c om
        OMElement handlerConfig = getHandlerOmElement();
        OMElement registryXML = getRegistryXmlOmElement();
        if (registryXML.getChildrenWithName(new QName("workList")).hasNext()) {
            registryXML.getChildrenWithName(new QName("workList")).remove();

        }

        registryXML.addChild(handlerConfig);
        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.getMessage());

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

    } catch (IOException e) {
        throw new IOException("XML stream exception" + e.getMessage());

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

}

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

public void editRegistryXMLWithNewConfig() throws XMLStreamException, IOException, InterruptedException {
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    File srcFile = new File(registryXmlPath);
    try {//from  ww  w  . j  a va2  s .co  m
        OMElement handlerConfig = getHandlerOmElementNew();
        OMElement registryXML = getRegistryXmlOmElement();

        registryXML.addChild(handlerConfig);
        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.getMessage());

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

    } catch (IOException e) {
        throw new IOException("XML stream exception" + e.getMessage());

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

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

/**
 * Update the registry XML file with tasks configurations
 *
 * @throws IOException/*from   w  w  w. j av a2s.  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. j  a v a  2s .  c  o 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.lifecycle.test.server.mgt.RegistryConfiguratorTestCase.java

private void changeServiceCreationElement() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    OMElement documentElement = getRegistryXmlOmElement();
    try {//from   ww w.  j a va2 s .  com
        AXIOMXPath xpathExpression = new AXIOMXPath("/wso2registry/handler/property");
        List<OMElement> nodes = xpathExpression.selectNodes(documentElement);
        for (OMElement node : nodes) {
            if (node.getAttributeValue(new QName("name")).equals("createSOAPService")) {
                node.setText("false");
            }
        }
        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        documentElement.serialize(writer);
        documentElement.build();
        Thread.sleep(2000);

    } catch (Exception e) {
        log.error("Failed to modify registry.xml " + e.getMessage());
        throw new Exception("Failed to modify registry.xml " + e.getMessage());
    } finally {
        assert fileOutputStream != null;
        fileOutputStream.close();
        assert writer != null;
        writer.flush();
    }

}

From source file:org.wso2.carbon.registry.lifecycle.test.server.mgt.RegistryConfiguratorTestCase.java

private void increaseSearchIndexStartTimeDelay() throws Exception {

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    OMElement documentElement = getRegistryXmlOmElement();
    try {//w w w .  j a  va  2  s . c  o  m
        AXIOMXPath xpathExpression = new AXIOMXPath(
                "/wso2registry/indexingConfiguration/startingDelayInSeconds");
        OMElement indexConfigNode = (OMElement) xpathExpression.selectSingleNode(documentElement);
        indexConfigNode.setText("30");
        AXIOMXPath xpathExpression1 = new AXIOMXPath(
                "/wso2registry/indexingConfiguration/indexingFrequencyInSeconds");
        OMElement indexConfigNode1 = (OMElement) xpathExpression1.selectSingleNode(documentElement);
        indexConfigNode1.setText("3");
        AXIOMXPath xpathExpression2 = new AXIOMXPath("/wso2registry/indexingConfiguration/batchSize");
        OMElement indexConfigNode2 = (OMElement) xpathExpression2.selectSingleNode(documentElement);
        indexConfigNode2.setText("70");
        AXIOMXPath xpathExpression3 = new AXIOMXPath("/wso2registry/indexingConfiguration/indexerPoolSize");
        OMElement indexConfigNode3 = (OMElement) xpathExpression3.selectSingleNode(documentElement);
        indexConfigNode3.setText("50");
        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        documentElement.serialize(writer);
        documentElement.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;
        fileOutputStream.close();
        assert writer != null;
        writer.flush();
    }
}

From source file:org.wso2.carbon.registry.lifecycle.test.server.mgt.RegistryConfiguratorTestCase.java

private void enableJmxManagement() throws Exception {

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    OMElement documentElement = getRegistryXmlOmElement();
    try {//from  www  .  ja  v  a2s .  c o  m
        AXIOMXPath xpathExpression = new AXIOMXPath("/wso2registry/jmx");
        OMElement indexConfigNode = (OMElement) xpathExpression.selectSingleNode(documentElement);
        OMAttribute omAttribute = indexConfigNode.getAttribute(new QName("enabled"));
        omAttribute.setAttributeValue("true");
        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        documentElement.serialize(writer);
        documentElement.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 {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }
}

From source file:org.wso2.carbon.registry.lifecycle.test.server.mgt.RegistryConfiguratorTestCase.java

private void enableWorkList() throws Exception {

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    String workList;/*from w  w  w  .j  ava  2  s .c  o  m*/
    workList = "<workList serverURL=\"local://services/\" remote=\"false\">\n" + "        <username>"
            + automationContext.getContextTenant().getContextUser().getUserName() + "</username>\n"
            + "        <password>" + automationContext.getContextTenant().getContextUser().getPassword()
            + "</password>\n" + "    </workList>";

    try {
        OMElement registryXML = getRegistryXmlOmElement();
        registryXML.addChild(AXIOMUtil.stringToOM(workList));
        registryXML.build();
        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (Exception e) {
        throw new Exception("registry.xml update fails");
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }

}