Previous Next

Technical Blog

June 24, 2010

One of the pain points that can arise during a development project is the payment gateway's support of recycled order numbers. Live production gateways will track all historical order numbers and generate a duplicate order number error if an existing order number is reused. This is fine in production but troublesome when the payment gateway test servers also behave in this manner. Most payment gateway providers I have worked with in the past offer duplicate checks within an hour or two on their test servers, but beyond that, order numbers can be recycled which is a good thing for development purposes. Some providers, however, appear to extend this duplicate check to a day, week, or may even provide no support for order number reuse.


If you use the database population scripts that come with Elastic Path to facilitate schema/data changes, this limitation can frustrate your development team, who will most likely end up manually tracking used order numbers and making frequent updates to TORDERNUMBERGENERATOR.


If your payment gateway provider doesn't support order number reuse or at least not to the frequency you desire, what can you do?

 

Some Alternatives

The simplest approach would be to externalize the initial order number to the env.config and assign everyone a unique order number block. The problem with this is that each environment would have to keep track of the last order number used and update the env.config with the next number in the block with every new database build.


Another approach would be to externalize the initial order number (or perhaps even the order number incrementer) to a central database. This database would have a simple mapping of environment to next order number and each environment would have a sufficient block assigned to last the duration of the project. The problem with this approach is that many teams/environments are distributed and you may not be able to set up a central database which can be accessed by all environments.

 

A Better Solution

