KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.catalina.ant.jmx;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.management.MBeanServerConnection JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26
27 /**
28  * Create new MBean at <em>JMX</em> JSR 160 MBeans Server.
29  * <ul>
30  * <li>Create Mbeans</li>
31  * <li>Create Mbeans with parameter</li>
32  * <li>Create remote Mbeans with different classloader</li>
33  * </ul>
34  * <p>
35  * Examples:
36  * <br/>
37  * create a new Mbean at jmx.server connection
38  * <pre>
39  * &lt;jmx:create
40  * ref="jmx.server"
41  * name="Catalina:type=MBeanFactory"
42  * className="org.apache.catalina.mbeans.MBeanFactory"
43  * classLoader="Catalina:type=ServerClassLoader,name=server"&gt;
44  * &lt;Arg value="org.apache.catalina.mbeans.MBeanFactory" /&gt;
45  * &lt;/jmxCreate/&gt;
46  * </pre>
47  * </p>
48  * <p>
49  * <b>WARNING</b>Not all Tomcat MBeans can create remotely and autoregister by its parents!
50  * Please, use the MBeanFactory operation to generate valves and realms.
51  * </p>
52  * <p>
53  * First call to a remote MBeanserver save the JMXConnection a reference <em>jmx.server</em>
54  * </p>
55  * These tasks require Ant 1.6 or later interface.
56  *
57  * @author Peter Rossbach
58  * @version $Revision: 467222 $
59  * @since 5.5.12
60  */

61 public class JMXAccessorCreateTask extends JMXAccessorTask {
62     // ----------------------------------------------------- Instance Variables
63

64     private String JavaDoc className;
65     private String JavaDoc classLoader;
66     private List JavaDoc args=new ArrayList JavaDoc();
67
68     // ----------------------------------------------------- Instance Info
69

70     /**
71      * Descriptive information describing this implementation.
72      */

73     private static final String JavaDoc info = "org.apache.catalina.ant.JMXAccessorCreateTask/1.0";
74
75     /**
76      * Return descriptive information about this implementation and the
77      * corresponding version number, in the format
78      * <code>&lt;description&gt;/&lt;version&gt;</code>.
79      * @return Returns the class info.
80      */

81     public String JavaDoc getInfo() {
82
83         return (info);
84
85     }
86
87     // ------------------------------------------------------------- Properties
88

89     /**
90      * @return Returns the classLoader.
91      */

92     public String JavaDoc getClassLoader() {
93         return classLoader;
94     }
95     
96     /**
97      * @param classLoader The classLoader to set.
98      */

99     public void setClassLoader(String JavaDoc classLoaderName) {
100         this.classLoader = classLoaderName;
101     }
102     
103     /**
104      * @return Returns the className.
105      */

106     public String JavaDoc getClassName() {
107         return className;
108     }
109     
110     /**
111      * @param className The className to set.
112      */

113     public void setClassName(String JavaDoc className) {
114         this.className = className;
115     }
116     
117     public void addArg(Arg arg ) {
118         args.add(arg);
119     }
120
121     /**
122      * @return Returns the args.
123      */

124     public List JavaDoc getArgs() {
125         return args;
126     }
127     /**
128      * @param args The args to set.
129      */

130     public void setArgs(List JavaDoc args) {
131         this.args = args;
132     }
133
134     // ------------------------------------------------------ protected Methods
135

136     /**
137      * Execute the specified command, based on the configured properties. The
138      * input stream will be closed upon completion of this task, whether it was
139      * executed successfully or not.
140      *
141      * @exception Exception
142      * if an error occurs
143      */

144     public String JavaDoc jmxExecute(MBeanServerConnection JavaDoc jmxServerConnection)
145         throws Exception JavaDoc {
146
147         if (getName() == null) {
148             throw new BuildException("Must specify a 'name'");
149         }
150         if ((className == null)) {
151             throw new BuildException(
152                     "Must specify a 'className' for get");
153         }
154         return jmxCreate(jmxServerConnection, getName());
155      }
156     
157     /**
158      * create new Mbean and when set from ClassLoader Objectname
159      * @param jmxServerConnection
160      * @param name
161      * @return The value of the given named attribute
162      * @throws Exception
163      */

164     protected String JavaDoc jmxCreate(MBeanServerConnection JavaDoc jmxServerConnection,
165             String JavaDoc name) throws Exception JavaDoc {
166         String JavaDoc error = null;
167         Object JavaDoc argsA[] = null;
168         String JavaDoc sigA[] = null;
169         if (args != null) {
170            argsA = new Object JavaDoc[ args.size()];
171            sigA = new String JavaDoc[args.size()];
172            for( int i=0; i<args.size(); i++ ) {
173                Arg arg=(Arg)args.get(i);
174                if( arg.type==null) {
175                    arg.type="java.lang.String";
176                    sigA[i]=arg.getType();
177                    argsA[i]=arg.getValue();
178                } else {
179                    sigA[i]=arg.getType();
180                    argsA[i]=convertStringToType(arg.getValue(),arg.getType());
181                }
182            }
183         }
184         if (classLoader != null && !"".equals(classLoader)) {
185             if (isEcho()) {
186                 handleOutput("create MBean " + name + " from class "
187                         + className + " with classLoader " + classLoader);
188             }
189             if(args == null)
190                 jmxServerConnection.createMBean(className, new ObjectName JavaDoc(name), new ObjectName JavaDoc(classLoader));
191             else
192                 jmxServerConnection.createMBean(className, new ObjectName JavaDoc(name), new ObjectName JavaDoc(classLoader),argsA,sigA);
193                 
194         } else {
195             if (isEcho()) {
196                 handleOutput("create MBean " + name + " from class "
197                         + className);
198             }
199             if(args == null)
200                 jmxServerConnection.createMBean(className, new ObjectName JavaDoc(name));
201             else
202                 jmxServerConnection.createMBean(className, new ObjectName JavaDoc(name),argsA,sigA);
203         }
204         return error;
205     }
206     
207 }
208
Popular Tags