org.jboss.staxmapper.XMLMapper Java Examples

The following examples show how to use org.jboss.staxmapper.XMLMapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testChildlessResource() throws Exception {
    MyParser parser = new ChildlessParser();
    String xml =
            "<subsystem xmlns=\"" + MyParser.NAMESPACE + "\">" +
                    "   <cluster attr1=\"alice\"/>" +
                    "</subsystem>";
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    ModelNode subsystem = opsToModel(operations);

    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance()
            .createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
 
Example #2
Source File: StandaloneXmlParser.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
public StandaloneXmlParser() {

        parserDelegate = new StandaloneXml(new ExtensionHandler() {
            @Override
            public void parseExtensions(XMLExtendedStreamReader reader, ModelNode address, Namespace namespace, List<ModelNode> list) throws XMLStreamException {
                reader.discardRemainder(); // noop
            }

            @Override
            public Set<ProfileParsingCompletionHandler> getProfileParsingCompletionHandlers() {
                return Collections.EMPTY_SET;
            }

            @Override
            public void writeExtensions(XMLExtendedStreamWriter writer, ModelNode modelNode) throws XMLStreamException {
                // noop
            }
        }, ParsingOption.IGNORE_SUBSYSTEM_FAILURES);

        xmlMapper = XMLMapper.Factory.create();
        xmlMapper.registerRootElement(new QName("urn:jboss:domain:4.0", "server"), parserDelegate);

    }
 
Example #3
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testElementParsers() throws Exception {

    MyParser parser = new MyParser();

    String xml = readResource("elements.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(5, operations.size());
    ModelNode subsystem = opsToModel(operations);

    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
 
Example #4
Source File: Jbas9020TestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testFSExploded() throws Exception {
    final String xml = "<?xml version='1.0' encoding='UTF-8'?>" +
            "<server name=\"example\" xmlns=\"urn:jboss:domain:1.0\">" +
            "    <deployments>" +
            "        <deployment name=\"test.war\" runtime-name=\"test-run.war\">" +
            "            <fs-exploded path=\"deployments/test.jar\" relative-to=\"jboss.server.base.dir\"/>" +
            "        </deployment>" +
            "    </deployments>" +
            "</server>";
    final XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xml));
    final StandaloneXml parser = new StandaloneXml(null, null, null, null);
    final List<ModelNode> operationList = new ArrayList<ModelNode>();
    final XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(namespace, "server"), parser);
    mapper.parseDocument(operationList, reader);
    final ModelNode content = operationList.get(1).get("content");
    assertEquals(false, content.get(0).get("archive").asBoolean());
    assertEquals("deployments/test.jar", content.get(0).get("path").asString());
    assertEquals("jboss.server.base.dir", content.get(0).get("relative-to").asString());
}
 
Example #5
Source File: Jbas9020TestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testFSArchive() throws Exception {
    final String xml = "<?xml version='1.0' encoding='UTF-8'?>" +
            "<server name=\"example\" xmlns=\"urn:jboss:domain:1.0\">" +
            "    <deployments>" +
            "        <deployment name=\"test.war\" runtime-name=\"test-run.war\">" +
            "            <fs-archive path=\"${jboss.home}/content/welcome.jar\"/>" +
            "        </deployment>" +
            "    </deployments>" +
            "</server>";
    final XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xml));
    final StandaloneXml parser = new StandaloneXml(null, null, null, null);
    final List<ModelNode> operationList = new ArrayList<ModelNode>();
    final XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(namespace, "server"), parser);
    mapper.parseDocument(operationList, reader);
    System.out.println(operationList.get(1));
    final ModelNode content = operationList.get(1).get("content");
    assertEquals(true, content.get(0).get("archive").asBoolean());
    assertEquals("${jboss.home}/content/welcome.jar", content.get(0).get("path").asString());
}
 