A better solution (though not 100% foolproof) is to construct the initial order number during the database build process using a unique element of the environment as well as a random element to get around frequent rebuilds on the same environment. To do this, we can create a custom Ant task that takes a fixed component (derived from either a fixed prefix or the environment's IP address) and appends a randomly generated number. If the random number is sufficiently large, the likelihood of duplicate hits will be minimal.

 

Creating the Ant Task

The following is an example of a custom Ant task that accepts min and max parameters for the random number component and a prefix. Regardless of whether a fixed prefix or an environment's IP address was provided, the task will use the hash code of the fixed value for the resulting order number in order to work with the out of the box numerical order number incrementer.

public class OrderNumberGeneratorTask  extends Task {
    private String min = null;
    private String max = null;
    private String property = null;
    private String prefix = null;
    private Random random = new Random(System.currentTimeMillis());
 
    @Override
    public void execute() throws BuildException {
         String localPrefix = null;
        if (min == null || min.equals(""))
            throw new BuildException("Min property is missing.");
 
        if (max == null || max.equals(""))
            throw new BuildException("Max property is missing.");
 
        int minInt = Integer.parseInt(min);
        int maxInt = Integer.parseInt(max);
 
        if (minInt > maxInt)
            throw new BuildException("Min is bigger than max.");
 
        localPrefix = prefix;
        if (localPrefix == null) {
             try {
                  localPrefix = InetAddress.getLocalHost().getHostAddress();
               } catch (UnknownHostException e) {
                    throw new BuildException("Unable to get local host address.", e);
               }
        }
        int randomInt = calculateRandom(minInt, maxInt);
 
        getProject().setNewProperty(property, String.valueOf(localPrefix.hashCode()) + String.valueOf(randomInt));
    }
 
    protected int calculateRandom(final int minInt, final int maxInt) {
        return minInt + random.nextInt(maxInt - minInt + 1);
    }

 

Updating the Build Process

To use this task, we need to package the compiled class in a jar and add it to the Maven repository along with an entry in the ant/setup/pom.xml so that it gets included in the ant setup script. Once the class is available on the Ant class path, we add the following task definition and task invocation to the ant/default.xml setup file. This will create the order number and store it in the ep.initial.order.number variable.

<?xml version="1.0" encoding="UTF-8"?>

<project name="ant_default" xmlns:artifact="antlib:org.apache.maven.artifact.ant" xmlns:contrib="antlib:net.sf.antcontrib">

  <taskdef name="ordernumbergenerator" classname="com.elasticpath.antextension.OrderNumberGeneratorTask"></taskdef>
... 
  <ordernumbergenerator min="1" max="1000000" property="ep.initial.order.number"></ordernumbergenerator>
...

 

Now we can use this variable in the database/src/base-insert.xml.vm file as the initial order number of the TORDERNUMBERGENERATOR table.

     <Tordernumbergenerator Uidpk="1" NextOrderNumber="${ep_initial_order_number}" />
1 Comments Permalink

PMD contains a useful set of default rules for enforcing best practices in Java code, but in some circumstances it may be useful to create additional PMD rules for best practices that are specific to your company's internal coding standards, a particular Java framework (such as Spring, OpenJPA, DROOLS, etc), or specifics of the Elastic Path code-base. Capturing these best-practices in PMD rules is often more effective than simply posting a list of standards on a wiki, which can easily be ignored or forgotten. Adding custom PMD rules is relatively simple, but not immediately obvious. In this blog post, I will outline the steps for adding custom PMD rules to your Elastic Path project and provide a reference for PMD attributes which do not appear to be documented anywhere online.

Defining an XPath Expression for the Rule

There are two ways to create custom PMD rules:

  • Write a rule using Java
  • Write an XPath expression

 

XPath expressions are much quicker to create, and are flexible enough to identify most conditions. For the purposes of this blog post, I will only focus on XPath expressions. For details about creating rules using Java classes, read PMD - How to Write a Rule.

 

The best way to get started is to write a simple class or interface that violates the rule that you chose.  For example, best practices state that Spring setters should be public on the class but not exposed on the interface. So I will create a rule to identify Spring setters that are exposed on an interface. Here is my simple example:

public interface CustomerAuthenticationService {
    void setElasticPath(final ElasticPath elasticPath);
    void setStoreProductService(final StoreProductService storeProductService);
}

First, launch the PMD Rule Designer. This can be run from the command line by executing designer.bat (or designer.sh in Linux) from <PMD_ROOT>/bin.  Alternatively, this tool can be launched from Eclipse (if PMD for Eclipse is installed) by opening Window --> Preferences, then PMD --> Rules Configuration, and clicking the Rule Designer button. Paste your example code into the "Source Code" text box, and then click the Go button below the "XPath Query" text box. The designer should then show an Abstract Syntax Tree as shown in the following screenshot.

 

PMD Rule Designer 1.png

 

We can use the Abstract Syntax Tree (AST) to help us create an XPath expression to identify the problem code. Each node in the AST represents an element of the Java Syntactical Grammar representing the example code we specified.  The XPath expression should navigate through the grammar hierarchy and select elements that violate your chosen rule. PMD also supports the use of XPath attributes (in square brackets) to further filter specific grammar elements based on name, access modifiers, class types, level of nesting, existence of comments, etc. Unfortunately, these attributes are not listed in the Rule Designer or documented anywhere on the PMD site. At the end of this article, I've listed some of the more useful PMD attributes (pulled from existing PMD rules).

 

If your XPath syntax skills are a bit rusty, Microsoft has an excellent tutorial available. There is also a simple XPath Rule Tutorial available on the PMD site. You may also find it useful to review the XPath expressions for the default PMD rules in  <PROJECT_ROOT>/ant/target/pmd/pmd-elasticpath-rules.xml to get a better feel for available XPath grammar nodes and attributes.

 

Here is the XPath expression that I came up with for this rule:

//ClassOrInterfaceDeclaration[@Interface='true']/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration/MethodDeclarator[starts-with(@Image, 'set') and (ends-with(@Image, 'Service') or ends-with(@Image, 'ElasticPath') or ends-with(@Image, 'Helper') or ends-with(@Image, 'Utility') or ends-with(@Image, 'Engine'))

 

To test the XPath expression in the Rule Designer, simply paste the expression into the "XPath Query" field, and click Go again.  We know our XPath expression works because the field to the right of the Abstract Syntax Tree indicates the position of two matches from our example source code (one for each setter method). Notice that the XPath expression closely matches the node heirarchy in the Abstract Syntax Tree.

Adding the Rule to the Ruleset File

Now that we have created the XPath expression for our rule, we need to add it to our project's ruleset file. Open <PROJECT_ROOT>/ant/target/pmd/pmd-elasticpath-rules.xml in a text editor and add the following immediately before the terminating </ruleset> tag:

<rule name="SpringInjectedSetterInInterface" 
      message="Spring-injected setters should not be declared in interface." 
      class="net.sourceforge.pmd.rules.XPathRule" dfa="false" externalInfoUrl="" typeResolution="true">
    <description>Spring-injected setters should not be declared in interface.</description>
    <priority>3</priority>
    <properties>
        <property name="xpath">
            <value><![CDATA[//ClassOrInterfaceDeclaration[@Interface='true']/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration/MethodDeclarator[starts-with(@Image, 'set') and (ends-with(@Image, 'Service') or ends-with(@Image, 'ElasticPath') or ends-with(@Image, 'Helper') or ends-with(@Image, 'Utility') or ends-with(@Image, 'Engine'))]]]></value>
        </property>
    </properties>
    <example><![CDATA[
public interface CustomerAuthenticationService {
     void setElasticPath(final ElasticPath elasticPath);
     void setStoreProductService(final StoreProductService storeProductService);
}
    ]]></example>
</rule>

The rule tag contains the following attributes:

  • name: The rule name which will be used for suppression annotations and for identifying the rule in build-stats.
  • message: The friendly rule description which will be displayed when the rule is violated.
  • class: For XPath-based rules, this should always be set to "net.sourceforge.pmd.rules.XPathRule"
  • dfa: Data Flow Analysis - this should always be set to "false" for XPath rules.
  • externalInfoUrl: If there is a web page that provides additional information about the violation, provide the link here.
  • typeResolution:This should always be set to "false" for XPath rules.

 

The rule tag also contains the following inner tags:

  • description: This should be the same as the rule tag message attribute value.
  • priority: An integer value in the range 1 to 5:
    • 1 = Error High - Change absolutely required. Behavior is critically broken/buggy.
    • 2 = Error - Change highly recommended. Behavior is quite likely to be broken/buggy.
    • 3 = Warning High - Change recommended. Behavior is confusing, perhaps buggy, and/or against standards/best practices.
    • 4 = Warning - Change optional. Behavior is not likely to be buggy, but more just flies in the face of standards/style/good taste.
    • 5 = Information - Change highly optional. Nice to have, such as a consistent naming policy for package/class/fields.
  • properties: This should contain a single <property> tag with name="xpath", containing a <value> tag with the XPath expression.
  • example: A simple example of the rule being violated.  It is often best to simply use the class or interface that you created for the Rule Designer when designing your XPath expression.

Making the Rule Available to Ant and PMD for Eclipse

Once the ruleset file has been updated, we can immediately run PMD from the command line by running ant pmd-all from the project root, or ant pmd from a specific project folder.

 

To detect the new rule from Eclipse using the PMD-Eclipse plugin, follow these steps:

  1. From the root of the EP source directory, run ant eclipse-setup-all.
  2. Refresh all projects in Eclipse.
  3. Right-click on the project and click PMD->Check Code with PMD.
  4. Wait for "Review Code" task to complete.

 

The new rule violations should now appear in your "Problems" view:


PMD_Problems_1.png

Custom PMD rules are an effective way to automatically enforce coding standards and best practices for all developers in a project. Since rule checking is automatic, developers don't need to know all of the standards by heart; they can learn the rules by breaking them and getting immediate feedback. This helps to train developers to create code that is more consistent and readable by all members of the team. There are even circumstances where PMD can identify bugs that wouldn't get caught by the Java compiler.

 

For example, a team I worked on was responsible for continuous improvement on a project that involved extensive use of integration tests to validate core functionality. Most of the integration classes extended an abstract class that provided helper methods and a constructor for setting up the initial database. Unfortunately, some of the test classes defined their own constructors that would re-initialize the database, even though the abstract class had already done the initialization. Although this didn't cause any actual problems for the tests, it significantly slowed down execution of the tests. A PMD rule was written to identify these circumstances. By creating this rule, we were able to not only validate that all of the test classes were fixed, but we were also able to ensure that future developers didn't make the same mistake.

 

Try to identify common pitfalls for your own team's developers, and create custom PMD rules to flag such patterns for future developers. The following attributes are grouped based on which node type they are available for. This is not an exhaustive list, but it represents the more commonly-used attributes.

 

  • All 
    • @Image (string) - Class/method/variable name
  • ClassOrInterfaceDeclaration 
    • @Interface (boolean) - Is an interface
    • @Class (boolean) - Is a class
    • @Nested (boolean) - Is a nested class (or interface)
    • @Abstract (boolean) - Is an abstract class
  • Block 
    • @ContainsComment (boolean) - Contains a comment
  • Initializer 
    • @Static (boolean) - Is declared as static
    • @Final (boolean) - Is declared as final
  • IfStatement 
    • @Else (boolean) - Is else portion of an if statement
  • SwitchStatement 
    • @Default (boolean) - Is default section of a switch statement
  • AllocationExpression 
    • @ArgumentCount (int) - Number of arguments in expression
  • FieldDeclaration / ConstructorDeclaration / MethodDeclaration 
    • @Public(boolean) - Is declared as public
    • @Protected (boolean) - Is declared as protected
    • @Private (boolean) - Is declared as private
    • @Static (boolean) - Is declared as static
    • @Synchronized (boolean) - Is declared as synchronized
  • PrimaryExpression 
    • @ArrayDereference (boolean)
0 Comments Permalink