FileDocCategorySizeDatePackage
JMXAccessorCondition.javaAPI DocApache Tomcat 6.0.1412699Fri Jul 20 04:20:36 BST 2007org.apache.catalina.ant.jmx

JMXAccessorCondition

public class JMXAccessorCondition extends org.apache.tools.ant.ProjectComponent implements org.apache.tools.ant.taskdefs.condition.Condition
Definition:
<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"/>
Usage: Wait for start backup node
<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>

Allowed operation between jmx attribute and reference value:
  • == equals
  • != not equals
  • > greater than (&gt;)
  • >= greater than or equals (&gt;=)
  • < lesser than (&lt;)
  • <= lesser than or equals (&lt;=)
NOTE: For numeric expressions the type must be set and use xml entities as operations.
As type we currently support long and double.
author
Peter Rossbach
version
$Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
since
5.5.10

Fields Summary
private String
url
private String
host
private String
port
private String
password
private String
username
private String
name
private String
attribute
private String
value
private String
operation
private String
type
private String
ref
private String
unlessCondition
private String
ifCondition
private static final String
info
Descriptive information describing this implementation.
Constructors Summary
Methods Summary
protected java.lang.StringaccessJMXValue()
Get value from MBeans attribute

return
The value

        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;
    
public booleaneval()
This method evaluates the condition It support for operation ">,>=,<,<=" the types long and double.

return
expression jmxValue operation value

        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");
        }
        if (testIfCondition() && testUnlessCondition()) {
            String jmxValue = accessJMXValue();
            if (jmxValue != null) {
                String op = getOperation();
                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;
        }
        return true;
    
public java.lang.StringgetAttribute()

return
Returns the attribute.

        return attribute;
    
public java.lang.StringgetHost()

return
Returns the host.

        return host;
    
public java.lang.StringgetIf()

return
Returns the ifCondition.

        return ifCondition;
    
public java.lang.StringgetInfo()
Return descriptive information about this implementation and the corresponding version number, in the format <description>/<version>.


                        
       

        return (info);

    
protected javax.management.MBeanServerConnectiongetJMXConnection()
Get JMXConnection (default look at jmx.server project reference from jmxOpen Task)

return
active JMXConnection
throws
MalformedURLException
throws
IOException

        return JMXAccessorTask.accessJMXConnection(
                getProject(),
                getUrl(), getHost(),
                getPort(), getUsername(), getPassword(), ref);
    
public java.lang.StringgetName()

return
Returns the name.

        return name;
    
public java.lang.StringgetOperation()

return
Returns the operation.

        return operation;
    
public java.lang.StringgetPassword()

return
Returns the password.

        return password;
    
public java.lang.StringgetPort()

return
Returns the port.

        return port;
    
public java.lang.StringgetRef()

return
Returns the ref.

        return ref;
    
public java.lang.StringgetType()

return
Returns the type.

        return type;
    
public java.lang.StringgetUnless()

return
Returns the unlessCondition.

        return unlessCondition;
    
public java.lang.StringgetUrl()

return
Returns the url.

        return url;
    
public java.lang.StringgetUsername()

return
Returns the username.

        return username;
    
public java.lang.StringgetValue()

return
Returns the value.

        return value;
    
public voidsetAttribute(java.lang.String attribute)

param
attribute The attribute to set.

        this.attribute = attribute;
    
public voidsetHost(java.lang.String host)

param
host The host to set.

        this.host = host;
    
public voidsetIf(java.lang.String c)
Only execute if a property of the given name exists in the current project.

param
c property name

        ifCondition = c;
    
public voidsetName(java.lang.String objectName)

param
objectName The name to set.

        this.name = objectName;
    
public voidsetOperation(java.lang.String operation)

param
operation The operation to set.

        this.operation = operation;
    
public voidsetPassword(java.lang.String password)

param
password The password to set.

        this.password = password;
    
public voidsetPort(java.lang.String port)

param
port The port to set.

        this.port = port;
    
public voidsetRef(java.lang.String refId)

param
refId The ref to set.

        this.ref = refId;
    
public voidsetType(java.lang.String type)

param
type The type to set.

        this.type = type;
    
public voidsetUnless(java.lang.String c)
Only execute if a property of the given name does not exist in the current project.

param
c property name

        unlessCondition = c;
    
public voidsetUrl(java.lang.String url)

param
url The url to set.

        this.url = url;
    
public voidsetUsername(java.lang.String username)

param
username The username to set.

        this.username = username;
    
public voidsetValue(java.lang.String value)

        this.value = value;
    
protected booleantestIfCondition()
test the if condition

return
true if there is no if condition, or the named property exists

        if (ifCondition == null || "".equals(ifCondition)) {
            return true;
        }
        return getProject().getProperty(ifCondition) != null;
    
protected booleantestUnlessCondition()
test the unless condition

return
true if there is no unless condition, or there is a named property but it doesn't exist

        if (unlessCondition == null || "".equals(unlessCondition)) {
            return true;
        }
        return getProject().getProperty(unlessCondition) == null;