KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > ant > MLETTask


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.modeler.ant;
18
19 import java.net.URL JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.management.JMException JavaDoc;
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.MBeanServerFactory JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.loading.MLet JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Task;
33
34 /**
35  * Load an MBean. The syntax is similar with the <mlet>, with few
36  * ant-specific extensions.
37  *
38  * A separate classloader can be used, the mechanism is similar with
39  * what taskdef is using.
40  *
41  * Note that mlet will use the arguments in the constructor.
42  *
43  *
44  */

45 public class MLETTask extends Task {
46     private static Log log = LogFactory.getLog(MLETTask.class);
47     String JavaDoc code;
48     String JavaDoc archive;
49     String JavaDoc codebase;
50     String JavaDoc objectName;
51     ObjectName JavaDoc oname;
52
53     List JavaDoc args=new ArrayList JavaDoc();
54     List JavaDoc attributes=new ArrayList JavaDoc();
55
56     // ant specific
57
String JavaDoc loaderRef; // class loader ref
58

59     public MLETTask() {
60     }
61
62     public void addArg(Arg arg ) {
63         args.add(arg);
64     }
65
66     public void addAttribute( JmxSet arg ) {
67         attributes.add( arg );
68     }
69
70
71     public void setCode(String JavaDoc code) {
72         this.code = code;
73     }
74
75     public void setArchive(String JavaDoc archive) {
76         this.archive = archive;
77     }
78
79     public void setCodebase(String JavaDoc codebase) {
80         this.codebase = codebase;
81     }
82
83     public void setName(String JavaDoc name) {
84         this.objectName = name;
85     }
86
87     MBeanServer JavaDoc server;
88
89     public MBeanServer JavaDoc getMBeanServer() {
90         if( server!= null ) return server;
91
92         server=(MBeanServer JavaDoc)project.getReference("jmx.server");
93
94         if (server != null) return server;
95
96         try {
97             if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) {
98                 server=(MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(null).get(0);
99             } else {
100                 server=MBeanServerFactory.createMBeanServer();
101
102                 // Register a loader that will be find ant classes.
103
ObjectName JavaDoc defaultLoader= new ObjectName JavaDoc("modeler-ant",
104                         "loader", "ant");
105                 MLet JavaDoc mlet=new MLet JavaDoc( new URL JavaDoc[0], this.getClass().getClassLoader());
106                 server.registerMBean(mlet, defaultLoader);
107
108                 if( log.isDebugEnabled())
109                     log.debug("Creating mbean server and loader "+ mlet +
110                             " " + this.getClass().getClassLoader());
111             }
112             project.addReference("jmx.server", server);
113
114             // Create the MLet object
115
} catch( JMException JavaDoc ex ) {
116             log.error("Error creating server", ex);
117         }
118
119         if( log.isDebugEnabled()) log.debug("Using Mserver " + server );
120
121         return server;
122     }
123
124     boolean modeler=false;
125
126     public void setModeler(boolean modeler) {
127         this.modeler = modeler;
128     }
129
130     protected void bindJmx(String JavaDoc objectName, String JavaDoc code,
131                            String JavaDoc arg0, List JavaDoc args)
132             throws Exception JavaDoc
133     {
134         MBeanServer JavaDoc server=getMBeanServer();
135         oname=new ObjectName JavaDoc( objectName );
136         if( modeler ) {
137             Arg codeArg=new Arg();
138             codeArg.setType("java.lang.String");
139             codeArg.setValue( code );
140             if( args==null) args=new ArrayList JavaDoc();
141             args.add(0, codeArg);
142             code="org.apache.commons.modeler.BaseModelMBean";
143         }
144
145         Object JavaDoc argsA[]=new Object JavaDoc[ args.size()];
146         String JavaDoc sigA[]=new String JavaDoc[args.size()];
147         for( int i=0; i<args.size(); i++ ) {
148             Arg arg=(Arg)args.get(i);
149             if( arg.type==null )
150                 arg.type="java.lang.String";
151             sigA[i]=arg.getType();
152             argsA[i]=arg.getValue();
153             // XXX Deal with not string types - IntrospectionUtils
154
}
155
156         // XXX Use the loader ref, if any
157
if( args.size()==0 ) {
158             server.createMBean(code, oname);
159         } else {
160             server.createMBean(code, oname, argsA, sigA );
161         }
162         log.debug( "Created MBEAN " + oname + " " + code);
163     }
164
165     public ObjectName JavaDoc getObjectName() {
166         return oname;
167     }
168
169     public void execute() throws BuildException {
170         try {
171             // create the mbean
172
bindJmx( objectName, code, null, args);
173
174             // process attributes
175
for( int i=0; i<attributes.size(); i++ ) {
176                 JmxSet att=(JmxSet)attributes.get(i);
177                 att.setObjectName( oname );
178                 log.info("Setting attribute " + oname + " " + att.getName());
179                 att.execute();
180             }
181         } catch(Exception JavaDoc ex) {
182             log.error("Can't create mbean " + objectName, ex);
183         }
184     }
185
186 }
187
Popular Tags