This page describes the standards used for Apache ServiceMix code (java, xml, whatever). Code is read by a human being more often than it is written by a human being, make the code a pleasure to read. If you're using Eclipse, configuration files for your IDE can be found at http://svn.apache.org/repos/asf/servicemix/smx3/trunk/etc/eclipse/

Indentation

Java

Lets follow Sun's coding standard rules which are pretty common in Java.

http://java.sun.com/docs/codeconv/

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

Correct brace style:

public class Foo {
    public void foo(boolean a, int x, int y, int z) {
        do {
            try {
                if (x > 0) {
                    int someVariable = a ?  x : y;
                } else if (x < 0) {
                    int someVariable = (y + z);
                    someVariable = x = x + y;
                } else {
                    for (int i = 0; i < 5; i++) {
                        doSomething(i);
                    }
                }

                switch (a) {
                    case 0:
                        doCase0();
                        break;
                    default:
                        doDefault();
                }
            } catch (Exception e) {
                processException(e.getMessage(), x + y, z, a);
            } finally {
                processFinally();
            }
        } while (true);

        if (2 < 3) {
            return;
        }

        if (3 < 4) {
            return;
        }

        do {
            x++
        } while (x < 10000);

        while (x < 50000) {
            x++;
        }

        for (int i = 0; i < 5; i++) {
            System.out.println(i);
        }
    }

    private class InnerClass implements I1, I2 {
        public void bar() throws E1, E2 {
        }
    }
}

XML

Interfaces

All methods of an interface are public abstract, therefore it is not necessary to specify public abstract modifiers. Similarly all fields are public static final.

However this behavior works best with most tools and IDEs and seems to be common practice so we see no reason to disallow this practice. e.g:

public interface MyInterface {
    public static final int MY_INTEGER = 0;

    public abstract void doSomething();
}

This has the added advantage that the interface can be converted into an abstract class (and copy and paste individual definitions) without changing anything.

Preferably add public/static/final to constants, and public/abstract to methods, but it's not mandatory. However, if it's there, don't take it out.

Exceptions

Package Naming

For example, if the module name is common, then the base package name should be org.apache.geronimo.common.

Note: This is more of a guideline than a rule, as some modules simply can not follow this convention, but where applicable they should.

Imports

IDE Auto-Formatting

The eclipse formater settings are available here.

JavaDoc Tags

@version Should be: @version $Revision$ $Date$

@author Should not be used in source code at all.

Unit Test Cases

Logging

Levels

Example

private static final Log log = LogFactory.getLog(MyClass.class);

public void doSomeStuff(Stuff stuff) throws StuffException {
    boolean logTrace = log.isTraceEnabled();
    try {
        if (logTrace) {
            log.trace("About to do stuff " + stuff);
        }
        stuff.doSomething();
        if (logTrace) {
            log.trace("Did some stuff ");
        }
    } catch (BadException e) {
        // don't log - leave it to caller
        throw new StuffException("Something bad happened", e);
    } catch (IgnorableException e) {
        // didn't cache this as we don't expect to come here a lot
        if (log.isDebugEnabled()) {
            log.debug("Ignoring problem doing stuff "+stuff, e);
        }
    }
}