You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2019/09/07 21:34:42 UTC

[tomcat] 01/02: Refactor to reduce code duplication identified by Simian.

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit a35cb870f50f5d90b1ff10728fc84b21e9141c4b
Author: Mark Emlyn David Thomas <ma...@apache.org>
AuthorDate: Mon Sep 8 12:21:13 2014 +0000

    Refactor to reduce code duplication identified by Simian.
---
 .../catalina/ant/jmx/JMXAccessorCondition.java     | 168 +-------------------
 ...ondition.java => JMXAccessorConditionBase.java} |  90 ++---------
 .../ant/jmx/JMXAccessorEqualsCondition.java        | 170 +--------------------
 3 files changed, 26 insertions(+), 402 deletions(-)

diff --git a/java/org/apache/catalina/ant/jmx/JMXAccessorCondition.java b/java/org/apache/catalina/ant/jmx/JMXAccessorCondition.java
index 3d2a21f..ca41ffb 100644
--- a/java/org/apache/catalina/ant/jmx/JMXAccessorCondition.java
+++ b/java/org/apache/catalina/ant/jmx/JMXAccessorCondition.java
@@ -14,18 +14,9 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.catalina.ant.jmx;
 
-import java.io.IOException;
-import java.net.MalformedURLException;
-
-import javax.management.MBeanServerConnection;
-import javax.management.ObjectName;
-
 import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.ProjectComponent;
-import org.apache.tools.ant.taskdefs.condition.Condition;
 
 /**
  *
@@ -87,21 +78,12 @@ import org.apache.tools.ant.taskdefs.condition.Condition;
  * @author Peter Rossbach
  * @since 5.5.10
  */
