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 javax.management.Attribute;
021    import javax.management.MBeanServer;
022    import javax.management.ObjectName;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.apache.commons.modeler.Registry;
027    import org.apache.tools.ant.Task;
028    
029    /**
030     * Set mbean properties.
031     *
032     */
033    public class JmxSet extends Task {
034        private static Log log = LogFactory.getLog(JmxSet.class);
035    
036        String attribute;
037        String value;
038        String valueRef;
039        Object objValue;
040        String objectName;
041        ObjectName oname;
042        String type;
043    
044    
045        public JmxSet() {
046        }
047    
048        public void setAttribute(String attribute) {
049            this.attribute = attribute;
050        }
051    
052        public void setName( String name ) {
053            this.attribute=name;
054        }
055    
056        public String getName() {
057            return attribute;
058        }
059    
060        public void setValue(String value) {
061            this.value = value;
062        }
063    
064        public void addText( String value ) {
065            this.value=value;
066        }
067    
068        public void setValueRef(String valueRef) {
069            this.valueRef = valueRef;
070        }
071    
072        public void setType(String type) {
073            this.type = type;
074        }
075    
076        public void setObjValue(Object objValue) {
077            this.objValue = objValue;
078        }
079    
080        public void setObjectName(String name) {
081            this.objectName = name;
082        }
083    
084        public void setObjectName( ObjectName oname ) {
085            this.oname=oname;
086        }
087    
088        public void execute() {
089            try {
090                Registry registry=Registry.getRegistry();
091                MBeanServer server=registry.getMBeanServer();
092    
093                if( oname==null )
094                    oname=new ObjectName(objectName);
095                if( type==null ) {
096                    type=registry.getType(oname, attribute);
097                    if( log.isDebugEnabled())
098                        log.debug("Discovered type " + type);
099                }
100    
101                // XXX convert value, use meta data to find type
102                 if( objValue==null && valueRef != null ) {
103                     objValue=project.getReference(valueRef);
104                 }
105                 if( objValue==null ) {
106                     objValue=registry.convertValue(type, value);
107    
108                 }
109                if( log.isDebugEnabled())
110                    log.debug("Setting " + oname + " " + attribute + " " +
111                            objValue);
112                server.setAttribute(oname, new Attribute(attribute, objValue));
113    
114            } catch(Exception ex) {
115                ex.printStackTrace();
116            }
117        }
118    
119    }