KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 package org.apache.catalina.ant.jmx;
20
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import org.apache.tools.ant.BuildException;
29
30
31 /**
32  * Access <em>JMX</em> JSR 160 MBeans Server.
33  * <ul>
34  * <li>open more then one JSR 160 rmi connection</li>
35  * <li>Get/Set Mbeans attributes</li>
36  * <li>Call Mbean Operation with arguments</li>
37  * <li>Argument values can be converted from string to int,long,float,double,boolean,ObjectName or InetAddress </li>
38  * <li>Query Mbeans</li>
39  * <li>Show Get, Call, Query result at Ant console log</li>
40  * <li>Bind Get, Call, Query result at Ant properties</li>
41  * </ul>
42  *
43  * Examples:
44  * <ul>
45  * <li>
46  * Get a session attribute hello from session with ref <em>${sessionid.0}</em> form
47  * app <em>Catalina:type=Manager,path=/ClusterTest,host=localhost</em>
48  * <pre>
49  * &lt;jmx:invoke
50  * name="Catalina:type=Manager,path=/ClusterTest,host=localhost"
51  * operation="getSessionAttribute"
52  * resultproperty="hello"&gt;
53  * &lt;arg value="${sessionid.0}"/&gt;
54  * &lt;arg value="Hello"/&gt;
55  * &lt;/jmx:invoke&gt;
56  * </pre>
57  * </li>
58  * <li>
59  * Create new AccessLogger at localhost
60  * <code>
61  * &lt;jmx:invoke
62  * name="Catalina:type=MBeanFactory"
63  * operation="createAcccesLoggerValve"
64  * resultproperty="acccesLoggerObjectName"
65  * &gt;
66  * &lt;arg value="Catalina:type=Host,host=localhost"/&gt;
67  * &lt;/jmx:invoke&gt;
68  *
69  * </code>
70  * </li>
71  * <li>
72  * Remove existing AccessLogger at localhost
73  * <code>
74  * &lt;jmx:invoke
75  * name="Catalina:type=MBeanFactory"
76  * operation="removeValve"
77  * &gt;
78  * &lt;arg value="Catalina:type=Valve,name=AccessLogValve,host=localhost"/&gt;
79  * &lt;/jmx:invoke&gt;
80  *
81  * </code>
82  * </li>
83  * </ul>
84  * <p>
85  * First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
86  * </p>
87  * These tasks require Ant 1.6 or later interface.
88  *
89  * @author Peter Rossbach
90  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
91  * @since 5.5.10
92  */

93
94 public class JMXAccessorInvokeTask extends JMXAccessorTask {
95
96
97     // ----------------------------------------------------- Instance Variables
98

99     private String JavaDoc operation ;
100     private List JavaDoc args=new ArrayList JavaDoc();
101
102     // ----------------------------------------------------- Instance Info
103

104     /**
105      * Descriptive information describing this implementation.
106      */

107     private static final String JavaDoc info = "org.apache.catalina.ant.JMXAccessorInvokeTask/1.0";
108
109     /**
110      * Return descriptive information about this implementation and the
111      * corresponding version number, in the format
112      * <code>&lt;description&gt;/&lt;version&gt;</code>.
113      */

114     public String JavaDoc getInfo() {
115
116         return (info);
117
118     }
119
120     // ------------------------------------------------------------- Properties
121

122     /**
123      * @return Returns the operation.
124      */

125     public String JavaDoc getOperation() {
126         return operation;
127     }
128     /**
129      * @param operation The operation to set.
130      */

131     public void setOperation(String JavaDoc operation) {
132         this.operation = operation;
133     }
134
135     public void addArg(Arg arg ) {
136         args.add(arg);
137     }
138
139     /**
140      * @return Returns the args.
141      */

142     public List JavaDoc getArgs() {
143         return args;
144     }
145     /**
146      * @param args The args to set.
147      */

148     public void setArgs(List JavaDoc args) {
149         this.args = args;
150     }
151     
152     // ------------------------------------------------------ protected Methods
153

154     /**
155      * Execute the specified command, based on the configured properties. The
156      * input stream will be closed upon completion of this task, whether it was
157      * executed successfully or not.
158      *
159      * @exception BuildException
160      * if an error occurs
161      */

162     public String JavaDoc jmxExecute(MBeanServerConnection JavaDoc jmxServerConnection)
163         throws Exception JavaDoc {
164
165         if (getName() == null) {
166             throw new BuildException("Must specify a 'name'");
167         }
168         if ((operation == null)) {
169             throw new BuildException(
170                     "Must specify a 'operation' for call");
171         }
172         return jmxInvoke(jmxServerConnection, getName());
173      }
174
175     /**
176      * @param jmxServerConnection
177      * @throws Exception
178      */

179     protected String JavaDoc jmxInvoke(MBeanServerConnection JavaDoc jmxServerConnection, String JavaDoc name) throws Exception JavaDoc {
180         Object JavaDoc result ;
181         if (args == null) {
182              result = jmxServerConnection.invoke(new ObjectName JavaDoc(name),
183                     operation, null, null);
184         } else {
185             Object JavaDoc argsA[]=new Object JavaDoc[ args.size()];
186             String JavaDoc sigA[]=new String JavaDoc[args.size()];
187             for( int i=0; i<args.size(); i++ ) {
188                 Arg arg=(Arg)args.get(i);
189                 if( arg.type==null) {
190                     arg.type="java.lang.String";
191                     sigA[i]=arg.getType();
192                     argsA[i]=arg.getValue();
193                 } else {
194                     sigA[i]=arg.getType();
195                     argsA[i]=convertStringToType(arg.getValue(),arg.getType());
196                 }
197             }
198             result = jmxServerConnection.invoke(new ObjectName JavaDoc(name), operation, argsA, sigA);
199         }
200         if(result != null) {
201             echoResult(operation,result);
202             createProperty(result);
203         }
204         return null;
205     }
206
207 }
208
Popular Tags