Cool improvement!

Please, send a patch diff to current svn head, then I test and commit the change :-)

Many thanks,
Peter



Am 30.08.2006 um 01:15 schrieb Didier Donsez:

Dear all

Catalina ant task <jmx:set> sets only one mbean attribute
My improvement proposes to set several attributes by using
<jmx:attribute> sun-elements
The MBeanServer.setAttributes(ObjectName name,AttributeList attributes)
method is used instead of the MBeanServer.setAttribute(ObjectName
name,Attribute attribute) method

Modified files are in attachment and modified and new code lines are
bracketed between // ADDED comments
My ICLA is signed and I'm a contributor of Felix (Apache incubated project).

Kind regards

Didier

PS: I hope to post this mail to the right mailing list ! I did not seen
Tomcat in https://issues.apache.org/jira/secure/BrowseProject.jspa

--
---------------------------------------------------------
Didier DONSEZ
Laboratoire LSR, Institut Imag, Universite Joseph Fourier
Bat. C, 220 rue de la Chimie, Domaine Universitaire
BP 53, 38041 Grenoble Cedex 9, France
GPS : lat 45°11'38.3"N, lon 05°46'14.7"E, alt 223m
http://www-adele.imag.fr/users/Didier.Donsez/map/map.html
Tel : +33 4 76 63 55 49           Fax : +33 4 76 63 55 50
mailto:[EMAIL PROTECTED]
URL: http://www-adele.imag.fr/users/Didier.Donsez
---------------------------------------------------------


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


package org.apache.catalina.ant.jmx;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import org.apache.tools.ant.BuildException;


/**
 * Access <em>JMX</em> JSR 160 MBeans Server.
 * <ul>
 * <li>Set Mbeans attributes</li>
 * </ul>
 * <p>
 * Examples:
 * Set a Mbean Manager attribute maxActiveSessions.
 * Set this attribute with fresh jmx connection without save reference
 * <pre>
 *   &lt;jmx:set
 *           host="127.0.0.1"
 *           port="9014"
 *           ref=""
* name="Catalina:type=Manager,path="/ ClusterTest",host=localhost"
 *           attribute="maxActiveSessions"
 *           value="100"
 *           type="int"
 *           echo="false"&gt;
 *       /&gt;
 * </pre>
 *
 * or
 *
 * <pre>
 *   &lt;jmx:set
 *           host="127.0.0.1"
 *           port="9014"
 *           ref=""
* name="Catalina:type=Manager,path="/ ClusterTest",host=localhost"
 *           echo="false"&gt;
 *       &gt;
 *
 *        &lt;jmx:attribute name="maxActiveSessions"
 *                                      value="100"
 *                                      type="int"/&gt;
 *        &lt;jmx:attribute name="minActiveSessions"
 *                                      value="10"
 *                                      type="int"/&gt;
 *   &lt;/jmx:set&gt;
 *
 * </pre>
 * </p>
 * <p>
* First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
 * </p>
 * These tasks require Ant 1.6 or later interface.
 *
 * @author Peter Rossbach
* @contributor Didier Donsez (add &lt;attributes name='...' value='...' type='...'&gt; * @version $Revision: 304089 $ $Date: 2005-09-14 09:28:29 -0400 (Wed, 14 Sep 2005) $
 * @since 5.5.10
 */

public class JMXAccessorSetTask extends JMXAccessorTask {

// ----------------------------------------------------- Instance Variables

    private String attribute;
    private String value;
    private String type;
    private boolean convert = false ;

    // ADDED BEGIN
    private List attributes=new ArrayList();
    // ADDED END



// ----------------------------------------------------- Instance Info

    /**
     * Descriptive information describing this implementation.
     */
private static final String info = "org.apache.catalina.ant.JMXAccessorSetTask/1.0";

    /**
* Return descriptive information about this implementation and the
     * corresponding version number, in the format
     * <code>&lt;description&gt;/&lt;version&gt;</code>.
     */
    public String getInfo() {

        return (info);

    }

// ------------------------------------------------------------- Properties

    // ADDED BEGIN
public void addAttribute(org.apache.catalina.ant.jmx.Attribute attribute) {
        attributes.add(attribute);
    }

    public List getAttributes() {
        return attributes;
    }

    // ADDED END


    /**
     * @return Returns the attribute.
     */
    public String getAttribute() {
        return attribute;
    }

    /**
     * @param attribute The attribute to set.
     */
    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }

    /**
     * @return Returns the value.
     */
    public String getValue() {
        return value;
    }
    /**
     * @param value The value to set.
     */
    public void setValue(String value) {
        this.value = value;
    }


    /**
     * @return Returns the type.
     */
    public String getType() {
        return type;
    }

    /**
     * @param valueType The type to set.
     */
    public void setType(String valueType) {
        this.type = valueType;
    }


    /**
     * @return Returns the convert.
     */
    public boolean isConvert() {
        return convert;
    }
    /**
     * @param convert The convert to set.
     */
    public void setConvert(boolean convert) {
        this.convert = convert;
    }
