modules/enterprise/gui/portal-war/src/main/java/org/rhq/enterprise/gui/alert/CustomContentUIBean.java | 9
modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java | 156 +++++++-
modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java | 81 ++++
modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/Token.java | 72 ++++
modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/TokenClass.java | 59 +++
modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/TokenReplacer.java | 178 ++++++++++
modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml | 47 +-
modules/enterprise/server/plugins/alert-operations/src/test/java/org/rhq/enterprise/server/plugins/alertOperations/TokenReplacementTest.java | 135 +++++++
8 files changed, 688 insertions(+), 49 deletions(-)
New commits:
commit 354dd6076c8e6c700086c6472963924f35b8deb1
Author: Heiko W. Rupp <hwr(a)redhat.com>
Date: Tue Feb 23 15:28:34 2010 +0100
Cleanup of variable names and obtain the right operation name.
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java
index deabeae..2a3b635 100644
--- a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java
@@ -28,7 +28,6 @@ import org.apache.commons.logging.LogFactory;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Scope;
-import org.jboss.seam.annotations.web.RequestParameter;
import org.rhq.core.domain.auth.Subject;
import org.rhq.core.domain.configuration.Configuration;
@@ -54,8 +53,8 @@ public class OperationsBackingBean extends CustomAlertSenderBackingBean {
private String resMode;
private String tokenMode;
Integer resId;
- private Integer operationName;
- private Map<String, Integer> operationNames = new HashMap<String, Integer>();
+ private Integer operationId;
+ private Map<String, Integer> operationIds = new HashMap<String, Integer>();
private String resourceName;
@@ -79,12 +78,12 @@ public class OperationsBackingBean extends CustomAlertSenderBackingBean {
if (resId != null) {
persistProperty(alertParameters, OperationsSender.RESOURCE_ID,resId);
- cleanProperty(alertParameters,OperationsSender.OPERATION_NAME);
+ cleanProperty(alertParameters,OperationsSender.OPERATION_ID);
cleanProperty(alertParameters,OperationsSender.USABLE);
}
- obtainOperationNames();
+ obtainOperationIds();
return ALERT_NOTIFICATIONS;
}
@@ -92,10 +91,10 @@ public class OperationsBackingBean extends CustomAlertSenderBackingBean {
public String selectOperation() {
- log.info("In selectOperation, resId is " + resId + " opName is " + operationName);
+ log.info("In selectOperation, resId is " + resId + " opName is " + operationId);
- if (operationName != null ) {
- persistProperty(alertParameters, OperationsSender.OPERATION_NAME,operationName);
+ if (operationId != null ) {
+ persistProperty(alertParameters, OperationsSender.OPERATION_ID, operationId);
lookupConfiguration();
}
@@ -112,10 +111,10 @@ public class OperationsBackingBean extends CustomAlertSenderBackingBean {
// int operationId = Integer.valueOf(FacesContextUtility.getRequiredRequestParameter("opId"));
OperationManagerLocal opMan = LookupUtil.getOperationManager();
- obtainOperationNames();
+ obtainOperationIds();
- OperationDefinition operationDefinition = opMan.getOperationDefinition(subject, operationName);
+ OperationDefinition operationDefinition = opMan.getOperationDefinition(subject, operationId);
configurationDefinition = operationDefinition.getParametersConfigurationDefinition();
@@ -140,7 +139,7 @@ log.info("gConfig: " + configuration + ", " + configuration.hashCode() + ", " +
return ALERT_NOTIFICATIONS;
}
- private void obtainOperationNames() {
+ private void obtainOperationIds() {
PropertySimple prop = alertParameters.getSimple(OperationsSender.RESOURCE_ID);
if (prop!=null)
@@ -152,7 +151,7 @@ log.info("gConfig: " + configuration + ", " + configuration.hashCode() + ", " +
Subject subject = LookupUtil.getSubjectManager().getOverlord(); // TODO replace with real subject
List<OperationDefinition> opDefs = opMan.findSupportedResourceOperations(subject, resId, false);
for (OperationDefinition def : opDefs) {
- operationNames.put(def.getDisplayName(),def.getId()); // TODO add more distinctive stuff in display
+ operationIds.put(def.getDisplayName(),def.getId()); // TODO add more distinctive stuff in display
}
}
}
@@ -205,30 +204,30 @@ log.info("gConfig: " + configuration + ", " + configuration.hashCode() + ", " +
this.resourceName = resourceName;
}
- public Integer getOperationName() {
+ public Integer getOperationId() {
- if (operationName==null) {
- PropertySimple prop = alertParameters.getSimple(OperationsSender.OPERATION_NAME);
+ if (operationId ==null) {
+ PropertySimple prop = alertParameters.getSimple(OperationsSender.OPERATION_ID);
if (prop!=null)
- operationName = prop.getIntegerValue();
+ operationId = prop.getIntegerValue();
}
- return operationName;
+ return operationId;
}
- public void setOperationName(Integer operationName) {
- this.operationName = operationName;
+ public void setOperationId(Integer operationId) {
+ this.operationId = operationId;
}
- public Map<String, Integer> getOperationNames() {
+ public Map<String, Integer> getOperationIds() {
- obtainOperationNames();
+ obtainOperationIds();
- return operationNames;
+ return operationIds;
}
- public void setOperationNames(Map<String, Integer> operationNames) {
- this.operationNames = operationNames;
+ public void setOperationIds(Map<String, Integer> operationIds) {
+ this.operationIds = operationIds;
}
public ConfigurationDefinition getConfigurationDefinition() {
@@ -239,15 +238,11 @@ log.info("gConfig: " + configuration + ", " + configuration.hashCode() + ", " +
}
public void setConfigurationDefinition(ConfigurationDefinition configurationDefinition) {
- log.info("set CD: " + configurationDefinition);
this.configurationDefinition = configurationDefinition;
}
public Configuration getConfiguration() {
-
return configuration;
-
-
}
public void setConfiguration(Configuration configuration) {
@@ -255,7 +250,7 @@ log.info("gConfig: " + configuration + ", " + configuration.hashCode() + ", " +
log.info("setC: " + configuration);
}
- public String getNullConfigurationDefinitionMessage() {
+ public String getNullConfigurationDefinitionMessage() {
return "This operation does not take any parameters.";
}
@@ -280,5 +275,4 @@ log.info("gConfig: " + configuration + ", " + configuration.hashCode() + ", " +
persistProperty(alertParameters, OperationsSender.TOKEN_MODE,tokenMode);
}
-
}
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java
index 4359ad4..4f6c88e 100644
--- a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java
@@ -18,9 +18,8 @@
*/
package org.rhq.enterprise.server.plugins.alertOperations;
+import java.util.List;
import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -34,7 +33,7 @@ import org.rhq.core.domain.alert.notification.SenderResult;
import org.rhq.core.domain.auth.Subject;
import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.PropertySimple;
-import org.rhq.core.domain.resource.Resource;
+import org.rhq.core.domain.operation.OperationDefinition;
import org.rhq.enterprise.server.configuration.ConfigurationManagerLocal;
import org.rhq.enterprise.server.exception.ScheduleException;
import org.rhq.enterprise.server.operation.OperationManagerLocal;
@@ -51,7 +50,7 @@ public class OperationsSender extends AlertSender {
private final Log log = LogFactory.getLog(OperationsSender.class);
static final String RESOURCE_ID = "resourceId";
- static final String OPERATION_NAME = "operationName";
+ static final String OPERATION_ID = "operationName";
static final String USABLE = "usable";
protected static final String TOKEN_MODE = "tokenMode";
private static final String LITERAL = "literal";
@@ -62,8 +61,8 @@ public class OperationsSender extends AlertSender {
public SenderResult send(Alert alert) {
PropertySimple resProp = alertParameters.getSimple(RESOURCE_ID);
- PropertySimple opNameProp = alertParameters.getSimple(OPERATION_NAME);
- if (resProp==null || resProp.getIntegerValue() == null || opNameProp == null || opNameProp.getStringValue() == null)
+ PropertySimple opIdProp = alertParameters.getSimple(OPERATION_ID);
+ if (resProp==null || resProp.getIntegerValue() == null || opIdProp == null || opIdProp.getStringValue() == null)
return new SenderResult(ResultState.FAILURE, "Not enough parameters given");
PropertySimple usableProp = alertParameters.getSimple(USABLE);
@@ -71,11 +70,26 @@ public class OperationsSender extends AlertSender {
return new SenderResult(ResultState.FAILURE,"Not yet configured");
Integer resourceId = resProp.getIntegerValue();
- String opName = opNameProp.getStringValue();
+ Integer opId = opIdProp.getIntegerValue();
+
+ String opName = null;
OperationManagerLocal opMgr = LookupUtil.getOperationManager();
Subject subject = LookupUtil.getSubjectManager().getOverlord(); // TODO get real subject
+ List<OperationDefinition> opdefs = opMgr.findSupportedResourceOperations(subject,resourceId,false);
+ for (OperationDefinition opdef : opdefs ) {
+ if (opdef.getId() == opId) {
+ opName = opdef.getName();
+ break;
+ }
+ }
+
+ if (opName==null) {
+ return new SenderResult(ResultState.FAILURE, "No operation found ");
+ }
+
+
PropertySimple parameterConfigProp = alertParameters.getSimple(PARAMETERS_CONFIG);
Configuration parameters = null ;
if (parameterConfigProp!=null) {
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml b/modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml
index 4a6c0e6..98dcada 100644
--- a/modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml
@@ -36,13 +36,13 @@
</h:panelGroup>
<h:panelGroup style="width:60%; vertical-align:top;" >
- <rich:panel rendered="#{not empty operationsBean.operationNames}">
+ <rich:panel rendered="#{not empty operationsBean.operationIds}">
Operations for: <h:outputText value=" #{operationsBean.resourceName}"/>
<br/>
- <h:selectOneMenu id="selectOperationMenu" value="#{operationsBean.operationName}">
- <f:selectItems value="#{operationsBean.operationNames}"/>
+ <h:selectOneMenu id="selectOperationMenu" value="#{operationsBean.operationId}">
+ <f:selectItems value="#{operationsBean.operationIds}"/>
</h:selectOneMenu>
<p/>
<h:commandButton id="opNameSubmit"
@@ -57,9 +57,9 @@
<p/>
<h:panelGroup style="width:100%">
<h:messages />
- <rich:panel rendered="#{not empty operationsBean.operationName}">
+ <rich:panel rendered="#{not empty operationsBean.operationId}">
<br/>
- Parameters for <h:outputText value=" #{operationsBean.operationName}"/>
+ Parameters for <h:outputText value=" #{operationsBean.operationId}"/>
<p/>
<h:selectOneMenu id="tokenModeMenu" value="#{operationsBean.tokenMode}">
<f:selectItem itemValue="literal" itemLabel="Literal"/>
commit 7bdec1c039fbda0b202c078fe0c7be1507d95612
Author: Heiko W. Rupp <hwr(a)redhat.com>
Date: Tue Feb 23 10:57:34 2010 +0100
Display, persist and use operation parameters.
diff --git a/modules/enterprise/gui/portal-war/src/main/java/org/rhq/enterprise/gui/alert/CustomContentUIBean.java b/modules/enterprise/gui/portal-war/src/main/java/org/rhq/enterprise/gui/alert/CustomContentUIBean.java
index 8b7c8d8..683057b 100644
--- a/modules/enterprise/gui/portal-war/src/main/java/org/rhq/enterprise/gui/alert/CustomContentUIBean.java
+++ b/modules/enterprise/gui/portal-war/src/main/java/org/rhq/enterprise/gui/alert/CustomContentUIBean.java
@@ -73,7 +73,12 @@ public class CustomContentUIBean {
* name of bean, but this class is not an "official" seam component.
*/
private void outjectBean(String name, CustomAlertSenderBackingBean bean) {
- Context pageContext = Contexts.getPageContext();
- pageContext.set(name, bean);
+
+ Context context = Contexts.getSessionContext();
+
+ CustomAlertSenderBackingBean csb = (CustomAlertSenderBackingBean) context.get(name);
+ if (csb ==null)
+ context.set(name,bean);
+
}
}
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java
index 32e9f16..deabeae 100644
--- a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsBackingBean.java
@@ -25,38 +25,64 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.web.RequestParameter;
+
import org.rhq.core.domain.auth.Subject;
import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.PropertySimple;
import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
import org.rhq.core.domain.operation.OperationDefinition;
+import org.rhq.core.domain.resource.Resource;
+import org.rhq.enterprise.server.configuration.ConfigurationManagerLocal;
import org.rhq.enterprise.server.operation.OperationManagerLocal;
import org.rhq.enterprise.server.plugin.pc.alert.CustomAlertSenderBackingBean;
+import org.rhq.enterprise.server.resource.ResourceManagerLocal;
import org.rhq.enterprise.server.util.LookupUtil;
/**
* Backing bean for the operations alert sender
* @author Heiko W. Rupp
*/
-
+(a)Scope(ScopeType.PAGE)
public class OperationsBackingBean extends CustomAlertSenderBackingBean {
private final Log log = LogFactory.getLog(OperationsBackingBean.class);
private String resMode;
+ private String tokenMode;
Integer resId;
- private String operationName;
- private Map<String,String> operationNames = new HashMap<String,String>();
+ private Integer operationName;
+ private Map<String, Integer> operationNames = new HashMap<String, Integer>();
+ private String resourceName;
+
+
private ConfigurationDefinition configurationDefinition;
private Configuration configuration;
private static final String ALERT_NOTIFICATIONS = "ALERT_NOTIFICATIONS";
+ public OperationsBackingBean() {
+ log.info("new " + hashCode());
+ }
+
+ @Create
+ public void init() {
+ log.info("init");
+ }
+
+
public String selectResource() {
log.info("In select Resource, resId is " + resId + " resMode is " + resMode);
- if (resId != null)
+ if (resId != null) {
persistProperty(alertParameters, OperationsSender.RESOURCE_ID,resId);
+ cleanProperty(alertParameters,OperationsSender.OPERATION_NAME);
+ cleanProperty(alertParameters,OperationsSender.USABLE);
+
+ }
obtainOperationNames();
@@ -68,15 +94,47 @@ public class OperationsBackingBean extends CustomAlertSenderBackingBean {
public String selectOperation() {
log.info("In selectOperation, resId is " + resId + " opName is " + operationName);
- if (operationName != null )
+ if (operationName != null ) {
persistProperty(alertParameters, OperationsSender.OPERATION_NAME,operationName);
+ lookupConfiguration();
+ }
return ALERT_NOTIFICATIONS;
}
+ private void lookupConfiguration() {
+
+
+// log.info("getCD: " + configurationDefinition);
+ try {
+// Subject subject = EnterpriseFacesContextUtility.getSubject();
+ Subject subject = LookupUtil.getSubjectManager().getOverlord(); // TODO replace with real subject
+
+// int operationId = Integer.valueOf(FacesContextUtility.getRequiredRequestParameter("opId"));
+ OperationManagerLocal opMan = LookupUtil.getOperationManager();
+ obtainOperationNames();
+
+
+ OperationDefinition operationDefinition = opMan.getOperationDefinition(subject, operationName);
+ configurationDefinition = operationDefinition.getParametersConfigurationDefinition();
+
+
+ // call a SLSB method to get around lazy initialization of configDefs and configTemplates
+ ConfigurationManagerLocal configurationManager = LookupUtil.getConfigurationManager();
+ configuration = configurationManager.getConfigurationFromDefaultTemplate(configurationDefinition);
+// Configuration newConfiguration = configuration.deepCopy(false);
+log.info("gConfig: " + configuration + ", " + configuration.hashCode() + ", " + configuration.getSimpleValue("detailedDiscovery","-unset-"));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
public String useConfiguration() {
- log.info("In useConfiguration");
+ log.info("In useConfiguration, Configuration is " + configuration );
+ // COnfiguration should be valid here ...
+ super.persistConfiguration(configuration);
+ persistProperty(alertParameters,OperationsSender.PARAMETERS_CONFIG,configuration.getId());
persistProperty(alertParameters, OperationsSender.USABLE,true);
return ALERT_NOTIFICATIONS;
@@ -94,7 +152,7 @@ public class OperationsBackingBean extends CustomAlertSenderBackingBean {
Subject subject = LookupUtil.getSubjectManager().getOverlord(); // TODO replace with real subject
List<OperationDefinition> opDefs = opMan.findSupportedResourceOperations(subject, resId, false);
for (OperationDefinition def : opDefs) {
- operationNames.put(def.getDisplayName(),def.getName()); // TODO add more distinctive stuff in display
+ operationNames.put(def.getDisplayName(),def.getId()); // TODO add more distinctive stuff in display
}
}
}
@@ -107,6 +165,7 @@ public class OperationsBackingBean extends CustomAlertSenderBackingBean {
public void setResMode(String resMode) {
this.resMode = resMode;
+ log.info("setResMode: " + resMode);
}
public Integer getResId() {
@@ -122,47 +181,104 @@ public class OperationsBackingBean extends CustomAlertSenderBackingBean {
public void setResId(Integer resId) {
this.resId = resId;
+ log.info("Set resid " + resId);
+ if (resId!=null) {
+ persistProperty(alertParameters,OperationsSender.RESOURCE_ID,resId);
+ }
}
- public String getOperationName() {
+ public String getResourceName() {
+ if (resId==null)
+ getResId();
+
+ if (resId!=null) {
+ ResourceManagerLocal resMgr = LookupUtil.getResourceManager();
+ Subject subject = LookupUtil.getSubjectManager().getOverlord(); // TODO replace with real subject
+ Resource res = resMgr.getResource(subject,resId);
+
+ resourceName = res.getName() + " (" + res.getResourceType().getName() + ")";
+ }
+ return resourceName;
+ }
+
+ public void setResourceName(String resourceName) {
+ this.resourceName = resourceName;
+ }
+
+ public Integer getOperationName() {
if (operationName==null) {
PropertySimple prop = alertParameters.getSimple(OperationsSender.OPERATION_NAME);
if (prop!=null)
- operationName = prop.getStringValue();
+ operationName = prop.getIntegerValue();
}
return operationName;
}
- public void setOperationName(String operationName) {
+ public void setOperationName(Integer operationName) {
this.operationName = operationName;
}
- public Map<String,String> getOperationNames() {
+ public Map<String, Integer> getOperationNames() {
obtainOperationNames();
return operationNames;
}
- public void setOperationNames(Map<String,String> operationNames) {
+ public void setOperationNames(Map<String, Integer> operationNames) {
this.operationNames = operationNames;
}
public ConfigurationDefinition getConfigurationDefinition() {
return configurationDefinition;
+
+
+
}
public void setConfigurationDefinition(ConfigurationDefinition configurationDefinition) {
+ log.info("set CD: " + configurationDefinition);
this.configurationDefinition = configurationDefinition;
}
public Configuration getConfiguration() {
+
return configuration;
+
+
}
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
+ log.info("setC: " + configuration);
}
+
+ public String getNullConfigurationDefinitionMessage() {
+ return "This operation does not take any parameters.";
+ }
+
+ public String getNullConfigurationMessage() {
+ return "This operation parameters definition has not been initialized.";
+ }
+
+ public String getTokenMode() {
+ if (tokenMode==null) {
+ PropertySimple prop = alertParameters.getSimple(OperationsSender.TOKEN_MODE);
+ if (prop!=null)
+ tokenMode = prop.getStringValue();
+ }
+
+ return tokenMode;
+ }
+
+ public void setTokenMode(String tokenMode) {
+ this.tokenMode = tokenMode;
+ log.info("token mode" + tokenMode);
+
+ persistProperty(alertParameters, OperationsSender.TOKEN_MODE,tokenMode);
+ }
+
+
}
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java
index 4fa9bba..4359ad4 100644
--- a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/OperationsSender.java
@@ -18,14 +18,24 @@
*/
package org.rhq.enterprise.server.plugins.alertOperations;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Scope;
+
import org.rhq.core.domain.alert.Alert;
import org.rhq.core.domain.alert.notification.ResultState;
import org.rhq.core.domain.alert.notification.SenderResult;
import org.rhq.core.domain.auth.Subject;
+import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.PropertySimple;
+import org.rhq.core.domain.resource.Resource;
+import org.rhq.enterprise.server.configuration.ConfigurationManagerLocal;
import org.rhq.enterprise.server.exception.ScheduleException;
import org.rhq.enterprise.server.operation.OperationManagerLocal;
import org.rhq.enterprise.server.operation.ResourceOperationSchedule;
@@ -36,12 +46,17 @@ import org.rhq.enterprise.server.util.LookupUtil;
* Alert sender that triggers an operation on the resource
* @author Heiko W. Rupp
*/
+@Scope(value = ScopeType.PAGE)
public class OperationsSender extends AlertSender {
private final Log log = LogFactory.getLog(OperationsSender.class);
static final String RESOURCE_ID = "resourceId";
static final String OPERATION_NAME = "operationName";
static final String USABLE = "usable";
+ protected static final String TOKEN_MODE = "tokenMode";
+ private static final String LITERAL = "literal";
+ private static final String INTERPRETED = "interpreted";
+ public static final String PARAMETERS_CONFIG = "parametersConfig";
@Override
public SenderResult send(Alert alert) {
@@ -52,7 +67,7 @@ public class OperationsSender extends AlertSender {
return new SenderResult(ResultState.FAILURE, "Not enough parameters given");
PropertySimple usableProp = alertParameters.getSimple(USABLE);
- if (usableProp==null || usableProp.getBooleanValue()== null || usableProp.getBooleanValue() == false)
+ if (usableProp==null || usableProp.getBooleanValue()== null || !usableProp.getBooleanValue())
return new SenderResult(ResultState.FAILURE,"Not yet configured");
Integer resourceId = resProp.getIntegerValue();
@@ -61,17 +76,53 @@ public class OperationsSender extends AlertSender {
OperationManagerLocal opMgr = LookupUtil.getOperationManager();
Subject subject = LookupUtil.getSubjectManager().getOverlord(); // TODO get real subject
+ PropertySimple parameterConfigProp = alertParameters.getSimple(PARAMETERS_CONFIG);
+ Configuration parameters = null ;
+ if (parameterConfigProp!=null) {
+ Integer paramId = parameterConfigProp.getIntegerValue();
+ if (paramId!=null) {
+ ConfigurationManagerLocal cmgr = LookupUtil.getConfigurationManager();
+ parameters = cmgr.getConfiguration(subject,paramId);
+ }
+ }
+
+
+ String tokenMode = alertParameters.getSimpleValue(TOKEN_MODE, LITERAL);
+
+ /*
+ * If we have parameters and the user wants tokens to be interpreted, then loop
+ * over the parameters and do token replacement.
+ */
+ if (parameters!=null && tokenMode.equals(INTERPRETED)) {
+ Map<String,PropertySimple> propsMap = parameters.getSimpleProperties();
+ if (!propsMap.isEmpty()) {
+ TokenReplacer tr = new TokenReplacer(alert);
+ for (PropertySimple prop : propsMap.values()) {
+ String tmp = prop.getStringValue();
+ tmp = tr.replaceTokens(tmp);
+ prop.setStringValue(tmp);
+ }
+ }
+ }
+
+
+
+ /*
+ * Now fire off the operation with no delay and no repetition.
+ */
ResourceOperationSchedule sched;
try {
- sched = opMgr.scheduleResourceOperation(subject, resourceId, opName, 0, 0, 0, 0, null,
+ sched = opMgr.scheduleResourceOperation(subject, resourceId, opName, 0, 0, 0, 0, parameters,
"Alert operation for " + alert.getAlertDefinition().getName());
} catch (ScheduleException e) {
return new SenderResult(ResultState.FAILURE, "Scheduling of operation " + opName + " on resource " + resourceId + " failed: " + e.getMessage());
}
- // TODO evaluate retuen of schedule() call and defer, so we can leter check
- // If op sending was successfull
+ // If op sending was successful
return new SenderResult(ResultState.SUCCESS, "Scheduled operation " + opName + " on resource " + resourceId + " with jobId " + sched.getJobId() );
}
+
+
+
}
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml b/modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml
index 1fd74ad..4a6c0e6 100644
--- a/modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/resources/operations.xhtml
@@ -29,12 +29,18 @@
<h:commandButton id="resourceIdSubmit"
value="Select"
type="submit"
- action="#{operationsBean.selectResource}" />
+ action="#{operationsBean.selectResource}"
+ styleClass="buttonmed"
+ />
</rich:panel>
</h:panelGroup>
- <h:panelGroup style="width:60%">
+ <h:panelGroup style="width:60%; vertical-align:top;" >
<rich:panel rendered="#{not empty operationsBean.operationNames}">
+
+ Operations for: <h:outputText value=" #{operationsBean.resourceName}"/>
+ <br/>
+
<h:selectOneMenu id="selectOperationMenu" value="#{operationsBean.operationName}">
<f:selectItems value="#{operationsBean.operationNames}"/>
</h:selectOneMenu>
@@ -42,31 +48,40 @@
<h:commandButton id="opNameSubmit"
value="Select"
type="submit"
- action="#{operationsBean.selectOperation}" />
+ action="#{operationsBean.selectOperation}"
+ styleClass="buttonmed"
+ />
</rich:panel>
</h:panelGroup>
</h:panelGrid>
<p/>
<h:panelGroup style="width:100%">
- <h:outputText value="3rd panel : "/>
- <h:outputText value="#{operationsBean.operationName}"/>
- <br/>
+ <h:messages />
<rich:panel rendered="#{not empty operationsBean.operationName}">
+ <br/>
+ Parameters for <h:outputText value=" #{operationsBean.operationName}"/>
+ <p/>
+ <h:selectOneMenu id="tokenModeMenu" value="#{operationsBean.tokenMode}">
+ <f:selectItem itemValue="literal" itemLabel="Literal"/>
+ <f:selectItem itemValue="interpreted" itemLabel="Interpreted"/>
+ </h:selectOneMenu>
+ <p/>
<onc:config configurationDefinition="#{operationsBean.configurationDefinition}"
configuration="#{operationsBean.configuration}"
- nullConfigurationDefinitionMessage="NUll configuration definition"
- nullConfigurationMessage="null configuration"
+ nullConfigurationDefinitionMessage="#{operationsBean.nullConfigurationDefinitionMessage}"
+ nullConfigurationMessage="#{nullConfigurationMessage}"
nullConfigurationStyle="InfoBlock"
-
- id="opParamConfiguration"
+ readOnly="false"
/>
<p/>
<h:commandButton id="opParamSubmit"
value="Set"
type="submit"
- action="#{operationsBean.useConfiguration}" />
+ action="#{operationsBean.useConfiguration}"
+ styleClass="buttonmed"
+ />
</rich:panel>
</h:panelGroup>
</body>
-</html>
\ No newline at end of file
+</html>
commit b196e883688008615778ad6eb73fb94caff12015
Author: Heiko W. Rupp <hwr(a)redhat.com>
Date: Tue Feb 23 10:56:59 2010 +0100
Implement code for token replacement
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/Token.java b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/Token.java
new file mode 100644
index 0000000..bcd7570
--- /dev/null
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/Token.java
@@ -0,0 +1,72 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2010 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.rhq.enterprise.server.plugins.alertOperations;
+
+import java.util.EnumSet;
+
+/**
+ * Tokens that can be replaced in
+ * @author Heiko W. Rupp
+ */
+public enum Token {
+
+ // Alert related tokens
+ ALERT_ID(TokenClass.ALERT, "id"),
+ ALERT_URL(TokenClass.ALERT, "url"),
+
+ // resource that triggered the alert related tokens
+ RESOURCE_ID(TokenClass.RESOURCE, "id"),
+ RESOURCE_NAME(TokenClass.RESOURCE, "name"),
+
+
+ // resource the operation is run on related tokens
+ TRESOURCE_ID(TokenClass.TARGET_RESOURCE, "id"),
+ TRESOURCE_NAME(TokenClass.TARGET_RESOURCE, "name"),
+
+
+ // only for testing
+ TEST_ECHO(TokenClass.TEST,"echo"),
+ TEST_FIX(TokenClass.TEST,"fix")
+
+ ;
+
+
+ private String text;
+
+ private Token(TokenClass tc, String text) {
+
+ this.text = tc.getText() + "." + text;
+ }
+
+ /**
+ * Return the token that matches the input text or null if not found.
+ * The token delimiters need to be already stripped from the input
+ * @param input a token text like <i>alert.id</i>, which would return the
+ * <i>ALERT_ID</i> token.
+ * @return The matching token or null if not found
+ */
+ public static Token getByText(String input) {
+ EnumSet<Token> es = EnumSet.allOf(Token.class);
+ for (Token t : es) {
+ if (t.text.equals(input))
+ return t;
+ }
+ return null;
+ }
+}
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/TokenClass.java b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/TokenClass.java
new file mode 100644
index 0000000..5125a72
--- /dev/null
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/TokenClass.java
@@ -0,0 +1,59 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2010 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.rhq.enterprise.server.plugins.alertOperations;
+
+import java.util.EnumSet;
+
+/**
+ * Class a @See{Token} can be in.
+ * @author Heiko W. Rupp
+ */
+public enum TokenClass {
+
+ ALERT("alert"),
+ RESOURCE("resource"),
+ TARGET_RESOURCE("targetResource"),
+ TEST("test");
+
+ private String text;
+
+ private TokenClass(String text) {
+ this.text = text;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ /**
+ * Return the tokenclass that matches the input text or null if not found.
+ * The token delimiters need to be already stripped from the input
+ * @param input a token text like <i>alert</i>, which would return the
+ * <i>ALERT</i> token class.
+ * @return The matching token class or null if not found
+ */
+ public static TokenClass getByText(String input) {
+ EnumSet<TokenClass> es = EnumSet.allOf(TokenClass.class);
+ for (TokenClass t : es) {
+ if (t.text.equals(input))
+ return t;
+ }
+ return null;
+ }
+}
diff --git a/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/TokenReplacer.java b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/TokenReplacer.java
new file mode 100644
index 0000000..0b230b4
--- /dev/null
+++ b/modules/enterprise/server/plugins/alert-operations/src/main/java/org/rhq/enterprise/server/plugins/alertOperations/TokenReplacer.java
@@ -0,0 +1,178 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2010 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.rhq.enterprise.server.plugins.alertOperations;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.rhq.core.domain.alert.Alert;
+import org.rhq.core.domain.resource.Resource;
+
+/**
+ * Helper to replace tokens by their values
+ * @author Heiko W. Rupp
+ */
+public class TokenReplacer {
+
+ private final Log log = LogFactory.getLog(TokenReplacer.class);
+ private static final String NOT_YET_IMPLEMENTED = " - not yet implemented -";
+ protected static final String THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG = "TheQuickBrownFoxJumpsOverTheLazyDOg";
+ private Alert alert;
+ private Pattern pattern;
+
+ public TokenReplacer(Alert alert) {
+ this.alert = alert;
+ pattern = Pattern.compile("<%\\s*([a-z]+\\.[a-z0-9]+)\\s*%>");
+ }
+
+ /**
+ * Replace all tokens on the input line. If no tokens are found the input is returned.
+ * Tokens have the form '<i><% class.sub %></i>'
+ * @param input a line of text
+ * @return input with tokens replaced.
+ * @see org.rhq.enterprise.server.plugins.alertOperations.Token
+ * @see org.rhq.enterprise.server.plugins.alertOperations.TokenClass
+ */
+ public String replaceTokens(String input) {
+
+ String work = input;
+ Matcher matcher = pattern.matcher(work);
+ if (!matcher.find()) {
+ log.warn("No tokens found in " + input);
+ return input;
+ }
+ matcher.reset();
+
+ do {
+// System.out.println(input);
+ matcher = pattern.matcher(work);
+ if (!matcher.find()) {
+ break;
+ }
+// System.out.println(matcher.regionStart() + ":" + matcher.regionEnd() + input.substring(matcher.regionStart(),matcher.regionEnd()));
+// System.out.println(matcher.group(1));
+ String replacement = replaceToken(matcher.group(1) );
+ String s = matcher.replaceFirst(replacement);
+// System.out.println(s);
+ work = s;
+
+// System.out.println("----");
+ } while (true);
+
+ return work;
+ }
+
+
+
+ /**
+ * Replace the token string passed (without the token delimiters ) with the actual value
+ * @param tokenString Input like alert.id
+ * @return replacement string or the input if the token was not valid.
+ */
+ public String replaceToken(String tokenString) {
+
+ // Ok, we have at least one token. Now split the tokenString and loop over the tokens
+
+ if (!tokenString.contains("."))
+ return tokenString;
+
+ String tmp = tokenString.substring(0, tokenString.indexOf("."));
+ TokenClass tc = TokenClass.getByText(tmp);
+ if (tc==null) {
+ log.warn("Unknown token class in [" + tokenString + "], not replacing tokens");
+ return tokenString;
+ }
+
+ Token token = Token.getByText(tokenString);
+ if (token == null) {
+ log.warn("No known token found in [" + tokenString + "], not replacing token");
+ return tokenString;
+ }
+ String ret = null;
+ switch (tc) {
+ case ALERT:
+ ret = replaceAlertToken(token,alert);
+ break;
+ case RESOURCE:
+ ret = replaceResourceToken(token,alert.getAlertDefinition().getResource());
+ break;
+ case TARGET_RESOURCE:
+ Resource resource = null; // TODO
+ ret = replaceTargetResourceToken(token, resource);
+ break;
+ case TEST:
+ switch (token) {
+ case TEST_ECHO:
+ ret = tokenString;
+ break;
+ case TEST_FIX:
+ ret = THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG;
+ break;
+ default:
+ ret = NOT_YET_IMPLEMENTED;
+ }
+ break;
+ }
+ return ret;
+ }
+
+ private String replaceAlertToken(Token token, Alert alert) {
+
+ switch (token) {
+ case ALERT_ID:
+ return String.valueOf(alert.getId());
+ case ALERT_URL:
+ return NOT_YET_IMPLEMENTED;
+
+ default:
+ return NOT_YET_IMPLEMENTED;
+ }
+
+ }
+
+ private String replaceResourceToken(Token token, Resource resource) {
+
+ switch (token) {
+ case RESOURCE_ID:
+ return String.valueOf(resource.getId());
+ case RESOURCE_NAME:
+ return resource.getName();
+
+ default:
+ return NOT_YET_IMPLEMENTED;
+ }
+ }
+
+ private String replaceTargetResourceToken(Token token, Resource resource) {
+
+ switch (token) {
+ case TRESOURCE_ID:
+ return String.valueOf(resource.getId());
+ case TRESOURCE_NAME:
+ return resource.getName();
+
+ default:
+ return NOT_YET_IMPLEMENTED;
+ }
+ }
+
+}
diff --git a/modules/enterprise/server/plugins/alert-operations/src/test/java/org/rhq/enterprise/server/plugins/alertOperations/TokenReplacementTest.java b/modules/enterprise/server/plugins/alert-operations/src/test/java/org/rhq/enterprise/server/plugins/alertOperations/TokenReplacementTest.java
new file mode 100644
index 0000000..f41fc85
--- /dev/null
+++ b/modules/enterprise/server/plugins/alert-operations/src/test/java/org/rhq/enterprise/server/plugins/alertOperations/TokenReplacementTest.java
@@ -0,0 +1,135 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2010 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.rhq.enterprise.server.plugins.alertOperations;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.testng.annotations.Test;
+
+import org.rhq.core.domain.alert.Alert;
+import org.rhq.core.domain.alert.AlertDefinition;
+import org.rhq.core.domain.resource.Resource;
+
+/**
+ * // TODO: Document this
+ * @author Heiko W. Rupp
+ */
+@Test
+public class TokenReplacementTest {
+
+ private final Log log = LogFactory.getLog(TokenReplacementTest.class);
+ private static final String TEST_ECHO = "test.echo";
+ private static final String FOO_BAR = "fooBar";
+ private static final String FOO_DOT_BAR = "foo.bar";
+ private static final String ALERT_BAR = "alert.bar";
+
+ public void testSimpleReplacement() throws Exception {
+
+ Alert al = new Alert();
+ TokenReplacer tr = new TokenReplacer(al);
+
+
+ String res = tr.replaceToken("test.fix");
+ assert res != null;
+ assert res.equals(TokenReplacer.THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG);
+
+ res = tr.replaceToken(TEST_ECHO);
+ assert res != null;
+ assert res.equals(TEST_ECHO);
+
+ res = tr.replaceToken("alert.id");
+ assert res != null;
+ assert res.equals("0");
+
+ Resource r = new Resource(1234);
+ r.setName("A resource");
+ AlertDefinition def = new AlertDefinition();
+ def.setResource(r);
+ al = new Alert(def, System.currentTimeMillis());
+ tr = new TokenReplacer(al);
+
+ res = tr.replaceToken("resource.id");
+ assert "1234".equals(res);
+ res = tr.replaceToken("resource.name");
+ assert "A resource".equals(res);
+ }
+
+ public void testInvalidTokenString() {
+
+ Alert al = new Alert();
+ TokenReplacer tr = new TokenReplacer(al);
+
+
+ String res = tr.replaceToken(FOO_BAR);
+ assert res != null;
+ assert res.equals(FOO_BAR);
+
+ res = tr.replaceToken(FOO_DOT_BAR);
+ assert res != null;
+ assert res.equals(FOO_DOT_BAR);
+
+ res = tr.replaceToken(ALERT_BAR);
+ assert res != null;
+ assert res.equals(ALERT_BAR);
+
+ }
+
+ public void testFullTokenSimple() {
+
+ Alert al = new Alert();
+ TokenReplacer tr = new TokenReplacer(al);
+
+
+ String res = tr.replaceTokens("<%test.fix%>");
+ assert res != null;
+ assert !res.equals("<%test.fix%>") : "Res was " + res;
+ assert res.equals(TokenReplacer.THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG);
+
+ String TF2 = "<% test.fix %>";
+ res = tr.replaceTokens(TF2);
+ assert res != null;
+ assert !res.equals(TF2) : "Res was " + res;
+
+ String TF3 = "<% test.fix %>";
+ res = tr.replaceTokens(TF3);
+ assert res != null;
+ assert !res.equals(TF3) : "Res was " + res;
+
+ String TF4 = "<% test.fix%> xXx <% test.echo%>";
+ res = tr.replaceTokens(TF4);
+ assert res != null;
+ assert !res.equals(TF4) : "Res was " + res;
+
+ res = tr.replaceTokens(FOO_BAR);
+ assert res != null;
+ assert res.equals(FOO_BAR) : "Res was " + res;
+
+ Resource r = new Resource(1234);
+ r.setName("A resource");
+ AlertDefinition def = new AlertDefinition();
+ def.setResource(r);
+ al = new Alert(def, System.currentTimeMillis());
+
+ tr = new TokenReplacer(al);
+ res = tr.replaceTokens("<% test.fix%><%resource.id%>");
+ assert res!=null;
+ assert res.equals(TokenReplacer.THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG+"1234");
+ }
+
+}