PageRenderTime 105ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/controller/src/main/java/org/jboss/as/controller/operations/common/InterfaceCriteriaWriteHandler.java

#
Java | 175 lines | 126 code | 18 blank | 31 comment | 35 complexity | b5538409de7a282dd4a5d89732572a3c 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.controller.operations.common;
  23. import org.jboss.as.controller.AttributeDefinition;
  24. import org.jboss.as.controller.OperationContext;
  25. import org.jboss.as.controller.OperationFailedException;
  26. import org.jboss.as.controller.OperationStepHandler;
  27. import org.jboss.as.controller.PathAddress;
  28. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  29. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.NAME;
  30. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE;
  31. import org.jboss.as.controller.descriptions.common.InterfaceDescription;
  32. import org.jboss.as.controller.operations.validation.ParametersValidator;
  33. import org.jboss.as.controller.registry.ManagementResourceRegistration;
  34. import org.jboss.as.controller.registry.Resource;
  35. import org.jboss.dmr.ModelNode;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import static org.jboss.as.controller.ControllerMessages.MESSAGES;
  39. import org.jboss.dmr.ModelType;
  40. /**
  41. * Interface criteria write-attribute {@code OperationHandler}
  42. *
  43. * @author Emanuel Muckenhuber
  44. */
  45. public final class InterfaceCriteriaWriteHandler implements OperationStepHandler {
  46. public static final OperationStepHandler INSTANCE = new InterfaceCriteriaWriteHandler();
  47. private static final Map<String, AttributeDefinition> ATTRIBUTES = new HashMap<String, AttributeDefinition>();
  48. private static final OperationStepHandler VERIFY_HANDLER = new ModelValidationStep();
  49. private static final ParametersValidator nameValidator = new ParametersValidator();
  50. static {
  51. for(final AttributeDefinition def : InterfaceDescription.ROOT_ATTRIBUTES) {
  52. ATTRIBUTES.put(def.getName(), def);
  53. }
  54. }
  55. public static void register(final ManagementResourceRegistration registration) {
  56. for(final AttributeDefinition def : InterfaceDescription.ROOT_ATTRIBUTES) {
  57. registration.registerReadWriteAttribute(def, null, INSTANCE);
  58. }
  59. }
  60. private InterfaceCriteriaWriteHandler() {
  61. //
  62. }
  63. @Override
  64. public void execute(final OperationContext context, final ModelNode operation) throws OperationFailedException {
  65. nameValidator.validate(operation);
  66. final String attributeName = operation.require(NAME).asString();
  67. final ModelNode newValue = operation.hasDefined(VALUE) ? operation.get(VALUE) : new ModelNode();
  68. final ModelNode submodel = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS).getModel();
  69. final AttributeDefinition attributeDefinition = ATTRIBUTES.get(attributeName);
  70. if (attributeDefinition != null) {
  71. final ModelNode syntheticOp = new ModelNode();
  72. syntheticOp.get(attributeName).set(newValue);
  73. attributeDefinition.validateAndSet(syntheticOp, submodel);
  74. } else {
  75. throw new OperationFailedException(new ModelNode().set(MESSAGES.unknownAttribute(attributeName)));
  76. }
  77. // Require a reload
  78. context.reloadRequired();
  79. // Verify the model in a later step
  80. context.addStep(VERIFY_HANDLER, OperationContext.Stage.MODEL);
  81. if (context.completeStep() != OperationContext.ResultAction.KEEP) {
  82. context.revertReloadRequired();
  83. }
  84. }
  85. static class ModelValidationStep implements OperationStepHandler {
  86. @Override
  87. public void execute(final OperationContext context, final ModelNode ignored) throws OperationFailedException {
  88. final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
  89. final ModelNode model = resource.getModel();
  90. for(final AttributeDefinition definition : InterfaceDescription.ROOT_ATTRIBUTES) {
  91. final String attributeName = definition.getName();
  92. final boolean has = model.hasDefined(attributeName);
  93. if(! has && isRequired(definition, model)) {
  94. throw new OperationFailedException(new ModelNode().set(MESSAGES.required(attributeName)));
  95. }
  96. if(has) {
  97. // Just ignore 'false'
  98. if(definition.getType() == ModelType.BOOLEAN && ! model.get(attributeName).asBoolean()) {
  99. continue;
  100. }
  101. if(! isAllowed(definition, model)) {
  102. // TODO probably move this into AttributeDefinition
  103. String[] alts = definition.getAlternatives();
  104. StringBuilder sb = null;
  105. if (alts != null) {
  106. for (String alt : alts) {
  107. if (model.hasDefined(alt)) {
  108. if (sb == null) {
  109. sb = new StringBuilder();
  110. } else {
  111. sb.append(", ");
  112. }
  113. sb.append(alt);
  114. }
  115. }
  116. }
  117. throw new OperationFailedException(new ModelNode().set(MESSAGES.invalidAttributeCombo(attributeName, sb)));
  118. }
  119. }
  120. }
  121. context.completeStep();
  122. }
  123. boolean isRequired(final AttributeDefinition def, final ModelNode model) {
  124. final boolean required = ! def.isAllowNull();
  125. return required ? ! hasAlternative(def.getAlternatives(), model, true) : required;
  126. }
  127. boolean isAllowed(final AttributeDefinition def, final ModelNode model) {
  128. final String[] alternatives = def.getAlternatives();
  129. if(alternatives != null) {
  130. for(final String alternative : alternatives) {
  131. if(model.hasDefined(alternative)) {
  132. if(ATTRIBUTES.get(alternative).getType() == ModelType.BOOLEAN) {
  133. return ! model.get(alternative).asBoolean();
  134. }
  135. return false;
  136. }
  137. }
  138. }
  139. return true;
  140. }
  141. boolean hasAlternative(final String[] alternatives, ModelNode operationObject, boolean ignoreBoolean) {
  142. if(alternatives != null) {
  143. for(final String alternative : alternatives) {
  144. if(operationObject.hasDefined(alternative)) {
  145. if(ignoreBoolean) {
  146. if(operationObject.get(alternative).getType() == ModelType.BOOLEAN) {
  147. return operationObject.get(alternative).asBoolean();
  148. }
  149. }
  150. return true;
  151. }
  152. }
  153. }
  154. return false;
  155. }
  156. }
  157. }