1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.catalina.ant.jmx;
19
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22
23 import javax.management.MBeanServerConnection;
24 import javax.management.ObjectName;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.ProjectComponent;
28 import org.apache.tools.ant.taskdefs.condition.Condition;
29
30 /**
31 *
32 * Definition
33 * <pre>
34 * <path id="catalina_ant">
35 * <fileset dir="${catalina.home}/server/lib">
36 * <include name="catalina-ant.jar"/>
37 * </fileset>
38 * </path>
39 *
40 * <typedef
41 * name="jmxEquals"
42 * classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition"
43 * classpathref="catalina_ant"/>
44 * </pre>
45 *
46 * usage: Wait for start backup node
47 * <pre>
48 * <target name="wait">
49 * <waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" >
50 * <and>
51 * <socket server="${server.name}" port="${server.port}"/>
52 * <http url="${url}"/>
53 * <jmxEquals
54 * host="localhost" port="9014" username="controlRole" password="tomcat"
55 * name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
56 * attribute="connected" value="true"
57 * />
58 * </and>
59 * </waitfor>
60 * <fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" />
61 * <echo message="Server ${url} alive" />
62 * </target>
63 *
64 * </pre>
65 *
66 * @author Peter Rossbach
67 * @version $Revision: 612599 $ $Date: 2008-01-16 23:37:36 +0100 (mer., 16 janv. 2008) $
68 * @since 5.5.10
69 *
70 */
71 public class JMXAccessorEqualsCondition extends ProjectComponent implements Condition {
72
73 // ----------------------------------------------------- Instance Variables
74
75 private String url = null;
76 private String host = "localhost";
77 private String port = "8050";
78 private String password = null;
79 private String username = null;
80 private String name = null;
81 private String attribute;
82 private String value;
83 private String ref = "jmx.server" ;
84 // ----------------------------------------------------- Instance Info
85
86 /**
87 * Descriptive information describing this implementation.
88 */
89 private static final String info = "org.apache.catalina.ant.JMXAccessorEqualsCondition/1.1";
90
91 /**
92 * Return descriptive information about this implementation and the
93 * corresponding version number, in the format
94 * <code><description>/<version></code>.
95 */
96 public String getInfo() {
97
98 return (info);
99
100 }
101 // ----------------------------------------------------- Properties
102
103 /**
104 * @return Returns the attribute.
105 */
106 public String getAttribute() {
107 return attribute;
108 }
109 /**
110 * @param attribute The attribute to set.
111 */
112 public void setAttribute(String attribute) {
113 this.attribute = attribute;
114 }
115 /**
116 * @return Returns the host.
117 */
118 public String getHost() {
119 return host;
120 }
121 /**
122 * @param host The host to set.
123 */
124 public void setHost(String host) {
125 this.host = host;
126 }
127 /**
128 * @return Returns the name.
129 */
130 public String getName() {
131 return name;
132 }
133 /**
134 * @param objectName The name to set.
135 */
136 public void setName(String objectName) {
137 this.name = objectName;
138 }
139 /**
140 * @return Returns the password.
141 */
142 public String getPassword() {
143 return password;
144 }
145 /**
146 * @param password The password to set.
147 */
148 public void setPassword(String password) {
149 this.password = password;
150 }
151 /**
152 * @return Returns the port.
153 */
154 public String getPort() {
155 return port;
156 }
157 /**
158 * @param port The port to set.
159 */
160 public void setPort(String port) {
161 this.port = port;
162 }
163 /**
164 * @return Returns the url.
165 */
166 public String getUrl() {
167 return url;
168 }
169 /**
170 * @param url The url to set.
171 */
172 public void setUrl(String url) {
173 this.url = url;
174 }
175 /**
176 * @return Returns the username.
177 */
178 public String getUsername() {
179 return username;
180 }
181 /**
182 * @param username The username to set.
183 */
184 public void setUsername(String username) {
185 this.username = username;
186 }
187 /**
188 * @return Returns the value.
189 */
190 public String getValue() {
191 return value;
192 }
193 // The setter for the "value" attribute
194 public void setValue(String value) {
195 this.value = value;
196 }
197
198 /**
199 * @return Returns the ref.
200 */
201 public String getRef() {
202 return ref;
203 }
204 /**
205 * @param refId The ref to set.
206 */
207 public void setRef(String refId) {
208 this.ref = refId;
209 }
210
211 protected MBeanServerConnection getJMXConnection()
212 throws MalformedURLException, IOException {
213 return JMXAccessorTask.accessJMXConnection(
214 getProject(),
215 getUrl(), getHost(),
216 getPort(), getUsername(), getPassword(), ref);
217 }
218
219 /**
220 * @return The value
221 */
222 protected String accessJMXValue() {
223 try {
224 Object result = getJMXConnection().getAttribute(
225 new ObjectName(name), attribute);
226 if(result != null)
227 return result.toString();
228 } catch (Exception e) {
229 // ignore access or connection open errors
230 }
231 return null;
232 }
233
234 // This method evaluates the condition
235 public boolean eval() {
236 if (value == null) {
237 throw new BuildException("value attribute is not set");
238 }
239 if ((name == null || attribute == null)) {
240 throw new BuildException(
241 "Must specify a 'attribute', name for equals condition");
242 }
243 //FIXME check url or host/parameter
244 String jmxValue = accessJMXValue();
245 if(jmxValue != null)
246 return jmxValue.equals(value);
247 return false;
248 }
249 }
250