001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.ant.jmx;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import javax.management.MBeanServerConnection;
024: import javax.management.ObjectName;
025:
026: import org.apache.tools.ant.BuildException;
027:
028: /**
029: * Access <em>JMX</em> JSR 160 MBeans Server.
030: * <ul>
031: * <li>open more then one JSR 160 rmi connection</li>
032: * <li>Get/Set Mbeans attributes</li>
033: * <li>Call Mbean Operation with arguments</li>
034: * <li>Argument values can be converted from string to int,long,float,double,boolean,ObjectName or InetAddress </li>
035: * <li>Query Mbeans</li>
036: * <li>Show Get, Call, Query result at Ant console log</li>
037: * <li>Bind Get, Call, Query result at Ant properties</li>
038: * </ul>
039: *
040: * Examples:
041: * <ul>
042: * <li>
043: * Get a session attribute hello from session with ref <em>${sessionid.0}</em> form
044: * app <em>Catalina:type=Manager,path=/ClusterTest,host=localhost</em>
045: * <pre>
046: * <jmx:invoke
047: * name="Catalina:type=Manager,path=/ClusterTest,host=localhost"
048: * operation="getSessionAttribute"
049: * resultproperty="hello">
050: * <arg value="${sessionid.0}"/>
051: * <arg value="Hello"/>
052: * </jmx:invoke>
053: * </pre>
054: * </li>
055: * <li>
056: * Create new AccessLogger at localhost
057: * <code>
058: * <jmx:invoke
059: * name="Catalina:type=MBeanFactory"
060: * operation="createAcccesLoggerValve"
061: * resultproperty="acccesLoggerObjectName"
062: * >
063: * <arg value="Catalina:type=Host,host=localhost"/>
064: * </jmx:invoke>
065: *
066: * </code>
067: * </li>
068: * <li>
069: * Remove existing AccessLogger at localhost
070: * <code>
071: * <jmx:invoke
072: * name="Catalina:type=MBeanFactory"
073: * operation="removeValve"
074: * >
075: * <arg value="Catalina:type=Valve,name=AccessLogValve,host=localhost"/>
076: * </jmx:invoke>
077: *
078: * </code>
079: * </li>
080: * </ul>
081: * <p>
082: * First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
083: * </p>
084: * These tasks require Ant 1.6 or later interface.
085: *
086: * @author Peter Rossbach
087: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
088: * @since 5.5.10
089: */
090:
091: public class JMXAccessorInvokeTask extends JMXAccessorTask {
092:
093: // ----------------------------------------------------- Instance Variables
094:
095: private String operation;
096: private List args = new ArrayList();
097:
098: // ----------------------------------------------------- Instance Info
099:
100: /**
101: * Descriptive information describing this implementation.
102: */
103: private static final String info = "org.apache.catalina.ant.JMXAccessorInvokeTask/1.0";
104:
105: /**
106: * Return descriptive information about this implementation and the
107: * corresponding version number, in the format
108: * <code><description>/<version></code>.
109: */
110: public String getInfo() {
111:
112: return (info);
113:
114: }
115:
116: // ------------------------------------------------------------- Properties
117:
118: /**
119: * @return Returns the operation.
120: */
121: public String getOperation() {
122: return operation;
123: }
124:
125: /**
126: * @param operation The operation to set.
127: */
128: public void setOperation(String operation) {
129: this .operation = operation;
130: }
131:
132: public void addArg(Arg arg) {
133: args.add(arg);
134: }
135:
136: /**
137: * @return Returns the args.
138: */
139: public List getArgs() {
140: return args;
141: }
142:
143: /**
144: * @param args The args to set.
145: */
146: public void setArgs(List args) {
147: this .args = args;
148: }
149:
150: // ------------------------------------------------------ protected Methods
151:
152: /**
153: * Execute the specified command, based on the configured properties. The
154: * input stream will be closed upon completion of this task, whether it was
155: * executed successfully or not.
156: *
157: * @exception BuildException
158: * if an error occurs
159: */
160: public String jmxExecute(MBeanServerConnection jmxServerConnection)
161: throws Exception {
162:
163: if (getName() == null) {
164: throw new BuildException("Must specify a 'name'");
165: }
166: if ((operation == null)) {
167: throw new BuildException(
168: "Must specify a 'operation' for call");
169: }
170: return jmxInvoke(jmxServerConnection, getName());
171: }
172:
173: /**
174: * @param jmxServerConnection
175: * @throws Exception
176: */
177: protected String jmxInvoke(
178: MBeanServerConnection jmxServerConnection, String name)
179: throws Exception {
180: Object result;
181: if (args == null) {
182: result = jmxServerConnection.invoke(new ObjectName(name),
183: operation, null, null);
184: } else {
185: Object argsA[] = new Object[args.size()];
186: String sigA[] = new String[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
196: .getType());
197: }
198: }
199: result = jmxServerConnection.invoke(new ObjectName(name),
200: operation, argsA, sigA);
201: }
202: if (result != null) {
203: echoResult(operation, result);
204: createProperty(result);
205: }
206: return null;
207: }
208:
209: }
|