pero 2005/06/30 06:01:27
Modified: catalina/src/share/org/apache/catalina/ant/jmx
JMXAccessorEqualsCondition.java
JMXAccessorGetTask.java JMXAccessorInvokeTask.java
JMXAccessorQueryTask.java JMXAccessorSetTask.java
JMXAccessorTask.java antlib.xml jmxaccessor.tasks
Added: catalina/src/share/org/apache/catalina/ant/jmx
JMXAccessorCondition.java
Log:
Add support for if and unless
Add more usefull Ant Condition that support reference jmx connection
Fix some docs
Revision Changes Path
1.2 +31 -16
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java
Index: JMXAccessorEqualsCondition.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JMXAccessorEqualsCondition.java 24 Jun 2005 11:52:51 -0000 1.1
+++ JMXAccessorEqualsCondition.java 30 Jun 2005 13:01:27 -0000 1.2
@@ -12,22 +12,22 @@
/**
*
* Definition
- * <code>
- * <path id="catalina_ant">
- * <fileset dir="${catalina.home}/server/lib">
- * <include name="catalina-ant.jar"/>
- * <include name="catalina-ant-jmx.jar"/>
- * </fileset>
- * </path>
+ * <pre>
+ * <path id="catalina_ant">
+ * <fileset dir="${catalina.home}/server/lib">
+ * <include name="catalina-ant.jar"/>
+ * <include name="catalina-ant-jmx.jar"/>
+ * </fileset>
+ * </path>
*
- * <typedef
+ * <typedef
* name="jmxEquals"
* classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition"
* classpathref="catalina_ant"/>
- * </code>
+ * </pre>
*
* usage: Wait for start backup node
- * <code>
+ * <pre>
* <target name="wait">
* <waitfor maxwait="${maxwait}" maxwaitunit="second"
timeoutproperty="server.timeout" >
* <and>
@@ -44,7 +44,7 @@
* <echo message="Server ${url} alive" />
* </target>
*
- * </code>
+ * </pre>
*
* @author Peter Rossbach
* @version $Revision$ $Date$
@@ -63,13 +63,13 @@
private String name = null;
private String attribute;
private String value;
-
+ private String ref = "jmx.server" ;
// ----------------------------------------------------- Instance Info
/**
* Descriptive information describing this implementation.
*/
- private static final String info =
"org.apache.catalina.ant.JMXAccessorEqualsCondition/1.0";
+ private static final String info =
"org.apache.catalina.ant.JMXAccessorEqualsCondition/1.1";
/**
* Return descriptive information about this implementation and the
@@ -178,10 +178,25 @@
this.value = value;
}
+ /**
+ * @return Returns the ref.
+ */
+ public String getRef() {
+ return ref;
+ }
+ /**
+ * @param ref The ref to set.
+ */
+ public void setRef(String refId) {
+ this.ref = refId;
+ }
+
protected MBeanServerConnection getJMXConnection()
throws MalformedURLException, IOException {
- return JMXAccessorTask.createJMXConnection(getUrl(), getHost(),
- getPort(), getUsername(), getPassword());
+ return JMXAccessorTask.accessJMXConnection(
+ getProject(),
+ getUrl(), getHost(),
+ getPort(), getUsername(), getPassword(), ref);
}
/**
1.2 +4 -4
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorGetTask.java
Index: JMXAccessorGetTask.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorGetTask.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JMXAccessorGetTask.java 24 Jun 2005 11:52:51 -0000 1.1
+++ JMXAccessorGetTask.java 30 Jun 2005 13:01:27 -0000 1.2
@@ -33,9 +33,9 @@
* </ul>
* <p>
* Examples:
- * <li>
+ * <br/>
* Get a Mbean IDataSender attribute nrOfRequests and create a new ant
property <em>IDataSender.9025.nrOfRequests</em>
- * <code>
+ * <pre>
* <jmx:get
* ref="jmx.server"
*
name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.1.2,senderPort=9025"
@@ -43,7 +43,7 @@
* resultproperty="IDataSender.9025.nrOfRequests"
* echo="false">
* />
- * </code>
+ * </pre>
* </p>
* <p>
* First call to a remote MBeanserver save the JMXConnection a referenz
<em>jmx.server</em>
1.2 +3 -3
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorInvokeTask.java
Index: JMXAccessorInvokeTask.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorInvokeTask.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JMXAccessorInvokeTask.java 24 Jun 2005 11:52:51 -0000 1.1
+++ JMXAccessorInvokeTask.java 30 Jun 2005 13:01:27 -0000 1.2
@@ -44,7 +44,7 @@
* <li>
* Get a session attribute hello from session with ref
<em>${sessionid.0}</em> form
* app <em>Catalina:type=Manager,path=/ClusterTest,host=localhost</em>
- * <code>
+ * <pre>
* <jmx:invoke
* name="Catalina:type=Manager,path=/ClusterTest,host=localhost"
* operation="getSessionAttribute"
@@ -52,7 +52,7 @@
* <arg value="${sessionid.0}"/>
* <arg value="Hello"/>
* </jmx:invoke>
- * </code>
+ * </pre>
* </li>
* <li>
* Create new AccessLogger at localhost
1.2 +3 -3
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorQueryTask.java
Index: JMXAccessorQueryTask.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorQueryTask.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JMXAccessorQueryTask.java 24 Jun 2005 11:52:51 -0000 1.1
+++ JMXAccessorQueryTask.java 30 Jun 2005 13:01:27 -0000 1.2
@@ -40,13 +40,13 @@
* </ul>
* <br/>
* Query a list of Mbean
- * <code>
+ * <pre>
* <jmxQuery
* host="127.0.0.1"
* port="9014"
* name="Catalina:type=Manager,*
* resultproperty="manager" />
- * </code>
+ * </pre>
* with attribute <em>attributebinding="true"</em> you can get
* all attributes also from result objects.<br/>
* The poperty manager.lenght show the size of the result
1.2 +3 -3
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorSetTask.java
Index: JMXAccessorSetTask.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorSetTask.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JMXAccessorSetTask.java 24 Jun 2005 11:52:51 -0000 1.1
+++ JMXAccessorSetTask.java 30 Jun 2005 13:01:27 -0000 1.2
@@ -38,7 +38,7 @@
* Examples:
* Set a Mbean Manager attribute maxActiveSessions.
* Set this attribute with fresh jmx connection without save reference
- * <code>
+ * <pre>
* <jmx:set
* host="127.0.0.1"
* port="9014"
@@ -49,7 +49,7 @@
* type="int"
* echo="false">
* />
- * </code>
+ * </pre>
* </p>
* <p>
* First call to a remote MBeanserver save the JMXConnection a referenz
<em>jmx.server</em>
1.2 +92 -25
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorTask.java
Index: JMXAccessorTask.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorTask.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JMXAccessorTask.java 24 Jun 2005 11:52:51 -0000 1.1
+++ JMXAccessorTask.java 30 Jun 2005 13:01:27 -0000 1.2
@@ -52,7 +52,7 @@
*
* Examples:
* open server with reference and autorisation
- * <code>
+ * <pre>
* <jmxOpen
* host="127.0.0.1"
* port="9014"
@@ -60,12 +60,15 @@
* password="mysecret"
* ref="jmx.myserver"
* />
- * </code>
+ * </pre>
* All calls after opening with same refid reuse the connection.
* <p>
* First call to a remote MBeanserver save the JMXConnection a referenz
<em>jmx.server</em>
* </p>
- * These tasks require Ant 1.6 or later interface.
+ * All JMXAccessorXXXTask support the attribute <em>if</em> and
<em>unless</em>. With <em>if</em>
+ * the task is only execute when property exist and with <em>unless</em>
when property not exists.
+ * <br/>
+ * <b>NOTE</b>: These tasks require Ant 1.6 or later interface.
*
* @author Peter Rossbach
* @version $Revision$ $Date$
@@ -91,7 +94,9 @@
private boolean echo = false;
private boolean separatearrayresults = true;
private String delimiter;
-
+ private String unlessCondition;
+ private String ifCondition;
+
// ----------------------------------------------------- Instance Info
/**
@@ -239,7 +244,7 @@
public void setPort(String port) {
this.port = port;
}
-
+
/**
* @return Returns the useRef.
*/
@@ -261,6 +266,41 @@
}
+ /**
+ * @return Returns the ifCondition.
+ */
+ public String getIf() {
+ return ifCondition;
+ }
+ /**
+ * Only fail if a property of the given name exists in the current
project.
+ * @param c property name
+ */
+ public void setIf(String c) {
+ ifCondition = c;
+ }
+ /**
+ * @return Returns the unlessCondition.
+ */
+ public String getUnless() {
+ return unlessCondition;
+ }
+ /**
+ * @param unlessCondition The unlessCondition to set.
+ */
+ public void setUnlessCondition(String unlessCondition) {
+ this.unlessCondition = unlessCondition;
+ }
+
+ /**
+ * Only fail if a property of the given name does not
+ * exist in the current project.
+ * @param c property name
+ */
+ public void setUnless(String c) {
+ unlessCondition = c;
+ }
+
// --------------------------------------------------------- Public
Methods
/**
@@ -271,25 +311,26 @@
* @exception BuildException if a validation error occurs
*/
public void execute() throws BuildException {
+ if (testIfCondition() && testUnlessCondition()) {
+ try {
+ String error = null;
- try {
- String error = null;
-
- MBeanServerConnection jmxServerConnection = getJMXConnection();
- error = jmxExecute(jmxServerConnection);
- if (error != null && isFailOnError()) {
- // exception should be thrown only if failOnError == true
- // or error line will be logged twice
- throw new BuildException(error);
- }
- } catch (Throwable t) {
- if (isFailOnError()) {
- throw new BuildException(t);
- } else {
- handleErrorOutput(t.getMessage());
+ MBeanServerConnection jmxServerConnection =
getJMXConnection();
+ error = jmxExecute(jmxServerConnection);
+ if (error != null && isFailOnError()) {
+ // exception should be thrown only if failOnError == true
+ // or error line will be logged twice
+ throw new BuildException(error);
+ }
+ } catch (Throwable t) {
+ if (isFailOnError()) {
+ throw new BuildException(t);
+ } else {
+ handleErrorOutput(t.getMessage());
+ }
+ } finally {
+ closeRedirector();
}
- } finally {
- closeRedirector();
}
}
@@ -318,6 +359,30 @@
}
+
+ /**
+ * test the if condition
+ * @return true if there is no if condition, or the named property exists
+ */
+ protected boolean testIfCondition() {
+ if (ifCondition == null || "".equals(ifCondition)) {
+ return true;
+ }
+ return getProject().getProperty(ifCondition) != null;
+ }
+
+ /**
+ * test the unless condition
+ * @return true if there is no unless condition,
+ * or there is a named property but it doesn't exist
+ */
+ protected boolean testUnlessCondition() {
+ if (unlessCondition == null || "".equals(unlessCondition)) {
+ return true;
+ }
+ return getProject().getProperty(unlessCondition) == null;
+ }
+
/**
* Get Current Connection from <em>ref</em> parameter or create a new
* one!
@@ -331,14 +396,16 @@
String password, String refId) throws MalformedURLException,
IOException {
MBeanServerConnection jmxServerConnection = null;
- boolean isRef = refId != null;
+ boolean isRef = project != null && refId != null && refId.length()
> 0 ;
if (isRef) {
Object pref = project.getReference(refId);
try {
jmxServerConnection = (MBeanServerConnection) pref;
} catch (ClassCastException cce) {
- project.log("wrong object reference " + refId + " - "
+ if(project != null ) {
+ project.log("wrong object reference " + refId + " - "
+ pref.getClass());
+ }
return null;
}
}
1.2 +3 -0
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/antlib.xml
Index: antlib.xml
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/antlib.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- antlib.xml 24 Jun 2005 11:52:51 -0000 1.1
+++ antlib.xml 30 Jun 2005 13:01:27 -0000 1.2
@@ -18,4 +18,7 @@
<typedef
name="equals"
classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition" />
+ <typedef
+ name="condition"
+ classname="org.apache.catalina.ant.jmx.JMXAccessorCondition" />
</antlib>
\ No newline at end of file
1.2 +1 -1
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/jmxaccessor.tasks
Index: jmxaccessor.tasks
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/jmxaccessor.tasks,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- jmxaccessor.tasks 24 Jun 2005 11:52:51 -0000 1.1
+++ jmxaccessor.tasks 30 Jun 2005 13:01:27 -0000 1.2
@@ -1,6 +1,6 @@
# JMX
jmxOpen=org.apache.catalina.ant.jmx.JMXAccessorTask
-jmxSet=org.apache.catalina.ant..jmx.JMXAccessorSetTask
+jmxSet=org.apache.catalina.ant.jmx.JMXAccessorSetTask
jmxGet=org.apache.catalina.ant.jmx.JMXAccessorGetTask
jmxInvoke=org.apache.catalina.ant.jmx.JMXAccessorInvokeTask
jmxQuery=org.apache.catalina.ant.jmx.JMXAccessorQueryTask
1.1
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorCondition.java
Index: JMXAccessorCondition.java
===================================================================
package org.apache.catalina.ant.jmx;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.taskdefs.condition.Condition;
/**
*
* <b>Definition</b>:
* <pre>
* <path id="catalina_ant">
* <fileset dir="${catalina.home}/server/lib">
* <include name="catalina-ant.jar"/>
* <include name="catalina-ant-jmx.jar"/>
* </fileset>
* </path>
*
* <typedef
* name="jmxCondition"
* classname="org.apache.catalina.ant.jmx.JMXAccessorCondition"
* classpathref="catalina_ant"/>
* <taskdef
* name="jmxOpen"
* classname="org.apache.catalina.ant.jmx.JMXAccessorTask"
* classpathref="catalina_ant"/>
* </pre>
*
* <b>Usage</b>: Wait for start backup node
* <pre>
* <target name="wait">
* <jmxOpen
* host="${jmx.host}" port="${jmx.port}"
username="${jmx.username}" password="${jmx.password}" />
* <waitfor maxwait="${maxwait}" maxwaitunit="second"
timeoutproperty="server.timeout" >
* <and>
* <socket server="${server.name}" port="${server.port}"/>
* <http url="${url}"/>
* <jmxCondition
*
name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
* operation="=="
* attribute="connected" value="true"
* />
* <jmxCondition
* operation="&lt;"
*
name="Catalina:j2eeType=WebModule,name=//${tomcat.application.host}${tomcat.application.path},J2EEApplication=none,J2EEServer=none"
* attribute="startupTime" value="250"
* />
* </and>
* </waitfor>
* <fail if="server.timeout" message="Server ${url} don't answer
inside ${maxwait} sec" />
* <echo message="Server ${url} alive" />
* </target>
*
* </pre>
* Allowed operation between jmx attribute and reference value:
* <ul>
* <li>== equals</li>
* <li>!= not equals</li>
* <li>> greater than (&gt;)</li>
* <li>>= greater than or equals (&gt;=)</li>
* <li>< lesser than (&lt;)</li>
* <li><= lesser than or equals (&lt;=)</li>
* </ul>
* <b>NOTE</b>: For numeric expressions the type must be set and use xml
entities as operations.<br/>
* As type we currently support <em>long</em> and <em>double</em>.
* @author Peter Rossbach
* @version $Revision: 1.1 $ $Date: 2005/06/30 13:01:27 $
* @since 5.5.10
*
*/
public class JMXAccessorCondition extends ProjectComponent implements
Condition {
// ----------------------------------------------------- Instance
Variables
private String url = null;
private String host = "localhost";
private String port = "8050";
private String password = null;
private String username = null;
private String name = null;
private String attribute;
private String value;
private String operation = "==" ;
private String type = "long" ;
private String ref = "jmx.server";
// ----------------------------------------------------- Instance Info
/**
* Descriptive information describing this implementation.
*/
private static final String info =
"org.apache.catalina.ant.JMXAccessorCondition/1.1";
/**
* Return descriptive information about this implementation and the
* corresponding version number, in the format
* <code><description>/<version></code>.
*/
public String getInfo() {
return (info);
}
// ----------------------------------------------------- Properties
/**
* @return Returns the operation.
*/
public String getOperation() {
return operation;
}
/**
* @param operation The operation to set.
*/
public void setOperation(String operation) {
this.operation = operation;
}
/**
* @return Returns the type.
*/
public String getType() {
return type;
}
/**
* @param type The type to set.
*/
public void setType(String type) {
this.type = type;
}
/**
* @return Returns the attribute.
*/
public String getAttribute() {
return attribute;
}
/**
* @param attribute The attribute to set.
*/
public void setAttribute(String attribute) {
this.attribute = attribute;
}
/**
* @return Returns the host.
*/
public String getHost() {
return host;
}
/**
* @param host The host to set.
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String objectName) {
this.name = objectName;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return Returns the port.
*/
public String getPort() {
return port;
}
/**
* @param port The port to set.
*/
public void setPort(String port) {
this.port = port;
}
/**
* @return Returns the url.
*/
public String getUrl() {
return url;
}
/**
* @param url The url to set.
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return Returns the username.
*/
public String getUsername() {
return username;
}
/**
* @param username The username to set.
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return Returns the value.
*/
public String getValue() {
return value;
}
// The setter for the "value" attribute
public void setValue(String value) {
this.value = value;
}
/**
* @return Returns the ref.
*/
public String getRef() {
return ref;
}
/**
* @param ref The ref to set.
*/
public void setRef(String refId) {
this.ref = refId;
}
protected MBeanServerConnection getJMXConnection()
throws MalformedURLException, IOException {
return JMXAccessorTask.accessJMXConnection(
getProject(),
getUrl(), getHost(),
getPort(), getUsername(), getPassword(), ref);
}
/**
* @return
*/
protected String accessJMXValue() {
try {
Object result = getJMXConnection().getAttribute(
new ObjectName(name), attribute);
if(result != null)
return result.toString();
} catch (Exception e) {
// ignore access or connection open errors
}
return null;
}
// This method evaluates the condition
public boolean eval() {
if (operation == null) {
throw new BuildException("operation attribute is not set");
}
if (value == null) {
throw new BuildException("value attribute is not set");
}
if ((name == null || attribute == null)) {
throw new BuildException(
"Must specify a 'attribute', name for equals condition");
}
//FIXME check url or host/parameter
String jmxValue = accessJMXValue();
String op = getOperation() ;
if(jmxValue != null) {
if("==".equals(op)) {
return jmxValue.equals(value);
} else if("!=".equals(op)) {
return !jmxValue.equals(value);
} else {
if("long".equals(type)) {
long jvalue = Long.parseLong(jmxValue);
long lvalue = Long.parseLong(value);
if(">".equals(op)) {
return jvalue > lvalue ;
} else if(">=".equals(op)) {
return jvalue >= lvalue ;
} else if("<".equals(op)) {
return jvalue < lvalue ;
} else if ("<=".equals(op)) {
return jvalue <= lvalue;
}
} else if("double".equals(type)) {
double jvalue = Double.parseDouble(jmxValue);
double dvalue = Double.parseDouble(value);
if(">".equals(op)) {
return jvalue > dvalue ;
} else if(">=".equals(op)) {
return jvalue >= dvalue ;
} else if("<".equals(op)) {
return jvalue < dvalue ;
} else if ("<=".equals(op)) {
return jvalue <= dvalue;
}
}
}
}
return false;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]