Example #6
Source File: Jbas9020TestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testContent() throws Exception {
    final String xml = "<?xml version='1.0' encoding='UTF-8'?>" +
            "<server name=\"example\" xmlns=\"urn:jboss:domain:1.0\">" +
            "    <deployments>" +
            "        <deployment name=\"test.war\" runtime-name=\"test-run.war\">" +
            "            <content sha1=\"1234\"/>" +
            "        </deployment>" +
            "    </deployments>" +
            "</server>";
    final XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xml));
    final StandaloneXml parser = new StandaloneXml(null, null, null, null);
    final List<ModelNode> operationList = new ArrayList<ModelNode>();
    final XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(namespace, "server"), parser);
    mapper.parseDocument(operationList, reader);
    final ModelNode content = operationList.get(1).get("content");
    assertArrayEquals(new byte[] { 0x12, 0x34 }, content.get(0).get("hash").asBytes());
}
 
Example #7
Source File: JBossAllXMLParsingProcessor.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void parse(final InputStream source, final File file,final XMLMapper mapper, final JBossAllXmlParseContext context) throws DeploymentUnitProcessingException {
    try {

        final XMLInputFactory inputFactory = INPUT_FACTORY;
        setIfSupported(inputFactory, XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
        setIfSupported(inputFactory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
        final XMLStreamReader streamReader = inputFactory.createXMLStreamReader(source);
        try {
            mapper.parseDocument(context, streamReader);
        } finally {
            safeClose(streamReader);
        }
    } catch (XMLStreamException e) {
        throw ServerLogger.ROOT_LOGGER.errorLoadingJBossXmlFile(file.getPath(), e);
    }
}
 
Example #8
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testServerWithComplexAttributeParser() throws Exception {
    ServerParser parser = new ServerParser();

    String xml = readResource("server-complex-attribute.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(5, operations.size());
    ModelNode subsystem = opsToModel(operations);

    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
 
Example #9
Source File: DeferredExtensionContext.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private XMLStreamException loadModule(final String moduleName, final XMLMapper xmlMapper) throws XMLStreamException {
    // Register element handlers for this extension
    try {
        final Module module = moduleLoader.loadModule(ModuleIdentifier.fromString(moduleName));
        boolean initialized = false;
        for (final Extension extension : module.loadService(Extension.class)) {
            ClassLoader oldTccl = WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(extension.getClass());
            try {
                extensionRegistry.initializeParsers(extension, moduleName, xmlMapper);
            } finally {
                WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
            }
            if (!initialized) {
                initialized = true;
            }
        }
        if (!initialized) {
            throw ControllerLogger.ROOT_LOGGER.notFound("META-INF/services/", Extension.class.getName(), module.getName());
        }
        return null;
    } catch (final ModuleLoadException e) {
        throw ControllerLogger.ROOT_LOGGER.failedToLoadModule(e);
    }
}
 
Example #10
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testComplexAttributesStuff() throws Exception {
    CoreParser parser = new CoreParser();

    String xml = readResource("core-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName("urn:jboss:domain:core:1.0", "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(2, operations.size());
    Assert.assertEquals(2, operations.get(1).get("listeners").asList().size());
    ModelNode subsystem = opsToModel(operations);

    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
 
Example #11
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testORBSubsystem() throws Exception {
    IIOPSubsystemParser parser = new IIOPSubsystemParser();

    String xml = readResource("orb-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName("urn:jboss:domain:orb-test:1.0", "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(1, operations.size());
    ModelNode subsystem = opsToModel(operations);

    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
 
Example #12
Source File: JBossAllXMLParsingProcessor.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final ResourceRoot root = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);

    VirtualFile descriptor = null;
    for (final String loc : DEPLOYMENT_STRUCTURE_DESCRIPTOR_LOCATIONS) {
        final VirtualFile file = root.getRoot().getChild(loc);
        if (file.exists()) {
            descriptor = file;
            break;
        }
    }
    if(descriptor == null) {
        return;
    }
    final XMLMapper mapper = XMLMapper.Factory.create();
    final Map<QName, AttachmentKey<?>> namespaceAttachments = new HashMap<QName, AttachmentKey<?>>();
    for(final JBossAllXMLParserDescription<?> parser : deploymentUnit.getAttachmentList(JBossAllXMLParserDescription.ATTACHMENT_KEY)) {
        namespaceAttachments.put(parser.getRootElement(), parser.getAttachmentKey());
        mapper.registerRootElement(parser.getRootElement(), new JBossAllXMLElementReader(parser));
    }
    mapper.registerRootElement(new QName(Namespace.JBOSS_1_0.getUriString(), JBOSS), Parser.INSTANCE);
    mapper.registerRootElement(new QName(Namespace.NONE.getUriString(), JBOSS), Parser.INSTANCE);

    final JBossAllXmlParseContext context = new JBossAllXmlParseContext(deploymentUnit);
    parse(descriptor, mapper, context);

    //we use this map to detect the presence of two different but functionally equivalent namespaces
    final Map<AttachmentKey<?>, QName> usedNamespaces = new HashMap<AttachmentKey<?>, QName>();
    for(Map.Entry<QName, Object> entry : context.getParseResults().entrySet()) {
        final AttachmentKey attachmentKey = namespaceAttachments.get(entry.getKey());
        if(usedNamespaces.containsKey(attachmentKey)) {
            throw ServerLogger.ROOT_LOGGER.equivalentNamespacesInJBossXml(entry.getKey(), usedNamespaces.get(attachmentKey));
        }
        usedNamespaces.put(attachmentKey, entry.getKey());
        deploymentUnit.putAttachment(attachmentKey, entry.getValue());
    }
}
 
Example #13
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testSimpleParser() throws Exception {

    MyParser parser = new MyParser();

    String xml = readResource("simple-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(4, operations.size());
    ModelNode subsystem = opsToModel(operations);
    ModelNode resource = subsystem.get("resource","foo");

    ModelNode complexMap = MyParser.COMPLEX_MAP.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, resource);
    Assert.assertEquals("Model type should be map", ModelType.OBJECT, complexMap.getType());
    Assert.assertEquals("Map should have 3 elements", 3, complexMap.asList().size());
    Assert.assertEquals("some.class1", complexMap.get("key1", "name").asString());
    Assert.assertEquals("some.class2", complexMap.get("key2", "name").asString());
    Assert.assertEquals("some.module3", complexMap.get("key3", "module").asString());


    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
 
Example #14
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testMail() throws Exception {

    MyParser parser = new MailParser();

    String xml = readResource("mail-parser.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(4, operations.size());
    ModelNode subsystem = opsToModel(operations);


    ModelNode propsModel = subsystem.get("mail-session", "custom");
    assertTrue("Model should be defined", propsModel.has(MyParser.WRAPPED_PROPERTIES.getName()));
    Map<String,String> props = MyParser.WRAPPED_PROPERTIES.unwrap(ExpressionResolver.TEST_RESOLVER, propsModel);
    Assert.assertEquals(0, props.size());

    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
 
Example #15
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testServerParser() throws Exception {
    ServerParser parser = new ServerParser();

    String xml = readResource("server-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    Assert.assertEquals(4, operations.size());
    ModelNode subsystem = opsToModel(operations);

    ModelNode server = subsystem.get("server", "default");
    ModelNode interceptors = MyParser.INTERCEPTORS.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, server);
    Assert.assertEquals("Model type should be list", ModelType.LIST, interceptors.getType());
    Assert.assertEquals("List should have 0 elements", 0, interceptors.asList().size());

    ModelNode complexList = MyParser.COMPLEX_LIST.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, server); //this one should be undefined
    Assert.assertTrue("Should be empty", !complexList.isDefined());

    ModelNode complexListWithDefault = MyParser.COMPLEX_LIST_WITH_DEFAULT.resolveModelAttribute(ExpressionResolver.TEST_RESOLVER, server); //this one should be undefined
    Assert.assertEquals("Model type should be list", ModelType.LIST, complexListWithDefault.getType());
    Assert.assertEquals("List should have 1 elements", 1, complexListWithDefault.asList().size());

    /*List<ModelNode> unwrapped = MyParser.COMPLEX_LIST_WITH_DEFAULT.unwrap(ExpressionResolver.TEST_RESOLVER, server); //this one should be undefined
    Assert.assertEquals("it should contain one element", 1, unwrapped.size());*/


    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
 
Example #16
Source File: DeploymentStructureDescriptorParser.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public DeploymentStructureDescriptorParser() {
    mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(ROOT_1_0, JBossDeploymentStructureParser10.INSTANCE);
    mapper.registerRootElement(ROOT_1_1, JBossDeploymentStructureParser11.INSTANCE);
    mapper.registerRootElement(ROOT_1_2, JBossDeploymentStructureParser12.INSTANCE);
    mapper.registerRootElement(ROOT_1_3, JBossDeploymentStructureParser13.INSTANCE);
    mapper.registerRootElement(ROOT_NO_NAMESPACE, JBossDeploymentStructureParser13.INSTANCE);
}
 
Example #17
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test(expected = XMLStreamException.class)
public void testInvalidGroups() throws Exception {
    MyParser parser = new AttributeGroupParser();
    String xml =
            "<subsystem xmlns=\"" + MyParser.NAMESPACE + "\">" +
                    "   <invalid/>" +
                    "</subsystem>";
    StringReader strReader = new StringReader(xml);
    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
}
 
Example #18
Source File: SystemPropertiesParsingTest.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testSystemPropertyAlreadyExistIsCalled() throws Exception {
    // assign two properties in the system
    System.setProperty("org.jboss.as.server.parsing.test", "test-value");
    System.setProperty("org.jboss.as.server.parsing.secret", "super-secret-value");
    System.setProperty("org.jboss.as.server.parsing.secret-nested", "super-secret-value");
    try {
        final String xml = "<?xml version='1.0' encoding='UTF-8'?>"
                + "<server name=\"example\" xmlns=\"urn:jboss:domain:8.0\">"
                + "    <system-properties>\n"
                + "        <property name=\"org.jboss.as.server.parsing.secret\" value=\"${VAULT::vb::password::1}\"/>\n"
                + "        <property name=\"org.jboss.as.server.parsing.secret-nested\" value=\"${VAULT::vb::${not-found:password}::1}\"/>\n"
                + "        <property name=\"org.jboss.as.server.parsing.test\" value=\"other-value\"/>\n"
                + "    </system-properties>\n"
                + "</server>";
        final XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xml));
        final ExtensionRegistry extensionRegistry = new ExtensionRegistry(ProcessType.STANDALONE_SERVER,
                new RunningModeControl(RunningMode.NORMAL), null, null, null, RuntimeHostControllerInfoAccessor.SERVER);
        final StandaloneXml parser = new StandaloneXml(null, null, extensionRegistry);
        final List<ModelNode> operationList = new ArrayList<>();
        final XMLMapper mapper = XMLMapper.Factory.create();
        mapper.registerRootElement(new QName(namespace, "server"), parser);
        mapper.parseDocument(operationList, reader);
        // assert the method is called only once for test
        Mockito.verify(mockedLogger, Mockito.times(1)).systemPropertyAlreadyExist(Mockito.anyString());
        Mockito.verify(mockedLogger, Mockito.times(1)).systemPropertyAlreadyExist(Mockito.eq("org.jboss.as.server.parsing.test"));
    } finally {
        System.clearProperty("org.jboss.as.server.parsing.test");
        System.clearProperty("org.jboss.as.server.parsing.secret");
        System.clearProperty("org.jboss.as.server.parsing.secret-nested");
    }
}
 
Example #19
Source File: ChildFirstClassLoaderKernelServicesFactory.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static KernelServices create(List<ModelNode> bootOperations, ModelTestOperationValidatorFilter validateOpsFilter, ModelVersion legacyModelVersion,
        List<LegacyModelInitializerEntry> modelInitializerEntries) throws Exception {

    TestModelType type = TestModelType.DOMAIN;
    XMLMapper xmlMapper = XMLMapper.Factory.create();
    TestParser testParser = TestParser.create(null, xmlMapper, type);
    ModelInitializer modelInitializer = null;
    if (modelInitializerEntries != null && modelInitializerEntries.size() > 0) {
        modelInitializer = new LegacyModelInitializer(modelInitializerEntries);
    }

    RunningModeControl runningModeControl = new HostRunningModeControl(RunningMode.ADMIN_ONLY, RestartMode.HC_ONLY);
    ExtensionRegistry extensionRegistry = new ExtensionRegistry(ProcessType.HOST_CONTROLLER, runningModeControl);
    return AbstractKernelServicesImpl.create(ProcessType.HOST_CONTROLLER, runningModeControl, validateOpsFilter, bootOperations, testParser, legacyModelVersion, type, modelInitializer, extensionRegistry, null);
}
 
Example #20
Source File: TestParser.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static TestParser create(ExtensionRegistry registry, XMLMapper xmlMapper, TestModelType type) {
    TestParser testParser;
    String root;
    if (type == TestModelType.STANDALONE) {
        StandaloneXml standaloneXml = new StandaloneXml(null, Executors.newCachedThreadPool(), registry);
        testParser = new TestParser(type, standaloneXml, standaloneXml);
        root = "server";
    } else if (type == TestModelType.DOMAIN) {
        DomainXml domainXml = new DomainXml(null, Executors.newCachedThreadPool(), registry);
        testParser = new TestParser(type, domainXml, domainXml);
        root = "domain";
    } else if (type == TestModelType.HOST) {
        HostXml hostXml = new HostXml("master", RunningMode.NORMAL, false, null, Executors.newCachedThreadPool(), registry);
        testParser = new TestParser(type, hostXml, hostXml);
        root = "host";
    } else {
        throw new IllegalArgumentException("Unknown type " + type);
    }


    try {
        for (Namespace ns : Namespace.ALL_NAMESPACES) {
            xmlMapper.registerRootElement(new QName(ns.getUriString(), root), testParser);
        }
    } catch (NoSuchFieldError e) {
        //7.1.2 does not have the ALL_NAMESPACES field
        xmlMapper.registerRootElement(new QName(Namespace.DOMAIN_1_0.getUriString(), root), testParser);
        xmlMapper.registerRootElement(new QName(Namespace.DOMAIN_1_1.getUriString(), root), testParser);
        xmlMapper.registerRootElement(new QName(Namespace.DOMAIN_1_2.getUriString(), root), testParser);
    }
    return testParser;
}
 
Example #21
Source File: SubsystemTestDelegate.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
void initializeParser() throws Exception {
    //Initialize the parser
    xmlMapper = XMLMapper.Factory.create();
    extensionParsingRegistry = new ExtensionRegistry(getProcessType(), new RunningModeControl(RunningMode.NORMAL), null, null, null, RuntimeHostControllerInfoAccessor.SERVER);
    testParser = new TestParser(mainSubsystemName, extensionParsingRegistry);
    xmlMapper.registerRootElement(new QName(TEST_NAMESPACE, "test"), testParser);
    mainExtension.initializeParsers(extensionParsingRegistry.getExtensionParsingContext("Test", xmlMapper));
    addedExtraParsers = false;
}
 
Example #22
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test(expected = XMLStreamValidationException.class)
public void testInvalidMultipleObjectTypes() throws Exception {
    MyParser parser = new AttributeGroupParser();
    String xml = readResource("invalid-multiple-object-type-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
}
 
Example #23
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test(expected = XMLStreamValidationException.class)
public void testInvalidMultipleGroups() throws Exception {
    MyParser parser = new AttributeGroupParser();
    String xml = readResource("invalid-multiple-groups-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
}
 
Example #24
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testGroups() throws Exception {
    MyParser parser = new AttributeGroupParser();
    String xml = readResource("groups-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    ModelNode subsystem = opsToModel(operations);

    assertEquals("bar", subsystem.get("resource", "foo", "cluster-attr1").asString());
    assertEquals("baz", subsystem.get("resource", "foo", "cluster-attr2").asString());
    assertEquals("alice", subsystem.get("resource", "foo", "security-my-attr1").asString());
    assertEquals("bob", subsystem.get("resource", "foo", "security-my-attr2").asString());
    assertEquals("val", subsystem.get("resource", "foo", "props", "prop").asString());
    assertEquals("val", subsystem.get("resource", "foo", "wrapped-properties", "prop").asString());
    assertEquals("bar2", subsystem.get("resource", "foo2", "cluster-attr1").asString());
    assertEquals("baz2", subsystem.get("resource", "foo2", "cluster-attr2").asString());

    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance()
            .createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));

}
 
Example #25
Source File: PersistentResourceXMLParserTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testWrappersAndGroups() throws Exception {
    MyParser parser = new MyParser();
    String xml = readResource("groups-wrappers-subsystem.xml");
    StringReader strReader = new StringReader(xml);

    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);

    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);

    ModelNode subsystem = opsToModel(operations);

    assertEquals("bar", subsystem.get("resource", "foo", "cluster-attr1").asString());
    assertEquals("baz", subsystem.get("resource", "foo", "cluster-attr2").asString());
    assertEquals("alice", subsystem.get("resource", "foo", "security-my-attr1").asString());
    assertEquals("bob", subsystem.get("resource", "foo", "security-my-attr2").asString());
    assertEquals("bar2", subsystem.get("resource", "foo2", "cluster-attr1").asString());
    assertEquals("baz2", subsystem.get("resource", "foo2", "cluster-attr2").asString());

    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance()
            .createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));

}
 
Example #26
Source File: ExtensionXml.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void parseExtensions(final XMLExtendedStreamReader reader, final ModelNode address, final Namespace expectedNs, final List<ModelNode> list)
        throws XMLStreamException {
    DeferredExtensionContext ctx = this.deferredExtensionContext;
    if(ctx == null) {
        ctx = new DeferredExtensionContext(moduleLoader, extensionRegistry, bootExecutor);
    }
    long start = System.currentTimeMillis();

    requireNoAttributes(reader);

    final Set<String> found = new HashSet<String>();

    final XMLMapper xmlMapper = reader.getXMLMapper();

    while (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
        requireNamespace(reader, expectedNs);
        final Element element = Element.forName(reader.getLocalName());
        if (element != Element.EXTENSION) {
            throw unexpectedElement(reader);
        }

        // One attribute && require no content
        final String moduleName = readStringAttributeElement(reader, Attribute.MODULE.getLocalName());

        if (!found.add(moduleName)) {
            // duplicate module name
            throw ControllerLogger.ROOT_LOGGER.duplicateExtensionElement(Element.EXTENSION.getLocalName(), Attribute.MODULE.getLocalName(), moduleName, reader.getLocation());
        }
        ctx.addExtension(moduleName, xmlMapper);
        addExtensionAddOperation(address, list, moduleName);
    }

    if(deferredExtensionContext == null) {
        ctx.load();
    }
    long elapsed = System.currentTimeMillis() - start;
    if (ROOT_LOGGER.isDebugEnabled()) {
        ROOT_LOGGER.debugf("Parsed extensions in [%d] ms", elapsed);
    }
}
 
Example #27
Source File: XmlParsers.java    From galleon with Apache License 2.0 5 votes vote down vote up
private XmlParsers() {
    mapper = XMLMapper.Factory.create();
    new ConfigLayerXmlParser10().plugin(this);
    new ConfigXmlParser10().plugin(this);
    new FeatureConfigXmlParser10().plugin(this);
    new FeatureGroupXmlParser10().plugin(this);
    new FeaturePackXmlParser20().plugin(this);
    new FeatureSpecXmlParser10().plugin(this);
    new PackageXmlParser10().plugin(this);
    new PackageXmlParser20().plugin(this);
    new ProvisionedStateXmlParser30().plugin(this);
    new ProvisionedConfigXmlParser30().plugin(this);
    new ProvisioningXmlParser30().plugin(this);
}
 
Example #28
Source File: AbstractConfigurationPersister.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void marshallAsXml(final ModelNode model, final OutputStream output) throws ConfigurationPersistenceException {
    final XMLMapper mapper = XMLMapper.Factory.create();
    final Map<String, XMLElementWriter<SubsystemMarshallingContext>> localSubsystemWriters = new HashMap<>(subsystemWriters);
    try {
        XMLStreamWriter streamWriter = null;
        try {
            streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(output);
            final ModelMarshallingContext extensibleModel = new ModelMarshallingContext() {

                @Override
                public ModelNode getModelNode() {
                    return model;
                }

                @Override
                public XMLElementWriter<SubsystemMarshallingContext> getSubsystemWriter(String extensionName) {
                    //lazy create writer, but only once per config serialization
                    XMLElementWriter<SubsystemMarshallingContext> result = localSubsystemWriters.get(extensionName);
                    if (result == null) {
                        Supplier<XMLElementWriter<SubsystemMarshallingContext>> supplier = subsystemWriterSuppliers.get(extensionName);
                        if (supplier != null) {
                            result = supplier.get();
                            localSubsystemWriters.put(extensionName, result);
                        }
                    }
                    return result;
                }
            };
            mapper.deparseDocument(rootDeparser, extensibleModel, streamWriter);
            streamWriter.close();
        } finally {
            safeClose(streamWriter);
        }
    } catch (Exception e) {
        throw ControllerLogger.ROOT_LOGGER.failedToWriteConfiguration(e);
    }
}
 
Example #29
Source File: ExtensionRegistry.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private ExtensionParsingContextImpl(String extensionName, XMLMapper xmlMapper) {
    extension = getExtensionInfo(extensionName);
    if (xmlMapper != null) {
        synchronized (extension) {
            extension.xmlMapper = xmlMapper;
        }
    }
}
 
Example #30
Source File: StandaloneXMLParser.java    From thorntail with Apache License 2.0 5 votes vote down vote up
public StandaloneXMLParser() {
    ExtensionRegistry extensionRegistry = new ExtensionRegistry(
            ProcessType.SELF_CONTAINED,
            new RunningModeControl(RunningMode.NORMAL),
            null,
            null,
            null,
            RuntimeHostControllerInfoAccessor.SERVER
    );
    DeferredExtensionContext deferredExtensionContext =
        new DeferredExtensionContext(new BootModuleLoader(), extensionRegistry, Executors.newSingleThreadExecutor());
    parserDelegate = new StandaloneXml(
        new ExtensionHandler() {
            @Override
            public void parseExtensions(XMLExtendedStreamReader reader, ModelNode address, Namespace namespace, List<ModelNode> list) throws XMLStreamException {
                reader.discardRemainder(); // noop
            }

            @Override
            public Set<ProfileParsingCompletionHandler> getProfileParsingCompletionHandlers() {
                return Collections.emptySet();
            }

            @Override
            public void writeExtensions(XMLExtendedStreamWriter writer, ModelNode modelNode) throws XMLStreamException {
                // noop
            }
        },
        deferredExtensionContext,
        ParsingOption.IGNORE_SUBSYSTEM_FAILURES);

    xmlMapper = XMLMapper.Factory.create();

    addDelegate(new QName(Namespace.CURRENT.getUriString(), SERVER), parserDelegate);
    addDelegate(new QName("urn:jboss:domain:4.1", SERVER), parserDelegate);
    addDelegate(new QName("urn:jboss:domain:4.0", SERVER), parserDelegate);
    addDelegate(new QName("urn:jboss:domain:2.0", SERVER), parserDelegate);
}