Skip to content

Commit 528f95f

Browse files
ctomcbstansberry
authored andcommitted
AS7-5427 Add support for custom attribute marshallers
1 parent 7a8c2ae commit 528f95f

File tree

38 files changed

+331
-339
lines changed

38 files changed

+331
-339
lines changed

controller/src/main/java/org/jboss/as/controller/AbstractAttributeDefinitionBuilder.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ public abstract class AbstractAttributeDefinitionBuilder<BUILDER extends Abstrac
5252
protected boolean validateNull = true;
5353
protected int minSize;
5454
protected int maxSize;
55-
56-
5755
protected AttributeAccess.Flag[] flags;
56+
protected AttributeMarshaller attributeMarshaller = null;
5857

5958
public AbstractAttributeDefinitionBuilder(final String attributeName, final ModelType type) {
6059
this(attributeName, type, false);
@@ -67,7 +66,7 @@ public AbstractAttributeDefinitionBuilder(final String attributeName, final Mode
6766
this.xmlName = name;
6867
}
6968

70-
public AbstractAttributeDefinitionBuilder(final SimpleAttributeDefinition basis) {
69+
public AbstractAttributeDefinitionBuilder(final AttributeDefinition basis) {
7170
this.name = basis.getName();
7271
this.type = basis.getType();
7372
this.xmlName = basis.getXmlName();
@@ -80,6 +79,7 @@ public AbstractAttributeDefinitionBuilder(final SimpleAttributeDefinition basis)
8079
this.validator = basis.getValidator();
8180
Set<AttributeAccess.Flag> basisFlags = basis.getFlags();
8281
this.flags = basisFlags.toArray(new AttributeAccess.Flag[basisFlags.size()]);
82+
this.attributeMarshaller = basis.getAttributeMarshaller();
8383
}
8484

8585
public abstract ATTRIBUTE build();
@@ -228,4 +228,8 @@ public BUILDER setMinSize(final int minSize) {
228228
this.minSize = minSize;
229229
return (BUILDER) this;
230230
}
231+
public BUILDER setAttributeMarshaller(AttributeMarshaller marshaller){
232+
this.attributeMarshaller = marshaller;
233+
return (BUILDER)this;
234+
}
231235
}

controller/src/main/java/org/jboss/as/controller/AttributeDefinition.java

+29-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.List;
2828
import java.util.Locale;
2929
import java.util.ResourceBundle;
30-
3130
import javax.xml.stream.XMLStreamException;
3231
import javax.xml.stream.XMLStreamWriter;
3332

@@ -71,19 +70,20 @@ public abstract class AttributeDefinition {
7170
private final ParameterCorrector valueCorrector;
7271
private final ParameterValidator validator;
7372
private final EnumSet<AttributeAccess.Flag> flags;
73+
protected final AttributeMarshaller attributeMarshaller;
7474

7575
protected AttributeDefinition(String name, String xmlName, final ModelNode defaultValue, final ModelType type,
7676
final boolean allowNull, final boolean allowExpression, final MeasurementUnit measurementUnit,
7777
final ParameterValidator validator, final String[] alternatives, final String[] requires,
7878
final AttributeAccess.Flag... flags) {
7979
this(name, xmlName, defaultValue, type, allowNull, allowExpression, measurementUnit,
80-
null, validator, true, alternatives, requires, flags);
80+
null, validator, true, alternatives, requires, null, flags);
8181
}
8282

8383
protected AttributeDefinition(String name, String xmlName, final ModelNode defaultValue, final ModelType type,
8484
final boolean allowNull, final boolean allowExpression, final MeasurementUnit measurementUnit,
8585
final ParameterCorrector valueCorrector, final ParameterValidator validator,
86-
boolean validateNull, final String[] alternatives, final String[] requires,
86+
boolean validateNull, final String[] alternatives, final String[] requires, AttributeMarshaller attributeMarshaller,
8787
final AttributeAccess.Flag... flags) {
8888

8989
this.name = name;
@@ -113,6 +113,11 @@ protected AttributeDefinition(String name, String xmlName, final ModelNode defau
113113
} else {
114114
this.flags = EnumSet.of(flags[0], flags);
115115
}
116+
if (attributeMarshaller != null) {
117+
this.attributeMarshaller = attributeMarshaller;
118+
} else {
119+
this.attributeMarshaller = new DefaultAttributeMarshaller();
120+
}
116121
}
117122

118123
public String getName() {
@@ -170,7 +175,7 @@ public EnumSet<AttributeAccess.Flag> getFlags() {
170175
* @return {@code true} if the given {@code resourceModel} has a defined value under this attribute's {@link #getName()} () name}.
171176
*/
172177
public boolean isMarshallable(final ModelNode resourceModel) {
173-
return isMarshallable(resourceModel, true);
178+
return attributeMarshaller.isMarshallable(this, resourceModel, true);
174179
}
175180

176181
/**
@@ -183,7 +188,7 @@ public boolean isMarshallable(final ModelNode resourceModel) {
183188
* and {@code marshallDefault} is {@code true} or that value differs from this attribute's {@link #getDefaultValue() default value}.
184189
*/
185190
public boolean isMarshallable(final ModelNode resourceModel, final boolean marshallDefault) {
186-
return resourceModel.hasDefined(name) && (marshallDefault || !resourceModel.get(name).equals(defaultValue));
191+
return attributeMarshaller.isMarshallable(this, resourceModel, marshallDefault);
187192
}
188193

189194
/**
@@ -279,14 +284,27 @@ public boolean hasAlternative(final ModelNode operationObject) {
279284
}
280285

281286
/**
282-
* Marshalls the value from the given {@code resourceModel} as an xml element, if it
287+
* Marshalls the value from the given {@code resourceModel} as an xml attribute, if it
283288
* {@link #isMarshallable(org.jboss.dmr.ModelNode, boolean) is marshallable}.
284289
*
285290
* @param resourceModel the model, a non-null node of {@link org.jboss.dmr.ModelType#OBJECT}.
286291
* @param writer stream writer to use for writing the attribute
287292
* @throws javax.xml.stream.XMLStreamException if thrown by {@code writer}
288293
*/
289-
public abstract void marshallAsElement(final ModelNode resourceModel, final XMLStreamWriter writer) throws XMLStreamException;
294+
public void marshallAsElement(final ModelNode resourceModel, final XMLStreamWriter writer) throws XMLStreamException{
295+
marshallAsElement(resourceModel,true,writer);
296+
}
297+
298+
/**
299+
* Marshalls the value from the given {@code resourceModel} as an xml element, if it
300+
* {@link #isMarshallable(org.jboss.dmr.ModelNode, boolean) is marshallable}.
301+
*
302+
* @param resourceModel the model, a non-null node of {@link org.jboss.dmr.ModelType#OBJECT}.
303+
* @param writer stream writer to use for writing the attribute
304+
* @throws javax.xml.stream.XMLStreamException
305+
* if thrown by {@code writer}
306+
*/
307+
public abstract void marshallAsElement(final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException;
290308

291309
/**
292310
* Creates a returns a basic model node describing the attribute, after attaching it to the given overall resource
@@ -470,6 +488,10 @@ private ModelNode validateOperation(final ModelNode operationObject, final boole
470488
return node;
471489
}
472490

491+
public AttributeMarshaller getAttributeMarshaller() {
492+
return attributeMarshaller;
493+
}
494+
473495
private final OperationContext NO_OPERATION_CONTEXT_FOR_RESOLVING_MODEL_PARAMETERS = new OperationContext() {
474496

475497
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.jboss.as.controller;
2+
3+
import javax.xml.stream.XMLStreamException;
4+
import javax.xml.stream.XMLStreamWriter;
5+
6+
import org.jboss.dmr.ModelNode;
7+
8+
/**
9+
* @author <a href="mailto:[email protected]">Tomaz Cerar</a>
10+
*/
11+
public abstract class AttributeMarshaller {
12+
13+
/**
14+
* Gets whether the given {@code resourceModel} has a value for this attribute that should be marshalled to XML.
15+
* <p>
16+
* This is the same as {@code isMarshallable(resourceModel, true)}.
17+
* </p>
18+
* @param attribute - attribute for which marshaling is being done
19+
* @param resourceModel the model, a non-null node of {@link org.jboss.dmr.ModelType#OBJECT}.
20+
* @return {@code true} if the given {@code resourceModel} has a defined value under this attribute's {@link AttributeDefinition#getName()} () name}.
21+
*/
22+
public boolean isMarshallable(final AttributeDefinition attribute,final ModelNode resourceModel) {
23+
return isMarshallable(attribute,resourceModel, true);
24+
}
25+
26+
/**
27+
* Gets whether the given {@code resourceModel} has a value for this attribute that should be marshalled to XML.
28+
*
29+
* @param attribute - attribute for which marshaling is being done
30+
* @param resourceModel the model, a non-null node of {@link org.jboss.dmr.ModelType#OBJECT}.
31+
* @param marshallDefault {@code true} if the value should be marshalled even if it matches the default value
32+
* @return {@code true} if the given {@code resourceModel} has a defined value under this attribute's {@link AttributeDefinition#getName()} () name}
33+
* and {@code marshallDefault} is {@code true} or that value differs from this attribute's {@link AttributeDefinition#getDefaultValue() default value}.
34+
*/
35+
public boolean isMarshallable(final AttributeDefinition attribute, final ModelNode resourceModel, final boolean marshallDefault) {
36+
return resourceModel.hasDefined(attribute.getName()) && (marshallDefault || !resourceModel.get(attribute.getName()).equals(attribute.getDefaultValue()));
37+
}
38+
39+
/**
40+
* Marshalls the value from the given {@code resourceModel} as an xml element, if it
41+
* {@link #isMarshallable(AttributeDefinition, org.jboss.dmr.ModelNode, boolean) is marshallable}.
42+
*
43+
* @param attribute - attribute for which marshaling is being done
44+
* @param resourceModel the model, a non-null node of {@link org.jboss.dmr.ModelType#OBJECT}.
45+
* @param writer stream writer to use for writing the attribute
46+
* @throws javax.xml.stream.XMLStreamException
47+
* if thrown by {@code writer}
48+
*/
49+
50+
public void marshallAsAttribute(final AttributeDefinition attribute,final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException{
51+
52+
}
53+
54+
public void marshallAsElement(final AttributeDefinition attribute, final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException{
55+
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.jboss.as.controller;
2+
3+
import javax.xml.stream.XMLStreamException;
4+
import javax.xml.stream.XMLStreamWriter;
5+
6+
import org.jboss.dmr.ModelNode;
7+
8+
/**
9+
* @author <a href="mailto:[email protected]">Tomaz Cerar</a> (c) 2012 Red Hat Inc.
10+
*/
11+
public class DefaultAttributeMarshaller extends AttributeMarshaller {
12+
13+
14+
public void marshallAsAttribute(final AttributeDefinition attribute, final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException {
15+
if (isMarshallable(attribute, resourceModel, marshallDefault)) {
16+
writer.writeAttribute(attribute.getXmlName(), resourceModel.get(attribute.getName()).asString());
17+
}
18+
}
19+
20+
public void marshallAsElement(AttributeDefinition attribute, ModelNode resourceModel, XMLStreamWriter writer) throws XMLStreamException {
21+
marshallAsElement(attribute, resourceModel, true, writer);
22+
}
23+
24+
@Override
25+
public void marshallAsElement(final AttributeDefinition attribute, final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException {
26+
if (isMarshallable(attribute, resourceModel, marshallDefault)) {
27+
writer.writeStartElement(attribute.getXmlName());
28+
String content = resourceModel.get(attribute.getName()).asString();
29+
if (content.indexOf('\n') > -1) {
30+
// Multiline content. Use the overloaded variant that staxmapper will format
31+
writer.writeCharacters(content);
32+
} else {
33+
// Staxmapper will just output the chars without adding newlines if this is used
34+
char[] chars = content.toCharArray();
35+
writer.writeCharacters(chars, 0, chars.length);
36+
}
37+
writer.writeEndElement();
38+
}
39+
}
40+
}

controller/src/main/java/org/jboss/as/controller/ListAttributeDefinition.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.util.Locale;
2626
import java.util.ResourceBundle;
27-
2827
import javax.xml.stream.Location;
2928
import javax.xml.stream.XMLStreamException;
3029
import javax.xml.stream.XMLStreamReader;
@@ -47,22 +46,29 @@ public abstract class ListAttributeDefinition extends AttributeDefinition {
4746
private final ParameterValidator elementValidator;
4847

4948
public ListAttributeDefinition(final String name, final boolean allowNull, final ParameterValidator elementValidator) {
50-
this(name, name, allowNull, 0, Integer.MAX_VALUE, elementValidator, null, null);
49+
this(name, name, allowNull, 0, Integer.MAX_VALUE, elementValidator, null, null, null);
5150
}
5251

5352
public ListAttributeDefinition(final String name, final boolean allowNull, final ParameterValidator elementValidator, final AttributeAccess.Flag... flags) {
54-
this(name, name, allowNull, 0, Integer.MAX_VALUE, elementValidator, null, null, flags);
53+
this(name, name, allowNull, 0, Integer.MAX_VALUE, elementValidator, null, null, null, flags);
5554
}
5655

5756
public ListAttributeDefinition(final String name, final String xmlName, final boolean allowNull,
5857
final int minSize, final int maxSize, final ParameterValidator elementValidator) {
59-
this(name, xmlName, allowNull, minSize, maxSize, elementValidator, null, null);
58+
this(name, xmlName, allowNull, minSize, maxSize, elementValidator, null, null, null);
59+
}
60+
61+
public ListAttributeDefinition(final String name, final String xmlName, final boolean allowNull,
62+
final int minSize, final int maxSize, final ParameterValidator elementValidator,
63+
final String[] alternatives, final String[] requires, final AttributeMarshaller attributeMarshaller, final AttributeAccess.Flag... flags) {
64+
super(name, xmlName, null, ModelType.LIST, allowNull, false, null, null, new ListValidator(elementValidator, allowNull, minSize, maxSize), allowNull, alternatives, requires, attributeMarshaller, flags);
65+
this.elementValidator = elementValidator;
6066
}
6167

6268
public ListAttributeDefinition(final String name, final String xmlName, final boolean allowNull,
6369
final int minSize, final int maxSize, final ParameterValidator elementValidator,
6470
final String[] alternatives, final String[] requires, final AttributeAccess.Flag... flags) {
65-
super(name, xmlName, null, ModelType.LIST, allowNull, false, null, new ListValidator(elementValidator, allowNull, minSize, maxSize), alternatives, requires, flags);
71+
super(name, xmlName, null, ModelType.LIST, allowNull, false, null, null, new ListValidator(elementValidator, allowNull, minSize, maxSize), allowNull, alternatives, requires,null, flags);
6672
this.elementValidator = elementValidator;
6773
}
6874

@@ -215,6 +221,7 @@ public ModelNode addOperationParameterDescription(ResourceBundle bundle, String
215221
return result;
216222
}
217223

224+
218225
protected abstract void addValueTypeDescription(final ModelNode node, final ResourceBundle bundle);
219226

220227
protected abstract void addAttributeValueTypeDescription(final ModelNode node, final ResourceDescriptionResolver resolver,

controller/src/main/java/org/jboss/as/controller/ObjectListAttributeDefinition.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public class ObjectListAttributeDefinition extends ListAttributeDefinition {
5252
private final ObjectTypeAttributeDefinition valueType;
5353

5454
private ObjectListAttributeDefinition(final String name, final String xmlName, final ObjectTypeAttributeDefinition valueType, final boolean allowNull, final int minSize, final int maxSize, final String[] alternatives, final String[] requires, final AttributeAccess.Flag... flags) {
55-
super(name, xmlName, allowNull, minSize, maxSize, valueType.getValidator(), alternatives, requires, flags);
55+
super(name, xmlName, allowNull, minSize, maxSize, valueType.getValidator(), alternatives, requires, null,flags);
56+
this.valueType = valueType;
57+
}
58+
59+
private ObjectListAttributeDefinition(final String name, final String xmlName, final ObjectTypeAttributeDefinition valueType, final boolean allowNull, final int minSize, final int maxSize, final String[] alternatives, final String[] requires, final AttributeMarshaller attributeMarshaller, final AttributeAccess.Flag... flags) {
60+
super(name, xmlName, allowNull, minSize, maxSize, valueType.getValidator(), alternatives, requires,attributeMarshaller, flags);
5661
this.valueType = valueType;
5762
}
5863

@@ -91,7 +96,7 @@ protected void addOperationParameterValueTypeDescription(final ModelNode node, f
9196
}
9297

9398
@Override
94-
public void marshallAsElement(final ModelNode resourceModel, final XMLStreamWriter writer) throws XMLStreamException {
99+
public void marshallAsElement(final ModelNode resourceModel, final boolean marshalDefault, final XMLStreamWriter writer) throws XMLStreamException {
95100
if (resourceModel.hasDefined(getName())) {
96101
writer.writeStartElement(getXmlName());
97102
for (ModelNode handler : resourceModel.get(getName()).asList()) {
@@ -186,7 +191,7 @@ public static Builder of(final String name, final ObjectTypeAttributeDefinition
186191
public ObjectListAttributeDefinition build() {
187192
if (xmlName == null) xmlName = name;
188193
if (maxSize < 1) maxSize = Integer.MAX_VALUE;
189-
return new ObjectListAttributeDefinition(name, xmlName, valueType, allowNull, minSize, maxSize, alternatives, requires, flags);
194+
return new ObjectListAttributeDefinition(name, xmlName, valueType, allowNull, minSize, maxSize, alternatives, requires, attributeMarshaller,flags);
190195
}
191196

192197
/*

controller/src/main/java/org/jboss/as/controller/ObjectTypeAttributeDefinition.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ private ObjectTypeAttributeDefinition(final String name, final String xmlName, f
6868
/*
6969
* Constructor which allows specifying a custom ParameterValidator. Disabled by default.
7070
*/
71-
private ObjectTypeAttributeDefinition(final String name, final String xmlName, final String suffix, final AttributeDefinition[] valueTypes, final boolean allowNull, final ParameterValidator validator, final ParameterCorrector corrector, final String[] alternatives, final String[] requires, final AttributeAccess.Flag... flags) {
72-
super(name, xmlName, null, ModelType.OBJECT, allowNull, false, null, corrector, validator, false, alternatives, requires, flags);
71+
private ObjectTypeAttributeDefinition(final String name, final String xmlName, final String suffix, final AttributeDefinition[] valueTypes, final boolean allowNull, final ParameterValidator validator, final ParameterCorrector corrector, final String[] alternatives, final String[] requires, final AttributeMarshaller attributeMarshaller, final AttributeAccess.Flag... flags) {
72+
super(name, xmlName, null, ModelType.OBJECT, allowNull, false, null, corrector, validator, false, alternatives, requires, attributeMarshaller, flags);
7373
this.valueTypes = valueTypes;
7474
if (suffix == null) {
7575
this.suffix = "";
@@ -145,7 +145,7 @@ protected void addValueTypeDescription(final ModelNode node, final String prefix
145145
}
146146

147147
@Override
148-
public void marshallAsElement(final ModelNode resourceModel, final XMLStreamWriter writer) throws XMLStreamException {
148+
public void marshallAsElement(final ModelNode resourceModel, final boolean marshalDefault, final XMLStreamWriter writer) throws XMLStreamException {
149149
if (resourceModel.hasDefined(getName())) {
150150
writer.writeStartElement(getXmlName());
151151
for (AttributeDefinition valueType : valueTypes) {
@@ -252,7 +252,7 @@ public static Builder of(final String name, final AttributeDefinition[] valueTyp
252252

253253
public ObjectTypeAttributeDefinition build() {
254254
if (xmlName == null) { xmlName = name; }
255-
return new ObjectTypeAttributeDefinition(name, xmlName, suffix, valueTypes, allowNull, validator, corrector, alternatives, requires, flags);
255+
return new ObjectTypeAttributeDefinition(name, xmlName, suffix, valueTypes, allowNull, validator, corrector, alternatives, requires,attributeMarshaller, flags);
256256
}
257257

258258
public Builder setSuffix(final String suffix) {

0 commit comments

Comments
 (0)