KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attribute JavaDoc;
23 import javax.management.MBeanAttributeInfo JavaDoc;
24 import javax.management.MBeanInfo JavaDoc;
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>Get Mbeans attributes</li>
35  * <li>Show Get result as Ant console log</li>
36  * <li>Bind Get result as Ant properties</li>
37  * </ul>
38  * <p>
39  * Examples:
40  * Set a Mbean Manager attribute maxActiveSessions.
41  * Set this attribute with fresh jmx connection without save reference
42  * <pre>
43  * &lt;jmx:set
44  * host="127.0.0.1"
45  * port="9014"
46  * ref=""
47  * name="Catalina:type=Manager,path="/ClusterTest",host=localhost"
48  * attribute="maxActiveSessions"
49  * value="100"
50  * type="int"
51  * echo="false"&gt;
52  * /&gt;
53  * </pre>
54  * </p>
55  * <p>
56  * First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
57  * </p>
58  * These tasks require Ant 1.6 or later interface.
59  *
60  * @author Peter Rossbach
61  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
62  * @since 5.5.10
63  */

64
65 public class JMXAccessorSetTask extends JMXAccessorTask {
66
67     // ----------------------------------------------------- Instance Variables
68

69     private String JavaDoc attribute;
70     private String JavaDoc value;
71     private String JavaDoc type;
72     private boolean convert = false ;
73     
74     // ----------------------------------------------------- Instance Info
75

76     /**
77      * Descriptive information describing this implementation.
78      */

79     private static final String JavaDoc info = "org.apache.catalina.ant.JMXAccessorSetTask/1.0";
80
81     /**
82      * Return descriptive information about this implementation and the
83      * corresponding version number, in the format
84      * <code>&lt;description&gt;/&lt;version&gt;</code>.
85      */

86     public String JavaDoc getInfo() {
87
88         return (info);
89
90     }
91
92     // ------------------------------------------------------------- Properties
93

94     /**
95      * @return Returns the attribute.
96      */

97     public String JavaDoc getAttribute() {
98         return attribute;
99     }
100     
101     /**
102      * @param attribute The attribute to set.
103      */

104     public void setAttribute(String JavaDoc attribute) {
105         this.attribute = attribute;
106     }
107     
108     /**
109      * @return Returns the value.
110      */

111     public String JavaDoc getValue() {
112         return value;
113     }
114     /**
115      * @param value The value to set.
116      */

117     public void setValue(String JavaDoc value) {
118         this.value = value;
119     }
120     
121     
122     /**
123      * @return Returns the type.
124      */

125     public String JavaDoc getType() {
126         return type;
127     }
128     
129     /**
130      * @param valueType The type to set.
131      */

132     public void setType(String JavaDoc valueType) {
133         this.type = valueType;
134     }
135  
136  
137     /**
138      * @return Returns the convert.
139      */

140     public boolean isConvert() {
141         return convert;
142     }
143     /**
144      * @param convert The convert to set.
145      */

146     public void setConvert(boolean convert) {
147         this.convert = convert;
148     }
149     // ------------------------------------------------------ protected Methods
150

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

159     public String JavaDoc jmxExecute(MBeanServerConnection JavaDoc jmxServerConnection)
160         throws Exception JavaDoc {
161
162         if (getName() == null) {
163             throw new BuildException("Must specify a 'name'");
164         }
165         if ((attribute == null || value == null)) {
166             throw new BuildException(
167                     "Must specify a 'attribute' and 'value' for set");
168         }
169         return jmxSet(jmxServerConnection, getName());
170      }
171
172     /**
173      * @param jmxServerConnection
174      * @param name
175      * @throws Exception
176      */

177     protected String JavaDoc jmxSet(MBeanServerConnection JavaDoc jmxServerConnection,
178             String JavaDoc name) throws Exception JavaDoc {
179         Object JavaDoc realValue;
180         if (type != null) {
181             realValue = convertStringToType(value, type);
182         } else {
183             if (isConvert()) {
184                 String JavaDoc mType = getMBeanAttributeType(jmxServerConnection, name,
185                         attribute);
186                 realValue = convertStringToType(value, mType);
187             } else
188                 realValue = value;
189         }
190         jmxServerConnection.setAttribute(new ObjectName JavaDoc(name), new Attribute JavaDoc(
191                 attribute, realValue));
192         return null;
193     }
194     
195
196
197     /**
198      * Get MBean Attriute from Mbean Server
199      * @param jmxServerConnection
200      * @param name
201      * @param attribute
202      * @return The type
203      * @throws Exception
204      */

205     protected String JavaDoc getMBeanAttributeType(
206             MBeanServerConnection JavaDoc jmxServerConnection,
207             String JavaDoc name,
208             String JavaDoc attribute) throws Exception JavaDoc {
209         ObjectName JavaDoc oname = new ObjectName JavaDoc(name);
210         String JavaDoc mattrType = null;
211         MBeanInfo JavaDoc minfo = jmxServerConnection.getMBeanInfo(oname);
212         MBeanAttributeInfo JavaDoc attrs[] = minfo.getAttributes();
213         if (attrs != null) {
214             for (int i = 0; mattrType == null && i < attrs.length; i++) {
215                 if (attribute.equals(attrs[i].getName()))
216                     mattrType = attrs[i].getType();
217             }
218         }
219         return mattrType;
220     }
221  }
222
Popular Tags