KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ant > jmx > JMXAccessorCondition


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 JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22
23 import javax.management.MBeanServerConnection JavaDoc;
24 import javax.management.ObjectName JavaDoc;
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  * <b>Definition</b>:
33  * <pre>
34  * &lt;path id="catalina_ant">
35  * &lt;fileset dir="${catalina.home}/server/lib">
36  * &lt;include name="catalina-ant.jar"/>
37  * &lt;include name="catalina-ant-jmx.jar"/>
38  * &lt;/fileset>
39  * &lt;/path>
40  *
41  * &lt;typedef
42  * name="jmxCondition"
43  * classname="org.apache.catalina.ant.jmx.JMXAccessorCondition"
44  * classpathref="catalina_ant"/>
45  * &lt;taskdef
46  * name="jmxOpen"
47  * classname="org.apache.catalina.ant.jmx.JMXAccessorTask"
48  * classpathref="catalina_ant"/>
49  * </pre>
50  *
51  * <b>Usage</b>: Wait for start backup node
52  * <pre>
53  * &lt;target name="wait"&gt;
54  * &lt;jmxOpen
55  * host="${jmx.host}" port="${jmx.port}" username="${jmx.username}" password="${jmx.password}" /&gt;
56  * &lt;waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" &gt;
57  * &lt;and&gt;
58  * &lt;socket server="${server.name}" port="${server.port}"/&gt;
59  * &lt;http url="${url}"/&gt;
60  * &lt;jmxCondition
61  * name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
62  * operation="=="
63  * attribute="connected" value="true"
64  * /&gt;
65  * &lt;jmxCondition
66  * operation="&amp;lt;"
67  * name="Catalina:j2eeType=WebModule,name=//${tomcat.application.host}${tomcat.application.path},J2EEApplication=none,J2EEServer=none"
68  * attribute="startupTime" value="250"
69  * /&gt;
70  * &lt;/and&gt;
71  * &lt;/waitfor&gt;
72  * &lt;fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" /&gt;
73  * &lt;echo message="Server ${url} alive" /&gt;
74  * &lt;/target&gt;
75  *
76  * </pre>
77  * Allowed operation between jmx attribute and reference value:
78  * <ul>
79  * <li>== equals</li>
80  * <li>!= not equals</li>
81  * <li>&gt; greater than (&amp;gt;)</li>
82  * <li>&gt;= greater than or equals (&amp;gt;=)</li>
83  * <li>&lt; lesser than (&amp;lt;)</li>
84  * <li>&lt;= lesser than or equals (&amp;lt;=)</li>
85  * </ul>
86  * <b>NOTE</b>: For numeric expressions the type must be set and use xml entities as operations.<br/>
87  * As type we currently support <em>long</em> and <em>double</em>.
88  * @author Peter Rossbach
89  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
90  * @since 5.5.10
91  *
92  */

