KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.MBeanServerConnection JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26
27
28 /**
29  * Access <em>JMX</em> JSR 160 MBeans Server.
30  * <ul>
31  * <li>Get Mbeans attributes</li>
32  * <li>Show Get result as Ant console log</li>
33  * <li>Bind Get result as Ant properties</li>
34  * </ul>
35  * <p>
36  * Examples:
37  * <br/>
38  * Get a Mbean IDataSender attribute nrOfRequests and create a new ant property <em>IDataSender.9025.nrOfRequests</em>
39  * <pre>
40  * &lt;jmx:get
41  * ref="jmx.server"
42  * name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.1.2,senderPort=9025"
43  * attribute="nrOfRequests"
44  * resultproperty="IDataSender.9025.nrOfRequests"
45  * echo="false"&gt;
46  * /&gt;
47  * </pre>
48  * </p>
49  * <p>
50  * First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
51  * </p>
52  * These tasks require Ant 1.6 or later interface.
53  *
54  * @author Peter Rossbach
55  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
56  * @since 5.5.10
57  */

58
59 public class JMXAccessorGetTask extends JMXAccessorTask {
60
61
62     // ----------------------------------------------------- Instance Variables
63

64     private String JavaDoc attribute;
65
66     // ----------------------------------------------------- Instance Info
67

68     /**
69      * Descriptive information describing this implementation.
70      */

71     private static final String JavaDoc info = "org.apache.catalina.ant.JMXAccessorGetTask/1.0";
72
73     /**
74      * Return descriptive information about this implementation and the
75      * corresponding version number, in the format
76      * <code>&lt;description&gt;/&lt;version&gt;</code>.
77      */

78     public String JavaDoc getInfo() {
79
80         return (info);
81
82     }
83
84     // ------------------------------------------------------------- Properties
85

86     /**
87      * @return Returns the attribute.
88      */

89     public String JavaDoc getAttribute() {
90         return attribute;
91     }
92     
93     /**
94      * @param attribute The attribute to set.
95      */

96     public void setAttribute(String JavaDoc attribute) {
97         this.attribute = attribute;
98     }
99     
100   
101     // ------------------------------------------------------ protected Methods
102

103     /**
104      * Execute the specified command, based on the configured properties. The
105      * input stream will be closed upon completion of this task, whether it was
106      * executed successfully or not.
107      *
108      * @exception BuildException
109      * if an error occurs
110      */

111     public String JavaDoc jmxExecute(MBeanServerConnection JavaDoc jmxServerConnection)
112         throws Exception JavaDoc {
113
114         if (getName() == null) {
115             throw new BuildException("Must specify a 'name'");
116         }
117         if ((attribute == null)) {
118             throw new BuildException(
119                     "Must specify a 'attribute' for get");
120         }
121         return jmxGet(jmxServerConnection, getName());
122      }
123
124
125     /**
126      * @param jmxServerConnection
127      * @param name
128      * @return The value of the given named attribute
129      * @throws Exception
130      */

131     protected String JavaDoc jmxGet(MBeanServerConnection JavaDoc jmxServerConnection,String JavaDoc name) throws Exception JavaDoc {
132         String JavaDoc error = null;
133         if(isEcho()) {
134             handleOutput("MBean " + name + " get attribute " + attribute );
135         }
136         Object JavaDoc result = jmxServerConnection.getAttribute(
137                 new ObjectName JavaDoc(name), attribute);
138         if (result != null) {
139             echoResult(attribute,result);
140             createProperty(result);
141         } else
142             error = "Attribute " + attribute + " is empty";
143         return error;
144     }
145 }
146
Popular Tags