Sun Coding Conventions for the Java Programming Language:
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
All imports must be single class and explicit. I.e. import <package>.* is not allowed.
All indentation levels should be 4 spaces. No editor tabs are allowed unless they are converted to 4 spaces before saving the file.
Two models are allowed (they must never be mixed within the same source file though, and should not be mixed within the same package):
1) Curly brackets ‘{}’are put on separate lines. E.g.
for (index = 0; index < length; index++)
{
<code>
}
2) As defined in the Java Coding guidelines. E.g.
for (index = 0; index < length; index++) {
<code>
}
No acronyms or abbreviations should be used. E.g. ‘a = b + mVarLen’ should be avoided and instead use: ‘totalLength = partLength + newLength’.
Use ‘this.’ as prefix when referencing instance variables, e.g.:
public MyClass (ServicePropertiesInterface properties) {
this.properties = properties;
}
public int foo () {
int localInt = 3;
return this.instanceInt + localInt;
}
Even single line statements should be inside brackets. E.g.
if (isEmpty) {
return;
}
The Jakarta Commons API should be used exclusively. Log4j.properties should be used for configuring logs. System.out/err.println is not allowed.
Each component/class should have a JUnit test The tests should be put in test/ directory under each package directory.
Treat all code as a library, and as a reusable component. Calls to System.exit() are disallowed (except the main method)
For local exceptions inherit from org.globus.ogsa.GridServiceException. If the exceptions should be exposed to remote clients define it in WSDL and extend the GridServiceFault defined in schema/core/faults/grid_service_fault.wsdl. Even though your wsdl operations don't throw any interface specific exception declare them to throw GridServiceFault.
Do not use local loop-back address "127.0.0.1". Use "localhost" instead or better yet use InetAddress.getLocalHost() to obtain the local ip address.
Do not create URL strings by hand. Use the URL class instead, preferably with URL(protocol, host, port, path) constructors. The URL(spec) constructor can also be used if the spec string can be specified in the configuration or such.
Avoid using proprietary or implementation-specific API. Use the standard API as much as possible. If it is not possible to use the standard API try to abstract the proprietary calls into a separate calls so that it can be later replaced with another implementation.
For example:
org.apache.axis.MessageContext
vs javax.xml.rpc.handler.MessageContext
org.apache.xerces.dom.DocumentImpl
vs org.globus.ogsa.utils.XmlFactory.newDocument
or javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
org.apache.xerces.parsers.DOMParser
vs javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder()
Whenever dealing with external resources such as files, network or database connections, etc. always make sure to release the resource after you are done with it. The simplest way to do that is to put the code that releases the resource in the finally
clause of the try { .. } catch { .. }
statement. The finally
clause is always executed in any condition. For example:
FileInputStream in = null; try { in = new FileInputStream("foo"); in.read(); ... } catch (IOException e) { System.err.println(e.getMessage()); } finally { // will always be executed if (in != null) { in.close(); } }