93 public class JMXAccessorCondition extends ProjectComponent implements Condition {
94
95     // ----------------------------------------------------- Instance Variables
96

97     private String JavaDoc url = null;
98     private String JavaDoc host = "localhost";
99     private String JavaDoc port = "8050";
100     private String JavaDoc password = null;
101     private String JavaDoc username = null;
102     private String JavaDoc name = null;
103     private String JavaDoc attribute;
104     private String JavaDoc value;
105     private String JavaDoc operation = "==" ;
106     private String JavaDoc type = "long" ;
107     private String JavaDoc ref = "jmx.server";
108     private String JavaDoc unlessCondition;
109     private String JavaDoc ifCondition;
110      
111     // ----------------------------------------------------- Instance Info
112

113     /**
114      * Descriptive information describing this implementation.
115      */

116     private static final String JavaDoc info = "org.apache.catalina.ant.JMXAccessorCondition/1.1";
117
118     /**
119      * Return descriptive information about this implementation and the
120      * corresponding version number, in the format
121      * <code>&lt;description&gt;/&lt;version&gt;</code>.
122      */

123     public String JavaDoc getInfo() {
124
125         return (info);
126
127     }
128     // ----------------------------------------------------- Properties
129

130     /**
131      * @return Returns the operation.
132      */

133     public String JavaDoc getOperation() {
134         return operation;
135     }
136     /**
137      * @param operation The operation to set.
138      */

139     public void setOperation(String JavaDoc operation) {
140         this.operation = operation;
141     }
142
143     /**
144      * @return Returns the type.
145      */

146     public String JavaDoc getType() {
147         return type;
148     }
149     /**
150      * @param type The type to set.
151      */

152     public void setType(String JavaDoc type) {
153         this.type = type;
154     }
155     /**
156      * @return Returns the attribute.
157      */

158     public String JavaDoc getAttribute() {
159         return attribute;
160     }
161     /**
162      * @param attribute The attribute to set.
163      */

164     public void setAttribute(String JavaDoc attribute) {
165         this.attribute = attribute;
166     }
167     /**
168      * @return Returns the host.
169      */

170     public String JavaDoc getHost() {
171         return host;
172     }
173     /**
174      * @param host The host to set.
175      */

176     public void setHost(String JavaDoc host) {
177         this.host = host;
178     }
179     /**
180      * @return Returns the name.
181      */

182     public String JavaDoc getName() {
183         return name;
184     }
185     /**
186      * @param objectName The name to set.
187      */

188     public void setName(String JavaDoc objectName) {
189         this.name = objectName;
190     }
191     /**
192      * @return Returns the password.
193      */

194     public String JavaDoc getPassword() {
195         return password;
196     }
197     /**
198      * @param password The password to set.
199      */

200     public void setPassword(String JavaDoc password) {
201         this.password = password;
202     }
203     /**
204      * @return Returns the port.
205      */

206     public String JavaDoc getPort() {
207         return port;
208     }
209     /**
210      * @param port The port to set.
211      */

212     public void setPort(String JavaDoc port) {
213         this.port = port;
214     }
215     /**
216      * @return Returns the url.
217      */

218     public String JavaDoc getUrl() {
219         return url;
220     }
221     /**
222      * @param url The url to set.
223      */

224     public void setUrl(String JavaDoc url) {
225         this.url = url;
226     }
227     /**
228      * @return Returns the username.
229      */

230     public String JavaDoc getUsername() {
231         return username;
232     }
233     /**
234      * @param username The username to set.
235      */

236     public void setUsername(String JavaDoc username) {
237         this.username = username;
238     }
239     /**
240      * @return Returns the value.
241      */

242     public String JavaDoc getValue() {
243         return value;
244     }
245     // The setter for the "value" attribute
246
public void setValue(String JavaDoc value) {
247         this.value = value;
248     }
249  
250     /**
251      * @return Returns the ref.
252      */

253     public String JavaDoc getRef() {
254         return ref;
255     }
256     /**
257      * @param refId The ref to set.
258      */

259     public void setRef(String JavaDoc refId) {
260         this.ref = refId;
261     }
262     /**
263      * @return Returns the ifCondition.
264      */

265     public String JavaDoc getIf() {
266         return ifCondition;
267     }
268     /**
269      * Only execute if a property of the given name exists in the current project.
270      * @param c property name
271      */

272     public void setIf(String JavaDoc c) {
273         ifCondition = c;
274     }
275    /**
276      * @return Returns the unlessCondition.
277      */

278     public String JavaDoc getUnless() {
279         return unlessCondition;
280     }
281  
282     /**
283      * Only execute if a property of the given name does not
284      * exist in the current project.
285      * @param c property name
286      */

287     public void setUnless(String JavaDoc c) {
288         unlessCondition = c;
289     }
290
291     /**
292      * Get JMXConnection (default look at <em>jmx.server</em> project reference from jmxOpen Task)
293      * @return active JMXConnection
294      * @throws MalformedURLException
295      * @throws IOException
296      */

297     protected MBeanServerConnection JavaDoc getJMXConnection()
298             throws MalformedURLException JavaDoc, IOException JavaDoc {
299         return JMXAccessorTask.accessJMXConnection(
300                 getProject(),
301                 getUrl(), getHost(),
302                 getPort(), getUsername(), getPassword(), ref);
303     }
304
305     /**
306      * Get value from MBeans attribute
307      * @return The value
308      */

309     protected String JavaDoc accessJMXValue() {
310         try {
311             Object JavaDoc result = getJMXConnection().getAttribute(
312                     new ObjectName JavaDoc(name), attribute);
313             if(result != null)
314                 return result.toString();
315         } catch (Exception JavaDoc e) {
316             // ignore access or connection open errors
317
}
318         return null;
319     }
320
321     /**
322      * test the if condition
323      * @return true if there is no if condition, or the named property exists
324      */

325     protected boolean testIfCondition() {
326         if (ifCondition == null || "".equals(ifCondition)) {
327             return true;
328         }
329         return getProject().getProperty(ifCondition) != null;
330     }
331
332     /**
333      * test the unless condition
334      * @return true if there is no unless condition,
335      * or there is a named property but it doesn't exist
336      */

337     protected boolean testUnlessCondition() {
338         if (unlessCondition == null || "".equals(unlessCondition)) {
339             return true;
340         }
341         return getProject().getProperty(unlessCondition) == null;
342     }
343
344     /**
345      * This method evaluates the condition
346      * It support for operation ">,>=,<,<=" the types <code>long</code> and <code>double</code>.
347      * @return expression <em>jmxValue</em> <em>operation</em> <em>value</em>
348      */

349     public boolean eval() {
350         if (operation == null) {
351             throw new BuildException("operation attribute is not set");
352         }
353         if (value == null) {
354             throw new BuildException("value attribute is not set");
355         }
356         if ((name == null || attribute == null)) {
357             throw new BuildException(
358                     "Must specify a 'attribute', name for equals condition");
359         }
360         if (testIfCondition() && testUnlessCondition()) {
361             String JavaDoc jmxValue = accessJMXValue();
362             if (jmxValue != null) {
363                 String JavaDoc op = getOperation();
364                 if ("==".equals(op)) {
365                     return jmxValue.equals(value);
366                 } else if ("!=".equals(op)) {
367                     return !jmxValue.equals(value);
368                 } else {
369                     if ("long".equals(type)) {
370                         long jvalue = Long.parseLong(jmxValue);
371                         long lvalue = Long.parseLong(value);
372                         if (">".equals(op)) {
373                             return jvalue > lvalue;
374                         } else if (">=".equals(op)) {
375                             return jvalue >= lvalue;
376                         } else if ("<".equals(op)) {
377                             return jvalue < lvalue;
378                         } else if ("<=".equals(op)) {
379                             return jvalue <= lvalue;
380                         }
381                     } else if ("double".equals(type)) {
382                         double jvalue = Double.parseDouble(jmxValue);
383                         double dvalue = Double.parseDouble(value);
384                         if (">".equals(op)) {
385                             return jvalue > dvalue;
386                         } else if (">=".equals(op)) {
387                             return jvalue >= dvalue;
388                         } else if ("<".equals(op)) {
389                             return jvalue < dvalue;
390                         } else if ("<=".equals(op)) {
391                             return jvalue <= dvalue;
392                         }
393                     }
394                 }
395             }
396             return false;
397         }
398         return true;
399     }
400  }
401
402
Popular Tags