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 javax.management.MBeanServerConnection;
021: import javax.management.ObjectName;
022:
023: import org.apache.tools.ant.BuildException;
024:
025: /**
026: * Access <em>JMX</em> JSR 160 MBeans Server.
027: * <ul>
028: * <li>Get Mbeans attributes</li>
029: * <li>Show Get result as Ant console log</li>
030: * <li>Bind Get result as Ant properties</li>
031: * </ul>
032: * <p>
033: * Examples:
034: * <br/>
035: * Get a Mbean IDataSender attribute nrOfRequests and create a new ant property <em>IDataSender.9025.nrOfRequests</em>
036: * <pre>
037: * <jmx:get
038: * ref="jmx.server"
039: * name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.1.2,senderPort=9025"
040: * attribute="nrOfRequests"
041: * resultproperty="IDataSender.9025.nrOfRequests"
042: * echo="false">
043: * />
044: * </pre>
045: * </p>
046: * <p>
047: * First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
048: * </p>
049: * These tasks require Ant 1.6 or later interface.
050: *
051: * @author Peter Rossbach
052: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
053: * @since 5.5.10
054: */
055:
056: public class JMXAccessorGetTask extends JMXAccessorTask {
057:
058: // ----------------------------------------------------- Instance Variables
059:
060: private String attribute;
061:
062: // ----------------------------------------------------- Instance Info
063:
064: /**
065: * Descriptive information describing this implementation.
066: */
067: private static final String info = "org.apache.catalina.ant.JMXAccessorGetTask/1.0";
068:
069: /**
070: * Return descriptive information about this implementation and the
071: * corresponding version number, in the format
072: * <code><description>/<version></code>.
073: */
074: public String getInfo() {
075:
076: return (info);
077:
078: }
079:
080: // ------------------------------------------------------------- Properties
081:
082: /**
083: * @return Returns the attribute.
084: */
085: public String getAttribute() {
086: return attribute;
087: }
088:
089: /**
090: * @param attribute The attribute to set.
091: */
092: public void setAttribute(String attribute) {
093: this .attribute = attribute;
094: }
095:
096: // ------------------------------------------------------ protected Methods
097:
098: /**
099: * Execute the specified command, based on the configured properties. The
100: * input stream will be closed upon completion of this task, whether it was
101: * executed successfully or not.
102: *
103: * @exception BuildException
104: * if an error occurs
105: */
106: public String jmxExecute(MBeanServerConnection jmxServerConnection)
107: throws Exception {
108:
109: if (getName() == null) {
110: throw new BuildException("Must specify a 'name'");
111: }
112: if ((attribute == null)) {
113: throw new BuildException(
114: "Must specify a 'attribute' for get");
115: }
116: return jmxGet(jmxServerConnection, getName());
117: }
118:
119: /**
120: * @param jmxServerConnection
121: * @param name
122: * @return The value of the given named attribute
123: * @throws Exception
124: */
125: protected String jmxGet(MBeanServerConnection jmxServerConnection,
126: String name) throws Exception {
127: String error = null;
128: if (isEcho()) {
129: handleOutput("MBean " + name + " get attribute "
130: + attribute);
131: }
132: Object result = jmxServerConnection.getAttribute(
133: new ObjectName(name), attribute);
134: if (result != null) {
135: echoResult(attribute, result);
136: createProperty(result);
137: } else
138: error = "Attribute " + attribute + " is empty";
139: return error;
140: }
141: }
|