KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.tools.ant.BuildException;
55
56
57 /** This is an Ant task that allows a JMX mbean to be created and the attributes of
58  * the new JMX mbean's to be changed or retrieved.</br</br>
59  *
60  * Refer to the user documentation for more information and examples on how to use
61  * this task.
62  *
63  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
64  * @version $Id: CreateMBeanTask.java,v 1.3 2003/05/26 10:13:05 bdueck Exp $
65  *
66  */

67 public class CreateMBeanTask extends ConfigureMBeanTask {
68     
69     private String JavaDoc ifExists = IfExists.FAIL;
70     private String JavaDoc type = null;
71     
72     public void setType(String JavaDoc type) {
73         this.type = type;
74     }
75     
76     private String JavaDoc getType() {
77         // if the Type property is ommitted, default using the from Object type
78
//
79
if (type == null) {
80             try {
81                 type = getObjectName().getKeyProperty("Type");
82             } catch (Exception JavaDoc eatMe) {
83                 // this exception can be safely and silently ignored
84
}
85         }
86         
87         return type;
88     }
89     
90     /**
91      * Sets the <code>IfExists</code> attribute.
92      * This attribute controls the behaviour of this task
93      * when an mbean with the same name as the target mbean
94      * already exists.
95      *
96      * @param ifExists The value for the ifExists attribute.
97      */

98     public void setIfExists(IfExists ifExists) {
99         this.ifExists = ifExists.getValue();
100     }
101           
102     /**
103      * Process all nested <createMBean> elements
104      *
105      * @param mbserver The MBeanServer that hosts the mbean.
106      * @throws BuildException When an error occurs.
107      */

108     protected void execute(javax.management.MBeanServer JavaDoc mbserver) throws BuildException {
109             
110         try {
111             javax.management.ObjectName JavaDoc mbeanName = getObjectName();
112
113             removeMBeanIfExists(mbserver,mbeanName,ifExists);
114
115             if (!mbserver.isRegistered(mbeanName)) {
116                 try {
117                     mbeanName = getJMXServer().createMBean(getType(),getObjectName(),mbserver);
118                     if (!mbserver.isRegistered(mbeanName)) {
119                         throw new BuildException("Cannot create MBean. [" + toString() + "]");
120                     }
121                 } catch (Exception JavaDoc ex) {
122                     throw new BuildException("Cannot create MBean. [" + toString() + "]",ex);
123                 }
124             } else {
125                 throw new BuildException("Cannot create MBean. [" + toString() + "]");
126             }
127         } catch (javax.management.MalformedObjectNameException JavaDoc x) {
128             throw new BuildException(x);
129         }
130
131         // to process setAttribute and getAttribute nested elements
132
//
133
super.execute(mbserver);
134     }
135     
136     
137 }
138
139 /*
140  * $Log: CreateMBeanTask.java,v $
141  * Revision 1.3 2003/05/26 10:13:05 bdueck
142  * *** empty log message ***
143  *
144  * Revision 1.2 2003/04/21 15:29:42 bdueck
145  * Various changes in preparation for version 1.2.
146  *
147  *
148  */

149
Popular Tags