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.util.Vector;
021    
022    import javax.management.MBeanServer;
023    import javax.management.MBeanServerFactory;
024    import javax.management.ObjectName;
025    
026    import org.apache.tools.ant.Task;
027    
028    /**
029     * Set mbean properties.
030     *
031     */
032    public class JmxInvoke extends Task {
033        String objectName;
034    
035        String method;
036        Vector args;
037    
038        public JmxInvoke() {
039    
040        }
041    
042        public void setObjectName(String name) {
043            this.objectName = name;
044        }
045    
046        public void setOperation(String method) {
047                this.method = method;
048        }
049    
050        public void execute() {
051            try {
052                MBeanServer server=(MBeanServer)project.getReference("jmx.server");
053    
054                if (server == null) {
055                    if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) {
056                        server=(MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
057                    } else {
058                        System.out.println("Creating mbean server");
059                        server=MBeanServerFactory.createMBeanServer();
060                    }
061                    project.addReference("jmx.server", server);
062                }
063    
064                ObjectName oname=new ObjectName(objectName);
065    
066                if( args==null ) {
067                    server.invoke(oname, method, null, null);
068                } else {
069                    // XXX Use the loader ref, if any
070                    Object argsA[]=new Object[ args.size()];
071                    String sigA[]=new String[args.size()];
072                    for( int i=0; i<args.size(); i++ ) {
073                        Arg arg=(Arg)args.elementAt(i);
074                        if( arg.type==null )
075                            arg.type="java.lang.String";
076                        sigA[i]=arg.getType();
077                        argsA[i]=arg.getValue();
078                        // XXX Deal with not string types - IntrospectionUtils
079                    }
080                    server.invoke(oname, method, argsA, sigA);
081                }
082            } catch(Exception ex) {
083                ex.printStackTrace();
084            }
085        }
086    
087    }