KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jmx > ConfigureMBeanTask


1 package org.apache.tools.ant.taskdefs.optional.jmx;
2
3 /*
4  * ============================================================================
5  * The Apache Software License, Version 1.1
6  * ============================================================================
7  *
8  * Copyright (C) 2000-2002 The Apache Software Foundation. All
9  * rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modifica-
12  * tion, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any, must
22  * include the following acknowledgment: "This product includes software
23  * developed by the Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself, if
25  * and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Ant" and "Apache Software Foundation" must not be used to
28  * endorse or promote products derived from this software without prior
29  * written permission. For written permission, please contact
30  * apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache", nor may
33  * "Apache" appear in their name, without prior written permission of the
34  * Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
37  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
39  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
40  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
41  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * This software consists of voluntary contributions made by many individuals
48  * on behalf of the Apache Software Foundation. For more information on the
49  * Apache Software Foundation, please see <http://www.apache.org/>.
50  *
51  */

52
53
54
55 import java.util.Iterator JavaDoc;
56 import java.util.LinkedList JavaDoc;
57 import java.util.List JavaDoc;
58 import javax.management.Attribute JavaDoc;
59 import javax.management.MBeanAttributeInfo JavaDoc;
60 import org.apache.tools.ant.BuildException;
61 import org.apache.tools.ant.Project;
62 import org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory;
63
64
65
66 /**
67  * This is an Ant task that allows the attributes of a JMX mbean's to be changed
68  * or retrieved.
69  *
70  * Refer to the user documentation for more information and examples on how to use
71  * this task.
72  *
73  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
74  * @version $Id: ConfigureMBeanTask.java,v 1.4 2003/05/28 22:28:26 bdueck Exp $
75  *
76  */