// ------------------------------------------------------ protected Methods

    /**
* Execute the specified command, based on the configured properties. The * input stream will be closed upon completion of this task, whether it was
     * executed successfully or not.
     *
     * @exception Exception
     *                if an error occurs
     */
public String jmxExecute(MBeanServerConnection jmxServerConnection)
        throws Exception {

        if (getName() == null) {
            throw new BuildException("Must specify a 'name'");
        }

        // ADDED BEGIN

if ((attribute == null || value == null) && attributes.isEmpty()) {
            throw new BuildException(
"Must specify a 'attribute' and 'value' or 'attribute' elements for set ");
        }
        if (attributes.isEmpty()) {
            return  jmxSet(jmxServerConnection, getName());
        } else {
            if ((attribute != null && value != null)) {
org.apache.catalina.ant.jmx.Attribute a=new org.apache.catalina.ant.jmx.Attribute();
                a.setName(attribute);
                a.setValue(value);
                a.setType(type);
                attributes.add(a);
            }
            return  jmxMultiSet(jmxServerConnection, getName());
        }
        // ADDED END

     }

    /**
     * @param jmxServerConnection
     * @param name
     * @throws Exception
     */
    protected String jmxSet(MBeanServerConnection jmxServerConnection,
            String name) throws Exception {
        Object realValue;
        if (type != null) {
            realValue = convertStringToType(value, type);
        } else {
            if (isConvert()) {
String mType = getMBeanAttributeType (jmxServerConnection, name,
                        attribute);
                realValue = convertStringToType(value, mType);
            } else
                realValue = value;
        }
jmxServerConnection.setAttribute(new ObjectName(name), new Attribute(
                attribute, realValue));
        return null;
    }

    // ADDED BEGIN
    /**
     * set several attributes in one invocation to the server
     * @param jmxServerConnection
     * @param name
     * @throws Exception
     */
protected String jmxMultiSet(MBeanServerConnection jmxServerConnection,
            String name) throws Exception {

        AttributeList attributeList=new AttributeList();
        MBeanInfo minfo=null;

        for (Iterator iter = attributes.iterator(); iter.hasNext();) {
org.apache.catalina.ant.jmx.Attribute element = (org.apache.catalina.ant.jmx.Attribute) iter.next();
            Object realValue;
            if (element.getType() != null) {
realValue = convertStringToType(element.getValue(), element.getType());
            } else {
                if (isConvert()) {
if(minfo==null) minfo = getMBeanInfo (jmxServerConnection, name); String mType = getMBeanAttributeType(minfo, element.getName()); realValue = convertStringToType(element.getValue (), mType);
                } else
                    realValue = element.getValue();
            }
                        Attribute a=new Attribute(element.getName(), realValue);
                attributeList.add(a);
                }


AttributeList settedAttributes=jmxServerConnection.setAttributes(new ObjectName (name),attributeList);
        if(isEcho())
                if(settedAttributes.isEmpty()){
                        getProject().log("No setted attributes!");
                } else {
                        StringBuffer sb=new StringBuffer();
                                sb.append("Attributes");
                        for (Iterator iter = settedAttributes.iterator(); iter
                                                .hasNext();) {
                                        Attribute element = (Attribute) 
iter.next();
                                        sb.append(' 
').append(element.getName());
                                }
                                sb.append(" were setted");
                        getProject().log(sb.toString());                
                }
        return null;
    }
    // ADDED END


    // ADDED BEGIN
    /**
     * Get MBean Attribute from the MBeanInfo
     * @param minfo
     * @param attribute
     * @return The type
     * @throws Exception
     */
    protected String getMBeanAttributeType(
            MBeanInfo minfo,
            String attribute) throws Exception {
        String mattrType = null;
        MBeanAttributeInfo attrs[] = minfo.getAttributes();
        if (attrs != null) {
for (int i = 0; mattrType == null && i < attrs.length; i ++) {
                if (attribute.equals(attrs[i].getName()))
                    mattrType = attrs[i].getType();
            }
        }
        return mattrType;
    }


    /**
     * Get the MBeanInfo from Mbean Server
     * @param jmxServerConnection
     * @param name
     * @return The mbean info
     * @throws Exception
     */
    protected MBeanInfo getMBeanInfo(
            MBeanServerConnection jmxServerConnection,
            String name) throws Exception {
        ObjectName oname = new ObjectName(name);
        MBeanInfo minfo = jmxServerConnection.getMBeanInfo(oname);
        return minfo;
    }
    // ADDED END

    /**
     * Get MBean Attriute from Mbean Server
     * @param jmxServerConnection
     * @param name
     * @param attribute
     * @return The type
     * @throws Exception
     */
    protected String getMBeanAttributeType(
            MBeanServerConnection jmxServerConnection,
            String name,
            String attribute) throws Exception {
        ObjectName oname = new ObjectName(name);
        String mattrType = null;
        MBeanInfo minfo = jmxServerConnection.getMBeanInfo(oname);
        MBeanAttributeInfo attrs[] = minfo.getAttributes();
        if (attrs != null) {
for (int i = 0; mattrType == null && i < attrs.length; i ++) {
                if (attribute.equals(attrs[i].getName()))
                    mattrType = attrs[i].getType();
            }
        }
        return mattrType;
    }
 }


