KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Definition
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="jmxEquals"
43  * classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition"
44  * classpathref="catalina_ant"/>
45  * </pre>
46  *
47  * usage: Wait for start backup node
48  * <pre>
49  * &lt;target name="wait"&gt;
50  * &lt;waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" &gt;
51  * &lt;and&gt;
52  * &lt;socket server="${server.name}" port="${server.port}"/&gt;
53  * &lt;http url="${url}"/&gt;
54  * &lt;jmxEquals
55  * host="localhost" port="9014" username="controlRole" password="tomcat"
56  * name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
57  * attribute="connected" value="true"
58  * /&gt;
59  * &lt;/and&gt;
60  * &lt;/waitfor&gt;
61  * &lt;fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" /&gt;
62  * &lt;echo message="Server ${url} alive" /&gt;
63  * &lt;/target&gt;
64  *
65  * </pre>
66  *
67  * @author Peter Rossbach
68  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
69  * @since 5.5.10
70  *
71  */

72 public class JMXAccessorEqualsCondition extends ProjectComponent implements Condition {
73
74     // ----------------------------------------------------- Instance Variables
75

76     private String JavaDoc url = null;
77     private String JavaDoc host = "localhost";
78     private String JavaDoc port = "8050";
79     private String JavaDoc password = null;
80     private String JavaDoc username = null;
81     private String JavaDoc name = null;
82     private String JavaDoc attribute;
83     private String JavaDoc value;
84     private String JavaDoc ref = "jmx.server" ;
85     // ----------------------------------------------------- Instance Info
86

87     /**
88      * Descriptive information describing this implementation.
89      */

90     private static final String JavaDoc info = "org.apache.catalina.ant.JMXAccessorEqualsCondition/1.1";
91
92     /**
93      * Return descriptive information about this implementation and the
94      * corresponding version number, in the format
95      * <code>&lt;description&gt;/&lt;version&gt;</code>.
96      */

97     public String JavaDoc getInfo() {
98
99         return (info);
100
101     }
102     // ----------------------------------------------------- Properties
103

104     /**
105      * @return Returns the attribute.
106      */

107     public String JavaDoc getAttribute() {
108         return attribute;
109     }
110     /**
111      * @param attribute The attribute to set.
112      */

113     public void setAttribute(String JavaDoc attribute) {
114         this.attribute = attribute;
115     }
116     /**
117      * @return Returns the host.
118      */

119     public String JavaDoc getHost() {
120         return host;
121     }
122     /**
123      * @param host The host to set.
124      */

125     public void setHost(String JavaDoc host) {
126         this.host = host;
127     }
128     /**
129      * @return Returns the name.
130      */

131     public String JavaDoc getName() {
132         return name;
133     }
134     /**
135      * @param objectName The name to set.
136      */

137     public void setName(String JavaDoc objectName) {
138         this.name = objectName;
139     }
140     /**
141      * @return Returns the password.
142      */

143     public String JavaDoc getPassword() {
144         return password;
145     }
146     /**
147      * @param password The password to set.
148      */

149     public void setPassword(String JavaDoc password) {
150         this.password = password;
151     }
152     /**
153      * @return Returns the port.
154      */

155     public String JavaDoc getPort() {
156         return port;
157     }
158     /**
159      * @param port The port to set.
160      */

161     public void setPort(String JavaDoc port) {
162         this.port = port;
163     }
164     /**
165      * @return Returns the url.
166      */

167     public String JavaDoc getUrl() {
168         return url;
169     }
170     /**
171      * @param url The url to set.
172      */

173     public void setUrl(String JavaDoc url) {
174         this.url = url;
175     }
176     /**
177      * @return Returns the username.
178      */

179     public String JavaDoc getUsername() {
180         return username;
181     }
182     /**
183      * @param username The username to set.
184      */

185     public void setUsername(String JavaDoc username) {
186         this.username = username;
187     }
188     /**
189      * @return Returns the value.
190      */

191     public String JavaDoc getValue() {
192         return value;
193     }
194     // The setter for the "value" attribute
195
public void setValue(String JavaDoc value) {
196         this.value = value;
197     }
198
199     /**
200      * @return Returns the ref.
201      */

202     public String JavaDoc getRef() {
203         return ref;
204     }
205     /**
206      * @param refId The ref to set.
207      */

208     public void setRef(String JavaDoc refId) {
209         this.ref = refId;
210     }
211     
212     protected MBeanServerConnection JavaDoc getJMXConnection()
213             throws MalformedURLException JavaDoc, IOException JavaDoc {
214         return JMXAccessorTask.accessJMXConnection(
215                 getProject(),
216                 getUrl(), getHost(),
217                 getPort(), getUsername(), getPassword(), ref);
218     }
219
220     /**
221      * @return The value
222      */

223     protected String JavaDoc accessJMXValue() {
224         try {
225             Object JavaDoc result = getJMXConnection().getAttribute(
226                     new ObjectName JavaDoc(name), attribute);
227             if(result != null)
228                 return result.toString();
229         } catch (Exception JavaDoc e) {
230             // ignore access or connection open errors
231
}
232         return null;
233     }
234
235     // This method evaluates the condition
236
public boolean eval() {
237         if (value == null) {
238             throw new BuildException("value attribute is not set");
239         }
240         if ((name == null || attribute == null)) {
241             throw new BuildException(
242                     "Must specify a 'attribute', name for equals condition");
243         }
244         //FIXME check url or host/parameter
245
String JavaDoc jmxValue = accessJMXValue();
246         if(jmxValue != null)
247             return jmxValue.equals(value);
248         return false;
249     }
250 }
251
252
Popular Tags