Skip to content

Commit

Permalink
[WFLY-6390] Schema and version bump. Updated the parser and some test…
Browse files Browse the repository at this point in the history
…s to support version bumps.
  • Loading branch information
jamezp committed Nov 23, 2016
1 parent 554a913 commit 2b43304
Show file tree
Hide file tree
Showing 16 changed files with 478 additions and 70 deletions.
@@ -0,0 +1,113 @@
/*
* Copyright 2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.extension.batch.jberet;

import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.xml.stream.XMLStreamException;

import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.AttributeParser;
import org.jboss.as.controller.parsing.ParseUtils;
import org.jboss.dmr.ModelNode;
import org.jboss.staxmapper.XMLExtendedStreamReader;

/**
* Attribute parsing utilities.
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
class AttributeParsers {

/**
* An attribute parser for elements with a single {@code value} that don't allow any content within the element.
*/
static final AttributeParser VALUE = new AttributeParser() {
@Override
public void parseElement(final AttributeDefinition attribute, final XMLExtendedStreamReader reader, final ModelNode operation) throws XMLStreamException {
operation.get(attribute.getName()).set(readValueAttribute(reader));
ParseUtils.requireNoContent(reader);
}

@Override
public boolean isParseAsElement() {
return true;
}
};

/**
* Reads a {@code name} attribute on an element.
*
* @param reader the reader used to read the attribute with
*
* @return the name attribute or {@code null} if the name attribute was not defined
*
* @throws XMLStreamException if an XML processing error occurs
*/
static String readNameAttribute(final XMLExtendedStreamReader reader) throws XMLStreamException {
return readRequiredAttributes(reader, EnumSet.of(Attribute.NAME)).get(Attribute.NAME);
}

/**
* Reads a {@code value} attribute on an element.
*
* @param reader the reader used to read the attribute with
*
* @return the value attribute or {@code null} if the value attribute was not defined
*
* @throws XMLStreamException if an XML processing error occurs
*/
static String readValueAttribute(final XMLExtendedStreamReader reader) throws XMLStreamException {
return readRequiredAttributes(reader, EnumSet.of(Attribute.VALUE)).get(Attribute.VALUE);
}

