KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jmx > InvokeMBeanTask


1 package org.apache.tools.ant.taskdefs.optional.jmx;
2
3 /*
4  * ============================================================================
5  * The Apache Software License, Version 1.1
6  * ============================================================================
7  *
8  * Copyright (C) 2000-2002 The Apache Software Foundation. All
9  * rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modifica-
12  * tion, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any, must
22  * include the following acknowledgment: "This product includes software
23  * developed by the Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself, if
25  * and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Ant" and "Apache Software Foundation" must not be used to
28  * endorse or promote products derived from this software without prior
29  * written permission. For written permission, please contact
30  * apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache", nor may
33  * "Apache" appear in their name, without prior written permission of the
34  * Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
37  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
39  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
40  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
41  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * This software consists of voluntary contributions made by many individuals
48  * on behalf of the Apache Software Foundation. For more information on the
49  * Apache Software Foundation, please see <http://www.apache.org/>.
50  *
51  */

52
53
54
55 import javax.management.MBeanInfo JavaDoc;
56 import javax.management.MBeanOperationInfo JavaDoc;
57 import javax.management.MBeanParameterInfo JavaDoc;
58 import org.apache.tools.ant.BuildException;
59 import org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory;
60
61
62 /**
63  * This is an Ant task that allows the operations of a JMX mbean's to be invoked</br</br>
64  *
65  * Refer to the user documentation for more information and examples on how to use
66  * this task.
67  *
68  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
69  * @version $Id: InvokeMBeanTask.java,v 1.6 2003/05/28 22:28:26 bdueck Exp $
70  *
71  */

