PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/logging/src/main/java/org/jboss/as/logging/LogHandlerListAttributeDefinition.java

#
Java | 240 lines | 187 code | 27 blank | 26 comment | 31 complexity | b43cb9a6ef3b3c2eecfd202d732529b0 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.logging;
  23. import org.jboss.as.controller.ListAttributeDefinition;
  24. import org.jboss.as.controller.SimpleAttributeDefinition;
  25. import org.jboss.as.controller.client.helpers.MeasurementUnit;
  26. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  27. import org.jboss.as.controller.descriptions.ResourceDescriptionResolver;
  28. import org.jboss.as.controller.operations.validation.AllowedValuesValidator;
  29. import org.jboss.as.controller.operations.validation.MinMaxValidator;
  30. import org.jboss.as.controller.operations.validation.ParameterValidator;
  31. import org.jboss.as.controller.registry.AttributeAccess;
  32. import org.jboss.dmr.ModelNode;
  33. import javax.xml.stream.XMLStreamException;
  34. import javax.xml.stream.XMLStreamWriter;
  35. import java.util.List;
  36. import java.util.Locale;
  37. import java.util.ResourceBundle;
  38. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.DESCRIPTION;
  39. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE_TYPE;
  40. /**
  41. * Date: 13.10.2011
  42. *
  43. * @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
  44. */
  45. public class LogHandlerListAttributeDefinition extends ListAttributeDefinition {
  46. private final SimpleAttributeDefinition valueType;
  47. private LogHandlerListAttributeDefinition(final String name, final String xmlName, final SimpleAttributeDefinition valueType, final boolean allowNull, final int minSize, final int maxSize, final String[] alternatives, final String[] requires, final AttributeAccess.Flag... flags) {
  48. super(name, xmlName, allowNull, minSize, maxSize, valueType.getValidator(), alternatives, requires, flags);
  49. this.valueType = valueType;
  50. }
  51. @Override
  52. public ModelNode addResourceAttributeDescription(ResourceBundle bundle, String prefix, ModelNode resourceDescription) {
  53. final ModelNode result = super.addResourceAttributeDescription(bundle, prefix, resourceDescription);
  54. addValueTypeDescription(result, prefix, bundle);
  55. return result;
  56. }
  57. @Override
  58. public ModelNode addOperationParameterDescription(ResourceBundle bundle, String prefix, ModelNode operationDescription) {
  59. final ModelNode result = super.addOperationParameterDescription(bundle, prefix, operationDescription);
  60. addValueTypeDescription(result, prefix, bundle);
  61. return result;
  62. }
  63. @Override
  64. protected void addValueTypeDescription(final ModelNode node, final ResourceBundle bundle) {
  65. node.get(VALUE_TYPE, valueType.getName()).set(getValueTypeDescription(false));
  66. }
  67. protected void addValueTypeDescription(final ModelNode node, final String prefix, final ResourceBundle bundle) {
  68. final ModelNode valueTypeDesc = getValueTypeDescription(false);
  69. valueTypeDesc.get(DESCRIPTION).set(valueType.getAttributeTextDescription(bundle, prefix));
  70. node.get(VALUE_TYPE, valueType.getName()).set(valueTypeDesc);
  71. }
  72. @Override
  73. protected void addAttributeValueTypeDescription(final ModelNode node, final ResourceDescriptionResolver resolver, final Locale locale, final ResourceBundle bundle) {
  74. final ModelNode valueTypeDesc = getValueTypeDescription(false);
  75. valueTypeDesc.get(DESCRIPTION).set(resolver.getResourceAttributeValueTypeDescription(getName(), locale, bundle, valueType.getName()));
  76. node.get(VALUE_TYPE, valueType.getName()).set(valueTypeDesc);
  77. }
  78. @Override
  79. protected void addOperationParameterValueTypeDescription(final ModelNode node, final String operationName, final ResourceDescriptionResolver resolver, final Locale locale, final ResourceBundle bundle) {
  80. final ModelNode valueTypeDesc = getValueTypeDescription(true);
  81. valueTypeDesc.get(DESCRIPTION).set(resolver.getOperationParameterValueTypeDescription(operationName, getName(), locale, bundle, valueType.getName()));
  82. node.get(VALUE_TYPE, valueType.getName()).set(valueTypeDesc);
  83. }
  84. @Override
  85. public void marshallAsElement(final ModelNode resourceModel, final XMLStreamWriter writer) throws XMLStreamException {
  86. if (resourceModel.hasDefined(getName())) {
  87. writer.writeStartElement(getXmlName());
  88. for (ModelNode handler : resourceModel.get(getName()).asList()) {
  89. valueType.marshallAsElement(handler, writer);
  90. }
  91. writer.writeEndElement();
  92. }
  93. }
  94. private ModelNode getValueTypeDescription(boolean forOperation) {
  95. final ModelNode result = new ModelNode();
  96. result.get(ModelDescriptionConstants.TYPE).set(valueType.getType());
  97. result.get(ModelDescriptionConstants.DESCRIPTION); // placeholder
  98. result.get(ModelDescriptionConstants.EXPRESSIONS_ALLOWED).set(valueType.isAllowExpression());
  99. if (forOperation) {
  100. result.get(ModelDescriptionConstants.REQUIRED).set(!valueType.isAllowNull());
  101. }
  102. result.get(ModelDescriptionConstants.NILLABLE).set(isAllowNull());
  103. final ModelNode defaultValue = valueType.getDefaultValue();
  104. if (!forOperation && defaultValue != null && defaultValue.isDefined()) {
  105. result.get(ModelDescriptionConstants.DEFAULT).set(defaultValue);
  106. }
  107. MeasurementUnit measurementUnit = valueType.getMeasurementUnit();
  108. if (measurementUnit != null && measurementUnit != MeasurementUnit.NONE) {
  109. result.get(ModelDescriptionConstants.UNIT).set(measurementUnit.getName());
  110. }
  111. final String[] alternatives = valueType.getAlternatives();
  112. if (alternatives != null) {
  113. for (final String alternative : alternatives) {
  114. result.get(ModelDescriptionConstants.ALTERNATIVES).add(alternative);
  115. }
  116. }
  117. final String[] requires = valueType.getRequires();
  118. if (requires != null) {
  119. for (final String required : requires) {
  120. result.get(ModelDescriptionConstants.REQUIRES).add(required);
  121. }
  122. }
  123. final ParameterValidator validator = valueType.getValidator();
  124. if (validator instanceof MinMaxValidator) {
  125. MinMaxValidator minMax = (MinMaxValidator) validator;
  126. Long min = minMax.getMin();
  127. if (min != null) {
  128. switch (valueType.getType()) {
  129. case STRING:
  130. case LIST:
  131. case OBJECT:
  132. result.get(ModelDescriptionConstants.MIN_LENGTH).set(min);
  133. break;
  134. default:
  135. result.get(ModelDescriptionConstants.MIN).set(min);
  136. }
  137. }
  138. Long max = minMax.getMax();
  139. if (max != null) {
  140. switch (valueType.getType()) {
  141. case STRING:
  142. case LIST:
  143. case OBJECT:
  144. result.get(ModelDescriptionConstants.MAX_LENGTH).set(max);
  145. break;
  146. default:
  147. result.get(ModelDescriptionConstants.MAX).set(max);
  148. }
  149. }
  150. }
  151. if (validator instanceof AllowedValuesValidator) {
  152. AllowedValuesValidator avv = (AllowedValuesValidator) validator;
  153. List<ModelNode> allowed = avv.getAllowedValues();
  154. if (allowed != null) {
  155. for (ModelNode ok : allowed) {
  156. result.get(ModelDescriptionConstants.ALLOWED).add(ok);
  157. }
  158. }
  159. }
  160. return result;
  161. }
  162. public static class Builder {
  163. private final String name;
  164. private final SimpleAttributeDefinition valueType;
  165. private String xmlName;
  166. private boolean allowNull;
  167. private int minSize;
  168. private int maxSize;
  169. private String[] alternatives;
  170. private String[] requires;
  171. private AttributeAccess.Flag[] flags;
  172. public Builder(final String name, final SimpleAttributeDefinition valueType) {
  173. this.name = name;
  174. this.valueType = valueType;
  175. }
  176. public static Builder of(final String name, final SimpleAttributeDefinition valueType) {
  177. return new Builder(name, valueType);
  178. }
  179. public LogHandlerListAttributeDefinition build() {
  180. if (xmlName == null) xmlName = name;
  181. if (maxSize < 1) maxSize = Integer.MAX_VALUE;
  182. return new LogHandlerListAttributeDefinition(name, xmlName, valueType, allowNull, minSize, maxSize, alternatives, requires, flags);
  183. }
  184. public Builder setAllowNull(final boolean allowNull) {
  185. this.allowNull = allowNull;
  186. return this;
  187. }
  188. public Builder setAlternates(final String... alternates) {
  189. this.alternatives = alternates;
  190. return this;
  191. }
  192. public Builder setFlags(final AttributeAccess.Flag... flags) {
  193. this.flags = flags;
  194. return this;
  195. }
  196. public Builder setMaxSize(final int maxSize) {
  197. this.maxSize = maxSize;
  198. return this;
  199. }
  200. public Builder setMinSize(final int minSize) {
  201. this.minSize = minSize;
  202. return this;
  203. }
  204. public Builder setRequires(final String... requires) {
  205. this.requires = requires;
  206. return this;
  207. }
  208. public Builder setXmlName(final String xmlName) {
  209. this.xmlName = xmlName;
  210. return this;
  211. }
  212. }
  213. }