001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.ant.jmx;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import javax.management.MBeanServerConnection;
023: import javax.management.ObjectName;
024:
025: import org.apache.tools.ant.BuildException;
026:
027: /**
028: * Create new MBean at <em>JMX</em> JSR 160 MBeans Server.
029: * <ul>
030: * <li>Create Mbeans</li>
031: * <li>Create Mbeans with parameter</li>
032: * <li>Create remote Mbeans with different classloader</li>
033: * </ul>
034: * <p>
035: * Examples:
036: * <br/>
037: * create a new Mbean at jmx.server connection
038: * <pre>
039: * <jmx:create
040: * ref="jmx.server"
041: * name="Catalina:type=MBeanFactory"
042: * className="org.apache.catalina.mbeans.MBeanFactory"
043: * classLoader="Catalina:type=ServerClassLoader,name=server">
044: * <Arg value="org.apache.catalina.mbeans.MBeanFactory" />
045: * </jmxCreate/>
046: * </pre>
047: * </p>
048: * <p>
049: * <b>WARNING</b>Not all Tomcat MBeans can create remotely and autoregister by its parents!
050: * Please, use the MBeanFactory operation to generate valves and realms.
051: * </p>
052: * <p>
053: * First call to a remote MBeanserver save the JMXConnection a reference <em>jmx.server</em>
054: * </p>
055: * These tasks require Ant 1.6 or later interface.
056: *
057: * @author Peter Rossbach
058: * @version $Revision: 467222 $
059: * @since 5.5.12
060: */
061: public class JMXAccessorCreateTask extends JMXAccessorTask {
062: // ----------------------------------------------------- Instance Variables
063:
064: private String className;
065: private String classLoader;
066: private List args = new ArrayList();
067:
068: // ----------------------------------------------------- Instance Info
069:
070: /**
071: * Descriptive information describing this implementation.
072: */
073: private static final String info = "org.apache.catalina.ant.JMXAccessorCreateTask/1.0";
074:
075: /**
076: * Return descriptive information about this implementation and the
077: * corresponding version number, in the format
078: * <code><description>/<version></code>.
079: * @return Returns the class info.
080: */
081: public String getInfo() {
082:
083: return (info);
084:
085: }
086:
087: // ------------------------------------------------------------- Properties
088:
089: /**
090: * @return Returns the classLoader.
091: */
092: public String getClassLoader() {
093: return classLoader;
094: }
095:
096: /**
097: * @param classLoader The classLoader to set.
098: */
099: public void setClassLoader(String classLoaderName) {
100: this .classLoader = classLoaderName;
101: }
102:
103: /**
104: * @return Returns the className.
105: */
106: public String getClassName() {
107: return className;
108: }
109:
110: /**
111: * @param className The className to set.
112: */
113: public void setClassName(String 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 getArgs() {
125: return args;
126: }
127:
128: /**
129: * @param args The args to set.
130: */
131: public void setArgs(List args) {
132: this .args = args;
133: }
134:
135: // ------------------------------------------------------ protected Methods
136:
137: /**
138: * Execute the specified command, based on the configured properties. The
139: * input stream will be closed upon completion of this task, whether it was
140: * executed successfully or not.
141: *
142: * @exception Exception
143: * if an error occurs
144: */
145: public String jmxExecute(MBeanServerConnection jmxServerConnection)
146: throws Exception {
147:
148: if (getName() == null) {
149: throw new BuildException("Must specify a 'name'");
150: }
151: if ((className == null)) {
152: throw new BuildException(
153: "Must specify a 'className' for get");
154: }
155: return jmxCreate(jmxServerConnection, getName());
156: }
157:
158: /**
159: * create new Mbean and when set from ClassLoader Objectname
160: * @param jmxServerConnection
161: * @param name
162: * @return The value of the given named attribute
163: * @throws Exception
164: */
165: protected String jmxCreate(
166: MBeanServerConnection jmxServerConnection, String name)
167: throws Exception {
168: String error = null;
169: Object argsA[] = null;
170: String sigA[] = null;
171: if (args != null) {
172: argsA = new Object[args.size()];
173: sigA = new String[args.size()];
174: for (int i = 0; i < args.size(); i++) {
175: Arg arg = (Arg) args.get(i);
176: if (arg.type == null) {
177: arg.type = "java.lang.String";
178: sigA[i] = arg.getType();
179: argsA[i] = arg.getValue();
180: } else {
181: sigA[i] = arg.getType();
182: argsA[i] = convertStringToType(arg.getValue(), arg
183: .getType());
184: }
185: }
186: }
187: if (classLoader != null && !"".equals(classLoader)) {
188: if (isEcho()) {
189: handleOutput("create MBean " + name + " from class "
190: + className + " with classLoader "
191: + classLoader);
192: }
193: if (args == null)
194: jmxServerConnection.createMBean(className,
195: new ObjectName(name), new ObjectName(
196: classLoader));
197: else
198: jmxServerConnection.createMBean(className,
199: new ObjectName(name), new ObjectName(
200: classLoader), argsA, sigA);
201:
202: } else {
203: if (isEcho()) {
204: handleOutput("create MBean " + name + " from class "
205: + className);
206: }
207: if (args == null)
208: jmxServerConnection.createMBean(className,
209: new ObjectName(name));
210: else
211: jmxServerConnection.createMBean(className,
212: new ObjectName(name), argsA, sigA);
213: }
214: return error;
215: }
216:
217: }
|