KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
56 import javax.management.MalformedObjectNameException JavaDoc;
57 import javax.management.ObjectName JavaDoc;
58 import org.apache.tools.ant.BuildException;
59
60
61
62 /**
63  * This is an Ant task that allows a JMX mbean and all its attributes to be copied
64  * to a new domain, or mbean name.</br</br>
65  *
66  * Refer to the user documentation for more information and examples on how to use
67  * this task.
68  *
69  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
70  * @version $Id: CopyMBeanTask.java,v 1.3 2003/05/26 10:13:05 bdueck Exp $
71  *
72  */

73 public class CopyMBeanTask extends AbstractMBeanTask {
74     
75     private String JavaDoc toName;
76     private String JavaDoc ifExists = IfExists.FAIL;
77     private String JavaDoc type = null;
78         
79     /**
80      * Sets the <code>toName</code> attribute.
81      *
82      * @param toName The name of the new mbean.
83      */

84     public void setToName(String JavaDoc toName) {
85         this.toName = toName;
86     }
87
88     public void setType(String JavaDoc type) {
89         this.type = type;
90     }
91     
92     /**
93      * Sets the <code>IfExists</code> attribute.
94      * This attribute controls the behaviour of the copyMBean task
95      * when an mbean with the same name as the target mbean
96      * already exists.
97      *
98      * @param ifExists The value for the ifExists attribute.
99      */

100     public void setIfExists(IfExists ifExists) {
101         this.ifExists = ifExists.getValue();
102     }
103
104     /**
105      * Returns the target MBean name.
106      */

107     private String JavaDoc getToName() {
108         return toName;
109     }
110
111     /**
112      * Helper function to try to extract the mbean "Type" from the
113      * ObjectName.
114      *
115      * @return The value of Type attribute
116      */

117     private String JavaDoc getType() {
118         // if the Type property is ommitted, default using the from Object type
119
//
120
if (type == null) {
121             try {
122                 ObjectName JavaDoc toObjectName = new ObjectName JavaDoc(getToName());
123                 type = toObjectName.getKeyProperty("Type");
124             } catch (Exception JavaDoc eatMe) {
125                 // an exception may be thrown and safely ignored
126
// as the Type attribute may be missing from the ObjectName
127

128             }
129             
130             if (type == null) {
131                 try {
132                     ObjectName JavaDoc fromObjectName = getObjectName();
133                     type = fromObjectName.getKeyProperty("Type");
134                 } catch (Exception JavaDoc eatMe) {
135                     // an exception may be thrown and safely ignored
136
// as the Type attribute may be missing from the ObjectName
137
}
138             }
139         }
140         return type;
141     }
142     
143     /**
144      * Returns the MBean ObjectName based on the toName.
145      *
146      */

147     protected ObjectName JavaDoc getToObjectName() throws MalformedObjectNameException JavaDoc {
148         ObjectName JavaDoc toObjectName = new ObjectName JavaDoc(getToName());
149         
150         StringBuffer JavaDoc tempToObjectName = new StringBuffer JavaDoc(getToName());
151         ObjectName JavaDoc fromObjectName = getObjectName();
152         
153         // if the domain value is ommitted, default using the from Object domain
154
//
155
if ((toObjectName.getDomain() == null) || (toObjectName.getDomain().length() == 0)) {
156         
157             tempToObjectName.insert(0,fromObjectName.getDomain());
158         }
159         return new ObjectName JavaDoc(tempToObjectName.toString());
160     }
161
162     
163     /**
164      * Process all nested <copyMBeans> elements
165      *
166      * @param mbserver The MBeanServer that hosts the mbean.
167      * @throws BuildException When an error occurs.
168      */

169     protected void execute(javax.management.MBeanServer JavaDoc mbserver) throws BuildException {
170             
171         try {
172             ObjectName JavaDoc mbeanName = getObjectName();
173
174             if (mbserver.isRegistered(mbeanName)) {
175
176                 ObjectName JavaDoc toMBeanName = getToObjectName();
177                 
178                 removeMBeanIfExists(mbserver,toMBeanName,ifExists);
179
180                 if (!mbserver.isRegistered(toMBeanName)) {
181                     try {
182                         toMBeanName = getJMXServer().createMBean(getType(),getToObjectName(),mbserver);
183                         if (!mbserver.isRegistered(mbeanName)) {
184                             throw new BuildException("Cannot create MBean. " + toString());
185                         }
186                     } catch (Exception JavaDoc ex) {
187                         String JavaDoc message = "Cannot create toMBean. " + toMBeanName;
188                         throw new BuildException(message,ex);
189                     }
190
191                 }
192
193                 try {
194                     javax.management.MBeanAttributeInfo JavaDoc[] info = mbserver.getMBeanInfo(mbeanName).getAttributes();
195                     ArrayList JavaDoc attributeNames = new ArrayList JavaDoc();
196                     for (int counter = 0; counter < info.length; counter++) {
197                         // skip non-writeable attributes
198
//
199
if (info[counter].isWritable()) {
200                             attributeNames.add(info[counter].getName());
201                         }
202                     }
203                     
204                     String JavaDoc[] nameArray = new String JavaDoc[attributeNames.size()];
205                     java.util.Iterator JavaDoc nameIt = attributeNames.iterator();
206                     int counter = 0;
207                     while (nameIt.hasNext()) {
208                         nameArray[counter] = (String JavaDoc)nameIt.next();
209                         counter++;
210                     }
211                     javax.management.AttributeList JavaDoc attributeList = mbserver.getAttributes(mbeanName,nameArray);
212
213                     mbserver.setAttributes(toMBeanName,attributeList);
214
215                     // special handling for "Name" attribute
216
// silently ignore errors in case "Name" doesn't exist
217
//
218
try {
219                         mbserver.setAttribute(toMBeanName,new javax.management.Attribute JavaDoc("Name",getToObjectName().getKeyProperty("Name")));
220                     } catch (Exception JavaDoc eatMe) {
221                         // silently ignore this error
222
}
223
224                 } catch (Exception JavaDoc x) {
225                     String JavaDoc message = "Cannot copy mbean attributes to new mbean " + toMBeanName;
226                     throw new BuildException(message,x);
227                 }
228
229             } else {
230                 throw new BuildException("Cannot find MBean. " + toString());
231             }
232             
233         } catch (MalformedObjectNameException JavaDoc x) {
234             throw new BuildException(x);
235         }
236     }
237     
238 }
239
240 /*
241  * $Log: CopyMBeanTask.java,v $
242  * Revision 1.3 2003/05/26 10:13:05 bdueck
243  * *** empty log message ***
244  *
245  * Revision 1.2 2003/04/21 15:29:42 bdueck
246  * Various changes in preparation for version 1.2.
247  *
248  *
249  */

250  
Popular Tags