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