77 public class ConfigureMBeanTask extends AbstractMBeanTask {
78     
79     private List JavaDoc setAttributeList = new LinkedList JavaDoc(); // List<SetAttribute>
80
private List JavaDoc getAttributeList = new LinkedList JavaDoc(); // List<GetAttribute>
81

82     /**
83      * Adds a nested <code>setAttribute</code> element.
84      *
85      * @param prop The nested setAttribute element.
86      */

87     public void addSetAttribute(SetAttribute prop) {
88         setAttributeList.add(prop);
89     }
90
91     /**
92      * Adds a nested <code>getAttribute</code> element.
93      *
94      * @param prop The nested getAttribute element.
95      */

96     public void addGetAttribute(GetAttribute prop) {
97         getAttributeList.add(prop);
98     }
99
100     /**
101      * Nested <code>setAttribute</code> task. Sets the specified attribute
102      * <code>name</code> to the specified <code>value</code>.
103      */

104     public static class SetAttribute {
105         private String JavaDoc name; // accessible from execute()
106
private StringBuffer JavaDoc value = new StringBuffer JavaDoc();
107
108         /**
109          * Sets the <code>name</code> attribute.
110          * @param name The name of the mbean attribute to set.
111          */

112         public void setName(String JavaDoc name) {
113             this.name = name;
114         }
115
116         /**
117          * Sets the <code>value</value> attribute.
118          * @param value The value for the mbean attribute.
119          */

120         public void setValue(String JavaDoc value) {
121             this.value.append(value);
122         }
123
124         /**
125          * The attribute name.
126          * @return The mbean attribute name
127          */

128         public String JavaDoc getName() {
129             return name;
130         }
131
132         public void addText(String JavaDoc text) {
133             this.value.append(text);
134         }
135         
136         public String JavaDoc getValue() {
137             return value.toString();
138         }
139     }
140
141     /**
142      * Nested <code>getAttribute</code> task. Retrieves the specified attribute
143      * <code>name</code> and assigns it to an ANT property <code>value</code>.
144      *
145      */

146     public static class GetAttribute {
147         String JavaDoc name; // accessible from execute()
148
String JavaDoc property;
149
150         /**
151          * Sets the <code>name</code> attribute.
152          *
153          * @param name The name of the mbean attribute to retrieve.
154          */

155         public void setName(String JavaDoc name) {
156             this.name = name;
157         }
158
159         /**
160          * Sets the <code>value</value> attribute.
161          *
162          * @param property The ANT property that will receive the value of the mbean attribute.
163          */

164         public void setProperty(String JavaDoc property) {
165             this.property = property;
166         }
167
168         /**
169          * Returns the mbean attribute name
170          *
171          */

172         public String JavaDoc getName() {
173             return name;
174         }
175         
176         /**
177          * Returns the Ant property - defaults to the attribute name.
178          *
179          * @returns The Ant property name.
180          */

181         public String JavaDoc getProperty() {
182             if ( (property == null) || (property.equals("")) ) {
183                 return name;
184             }
185             return property;
186         }
187     }
188
189     
190     /**
191      * Process a <configureMBean> element.
192      *
193      * @param mbserver The MBeanServer that hosts the mbean.
194      * @throws BuildException When an error occurs.
195      */

196     protected void execute(javax.management.MBeanServer JavaDoc mbserver) throws BuildException {
197             
198         try {
199             javax.management.ObjectName JavaDoc mbeanName = getObjectName();
200
201             if (mbserver.isRegistered(mbeanName)) {
202
203                 java.util.Map JavaDoc attributeInfo = getAttributes(mbserver.getMBeanInfo(mbeanName));
204                 
205                 // process setAttribute nested elements
206
//
207
executeForSetAttribute(mbserver,attributeInfo);
208
209                 // process getAttribute nested elements
210
//
211
executeForGetAttribute(mbserver);
212
213             } else {
214                 throw new BuildException("Cannot find MBean. " + toString());
215             }
216         } catch (javax.management.InstanceNotFoundException JavaDoc x) {
217             throw new BuildException(x);
218         } catch (javax.management.IntrospectionException JavaDoc x) {
219             throw new BuildException(x);
220         } catch (javax.management.ReflectionException JavaDoc x) {
221             throw new BuildException(x);
222         } catch (javax.management.MalformedObjectNameException JavaDoc x) {
223             throw new BuildException(x);
224         }
225     }
226
227     /**
228      * Process each nested SetAttribute element.
229      * @param mbserver The MBeanServer being targetd.
230      * @param attributeInfo Information about the attributes for the mbean.
231      */

232     protected void executeForSetAttribute(javax.management.MBeanServer JavaDoc mbserver,
233         java.util.Map JavaDoc attributeInfo) {
234         Iterator JavaDoc setAttrIt = setAttributeList.iterator();
235                     
236         while (setAttrIt.hasNext()) {
237             SetAttribute attr = (SetAttribute) setAttrIt.next();
238
239             try {
240                 MBeanAttributeInfo JavaDoc attrInfo = (MBeanAttributeInfo JavaDoc) attributeInfo.get(attr.getName());
241
242                 Object JavaDoc value = ValueFactory.getInstance().valueOf(getProject().replaceProperties(attr.getValue()),attrInfo.getType());
243
244                 Attribute JavaDoc attribute = new Attribute JavaDoc(attrInfo.getName(),value);
245
246                 mbserver.setAttribute(getObjectName(),attribute);
247                             
248             } catch (Exception JavaDoc x) {
249                 x.printStackTrace();
250                 String JavaDoc message = "Cannot set mbean attribute value. attributeName["+attr.getName()+"], attributeProperty["+attr.value+"], " + this.toString();
251                 if (getFailOnError()) {
252                     throw new BuildException(message);
253                 } else {
254                     log("Warning: " + message,Project.MSG_WARN);
255                 }
256             }
257         }
258     }
259     
260     /**
261      * Process each nested GetAttribute element.
262      * @param mbserver The MBeanServer being targeted.
263      */

264     protected void executeForGetAttribute(javax.management.MBeanServer JavaDoc mbserver) {
265         Iterator JavaDoc getAttrIt = getAttributeList.iterator();
266         while (getAttrIt.hasNext()) {
267             GetAttribute attr = (GetAttribute) getAttrIt.next();
268             
269             try {
270                 Object JavaDoc value = mbserver.getAttribute(getObjectName(),attr.getName());
271                 if (value != null) {
272                     getProject().setProperty(attr.getProperty(),ValueFactory.toString(value));
273                 } else {
274                     getProject().setProperty(attr.getProperty(),"");
275                 }
276             } catch (Exception JavaDoc x) {
277                 String JavaDoc message = "Cannot get mbean attribute value. attributeName["+attr.getName()+"], property["+attr.getProperty()+"], " + toString();
278                 if (getFailOnError()) {
279                     throw new BuildException(message);
280                 } else {
281                     log("Warning: " + message,Project.MSG_WARN);
282                 }
283             }
284
285         }
286     }
287     
288 }
289
290 /*
291  * $Log: ConfigureMBeanTask.java,v $
292  * Revision 1.4 2003/05/28 22:28:26 bdueck
293  * *** empty log message ***
294  *
295  * Revision 1.3 2003/04/21 15:29:41 bdueck
296  * Various changes in preparation for version 1.2.
297  *
298  *
299  */
Popular Tags