001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.ant.jmx;
019:
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022:
023: import javax.management.MBeanServerConnection;
024: import javax.management.ObjectName;
025:
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.ProjectComponent;
028: import org.apache.tools.ant.taskdefs.condition.Condition;
029:
030: /**
031: *
032: * Definition
033: * <pre>
034: * <path id="catalina_ant">
035: * <fileset dir="${catalina.home}/server/lib">
036: * <include name="catalina-ant.jar"/>
037: * <include name="catalina-ant-jmx.jar"/>
038: * </fileset>
039: * </path>
040: *
041: * <typedef
042: * name="jmxEquals"
043: * classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition"
044: * classpathref="catalina_ant"/>
045: * </pre>
046: *
047: * usage: Wait for start backup node
048: * <pre>
049: * <target name="wait">
050: * <waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" >
051: * <and>
052: * <socket server="${server.name}" port="${server.port}"/>
053: * <http url="${url}"/>
054: * <jmxEquals
055: * host="localhost" port="9014" username="controlRole" password="tomcat"
056: * name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
057: * attribute="connected" value="true"
058: * />
059: * </and>
060: * </waitfor>
061: * <fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" />
062: * <echo message="Server ${url} alive" />
063: * </target>
064: *
065: * </pre>
066: *
067: * @author Peter Rossbach
068: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
069: * @since 5.5.10
070: *
071: */
072: public class JMXAccessorEqualsCondition extends ProjectComponent
073: implements Condition {
074:
075: // ----------------------------------------------------- Instance Variables
076:
077: private String url = null;
078: private String host = "localhost";
079: private String port = "8050";
080: private String password = null;
081: private String username = null;
082: private String name = null;
083: private String attribute;
084: private String value;
085: private String ref = "jmx.server";
086: // ----------------------------------------------------- Instance Info
087:
088: /**
089: * Descriptive information describing this implementation.
090: */
091: private static final String info = "org.apache.catalina.ant.JMXAccessorEqualsCondition/1.1";
092:
093: /**
094: * Return descriptive information about this implementation and the
095: * corresponding version number, in the format
096: * <code><description>/<version></code>.
097: */
098: public String getInfo() {
099:
100: return (info);
101:
102: }
103:
104: // ----------------------------------------------------- Properties
105:
106: /**
107: * @return Returns the attribute.
108: */
109: public String getAttribute() {
110: return attribute;
111: }
112:
113: /**
114: * @param attribute The attribute to set.
115: */
116: public void setAttribute(String attribute) {
117: this .attribute = attribute;
118: }
119:
120: /**
121: * @return Returns the host.
122: */
123: public String getHost() {
124: return host;
125: }
126:
127: /**
128: * @param host The host to set.
129: */
130: public void setHost(String host) {
131: this .host = host;
132: }
133:
134: /**
135: * @return Returns the name.
136: */
137: public String getName() {
138: return name;
139: }
140:
141: /**
142: * @param objectName The name to set.
143: */
144: public void setName(String objectName) {
145: this .name = objectName;
146: }
147:
148: /**
149: * @return Returns the password.
150: */
151: public String getPassword() {
152: return password;
153: }
154:
155: /**
156: * @param password The password to set.
157: */
158: public void setPassword(String password) {
159: this .password = password;
160: }
161:
162: /**
163: * @return Returns the port.
164: */
165: public String getPort() {
166: return port;
167: }
168:
169: /**
170: * @param port The port to set.
171: */
172: public void setPort(String port) {
173: this .port = port;
174: }
175:
176: /**
177: * @return Returns the url.
178: */
179: public String getUrl() {
180: return url;
181: }
182:
183: /**
184: * @param url The url to set.
185: */
186: public void setUrl(String url) {
187: this .url = url;
188: }
189:
190: /**
191: * @return Returns the username.
192: */
193: public String getUsername() {
194: return username;
195: }
196:
197: /**
198: * @param username The username to set.
199: */
200: public void setUsername(String username) {
201: this .username = username;
202: }
203:
204: /**
205: * @return Returns the value.
206: */
207: public String getValue() {
208: return value;
209: }
210:
211: // The setter for the "value" attribute
212: public void setValue(String value) {
213: this .value = value;
214: }
215:
216: /**
217: * @return Returns the ref.
218: */
219: public String getRef() {
220: return ref;
221: }
222:
223: /**
224: * @param refId The ref to set.
225: */
226: public void setRef(String refId) {
227: this .ref = refId;
228: }
229:
230: protected MBeanServerConnection getJMXConnection()
231: throws MalformedURLException, IOException {
232: return JMXAccessorTask.accessJMXConnection(getProject(),
233: getUrl(), getHost(), getPort(), getUsername(),
234: getPassword(), ref);
235: }
236:
237: /**
238: * @return The value
239: */
240: protected String accessJMXValue() {
241: try {
242: Object result = getJMXConnection().getAttribute(
243: new ObjectName(name), attribute);
244: if (result != null)
245: return result.toString();
246: } catch (Exception e) {
247: // ignore access or connection open errors
248: }
249: return null;
250: }
251:
252: // This method evaluates the condition
253: public boolean eval() {
254: if (value == null) {
255: throw new BuildException("value attribute is not set");
256: }
257: if ((name == null || attribute == null)) {
258: throw new BuildException(
259: "Must specify a 'attribute', name for equals condition");
260: }
261: //FIXME check url or host/parameter
262: String jmxValue = accessJMXValue();
263: if (jmxValue != null)
264: return jmxValue.equals(value);
265: return false;
266: }
267: }
|