PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/modcluster/src/main/java/org/jboss/as/modcluster/ModClusterAddMetric.java

#
Java | 114 lines | 75 code | 14 blank | 25 comment | 12 complexity | 7cfa51c7e154b11b4679f0489da5f8dd MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, 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.modcluster;
  23. import static org.jboss.as.modcluster.ModClusterMessages.MESSAGES;
  24. import java.util.Collections;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import org.jboss.as.controller.OperationContext;
  29. import org.jboss.as.controller.OperationFailedException;
  30. import org.jboss.as.controller.OperationStepHandler;
  31. import org.jboss.as.controller.PathAddress;
  32. import org.jboss.as.controller.descriptions.DescriptionProvider;
  33. import org.jboss.dmr.ModelNode;
  34. import org.jboss.dmr.Property;
  35. // implements ModelQueryOperationHandler, DescriptionProvider
  36. public class ModClusterAddMetric implements OperationStepHandler, DescriptionProvider {
  37. static final ModClusterAddMetric INSTANCE = new ModClusterAddMetric();
  38. @Override
  39. public ModelNode getModelDescription(Locale locale) {
  40. return ModClusterSubsystemDescriptions.getAddMetricDescription(locale);
  41. }
  42. @Override
  43. public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
  44. // Look for the dynamic-load-provider
  45. final ModelNode dynamicLoadProvider = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS).getModel()
  46. .get(CommonAttributes.DYNAMIC_LOAD_PROVIDER);
  47. // Add the metric to the dynamic-load-provider.
  48. final ModelNode metric = new ModelNode();
  49. List<Property> list = operation.asPropertyList();
  50. Iterator<Property> it = list.iterator();
  51. while (it.hasNext()) {
  52. Property prop = it.next();
  53. if (prop.getName().equals("property")) {
  54. String properties = prop.getValue().asString();
  55. ModelNode props = ModelNode.fromString(properties);
  56. metric.get("property").set(props);
  57. } else if (prop.getName().equals("type")) {
  58. metric.get(prop.getName()).set(prop.getValue().asString());
  59. }
  60. }
  61. if (!metric.get("type").isDefined()) {
  62. throw new OperationFailedException(new ModelNode().set(MESSAGES.typeAttributeRequired("add-metric")));
  63. }
  64. if (!dynamicLoadProvider.isDefined()) {
  65. // Create a default one.
  66. dynamicLoadProvider.get(CommonAttributes.HISTORY).set(9);
  67. dynamicLoadProvider.get(CommonAttributes.DECAY).set(2);
  68. }
  69. replaceMetric(dynamicLoadProvider, metric);
  70. if (context.isNormalServer()) {
  71. context.reloadRequired();
  72. }
  73. context.completeStep(new OperationContext.RollbackHandler() {
  74. @Override
  75. public void handleRollback(OperationContext context, ModelNode operation) {
  76. if (context.isNormalServer()) {
  77. context.revertReloadRequired();
  78. }
  79. }
  80. });
  81. }
  82. private void replaceMetric(ModelNode dynamicLoadProvider, ModelNode metric) {
  83. List<ModelNode> newlist = Collections.<ModelNode> emptyList();
  84. if (dynamicLoadProvider.get(CommonAttributes.LOAD_METRIC).isDefined()) {
  85. List<ModelNode> list = dynamicLoadProvider.get(CommonAttributes.LOAD_METRIC).asList();
  86. String type = metric.get("type").asString();
  87. Iterator<ModelNode> it = list.iterator();
  88. dynamicLoadProvider.get(CommonAttributes.LOAD_METRIC).set(newlist);
  89. while (it.hasNext()) {
  90. ModelNode node = it.next();
  91. if (!node.get("type").asString().equals(type)) {
  92. dynamicLoadProvider.get(CommonAttributes.LOAD_METRIC).add(node);
  93. }
  94. }
  95. } else {
  96. dynamicLoadProvider.get(CommonAttributes.LOAD_METRIC).set(newlist);
  97. }
  98. dynamicLoadProvider.get(CommonAttributes.LOAD_METRIC).add(metric);
  99. }
  100. }