modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/ASConnection.java
| 29 --
modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/BaseComponent.java
| 62 +++--
modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/json/Operation.java
| 121 ++++------
modules/plugins/jboss-as-7/src/main/resources/META-INF/rhq-plugin.xml
| 15 +
modules/plugins/jboss-as-7/src/test/java/org/rhq/modules/plugins/jbossas7/OperationJsonTest.java
| 26 ++
5 files changed, 151 insertions(+), 102 deletions(-)
New commits:
commit 63147effa639ef08f8cff763393efbafcd3a8607
Author: Heiko W. Rupp <hwr(a)redhat.com>
Date: Wed Mar 30 12:17:21 2011 +0200
Make executing of Operations more generic and implement add/remove of serverGroups.
diff --git
a/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/ASConnection.java
b/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/ASConnection.java
index 192a54f..401887b 100644
---
a/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/ASConnection.java
+++
b/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/ASConnection.java
@@ -148,6 +148,8 @@ public class ASConnection {
outcome = outcomeNode.getTextValue();
if (outcome.equals("failed")) {
JsonNode reasonNode = in.findValue("failure-description");
+ if (reasonNode==null)
+ reasonNode =
in.findValue("domain-failure-description");// TODO JBAS-9182
String reason = reasonNode.getTextValue();
log.info(reason);
return true;
@@ -163,11 +165,10 @@ public class ASConnection {
/**
* Execute an operation against the domain api
- * @param path Node to manipulate
- * @param operationName operation to run
- * @param attributeValue attribute-name-value pair
+ * @return JsonNode that describes the result
+ * @param operation an Operation that should be run on the domain controller
*/
- public JsonNode execute(String path, String operationName, NameValuePair
attributeValue) {
+ public JsonNode execute(Operation operation) {
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
@@ -177,8 +178,6 @@ public class ASConnection {
ObjectMapper mapper = new ObjectMapper();
- Operation operation = new
Operation(operationName,pathToAddress(path),attributeValue);
-
String result = mapper.writeValueAsString(operation);
System.out.println("Json to send: " + result);
mapper.writeValue(out, operation);
@@ -224,22 +223,6 @@ public class ASConnection {
return null;
}
- private List<PROPERTY_VALUE> pathToAddress(String path) {
- if (path.endsWith("/"))
- path = path.substring(0,path.length()-1);
-
- if (path.startsWith("/"))
- path = path.substring(1);
-
- List<PROPERTY_VALUE> result = new ArrayList<PROPERTY_VALUE>();
- String[] components = path.split("/");
- for (int i = 0; i < components.length ; i+=2) {
- PROPERTY_VALUE valuePair = new
PROPERTY_VALUE(components[i],components[i+1]);
- result.add(valuePair);
- }
-
- return result;
- }
private URL getBaseUrl(String base, String ops) throws MalformedURLException {
String spec;
@@ -268,6 +251,8 @@ public class ASConnection {
if (jsonNode==null)
return "getFailureDescription: -input was null-";
JsonNode node = jsonNode.findValue("failure-description");
+ if (node==null)
+ node = jsonNode.findValue("domain-failure-description"); // TODO
JBAS-9182
return node.getValueAsText();
}
diff --git
a/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/BaseComponent.java
b/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/BaseComponent.java
index 788531b..3b025f3 100644
---
a/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/BaseComponent.java
+++
b/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/BaseComponent.java
@@ -45,9 +45,13 @@ import
org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
import org.rhq.core.pluginapi.inventory.ResourceComponent;
import org.rhq.core.pluginapi.inventory.ResourceContext;
import org.rhq.core.pluginapi.measurement.MeasurementFacet;
+import org.rhq.core.pluginapi.operation.OperationFacet;
+import org.rhq.core.pluginapi.operation.OperationResult;
import org.rhq.modules.plugins.jbossas7.json.NameValuePair;
+import org.rhq.modules.plugins.jbossas7.json.Operation;
import org.rhq.modules.plugins.jbossas7.json.PROPERTY_VALUE;
+import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -149,22 +153,7 @@ public class BaseComponent implements ResourceComponent,
MeasurementFacet, Confi
public Configuration loadResourceConfiguration() throws Exception {
ConfigurationDefinition configDef =
context.getResourceType().getResourceConfigurationDefinition();
- ResourceComponent parentResourceComponent =
context.getParentResourceComponent();
- String parentPath =null;
- String myPath;
- if (parentResourceComponent instanceof BaseComponent) {
- BaseComponent parentComponent = (BaseComponent) parentResourceComponent;
- parentPath = parentComponent.getPath();
- }
-
- if (parentPath!=null) {
- if (parentPath.endsWith("/") || path.startsWith("/"))
- myPath = parentPath + path;
- else
- myPath = parentPath + "/" + path;
- }
- else
- myPath = path;
+ String myPath = getResultingPath();
JsonNode json = connection.getLevelData(myPath,true,false);
@@ -302,6 +291,26 @@ public class BaseComponent implements ResourceComponent,
MeasurementFacet, Confi
return ret;
}
+ private String getResultingPath() {
+ ResourceComponent parentResourceComponent =
context.getParentResourceComponent();
+ String parentPath =null;
+ String myPath;
+ if (parentResourceComponent instanceof BaseComponent) {
+ BaseComponent parentComponent = (BaseComponent) parentResourceComponent;
+ parentPath = parentComponent.getPath();
+ }
+
+ if (parentPath!=null) {
+ if (parentPath.endsWith("/") || path.startsWith("/"))
+ myPath = parentPath + path;
+ else
+ myPath = parentPath + "/" + path;
+ }
+ else
+ myPath = path;
+ return myPath;
+ }
+
public void updateResourceConfiguration(ConfigurationUpdateReport report) {
@@ -309,7 +318,8 @@ public class BaseComponent implements ResourceComponent,
MeasurementFacet, Confi
for (Map.Entry<String, PropertySimple> entry :
conf.getSimpleProperties().entrySet()) {
NameValuePair nvp = new
NameValuePair(entry.getKey(),entry.getValue().getStringValue());
- JsonNode result= connection.execute(key,"write-attribute",nvp); //
TODO path / key handling
+ Operation writeAttribute = new
Operation("write-attribute",pathToAddress(getResultingPath()),nvp);
+ JsonNode result= connection.execute(writeAttribute);
if(connection.isErrorReply(result)) {
report.setStatus(ConfigurationUpdateStatus.FAILURE);
report.setErrorMessage(connection.getFailureDescription(result));
@@ -317,4 +327,22 @@ public class BaseComponent implements ResourceComponent,
MeasurementFacet, Confi
}
}
+
+ private List<PROPERTY_VALUE> pathToAddress(String path) {
+ if (path.endsWith("/"))
+ path = path.substring(0,path.length()-1);
+
+ if (path.startsWith("/"))
+ path = path.substring(1);
+
+ List<PROPERTY_VALUE> result = new ArrayList<PROPERTY_VALUE>();
+ String[] components = path.split("/");
+ for (int i = 0; i < components.length ; i+=2) {
+ PROPERTY_VALUE valuePair = new
PROPERTY_VALUE(components[i],components[i+1]);
+ result.add(valuePair);
+ }
+
+ return result;
+ }
+
}
diff --git
a/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/json/Operation.java
b/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/json/Operation.java
index 642b41f..898f176 100644
---
a/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/json/Operation.java
+++
b/modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/json/Operation.java
@@ -20,10 +20,14 @@ package org.rhq.modules.plugins.jbossas7.json;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import org.codehaus.jackson.annotate.JsonAnyGetter;
+import org.codehaus.jackson.annotate.JsonAnySetter;
+import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
-import org.codehaus.jackson.annotate.JsonValue;
/**
* Operation to run on the server
@@ -31,23 +35,71 @@ import org.codehaus.jackson.annotate.JsonValue;
*/
public class Operation {
+ private String operation;
+ @JsonProperty
+ private List<PROPERTY_VALUE> address = Collections.emptyList();
+ private Map<String,String> additionalProperties;
+
+
public Operation(String operation, List<PROPERTY_VALUE> address, NameValuePair
payload) {
this.operation = operation;
this.address = address;
- this.name = payload.name;
- this.value = payload.value;
+ additionalProperties = new HashMap<String,String>(2);
+ additionalProperties.put("name",payload.name);
+ additionalProperties.put("value",payload.value);
+
+ }
+
+ public Operation(String operation, List<PROPERTY_VALUE> address,
Map<String,String> payload) {
+ this.operation = operation;
+ this.address = address;
+ this.additionalProperties = payload;
+ }
+
+ public Operation(String operation, List<PROPERTY_VALUE> address, String key,
String value) {
+ this.operation = operation;
+ this.address = address;
+ additionalProperties = new HashMap<String,String>(1);
+ additionalProperties.put(key,value);
+
}
public Operation() {
// needed for Jackson
}
- private String operation;
- @JsonProperty
- private List<PROPERTY_VALUE> address = Collections.emptyList();
+ @JsonAnySetter
+ public void addAdditionalProperty(String key, String value) {
+ if (additionalProperties == null)
+ additionalProperties = new HashMap<String, String>();
+ additionalProperties.put(key,value);
+ }
- private String name;
- private String value;
+ public void setAdditionalProperties(Map<String, String> additionalProperties)
{
+ this.additionalProperties = additionalProperties;
+ }
+
+ @JsonAnyGetter
+ public Map<String,String> getAdditionalProperties() {
+ return this.additionalProperties;
+ }
+
+ @JsonIgnore
+ public String getName() {
+ return getProperty("name");
+ }
+
+ @JsonIgnore
+ public String getValue() {
+ return getProperty("value");
+ }
+
+ private String getProperty(String key) {
+ if (additionalProperties.containsKey(key))
+ return additionalProperties.get(key);
+ else
+ return null;
+ }
public String getOperation() {
return operation;
@@ -72,58 +124,5 @@ public class Operation {
return address;
}
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getValue() {
- return value;
- }
-
- public void setValue(String value) {
- this.value = value;
- }
-
- /*
- [localhost:9999 /subsystem=web/connector=http]
:write-attribute(name=socket-binding,value=jndi)
- yield JSON to send:
- {
- "operation" : "write-attribute",
- "name" : "socket-binding",
- "value" : "jndi",
- "address" : [
- {
- "PROPERTY_VALUE" : {
- "subsystem" : "web"
- }
- },
- {
- "PROPERTY_VALUE" : {
- "connector" : "http"
- }
- }
- ]
- }
- */
-
-// @JsonValue
-// public String toString() {
-// StringBuilder b = new StringBuilder();
-// b.append('{');
-//
b.append("\"operation\":\"").append(operation).append("\",");
-// b.append("\"address\":");
-// if (address!=null && !address.isEmpty())
-// b.append(address);
-// else
-// b.append("[]");
-// b.append(',');
-// b.append(payload);
-// b.append("}");
-// return b.toString();
-// }
}
diff --git a/modules/plugins/jboss-as-7/src/main/resources/META-INF/rhq-plugin.xml
b/modules/plugins/jboss-as-7/src/main/resources/META-INF/rhq-plugin.xml
index ee6b21f..6af7de6 100644
--- a/modules/plugins/jboss-as-7/src/main/resources/META-INF/rhq-plugin.xml
+++ b/modules/plugins/jboss-as-7/src/main/resources/META-INF/rhq-plugin.xml
@@ -105,9 +105,21 @@
<server name="Domain"
discovery="DomainDiscovery"
- class="BaseComponent"
+ class="DomainComponent"
description="An AS7 management profile">
+ <operation name="server-group:add" displayName="Add
ServerGroup" description="Add a server group to the Domain.">
+ <parameters>
+ <c:simple-property name="name" description="Name of
Group to add"/>
+ <c:simple-property name="profile" description="Profile
to add this group to"/>
+ </parameters>
+ </operation>
+ <operation name="server-group:remove" displayName="Remove
ServerGroup" description="Remove a server group from the Domain.">
+ <parameters>
+ <c:simple-property name="name" description="Name of
Group to add"/>
+ </parameters>
+ </operation>
+
<server name="Profile"
description="One profile in a domain"
@@ -125,7 +137,6 @@
<plugin-configuration>
<c:simple-property name="path"
default="/server-group/*" readOnly="true"/>
</plugin-configuration>
-
<resource-configuration>
<c:simple-property name="profile" />
<c:simple-property name="socket-binding-group"/>
diff --git
a/modules/plugins/jboss-as-7/src/test/java/org/rhq/modules/plugins/jbossas7/OperationJsonTest.java
b/modules/plugins/jboss-as-7/src/test/java/org/rhq/modules/plugins/jbossas7/OperationJsonTest.java
index 4909595..929a87c 100644
---
a/modules/plugins/jboss-as-7/src/test/java/org/rhq/modules/plugins/jbossas7/OperationJsonTest.java
+++
b/modules/plugins/jboss-as-7/src/test/java/org/rhq/modules/plugins/jbossas7/OperationJsonTest.java
@@ -19,7 +19,9 @@
package org.rhq.modules.plugins.jbossas7;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.testng.annotations.Test;
@@ -70,4 +72,28 @@ public class OperationJsonTest {
assert pv.getKey().equals("myKey") : "Key is " +
pv.getKey();
assert pv.getValue().equals("myValue"): "Value is " +
pv.getValue();
}
+
+ public void anyPayloadTest() throws Exception {
+
+ List<PROPERTY_VALUE> address=new ArrayList<PROPERTY_VALUE>(2);
+ PROPERTY_VALUE part = new
PROPERTY_VALUE("/server-group","newOne");
+ address.add(part);
+
+ Map<String,String> props = new HashMap<String, String>();
+ props.put("profile","default");
+
+ Operation operation = new Operation("add",address,props);
+
+ ObjectMapper mapper = new ObjectMapper();
+
+ String result = mapper.writeValueAsString(operation);
+ System.out.println(result);
+
+ assert !result.contains("name") : "Result contains a name property
but should not : " + result;
+ assert !result.contains("null") : "Result contains null values but
should not : " + result;
+
+ Operation op = mapper.readValue(result,Operation.class);
+ assert op.getOperation().equals(operation.getOperation()) : "Operation is
" + op.getOperation();
+
+ }
}