/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.catalina.ant.jmx; import org.apache.tools.ant.BuildException; /** * Definition: *
* <path id="catalina_ant">
* <fileset dir="${catalina.home}/server/lib">
* <include name="catalina-ant.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="<"
* 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:
* long and double.
* @return expression jmxValue operation value
*/
@Override
public boolean eval() {
String value = getValue();
if (operation == null) {
throw new BuildException("operation attribute is not set");
}
if (value == null) {
throw new BuildException("value attribute is not set");
}
if ((getName() == null || getAttribute() == null)) {
throw new BuildException(
"Must specify an MBean name and attribute for 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;
}
}