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:
018: package org.apache.commons.modeler.ant;
019:
020: import java.net.URL;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.management.JMException;
025: import javax.management.MBeanServer;
026: import javax.management.MBeanServerFactory;
027: import javax.management.ObjectName;
028: import javax.management.loading.MLet;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.tools.ant.BuildException;
033: import org.apache.tools.ant.Task;
034:
035: /**
036: * Load an MBean. The syntax is similar with the <mlet>, with few
037: * ant-specific extensions.
038: *
039: * A separate classloader can be used, the mechanism is similar with
040: * what taskdef is using.
041: *
042: * Note that mlet will use the arguments in the constructor.
043: *
044: *
045: */
046: public class MLETTask extends Task {
047: private static Log log = LogFactory.getLog(MLETTask.class);
048: String code;
049: String archive;
050: String codebase;
051: String objectName;
052: ObjectName oname;
053:
054: List args = new ArrayList();
055: List attributes = new ArrayList();
056:
057: // ant specific
058: String loaderRef; // class loader ref
059:
060: public MLETTask() {
061: }
062:
063: public void addArg(Arg arg) {
064: args.add(arg);
065: }
066:
067: public void addAttribute(JmxSet arg) {
068: attributes.add(arg);
069: }
070:
071: public void setCode(String code) {
072: this .code = code;
073: }
074:
075: public void setArchive(String archive) {
076: this .archive = archive;
077: }
078:
079: public void setCodebase(String codebase) {
080: this .codebase = codebase;
081: }
082:
083: public void setName(String name) {
084: this .objectName = name;
085: }
086:
087: MBeanServer server;
088:
089: public MBeanServer getMBeanServer() {
090: if (server != null)
091: return server;
092:
093: server = (MBeanServer) project.getReference("jmx.server");
094:
095: if (server != null)
096: return server;
097:
098: try {
099: if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
100: server = (MBeanServer) MBeanServerFactory
101: .findMBeanServer(null).get(0);
102: } else {
103: server = MBeanServerFactory.createMBeanServer();
104:
105: // Register a loader that will be find ant classes.
106: ObjectName defaultLoader = new ObjectName(
107: "modeler-ant", "loader", "ant");
108: MLet mlet = new MLet(new URL[0], this .getClass()
109: .getClassLoader());
110: server.registerMBean(mlet, defaultLoader);
111:
112: if (log.isDebugEnabled())
113: log.debug("Creating mbean server and loader "
114: + mlet + " "
115: + this .getClass().getClassLoader());
116: }
117: project.addReference("jmx.server", server);
118:
119: // Create the MLet object
120: } catch (JMException ex) {
121: log.error("Error creating server", ex);
122: }
123:
124: if (log.isDebugEnabled())
125: log.debug("Using Mserver " + server);
126:
127: return server;
128: }
129:
130: boolean modeler = false;
131:
132: public void setModeler(boolean modeler) {
133: this .modeler = modeler;
134: }
135:
136: protected void bindJmx(String objectName, String code, String arg0,
137: List args) throws Exception {
138: MBeanServer server = getMBeanServer();
139: oname = new ObjectName(objectName);
140: if (modeler) {
141: Arg codeArg = new Arg();
142: codeArg.setType("java.lang.String");
143: codeArg.setValue(code);
144: if (args == null)
145: args = new ArrayList();
146: args.add(0, codeArg);
147: code = "org.apache.commons.modeler.BaseModelMBean";
148: }
149:
150: Object argsA[] = new Object[args.size()];
151: String sigA[] = new String[args.size()];
152: for (int i = 0; i < args.size(); i++) {
153: Arg arg = (Arg) args.get(i);
154: if (arg.type == null)
155: arg.type = "java.lang.String";
156: sigA[i] = arg.getType();
157: argsA[i] = arg.getValue();
158: // XXX Deal with not string types - IntrospectionUtils
159: }
160:
161: // XXX Use the loader ref, if any
162: if (args.size() == 0) {
163: server.createMBean(code, oname);
164: } else {
165: server.createMBean(code, oname, argsA, sigA);
166: }
167: log.debug("Created MBEAN " + oname + " " + code);
168: }
169:
170: public ObjectName getObjectName() {
171: return oname;
172: }
173:
174: public void execute() throws BuildException {
175: try {
176: // create the mbean
177: bindJmx(objectName, code, null, args);
178:
179: // process attributes
180: for (int i = 0; i < attributes.size(); i++) {
181: JmxSet att = (JmxSet) attributes.get(i);
182: att.setObjectName(oname);
183: log.info("Setting attribute " + oname + " "
184: + att.getName());
185: att.execute();
186: }
187: } catch (Exception ex) {
188: log.error("Can't create mbean " + objectName, ex);
189: }
190: }
191:
192: }
|