-public class JMXAccessorCondition extends ProjectComponent implements Condition {
+public class JMXAccessorCondition extends JMXAccessorConditionBase {
 
     // ----------------------------------------------------- Instance Variables
 
-    private String url = null;
-    private String host = "localhost";
-    private String port = "8050";
-    private String password = null;
-    private String username = null;
-    private String name = null;
-    private String attribute;
-    private String value;
     private String operation = "==" ;
     private String type = "long" ;
-    private String ref = "jmx.server";
     private String unlessCondition;
     private String ifCondition;
 
@@ -118,10 +100,9 @@ public class JMXAccessorCondition extends ProjectComponent implements Condition
      * <code>&lt;description&gt;/&lt;version&gt;</code>.
      */
     public String getInfo() {
-
-        return (info);
-
+        return info;
     }
+
     // ----------------------------------------------------- Properties
 
     /**
@@ -149,114 +130,8 @@ public class JMXAccessorCondition extends ProjectComponent implements Condition
     public void setType(String type) {
         this.type = type;
     }
-    /**
-     * @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 host.
-     */
-    public String getHost() {
-        return host;
-    }
-    /**
-     * @param host The host to set.
-     */
-    public void setHost(String host) {
-        this.host = host;
-    }
-    /**
-     * @return Returns the name.
-     */
-    public String getName() {
-        return name;
-    }
-    /**
-     * @param objectName The name to set.
-     */
-    public void setName(String objectName) {
-        this.name = objectName;
-    }
-    /**
-     * @return Returns the password.
-     */
-    public String getPassword() {
-        return password;
-    }
-    /**
-     * @param password The password to set.
-     */
-    public void setPassword(String password) {
-        this.password = password;
-    }
-    /**
-     * @return Returns the port.
-     */
-    public String getPort() {
-        return port;
-    }
-    /**
-     * @param port The port to set.
-     */
-    public void setPort(String port) {
-        this.port = port;
-    }
-    /**
-     * @return Returns the url.
-     */
-    public String getUrl() {
-        return url;
-    }
-    /**
-     * @param url The url to set.
-     */
-    public void setUrl(String url) {
-        this.url = url;
-    }
-    /**
-     * @return Returns the username.
-     */
-    public String getUsername() {
-        return username;
-    }
-    /**
-     * @param username The username to set.
-     */
-    public void setUsername(String username) {
-        this.username = username;
-    }
-    /**
-     * @return Returns the value.
-     */
-    public String getValue() {
-        return value;
-    }
-    // The setter for the "value" attribute
-    public void setValue(String value) {
-        this.value = value;
-    }
 
     /**
-     * @return Returns the ref.
-     */
-    public String getRef() {
-        return ref;
-    }
-    /**
-     * @param refId The ref to set.
-     */
-    public void setRef(String refId) {
-        this.ref = refId;
-    }
-    /**
      * @return Returns the ifCondition.
      */
     public String getIf() {
@@ -269,13 +144,13 @@ public class JMXAccessorCondition extends ProjectComponent implements Condition
     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.
@@ -286,36 +161,6 @@ public class JMXAccessorCondition extends ProjectComponent implements Condition
     }
 
     /**
-     * Get JMXConnection (default look at <em>jmx.server</em> project reference from jmxOpen Task)
-     * @return active JMXConnection
-     * @throws MalformedURLException
-     * @throws IOException
-     */
-    protected MBeanServerConnection getJMXConnection()
-            throws MalformedURLException, IOException {
-        return JMXAccessorTask.accessJMXConnection(
-                getProject(),
-                getUrl(), getHost(),
-                getPort(), getUsername(), getPassword(), ref);
-    }
-
-    /**
-     * Get value from MBeans attribute
-     * @return The value
-     */
-    protected String accessJMXValue() {
-        try {
-            Object result = getJMXConnection().getAttribute(
-                    new ObjectName(name), attribute);
-            if(result != null)
-                return result.toString();
-        } catch (Exception e) {
-            // ignore access or connection open errors
-        }
-        return null;
-    }
-
-    /**
      * test the if condition
      * @return true if there is no if condition, or the named property exists
      */
@@ -345,15 +190,16 @@ public class JMXAccessorCondition extends ProjectComponent implements Condition
      */
     @Override
     public boolean eval() {
+        String value = getValue();
         if (operation == null) {
             throw new BuildException("operation attribute is not set");
         }
         if (value == null) {
             throw new BuildException("value attribute is not set");
         }
-        if ((name == null || attribute == null)) {
+        if ((getName() == null || getAttribute() == null)) {
             throw new BuildException(
-                    "Must specify a 'attribute', name for equals condition");
+                    "Must specify an MBean name and attribute for condition");
         }
         if (testIfCondition() && testUnlessCondition()) {
             String jmxValue = accessJMXValue();
diff --git a/java/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java b/java/org/apache/catalina/ant/jmx/JMXAccessorConditionBase.java
similarity index 61%
copy from java/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java
copy to java/org/apache/catalina/ant/jmx/JMXAccessorConditionBase.java
index b001c0e..8348213 100644
--- a/java/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java
+++ b/java/org/apache/catalina/ant/jmx/JMXAccessorConditionBase.java
@@ -14,7 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.catalina.ant.jmx;
 
 import java.io.IOException;
@@ -23,52 +22,10 @@ import java.net.MalformedURLException;
 import javax.management.MBeanServerConnection;
 import javax.management.ObjectName;
 
-import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.ProjectComponent;
 import org.apache.tools.ant.taskdefs.condition.Condition;
 
-/**
- *
- * Definition
- * <pre>
- *   &lt;path id="catalina_ant">
- *       &lt;fileset dir="${catalina.home}/server/lib">
- *           &lt;include name="catalina-ant.jar"/>
- *       &lt;/fileset>
- *   &lt;/path>
- *
- *   &lt;typedef
- *       name="jmxEquals"
- *       classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition"
- *       classpathref="catalina_ant"/>
- * </pre>
- *
- * usage: Wait for start backup node
- * <pre>
- *     &lt;target name="wait"&gt;
- *        &lt;waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" &gt;
- *           &lt;and&gt;
- *               &lt;socket server="${server.name}" port="${server.port}"/&gt;
- *               &lt;http url="${url}"/&gt;
- *               &lt;jmxEquals
- *                   host="localhost" port="9014" username="controlRole" password="tomcat"
- *                   name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
- *                   attribute="connected" value="true"
- *               /&gt;
- *           &lt;/and&gt;
- *       &lt;/waitfor&gt;
- *       &lt;fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" /&gt;
- *       &lt;echo message="Server ${url} alive" /&gt;
- *   &lt;/target&gt;
- *
- * </pre>
- *
- * @author Peter Rossbach
- * @since 5.5.10
- */
-public class JMXAccessorEqualsCondition  extends ProjectComponent  implements Condition {
-
-    // ----------------------------------------------------- Instance Variables
+public abstract class JMXAccessorConditionBase extends ProjectComponent implements Condition {
 
     private String url = null;
     private String host = "localhost";
@@ -79,24 +36,6 @@ public class JMXAccessorEqualsCondition  extends ProjectComponent  implements Co
     private String attribute;
     private String value;
     private String ref = "jmx.server" ;
-    // ----------------------------------------------------- Instance Info
-
-    /**
-     * Descriptive information describing this implementation.
-     */
-    private static final String info = "org.apache.catalina.ant.JMXAccessorEqualsCondition/1.1";
-
-    /**
-     * 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
 
     /**
      * @return Returns the attribute.
@@ -206,6 +145,14 @@ public class JMXAccessorEqualsCondition  extends ProjectComponent  implements Co
         this.ref = refId;
     }
 
+    /**
+     * Get JMXConnection (default look at <em>jmx.server</em> project reference
+     * from jmxOpen Task).
+     *
+     * @return active JMXConnection
+     * @throws MalformedURLException
+     * @throws IOException
+     */
     protected MBeanServerConnection getJMXConnection()
             throws MalformedURLException, IOException {
         return JMXAccessorTask.accessJMXConnection(
@@ -215,6 +162,8 @@ public class JMXAccessorEqualsCondition  extends ProjectComponent  implements Co
     }
 
     /**
+     * Get value from MBeans attribute.
+     *
      * @return The value
      */
     protected String accessJMXValue() {
@@ -228,22 +177,5 @@ public class JMXAccessorEqualsCondition  extends ProjectComponent  implements Co
         }
         return null;
     }
-
-    // This method evaluates the condition
-    @Override
-    public boolean eval() {
-        if (value == null) {
-            throw new BuildException("value attribute is not set");
-        }
-        if ((name == null || attribute == null)) {
-            throw new BuildException(
-                    "Must specify a 'attribute', name for equals condition");
-        }
-        //FIXME check url or host/parameter
-        String jmxValue = accessJMXValue();
-        if(jmxValue != null)
-            return jmxValue.equals(value);
-        return false;
-    }
 }
 
diff --git a/java/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java b/java/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java
index b001c0e..16a9ae1 100644
--- a/java/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java
+++ b/java/org/apache/catalina/ant/jmx/JMXAccessorEqualsCondition.java
@@ -14,18 +14,9 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.catalina.ant.jmx;
 
-import java.io.IOException;
-import java.net.MalformedURLException;
-
-import javax.management.MBeanServerConnection;
-import javax.management.ObjectName;
-
 import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.ProjectComponent;
-import org.apache.tools.ant.taskdefs.condition.Condition;
 
 /**
  *
@@ -66,20 +57,7 @@ import org.apache.tools.ant.taskdefs.condition.Condition;
  * @author Peter Rossbach
  * @since 5.5.10
  */
-public class JMXAccessorEqualsCondition  extends ProjectComponent  implements Condition {
-
-    // ----------------------------------------------------- Instance Variables
-
-    private String url = null;
-    private String host = "localhost";
-    private String port = "8050";
-    private String password = null;
-    private String username = null;
-    private String name = null;
-    private String attribute;
-    private String value;
-    private String ref = "jmx.server" ;
-    // ----------------------------------------------------- Instance Info
+public class JMXAccessorEqualsCondition extends JMXAccessorConditionBase {
 
     /**
      * Descriptive information describing this implementation.
@@ -92,157 +70,25 @@ public class JMXAccessorEqualsCondition  extends ProjectComponent  implements Co
      * <code>&lt;description&gt;/&lt;version&gt;</code>.
      */
     public String getInfo() {
-
-        return (info);
-
-    }
-    // ----------------------------------------------------- Properties
-
-    /**
-     * @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 host.
-     */
-    public String getHost() {
-        return host;
-    }
-    /**
-     * @param host The host to set.
-     */
-    public void setHost(String host) {
-        this.host = host;
-    }
-    /**
-     * @return Returns the name.
-     */
-    public String getName() {
-        return name;
-    }
-    /**
-     * @param objectName The name to set.
-     */
-    public void setName(String objectName) {
-        this.name = objectName;
-    }
-    /**
-     * @return Returns the password.
-     */
-    public String getPassword() {
-        return password;
-    }
-    /**
-     * @param password The password to set.
-     */
-    public void setPassword(String password) {
-        this.password = password;
-    }
-    /**
-     * @return Returns the port.
-     */
-    public String getPort() {
-        return port;
-    }
-    /**
-     * @param port The port to set.
-     */
-    public void setPort(String port) {
-        this.port = port;
-    }
-    /**
-     * @return Returns the url.
-     */
-    public String getUrl() {
-        return url;
-    }
-    /**
-     * @param url The url to set.
-     */
-    public void setUrl(String url) {
-        this.url = url;
-    }
-    /**
-     * @return Returns the username.
-     */
-    public String getUsername() {
-        return username;
-    }
-    /**
-     * @param username The username to set.
-     */
-    public void setUsername(String username) {
-        this.username = username;
-    }
-    /**
-     * @return Returns the value.
-     */
-    public String getValue() {
-        return value;
-    }
-    // The setter for the "value" attribute
-    public void setValue(String value) {
-        this.value = value;
+        return info;
     }
 
-    /**
-     * @return Returns the ref.
-     */
-    public String getRef() {
-        return ref;
-    }
-    /**
-     * @param refId The ref to set.
-     */
-    public void setRef(String refId) {
-        this.ref = refId;
-    }
-
-    protected MBeanServerConnection getJMXConnection()
-            throws MalformedURLException, IOException {
-        return JMXAccessorTask.accessJMXConnection(
-                getProject(),
-                getUrl(), getHost(),
-                getPort(), getUsername(), getPassword(), ref);
-    }
-
-    /**
-     * @return The value
-     */
-    protected String accessJMXValue() {
-        try {
-            Object result = getJMXConnection().getAttribute(
-                    new ObjectName(name), attribute);
-            if(result != null)
-                return result.toString();
-        } catch (Exception e) {
-            // ignore access or connection open errors
-        }
-        return null;
-    }
-
-    // This method evaluates the condition
     @Override
     public boolean eval() {
+        String value = getValue();
+
         if (value == null) {
             throw new BuildException("value attribute is not set");
         }
-        if ((name == null || attribute == null)) {
+        if (getName() == null || getAttribute() == null) {
             throw new BuildException(
-                    "Must specify a 'attribute', name for equals condition");
+                    "Must specify an MBean name and attribute for equals condition");
         }
         //FIXME check url or host/parameter
         String jmxValue = accessJMXValue();
-        if(jmxValue != null)
+        if (jmxValue != null) {
             return jmxValue.equals(value);
+        }
         return false;
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org