View Javadoc

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  
18  package org.apache.commons.modeler.ant;
19  
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.management.JMException;
25  import javax.management.MBeanServer;
26  import javax.management.MBeanServerFactory;
27  import javax.management.ObjectName;
28  import javax.management.loading.MLet;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.tools.ant.BuildException;
33  import org.apache.tools.ant.Task;
34  
35  /***
36   * Load an MBean. The syntax is similar with the <mlet>, with few
37   * ant-specific extensions.
38   *
39   * A separate classloader can be used, the mechanism is similar with
40   * what taskdef is using.
41   *
42   * Note that mlet will use the arguments in the constructor.
43   *
44   *
45   */
46  public class MLETTask extends Task {
47      private static Log log = LogFactory.getLog(MLETTask.class);
48      String code;
49      String archive;
50      String codebase;
51      String objectName;
52      ObjectName oname;
53  
54      List args=new ArrayList();
55      List attributes=new ArrayList();
56  
57      // ant specific
58      String loaderRef; // class loader ref
59  
60      public MLETTask() {
61      }
62  
63      public void addArg(Arg arg ) {
64          args.add(arg);
65      }
66  
67      public void addAttribute( JmxSet arg ) {
68          attributes.add( arg );
69      }
70  
71  
72      public void setCode(String code) {
73          this.code = code;
74      }
75  
76      public void setArchive(String archive) {
77          this.archive = archive;
78      }
79  
80      public void setCodebase(String codebase) {
81          this.codebase = codebase;
82      }
83  
84      public void setName(String name) {
85          this.objectName = name;
86      }
87  
88      MBeanServer server;
89  
90      public MBeanServer getMBeanServer() {
91          if( server!= null ) return server;
92  
93          server=(MBeanServer)project.getReference("jmx.server");
94  
95          if (server != null) return server;
96  
97          try {
98              if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) {
99                  server=(MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
100             } else {
101                 server=MBeanServerFactory.createMBeanServer();
102 
103                 // Register a loader that will be find ant classes.
104                 ObjectName defaultLoader= new ObjectName("modeler-ant",
105                         "loader", "ant");
106                 MLet mlet=new MLet( new URL[0], this.getClass().getClassLoader());
107                 server.registerMBean(mlet, defaultLoader);
108 
109                 if( log.isDebugEnabled())
110                     log.debug("Creating mbean server and loader "+ mlet +
111                             " " + this.getClass().getClassLoader());
112             }
113             project.addReference("jmx.server", server);
114 
115             // Create the MLet object
116         } catch( JMException ex ) {
117             log.error("Error creating server", ex);
118         }
119 
120         if( log.isDebugEnabled()) log.debug("Using Mserver " + server );
121 
122         return server;
123     }
124 
125     boolean modeler=false;
126 
127     public void setModeler(boolean modeler) {
128         this.modeler = modeler;
129     }
130 
131     protected void bindJmx(String objectName, String code,
132                            String arg0, List args)
133             throws Exception
134     {
135         MBeanServer server=getMBeanServer();
136         oname=new ObjectName( objectName );
137         if( modeler ) {
138             Arg codeArg=new Arg();
139             codeArg.setType("java.lang.String");
140             codeArg.setValue( code );
141             if( args==null) args=new ArrayList();
142             args.add(0, codeArg);
143             code="org.apache.commons.modeler.BaseModelMBean";
144         }
145 
146         Object argsA[]=new Object[ args.size()];
147         String sigA[]=new String[args.size()];
148         for( int i=0; i<args.size(); i++ ) {
149             Arg arg=(Arg)args.get(i);
150             if( arg.type==null )
151                 arg.type="java.lang.String";
152             sigA[i]=arg.getType();
153             argsA[i]=arg.getValue();
154             // XXX Deal with not string types - IntrospectionUtils
155         }
156 
157         // XXX Use the loader ref, if any
158         if( args.size()==0 ) {
159             server.createMBean(code, oname);
160         } else {
161             server.createMBean(code, oname, argsA, sigA );
162         }
163         log.debug( "Created MBEAN " + oname + " " + code);
164     }
165 
166     public ObjectName getObjectName() {
167         return oname;
168     }
169 
170     public void execute() throws BuildException {
171         try {
172             // create the mbean
173             bindJmx( objectName, code, null, args);
174 
175             // process attributes
176             for( int i=0; i<attributes.size(); i++ ) {
177                 JmxSet att=(JmxSet)attributes.get(i);
178                 att.setObjectName( oname );
179                 log.info("Setting attribute " + oname + " " + att.getName());
180                 att.execute();
181             }
182         } catch(Exception ex) {
183             log.error("Can't create mbean " + objectName, ex);
184         }
185     }
186 
187 }