<!-- Copy your catalina-ant.jar from $CATALINA_HOME/server/lib to $ANT_HOME/lib -->
<project     name="jmx.query"
                xmlns:jmx="antlib:org.apache.catalina.ant.jmx"
                default="all"
                basedir="."
                >
        <!-- Author : Didier Donsez -->
                
        <description>Example of ANT JMX</description>
                
<property name="jmx.serviceurl" value="service:jmx:rmi:///jndi/ rmi://localhost:9999/server"/>
        <property name="jmx.host" value="localhost"/>
        <property name="jmx.port" value="9999"/>
        <property name="jmx.username" value=""/>
        <property name="jmx.password" value=""/>

        <target name="all" depends="open,query,get,set,invoke"/>
        
        <target name="open" depends="open-url"/>

        <target name="open-serverport"
                description="Open the JSR160 connection"
                unless="jmx.serviceurl"
        >
                <jmx:open
                                host="${jmx.host}"
                                port="${jmx.port}"
                                username="${jmx.username}"
                                password="${jmx.password}"
                                ref="my.jmx.server"
                                />                           
        </target>
        
        <target name="open-url"
                description="Open the JSR160 coonection"
                if="jmx.serviceurl"
        >
                <jmx:open
                                url="${jmx.serviceurl}"
                                username="${jmx.username}"
                                password="${jmx.password}"
                                ref="my.jmx.server"
                                />                           
        </target>

        <!-- Query MBean list -->
        <target name="query"
                depends="open"
description="Example to get all MBeans from a server and store inside an external xml property file">

                <jmx:query
                                ref="my.jmx.server"
                                name="*:*"
                                resultproperty="mbeans"
                                attributebinding="false"
                                />

<echo message="Number of MBeans in server ${jmx.serviceurl} is $ {mbeans.Length}"/>

                <echoproperties
                                destfile="mbeans.properties"
                                prefix="mbeans."
                                format="xml"
                                />

                <echoproperties
                                prefix="mbeans."
                                />
        </target>

        <!-- Test Get -->
        <target name="get"
                depends="open">
                <jmx:get
                        ref="my.jmx.server"
                        name="java.lang:type=Memory"
                        attribute="Verbose"
                        resultproperty="mbeans"
                        delimiter=" "
                        echo="true"
                />
        </target>

        
        <!-- Test Set -->
        <target name="set"
                depends="open">

                <echo message="Set java.lang:type=Memory"/>     
                <jmx:set
                        ref="my.jmx.server"
                        name="java.lang:type=Memory"
                        attribute="Verbose"
                        value="true"
                        type="boolean"
                        echo="true"
                >
                </jmx:set>
                
                <sleep seconds="20"/>

                <echo message="Set java.lang:type=Memory"/>     
                <jmx:set
                        ref="my.jmx.server"
                        name="java.lang:type=Memory"
                        echo="true"
                >
                        <attribute name="Verbose" value="false" type="boolean"/>
                </jmx:set>
                
                <echo message="Set java.lang:type=Threading"/>  
                <jmx:set
                        ref="my.jmx.server"
                        name="java.lang:type=Threading"
                        echo="true"
                >
<attribute name="ThreadContentionMonitoringEnabled" value="true" type="boolean"/> <attribute name="ThreadCpuTimeEnabled" value="false" type="boolean"/>
                </jmx:set>

                <sleep seconds="20"/>
                
                <echo message="Set java.lang:type=Threading"/>  
                <jmx:set
                        ref="my.jmx.server"
                        name="java.lang:type=Threading"
                        echo="true"
                >
<attribute name="ThreadContentionMonitoringEnabled" value="false" type="boolean"/> <attribute name="ThreadCpuTimeEnabled" value="true" type="boolean"/>
                </jmx:set>

                
        </target>
        
        <target name="invoke"
                depends="open">
                <echo message="Invoke"/>        
                <jmx:invoke
                        ref="my.jmx.server"
                        name="java.lang:type=Threading"
                        operation="getThreadUserTime"
                >
                        <arg value="2" type="long" />
                </jmx:invoke>
        
                
                                <echo message="Invoke"/>        
                <jmx:invoke
                        ref="my.jmx.server"
                        name="java.lang:type=Memory"
                        operation="gc"
                >
                </jmx:invoke>
        </target>

</project>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to