pero 2005/07/22 04:39:08 Modified: catalina/src/share/org/apache/catalina/ant/jmx JMXAccessorGetTask.java JMXAccessorInvokeTask.java JMXAccessorQueryTask.java JMXAccessorSetTask.java JMXAccessorTask.java Log: Make Tasks also usefull outside ant script projects. handle empty project attribute properties can save also as local map. Revision Changes Path 1.3 +2 -2 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorGetTask.java Index: JMXAccessorGetTask.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorGetTask.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- JMXAccessorGetTask.java 30 Jun 2005 13:01:27 -0000 1.2 +++ JMXAccessorGetTask.java 22 Jul 2005 11:39:08 -0000 1.3 @@ -107,7 +107,7 @@ * @exception BuildException * if an error occurs */ - protected String jmxExecute(MBeanServerConnection jmxServerConnection) + public String jmxExecute(MBeanServerConnection jmxServerConnection) throws Exception { if (getName() == null) { 1.3 +2 -2 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorInvokeTask.java Index: JMXAccessorInvokeTask.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorInvokeTask.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- JMXAccessorInvokeTask.java 30 Jun 2005 13:01:27 -0000 1.2 +++ JMXAccessorInvokeTask.java 22 Jul 2005 11:39:08 -0000 1.3 @@ -158,7 +158,7 @@ * @exception BuildException * if an error occurs */ - protected String jmxExecute(MBeanServerConnection jmxServerConnection) + public String jmxExecute(MBeanServerConnection jmxServerConnection) throws Exception { if (getName() == null) { 1.4 +2 -2 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorQueryTask.java Index: JMXAccessorQueryTask.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorQueryTask.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- JMXAccessorQueryTask.java 8 Jul 2005 20:54:40 -0000 1.3 +++ JMXAccessorQueryTask.java 22 Jul 2005 11:39:08 -0000 1.4 @@ -109,7 +109,7 @@ * @exception BuildException * if an error occurs */ - protected String jmxExecute(MBeanServerConnection jmxServerConnection) + public String jmxExecute(MBeanServerConnection jmxServerConnection) throws Exception { if (getName() == null) { 1.3 +2 -2 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorSetTask.java Index: JMXAccessorSetTask.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorSetTask.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- JMXAccessorSetTask.java 30 Jun 2005 13:01:27 -0000 1.2 +++ JMXAccessorSetTask.java 22 Jul 2005 11:39:08 -0000 1.3 @@ -155,7 +155,7 @@ * @exception BuildException * if an error occurs */ - protected String jmxExecute(MBeanServerConnection jmxServerConnection) + public String jmxExecute(MBeanServerConnection jmxServerConnection) throws Exception { if (getName() == null) { 1.10 +231 -160 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorTask.java Index: JMXAccessorTask.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ant/jmx/JMXAccessorTask.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- JMXAccessorTask.java 21 Jul 2005 10:45:45 -0000 1.9 +++ JMXAccessorTask.java 22 Jul 2005 11:39:08 -0000 1.10 @@ -14,10 +14,8 @@ * limitations under the License. */ - package org.apache.catalina.ant.jmx; - import java.io.IOException; import java.lang.reflect.Array; import java.net.InetAddress; @@ -27,6 +25,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; @@ -47,39 +46,43 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; - /** - * Access <em>JMX</em> JSR 160 MBeans Server. + * Access <em>JMX</em> JSR 160 MBeans Server. * <ul> * <li>open more then one JSR 160 rmi connection</li> * <li>Get/Set Mbeans attributes</li> * <li>Call Mbean Operation with arguments</li> - * <li>Argument values can be converted from string to int,long,float,double,boolean,ObjectName or InetAddress </li> + * <li>Argument values can be converted from string to + * int,long,float,double,boolean,ObjectName or InetAddress</li> * <li>Query Mbeans</li> * <li>Show Get, Call, Query result at Ant console log</li> * <li>Bind Get, Call, Query result at Ant properties</li> * </ul> - * - * Examples: - * open server with reference and autorisation + * + * Examples: open server with reference and autorisation + * * <pre> - * <jmxOpen - * host="127.0.0.1" - * port="9014" - * username="monitorRole" - * password="mysecret" - * ref="jmx.myserver" - * /> + * + * <jmxOpen + * host="127.0.0.1" + * port="9014" + * username="monitorRole" + * password="mysecret" + * ref="jmx.myserver" + * /> + * * </pre> + * * All calls after opening with same refid reuse the connection. * <p> - * First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em> + * First call to a remote MBeanserver save the JMXConnection a referenz + * <em>jmx.server</em> * </p> - * All JMXAccessorXXXTask support the attribute <em>if</em> and <em>unless</em>. With <em>if</em> - * the task is only execute when property exist and with <em>unless</em> when property not exists. - * <br/> - * <b>NOTE</b>: These tasks require Ant 1.6 or later interface. - * + * All JMXAccessorXXXTask support the attribute <em>if</em> and + * <em>unless</em>. With <em>if</em> the task is only execute when property + * exist and with <em>unless</em> when property not exists. <br/><b>NOTE + * </b>: These tasks require Ant 1.6 or later interface. + * * @author Peter Rossbach * @version $Revision$ $Date$ * @since 5.5.10 @@ -87,26 +90,40 @@ public class JMXAccessorTask extends BaseRedirectorHelperTask { - // ----------------------------------------------------- Instance Variables public static String JMX_SERVICE_PREFIX = "service:jmx:rmi:///jndi/rmi://"; + public static String JMX_SERVICE_SUFFIX = "/jmxrmi"; private String name = null; + private String resultproperty; + private String url = null; + private String host = "localhost"; + private String port = "8050"; + private String password = null; + private String username = null; + private String ref = "jmx.server"; + private boolean echo = false; + private boolean separatearrayresults = true; + private String delimiter; + private String unlessCondition; + private String ifCondition; - + + private Properties properties = new Properties(); + // ----------------------------------------------------- Instance Info /** @@ -138,29 +155,32 @@ public void setName(String objectName) { this.name = objectName; } - + /** * @return Returns the resultproperty. */ public String getResultproperty() { return resultproperty; } + /** - * @param resultproperty The resultproperty to set. + * @param resultproperty + * The resultproperty to set. */ public void setResultproperty(String propertyName) { this.resultproperty = propertyName; } - + /** * @return Returns the delimiter. */ public String getDelimiter() { return delimiter; } - + /** - * @param delimiter The delimiter to set. + * @param delimiter + * The delimiter to set. */ public void setDelimiter(String separator) { this.delimiter = separator; @@ -172,28 +192,30 @@ public boolean isEcho() { return echo; } - + /** - * @param echo The echo to set. + * @param echo + * The echo to set. */ public void setEcho(boolean echo) { this.echo = echo; } - + /** * @return Returns the separatearrayresults. */ public boolean isSeparatearrayresults() { return separatearrayresults; } - + /** - * @param separatearrayresults The separatearrayresults to set. + * @param separatearrayresults + * The separatearrayresults to set. */ public void setSeparatearrayresults(boolean separateArrayResults) { this.separatearrayresults = separateArrayResults; - } - + } + /** * The login password for the <code>Manager</code> application. */ @@ -206,7 +228,6 @@ this.password = password; } - /** * The login username for the <code>JMX</code> MBeanServer. */ @@ -222,7 +243,7 @@ /** * The URL of the <code>JMX JSR 160</code> MBeanServer to be used. */ - + public String getUrl() { return (this.url); } @@ -234,7 +255,7 @@ /** * The Host of the <code>JMX JSR 160</code> MBeanServer to be used. */ - + public String getHost() { return (this.host); } @@ -246,7 +267,7 @@ /** * The Port of the <code>JMX JSR 160</code> MBeanServer to be used. */ - + public String getPort() { return (this.port); } @@ -254,52 +275,60 @@ public void setPort(String port) { this.port = port; } - + /** * @return Returns the useRef. */ public boolean isUseRef() { return ref != null && !"".equals(ref); } - + /** * @return Returns the ref. */ public String getRef() { return ref; } + /** - * @param ref The ref to set. + * @param ref + * The ref to set. */ public void setRef(String refId) { this.ref = refId; } - - + /** * @return Returns the ifCondition. */ public String getIf() { return ifCondition; } + /** - * Only execute if a property of the given name exists in the current project. - * @param c property name + * Only execute if a property of the given name exists in the current + * project. + * + * @param c + * property name */ public void setIf(String c) { ifCondition = c; } - /** + + /** * @return Returns the unlessCondition. */ public String getUnless() { return unlessCondition; } - + /** - * Only execute if a property of the given name does not - * exist in the current project. - * @param c property name + * Only execute if a property of the given name does not exist in the + * current project. + * + * @param c + * property name */ public void setUnless(String c) { unlessCondition = c; @@ -308,11 +337,12 @@ // --------------------------------------------------------- Public Methods /** - * Execute the specified command. This logic only performs the common - * attribute validation required by all subclasses; it does not perform - * any functional logic directly. - * - * @exception BuildException if a validation error occurs + * Execute the specified command. This logic only performs the common + * attribute validation required by all subclasses; it does not perform any + * functional logic directly. + * + * @exception BuildException + * if a validation error occurs */ public void execute() throws BuildException { if (testIfCondition() && testUnlessCondition()) { @@ -337,7 +367,7 @@ } } } - + /** * create a new JMX Connection with auth when username and password is set. */ @@ -363,33 +393,33 @@ } - /** * test the if condition + * * @return true if there is no if condition, or the named property exists */ protected boolean testIfCondition() { if (ifCondition == null || "".equals(ifCondition)) { return true; } - return getProject().getProperty(ifCondition) != null; + return getProperty(ifCondition) != null; } /** * test the unless condition - * @return true if there is no unless condition, - * or there is a named property but it doesn't exist + * + * @return true if there is no unless condition, or there is a named + * property but it doesn't exist */ protected boolean testUnlessCondition() { if (unlessCondition == null || "".equals(unlessCondition)) { return true; } - return getProject().getProperty(unlessCondition) == null; + return getProperty(unlessCondition) == null; } /** - * Get Current Connection from <em>ref</em> parameter or create a new - * one! + * Get Current Connection from <em>ref</em> parameter or create a new one! * * @return * @throws MalformedURLException @@ -400,15 +430,15 @@ String password, String refId) throws MalformedURLException, IOException { MBeanServerConnection jmxServerConnection = null; - boolean isRef = project != null && refId != null && refId.length() > 0 ; + boolean isRef = project != null && refId != null && refId.length() > 0; if (isRef) { Object pref = project.getReference(refId); try { jmxServerConnection = (MBeanServerConnection) pref; } catch (ClassCastException cce) { - if(project != null ) { + if (project != null) { project.log("wrong object reference " + refId + " - " - + pref.getClass()); + + pref.getClass()); } return null; } @@ -426,7 +456,8 @@ // ------------------------------------------------------ protected Methods /** - * get JMXConnection + * get JMXConnection + * * @return * @throws MalformedURLException * @throws IOException @@ -436,16 +467,21 @@ MBeanServerConnection jmxServerConnection = null; if (isUseRef()) { - Object pref = getProject().getReference(getRef()); - try { - jmxServerConnection = (MBeanServerConnection) pref; - } catch (ClassCastException cce) { - getProject().log( - "Wrong object reference " + getRef() + " - " - + pref.getClass()); - return null; + Object pref = null ; + if(getProject() != null) { + pref = getProject().getReference(getRef()); + if (pref != null) { + try { + jmxServerConnection = (MBeanServerConnection) pref; + } catch (ClassCastException cce) { + getProject().log( + "Wrong object reference " + getRef() + " - " + + pref.getClass()); + return null; + } + } } - if (jmxServerConnection == null) { + if (jmxServerConnection == null) { jmxServerConnection = accessJMXConnection(getProject(), getUrl(), getHost(), getPort(), getUsername(), getPassword(), getRef()); @@ -456,7 +492,6 @@ } return jmxServerConnection; } - /** * Execute the specified command, based on the configured properties. The @@ -466,92 +501,91 @@ * @exception BuildException * if an error occurs */ - protected String jmxExecute(MBeanServerConnection jmxServerConnection) + public String jmxExecute(MBeanServerConnection jmxServerConnection) throws Exception { if ((jmxServerConnection == null)) { - throw new BuildException("Must open a connection!"); + throw new BuildException("Must open a connection!"); } else if (isEcho()) { handleOutput("JMX Connection ref=" + ref + " is open!"); } - return null; + return null; } /** - * Convert string to datatype - * FIXME How we can transfer values from ant project reference store (ref)? + * Convert string to datatype FIXME How we can transfer values from ant + * project reference store (ref)? + * * @param value * @param type * @return */ - protected Object convertStringToType(String value, String valueType) - { + protected Object convertStringToType(String value, String valueType) { if ("java.lang.String".equals(valueType)) return value; - - Object convertValue = value ; - if ("java.lang.Integer".equals(valueType) - || "int".equals(valueType)) { + + Object convertValue = value; + if ("java.lang.Integer".equals(valueType) || "int".equals(valueType)) { try { convertValue = new Integer(value); } catch (NumberFormatException ex) { - if(isEcho()) + if (isEcho()) handleErrorOutput("Unable to convert to integer:" + value); } - }else if ("java.lang.Long".equals(valueType) - || "long".equals(valueType)) { - try { - convertValue = new Long(value); - } catch (NumberFormatException ex) { - if(isEcho()) - handleErrorOutput("Unable to convert to long:" + value); - } + } else if ("java.lang.Long".equals(valueType) + || "long".equals(valueType)) { + try { + convertValue = new Long(value); + } catch (NumberFormatException ex) { + if (isEcho()) + handleErrorOutput("Unable to convert to long:" + value); + } } else if ("java.lang.Boolean".equals(valueType) || "boolean".equals(valueType)) { convertValue = new Boolean(value); - }else if ("java.lang.Float".equals(valueType) + } else if ("java.lang.Float".equals(valueType) || "float".equals(valueType)) { try { convertValue = new Float(value); } catch (NumberFormatException ex) { - if(isEcho()) + if (isEcho()) handleErrorOutput("Unable to convert to float:" + value); } - }else if ("java.lang.Double".equals(valueType) + } else if ("java.lang.Double".equals(valueType) || "double".equals(valueType)) { try { convertValue = new Double(value); } catch (NumberFormatException ex) { - if(isEcho()) + if (isEcho()) handleErrorOutput("Unable to convert to double:" + value); } - }else if ("javax.management.ObjectName".equals(valueType) + } else if ("javax.management.ObjectName".equals(valueType) || "name".equals(valueType)) { try { convertValue = new ObjectName(value); } catch (MalformedObjectNameException e) { - if(isEcho()) - handleErrorOutput("Unable to convert to ObjectName:" + value); + if (isEcho()) + handleErrorOutput("Unable to convert to ObjectName:" + + value); } } else if ("java.net.InetAddress".equals(valueType)) { try { convertValue = InetAddress.getByName(value); } catch (UnknownHostException exc) { - if(isEcho()) + if (isEcho()) handleErrorOutput("Unable to resolve host name:" + value); } } return convertValue; } - - /** - * @param name context of result + * @param name + * context of result * @param result */ - protected void echoResult(String name,Object result ) { - if(isEcho()) { + protected void echoResult(String name, Object result) { + if (isEcho()) { if (result.getClass().isArray()) { for (int i = 0; i < Array.getLength(result); i++) { handleOutput(name + "." + i + "=" + Array.get(result, i)); @@ -562,86 +596,91 @@ } /** - * create result as property with name from attribute resultproperty + * create result as property with name from attribute resultproperty + * * @param result * @see #createProperty(String, Object) */ protected void createProperty(Object result) { if (resultproperty != null) { - createProperty(resultproperty,result); + createProperty(resultproperty, result); } } - - /** - * create result as property with name from property prefix - * When result is an array and isSeparateArrayResults is true, - * resultproperty used as prefix (<code>resultproperty.0-array.length</code> - * and store the result array length at <code>resultproperty.length</code>. - * Other option is that you delemit your result with a delimiter (java.util.StringTokenizer is used). - * @param propertyPrefix - * @param result - */ + + /** + * create result as property with name from property prefix When result is + * an array and isSeparateArrayResults is true, resultproperty used as + * prefix (<code>resultproperty.0-array.length</code> and store the + * result array length at <code>resultproperty.length</code>. Other + * option is that you delemit your result with a delimiter + * (java.util.StringTokenizer is used). + * + * @param propertyPrefix + * @param result + */ protected void createProperty(String propertyPrefix, Object result) { if (propertyPrefix == null) propertyPrefix = ""; if (result instanceof CompositeDataSupport) { - CompositeDataSupport data = (CompositeDataSupport) result ; + CompositeDataSupport data = (CompositeDataSupport) result; CompositeType compositeType = data.getCompositeType(); - Set keys = compositeType.keySet() ; + Set keys = compositeType.keySet(); for (Iterator iter = keys.iterator(); iter.hasNext();) { - String key = (String) iter.next(); + String key = (String) iter.next(); Object value = data.get(key); OpenType type = compositeType.getType(key); - if(type instanceof SimpleType ) { - setProperty(propertyPrefix + "." + key , value); - } else { - createProperty(propertyPrefix + "." + key, value ); + if (type instanceof SimpleType) { + setProperty(propertyPrefix + "." + key, value); + } else { + createProperty(propertyPrefix + "." + key, value); } - } + } } else if (result instanceof TabularDataSupport) { - TabularDataSupport data = (TabularDataSupport) result ; + TabularDataSupport data = (TabularDataSupport) result; for (Iterator iter = data.keySet().iterator(); iter.hasNext();) { Object key = iter.next(); - for(Iterator iter1 = ((List)key).iterator(); iter1.hasNext();) { - Object key1= iter1.next(); - CompositeData valuedata = data.get(new Object[] { key1 } ); + for (Iterator iter1 = ((List) key).iterator(); iter1.hasNext();) { + Object key1 = iter1.next(); + CompositeData valuedata = data.get(new Object[] { key1 }); Object value = valuedata.get("value"); - OpenType type = valuedata.getCompositeType().getType("value"); - if(type instanceof SimpleType ) { - setProperty(propertyPrefix + "." + key1 , value); - } else { - createProperty(propertyPrefix + "." + key1, value ); + OpenType type = valuedata.getCompositeType().getType( + "value"); + if (type instanceof SimpleType) { + setProperty(propertyPrefix + "." + key1, value); + } else { + createProperty(propertyPrefix + "." + key1, value); } } - } + } } else if (result.getClass().isArray()) { if (isSeparatearrayresults()) { - int size = 0 ; + int size = 0; for (int i = 0; i < Array.getLength(result); i++) { - if(setProperty(propertyPrefix + "." + size , Array.get(result, i))) { + if (setProperty(propertyPrefix + "." + size, Array.get( + result, i))) { size++; } } - if(size > 0) { - setProperty(propertyPrefix + ".Length", - Integer.toString(size)); + if (size > 0) { + setProperty(propertyPrefix + ".Length", Integer + .toString(size)); } } } else { String delim = getDelimiter(); if (delim != null) { - StringTokenizer tokenizer = new StringTokenizer(result.toString(), - delim); + StringTokenizer tokenizer = new StringTokenizer(result + .toString(), delim); int size = 0; for (; tokenizer.hasMoreTokens();) { String token = tokenizer.nextToken(); - if(setProperty(propertyPrefix + "." + size, token)) { + if (setProperty(propertyPrefix + "." + size, token)) { size++; } } - if(size>0) - setProperty(propertyPrefix + ".Length", - Integer.toString(size)); + if (size > 0) + setProperty(propertyPrefix + ".Length", Integer + .toString(size)); } else { setProperty(propertyPrefix, result.toString()); } @@ -649,19 +688,51 @@ } /** + * get all properties, when project is there got all project Properties + * @return properties + */ + public Map getProperties() { + Project currentProject = getProject(); + if (currentProject != null) { + return currentProject.getProperties(); + } else { + return properties; + } + } + + /** + * get all Properties + * @param property + * @return + */ + public String getProperty(String property) { + Project currentProject = getProject(); + if (currentProject != null) { + return currentProject.getProperty(property); + } else { + return properties.getProperty(property); + } + } + + /** * @param propertyPrefix * @param value */ - protected boolean setProperty(String property, Object value) { - if(property != null ) { - if(value == null ) - value="" ; + public boolean setProperty(String property, Object value) { + if (property != null) { + if (value == null) + value = ""; if (isEcho()) { handleOutput(property + "=" + value.toString()); } - getProject().setNewProperty(property,value.toString()); - return true ; + Project currentProject = getProject(); + if (currentProject != null) { + currentProject.setNewProperty(property, value.toString()); + } else { + properties.setProperty(property, value.toString()); + } + return true; } - return false ; + return false; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]