/**
* Reads the required attributes from an XML configuration.
* <p>
* The reader must be on an element with attributes.
* </p>
*
* @param reader the reader for the attributes
* @param attributes the required attributes
*
* @return a map of the required attributes with the key being the attribute and the value being the value of the
* attribute
*
* @throws XMLStreamException if an XML processing error occurs
*/
static Map<Attribute, String> readRequiredAttributes(final XMLExtendedStreamReader reader, final Set<Attribute> attributes) throws XMLStreamException {
final int attributeCount = reader.getAttributeCount();
final Map<Attribute, String> result = new EnumMap<>(Attribute.class);
for (int i = 0; i < attributeCount; i++) {
final Attribute current = Attribute.forName(reader.getAttributeLocalName(i));
if (attributes.contains(current)) {
if (result.put(current, reader.getAttributeValue(i)) != null) {
throw ParseUtils.duplicateAttribute(reader, current.getLocalName());
}
} else {
throw ParseUtils.unexpectedAttribute(reader, i, attributes.stream().map(Attribute::getLocalName).collect(Collectors.toSet()));
}
}
if (result.isEmpty()) {
throw ParseUtils.missingRequired(reader, attributes.stream().map(Attribute::getLocalName).collect(Collectors.toSet()));
}
return result;
}
}
Expand Up @@ -91,6 +91,7 @@ public class BatchSubsystemDefinition extends SimpleResourceDefinition {
static final SimpleAttributeDefinition RESTART_JOBS_ON_RESUME = SimpleAttributeDefinitionBuilder.create("restart-jobs-on-resume", ModelType.BOOLEAN, true) static final SimpleAttributeDefinition RESTART_JOBS_ON_RESUME = SimpleAttributeDefinitionBuilder.create("restart-jobs-on-resume", ModelType.BOOLEAN, true)
.setAllowExpression(true) .setAllowExpression(true)
.setDefaultValue(new ModelNode(true)) .setDefaultValue(new ModelNode(true))
.setAttributeParser(AttributeParsers.VALUE)
.setAttributeMarshaller(AttributeMarshallers.VALUE) .setAttributeMarshaller(AttributeMarshallers.VALUE)
.build(); .build();


Expand Down
Expand Up @@ -34,8 +34,8 @@


public class BatchSubsystemExtension implements Extension { public class BatchSubsystemExtension implements Extension {


private static final int MANAGEMENT_API_MAJOR_VERSION = 1; private static final int MANAGEMENT_API_MAJOR_VERSION = 2;
private static final int MANAGEMENT_API_MINOR_VERSION = 1; private static final int MANAGEMENT_API_MINOR_VERSION = 0;
private static final int MANAGEMENT_API_MICRO_VERSION = 0; private static final int MANAGEMENT_API_MICRO_VERSION = 0;


/** /**
Expand All @@ -48,7 +48,8 @@ public class BatchSubsystemExtension implements Extension {


@Override @Override
public void initializeParsers(final ExtensionParsingContext context) { public void initializeParsers(final ExtensionParsingContext context) {
context.setSubsystemXmlMapping(BatchSubsystemDefinition.NAME, Namespace.BATCH_1_0.getUriString(), new BatchSubsystemParser_1_0()); context.setSubsystemXmlMapping(BatchSubsystemDefinition.NAME, Namespace.BATCH_1_0.getUriString(), BatchSubsystemParser_1_0::new);
context.setSubsystemXmlMapping(BatchSubsystemDefinition.NAME, Namespace.BATCH_2_0.getUriString(), BatchSubsystemParser_2_0::new);
} }


@Override @Override
Expand Down
Expand Up @@ -24,16 +24,18 @@


import static org.jboss.as.threads.Namespace.THREADS_1_1; import static org.jboss.as.threads.Namespace.THREADS_1_1;


import javax.xml.stream.XMLStreamConstants; import java.util.Collections;
import javax.xml.stream.XMLStreamException;
import java.util.EnumMap;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;


import org.jboss.as.controller.AttributeParser;
import org.jboss.as.controller.PathAddress; import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.SimpleAttributeDefinition;
import org.jboss.as.controller.operations.common.Util; import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.controller.parsing.ParseUtils; import org.jboss.as.controller.parsing.ParseUtils;
import org.jboss.as.threads.ThreadsParser; import org.jboss.as.threads.ThreadsParser;
Expand All @@ -51,6 +53,19 @@ public class BatchSubsystemParser_1_0 implements XMLStreamConstants, XMLElementR


public static final BatchSubsystemParser_1_0 INSTANCE = new BatchSubsystemParser_1_0(); public static final BatchSubsystemParser_1_0 INSTANCE = new BatchSubsystemParser_1_0();


private final Map<Element, SimpleAttributeDefinition> attributeElements;

public BatchSubsystemParser_1_0() {
this(Collections.emptyMap());
}

BatchSubsystemParser_1_0(final Map<Element, SimpleAttributeDefinition> additionalElements) {
attributeElements = new HashMap<>(additionalElements);
attributeElements.put(Element.DEFAULT_JOB_REPOSITORY, BatchSubsystemDefinition.DEFAULT_JOB_REPOSITORY);
attributeElements.put(Element.DEFAULT_THREAD_POOL, BatchSubsystemDefinition.DEFAULT_THREAD_POOL);
attributeElements.put(Element.RESTART_JOBS_ON_RESUME, BatchSubsystemDefinition.RESTART_JOBS_ON_RESUME);
}

@Override @Override
public void readElement(final XMLExtendedStreamReader reader, final List<ModelNode> ops) throws XMLStreamException { public void readElement(final XMLExtendedStreamReader reader, final List<ModelNode> ops) throws XMLStreamException {
final ThreadsParser threadsParser = ThreadsParser.getInstance(); final ThreadsParser threadsParser = ThreadsParser.getInstance();
Expand All @@ -59,25 +74,32 @@ public void readElement(final XMLExtendedStreamReader reader, final List<ModelNo
final ModelNode subsystemAddOp = Util.createAddOperation(subsystemAddress); final ModelNode subsystemAddOp = Util.createAddOperation(subsystemAddress);
ops.add(subsystemAddOp); ops.add(subsystemAddOp);


final Set<Element> requiredElements = EnumSet.of(Element.JOB_REPOSITORY, Element.THREAD_POOL, Element.DEFAULT_JOB_REPOSITORY, Element.DEFAULT_THREAD_POOL); // Find the required elements
final Set<Element> requiredElements = EnumSet.of(Element.JOB_REPOSITORY, Element.THREAD_POOL);
attributeElements.forEach((element, attribute) -> {
if (!attribute.isAllowNull() && attribute.getDefaultValue() == null) {
requiredElements.add(element);
}
});

final Namespace namespace = Namespace.forUri(reader.getNamespaceURI()); final Namespace namespace = Namespace.forUri(reader.getNamespaceURI());


while (reader.hasNext() && reader.nextTag() != END_ELEMENT) { while (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
final String localName = reader.getLocalName(); final String localName = reader.getLocalName();
final Element element = Element.forName(localName); final Element element = Element.forName(localName);
if (element == Element.DEFAULT_THREAD_POOL) { final SimpleAttributeDefinition attribute = attributeElements.get(element);
BatchSubsystemDefinition.DEFAULT_THREAD_POOL.parseAndSetParameter(readNameAttribute(reader), subsystemAddOp, reader); if (attribute != null) {
ParseUtils.requireNoContent(reader); final AttributeParser parser = attribute.getParser();
requiredElements.remove(Element.DEFAULT_THREAD_POOL); if (parser.isParseAsElement()) {
} else if (element == Element.DEFAULT_JOB_REPOSITORY) { parser.parseElement(attribute, reader, subsystemAddOp);
BatchSubsystemDefinition.DEFAULT_JOB_REPOSITORY.parseAndSetParameter(readNameAttribute(reader), subsystemAddOp, reader); } else {
ParseUtils.requireNoContent(reader); // Assume this is an element with a single name attribute
requiredElements.remove(Element.DEFAULT_JOB_REPOSITORY); parser.parseAndSetParameter(attribute, AttributeParsers.readNameAttribute(reader), subsystemAddOp, reader);
} else if (element == Element.RESTART_JOBS_ON_RESUME) { ParseUtils.requireNoContent(reader);
BatchSubsystemDefinition.RESTART_JOBS_ON_RESUME.parseAndSetParameter(readValueAttribute(reader), subsystemAddOp, reader); }
ParseUtils.requireNoContent(reader); requiredElements.remove(element);
} else if (element == Element.JOB_REPOSITORY) { } else if (element == Element.JOB_REPOSITORY) {
final String name = readNameAttribute(reader); final String name = AttributeParsers.readNameAttribute(reader);
parseJobRepository(reader, subsystemAddress, name, ops); parseJobRepository(reader, subsystemAddress, name, ops);
requiredElements.remove(Element.JOB_REPOSITORY); requiredElements.remove(Element.JOB_REPOSITORY);
} else if (element == Element.THREAD_POOL) { } else if (element == Element.THREAD_POOL) {
Expand Down Expand Up @@ -107,7 +129,7 @@ private void parseJobRepository(final XMLExtendedStreamReader reader, final Path
ops.add(Util.createAddOperation(subsystemAddress.append(InMemoryJobRepositoryDefinition.NAME, name))); ops.add(Util.createAddOperation(subsystemAddress.append(InMemoryJobRepositoryDefinition.NAME, name)));
ParseUtils.requireNoContent(reader); ParseUtils.requireNoContent(reader);
} else if (element == Element.JDBC) { } else if (element == Element.JDBC) {
final Map<Attribute, String> attributes = readRequiredAttributes(reader, EnumSet.of(Attribute.DATA_SOURCE)); final Map<Attribute, String> attributes = AttributeParsers.readRequiredAttributes(reader, EnumSet.of(Attribute.DATA_SOURCE));
final ModelNode op = Util.createAddOperation(subsystemAddress.append(JdbcJobRepositoryDefinition.NAME, name)); final ModelNode op = Util.createAddOperation(subsystemAddress.append(JdbcJobRepositoryDefinition.NAME, name));
JdbcJobRepositoryDefinition.DATA_SOURCE.parseAndSetParameter(attributes.get(Attribute.DATA_SOURCE), op, reader); JdbcJobRepositoryDefinition.DATA_SOURCE.parseAndSetParameter(attributes.get(Attribute.DATA_SOURCE), op, reader);
ops.add(op); ops.add(op);
Expand All @@ -117,45 +139,4 @@ private void parseJobRepository(final XMLExtendedStreamReader reader, final Path
} }
} }
} }

}
static String readNameAttribute(final XMLExtendedStreamReader reader) throws XMLStreamException {
return readRequiredAttributes(reader, EnumSet.of(Attribute.NAME)).get(Attribute.NAME);
}

static String readValueAttribute(final XMLExtendedStreamReader reader) throws XMLStreamException {
return readRequiredAttributes(reader, EnumSet.of(Attribute.VALUE)).get(Attribute.VALUE);
}

/**
* Reads the required attributes from an XML configuration.
* <p>
* The reader must be on an element with attributes.
* </p>
*
* @param reader the reader for the attributes
* @param attributes the required attributes
*
* @return a map of the required attributes with the key being the attribute and the value being the value of the
* attribute
*
* @throws XMLStreamException if an XML processing error occurs
*/
static Map<Attribute, String> readRequiredAttributes(final XMLExtendedStreamReader reader, final Set<Attribute> attributes) throws XMLStreamException {
final int attributeCount = reader.getAttributeCount();
final Map<Attribute, String> result = new EnumMap<>(Attribute.class);
for (int i = 0; i < attributeCount; i++) {
final Attribute current = Attribute.forName(reader.getAttributeLocalName(i));
if (attributes.contains(current)) {
if (result.put(current, reader.getAttributeValue(i)) != null) {
throw ParseUtils.duplicateAttribute(reader, current.getLocalName());
}
} else {
throw ParseUtils.unexpectedAttribute(reader, i, attributes.stream().map(Attribute::getLocalName).collect(Collectors.toSet()));
}
}
if (result.isEmpty()) {
throw ParseUtils.missingRequired(reader, attributes.stream().map(Attribute::getLocalName).collect(Collectors.toSet()));
}
return result;
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.extension.batch.jberet;

import java.util.List;
import javax.xml.stream.XMLStreamConstants;

import org.jboss.dmr.ModelNode;
import org.jboss.staxmapper.XMLElementReader;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
class BatchSubsystemParser_2_0 extends BatchSubsystemParser_1_0 implements XMLStreamConstants, XMLElementReader<List<ModelNode>> {

public static final BatchSubsystemParser_2_0 INSTANCE = new BatchSubsystemParser_2_0();

public BatchSubsystemParser_2_0() {
}
}
Expand Up @@ -33,12 +33,13 @@ enum Namespace {
UNKNOWN(null), UNKNOWN(null),


BATCH_1_0("urn:jboss:domain:batch-jberet:1.0"), BATCH_1_0("urn:jboss:domain:batch-jberet:1.0"),
BATCH_2_0("urn:jboss:domain:batch-jberet:2.0"),
; ;


/** /**
* The current namespace version. * The current namespace version.
*/ */
public static final Namespace CURRENT = BATCH_1_0; public static final Namespace CURRENT = BATCH_2_0;


private final String name; private final String name;


Expand Down

0 comments on commit 2b43304

Please sign in to comment.