72 public class InvokeMBeanTask extends AbstractMBeanTask {
73     
74     private String JavaDoc operation;
75     private String JavaDoc resultProperty;
76     
77     private java.util.List JavaDoc parameterList = new java.util.LinkedList JavaDoc(); // List<Parameter>
78

79     public void setOperation(String JavaDoc operation) {
80         this.operation = operation;
81     }
82
83     /**
84      * Sets the name of the property to store the result (return value) of the opearation.
85      * This is an optional property, if ommitted, the value will be stored in a property with the name
86      * <operation>.result
87      */

88     public void setResultProperty(String JavaDoc resultProperty) {
89         this.resultProperty = resultProperty;
90     }
91     
92     /**
93      * Returns the name of the Ant property the method result will be stored in.
94      * See setResultProperty() for more information.
95      *
96      */

97     public String JavaDoc getResultProperty() {
98         if ( (resultProperty == null) || (resultProperty.equals("")) ) {
99             return operation+".result";
100         }
101         else {
102             return resultProperty;
103         }
104     }
105     
106     /**
107      * Adds a nested <code>parameter</code> element.
108      *
109      * @param prop The nested parameter element.
110      *
111      */

112     public void addParameter(Parameter param) {
113         parameterList.add(param);
114     }
115
116     /**
117      * Nested <code>parameter</code> task. Sets the specified attribute
118      * <code>name</code> to the specified <code>value</code>.
119      *
120      */

121     public static class Parameter {
122         private String JavaDoc type;
123         private StringBuffer JavaDoc value = new StringBuffer JavaDoc();
124
125         /**
126          * Sets the <code>value</code> attribute.
127          * @param value The value for the mbean operation parameter.
128          */

129         public void setValue(String JavaDoc value) {
130             this.value.append(value);
131         }
132         
133         public void addText(String JavaDoc text) {
134             this.value.append(text);
135         }
136         
137         public String JavaDoc getValue() {
138             return value.toString();
139         }
140
141         /**
142          * Sets the <code>type</code> attribute.
143          * @param type The class name for the parameter. If type is not
144          * provided, no type checking is performed.
145          */

146         public void setType(String JavaDoc type) {
147             this.type = type;
148         }
149
150         public String JavaDoc getType() {
151             return type;
152         }
153     }
154
155                 
156     /**
157      * Find the MBeanAttributeInfo for the indicated attribute name.
158      *
159      * @returns The attribute info value or null if the named attribute cannot be
160      * found.
161      */

162     public MBeanParameterInfo JavaDoc getParameterInfo(MBeanOperationInfo JavaDoc opInfo, String JavaDoc parameterName) {
163         MBeanParameterInfo JavaDoc[] params = opInfo.getSignature();
164         for (int counter = 0; counter < params.length; counter++) {
165             if (params[counter].getName().equals(parameterName)) {
166                 return params[counter];
167             }
168         }
169         return null;
170     }
171
172     /**
173      * Finds the MBeanOperationInfo for the indicated operation name.
174      */

175     public MBeanOperationInfo JavaDoc getOperationInfo(MBeanInfo JavaDoc beanInfo, String JavaDoc operationName, java.util.List JavaDoc parameterList) {
176         MBeanOperationInfo JavaDoc[] operations = beanInfo.getOperations();
177         for (int counter = 0; counter < operations.length; counter++) {
178             if (operations[counter].getName().equals(operationName)) {
179                 if (checkSignature(operations[counter], parameterList)) {
180                     return operations[counter];
181                 }
182             }
183         }
184         return null;
185     }
186    
187     /**
188      * Compares the number of parameters required for an MBean operation and
189      * the parameter types against the values provided in
190      * <code>parameterList</code>. If no type is specified for a parameter in
191      * <code>parameterList</code>, no type checking is done.
192      *
193      * @returns true if parameterList is valid for the given operation,
194      * otherwise false.
195      */

196     public boolean checkSignature(MBeanOperationInfo JavaDoc operation, java.util.List JavaDoc parameterList) {
197         MBeanParameterInfo JavaDoc[] paramInfos = operation.getSignature();
198         if (paramInfos.length != parameterList.size()) {
199             return false;
200         }
201         java.util.Iterator JavaDoc paramIt = parameterList.iterator();
202         for (int counter = 0; counter < paramInfos.length; counter++) {
203             Parameter param = (Parameter) paramIt.next();
204             if (param.getType() != null && ! param.getType().equals(paramInfos[counter].getType())) {
205                 return false;
206             }
207         }
208         return true;
209     }
210  
211     /**
212      * Process an <invokeMBean> element by invoking a method on a JMX MBean.
213      *
214      * @param mbserver The MBeanServer that hosts the mbean.
215      * @throws BuildException When an error occurs.
216      */

217     protected void execute(javax.management.MBeanServer JavaDoc mbserver) throws BuildException {
218             
219         try {
220             javax.management.ObjectName JavaDoc mbeanName = getObjectName();
221
222             if (mbserver.isRegistered(mbeanName)) {
223                 MBeanInfo JavaDoc info = mbserver.getMBeanInfo(mbeanName);
224
225                 MBeanOperationInfo JavaDoc operation = getOperationInfo(info,this.operation, this.parameterList);
226                 if (operation != null) {
227                     
228                     Object JavaDoc[] paramValues = new Object JavaDoc[parameterList.size()];
229                     String JavaDoc[] paramTypes = new String JavaDoc[parameterList.size()];
230                     
231                     MBeanParameterInfo JavaDoc[] paramInfo = operation.getSignature();
232                     
233                     // must have same number of params
234
//
235
if (paramInfo.length == parameterList.size()) {
236                         java.util.Iterator JavaDoc paramIt = parameterList.iterator();
237                         for (int counter = 0; counter < paramInfo.length; counter++) {
238                             
239                             Parameter param = (Parameter) paramIt.next();
240
241                             paramTypes[counter] = paramInfo[counter].getType();
242                             paramValues[counter] = ValueFactory.getInstance().valueOf(getProject().replaceProperties(param.getValue()),paramInfo[counter].getType());
243                         }
244
245                         Object JavaDoc result = mbserver.invoke(mbeanName,operation.getName(),paramValues,paramTypes);
246                         if (result != null) {
247                             getProject().setProperty(this.getResultProperty(),ValueFactory.toString(result));
248                         } else {
249                             getProject().setProperty(this.getResultProperty(),"");
250                         }
251                     } else {
252                         String JavaDoc message = "Parameter count mismatch. operation["+this.operation+"] requires["+paramInfo.length+"] parameters, but received["+this.parameterList.size()+"] for mbean " + toString();
253                         throw new BuildException(message);
254                     }
255                     
256                 } else {
257                     String JavaDoc message = "Cannot find operation["+this.operation+"] for mbean " + toString() + " for the given parameters";
258                     throw new BuildException(message);
259                 }
260
261             } else {
262                 throw new BuildException("Cannot find MBean. " + toString());
263             }
264         } catch (BuildException x) {
265             throw x;
266         } catch (Exception JavaDoc x) {
267             throw new BuildException(x);
268         }
269     }
270     
271 }
272
273 /*
274  * $Log: InvokeMBeanTask.java,v $
275  * Revision 1.6 2003/05/28 22:28:26 bdueck
276  * *** empty log message ***
277  *
278  * Revision 1.5 2003/05/26 09:34:31 bdueck
279  * Added optional type attribute to the <parameter> element of <invokeMBean>.
280  * This allows parameters to be specified in any order (if type is supplied), and
281  * also allows for MBeans with overloaded methods to be invoked.
282  * This contribution was submitted by John Vasileff - many thanks!
283  *
284  * Revision 1.4 2003/05/26 09:18:39 bdueck
285  * Updated comments
286  *
287  *
288  */
Popular Tags