I use this blog for english improvement, now I want to use it for my picture-taking experience. If you have some objections give me response in comments.
This week photo (one of them) I want to share is:
There was a long time from java specification chapter four review. I think that one of the main reason is that chapter five is boring. Let’s start with it.
The best description in in JavaSpec-ch5 introduction which goes like this: Every expression has a type that can be deduced, it is possible to write an expression where the type is not appropriate, in some cases this lead to error at compile time, in other cases the context is able to accept a type.
Today we managed other cases






Thanks to maven, all Project I’m involved in can have the same reports. I will describe which reports I’m using and way.
Findbugs report, in my opinion is one of the most important reports. Here you find my config. Version 2.0 is in snapshot repository and I suppose it will be release in nearly future. This report provide as information about static analyses problems. I use it to discover common mistakes, and i configure it to Normal level which is ok to begin work with findbugs.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<threshold>Normal</threshold>
<effort>Max</effort>
</configuration>
</plugin>
Surefire & Cobertura
Next must to have duet is surfire plugin with cobertura. First one serves us test report (about execution) the second one serves us information about code coverage. The config looks like this. This duet runs test and provides report from them and code coverage information comes from cobertura.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<report>report-only</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
This duet search duplicated code, and report it. If you want to be DRY, you should use this.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<linkXref>true</linkXref>
<sourceEncoding>utf-8</sourceEncoding>
<targetJdk>1.5</targetJdk>
</configuration>
</plugin>
Checkstyle provides information about code style violation, I use it with own setting because in my opinion 80 character line for today screens is not so wise decision. And of course I can tune some of checkers.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugin>
This one provides information about dependencies in our code, so we can improve design. It also provides us some code metrics, which we can use to plan some refactoring.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jdepend-maven-plugin</artifactId>
</plugin>
Cross reference source generators used by other plugins to make links between raport and the code.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
</plugin>
Tag list plugin will keep all configured tags in one beautiful list.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>taglist-maven-plugin</artifactId>
<configuration>
<tags>
<tag>TODO</tag>
<tag>FIXME</tag>
<tag>@todo</tag>
<tag>@deprecated</tag>
</tags>
</configuration>
</plugin>
As everybody knows we should write javadocs, and if we … blah blah. Javadoc is ok, but what about javadoc and UML, you can do it. Simply add uml doclet config. Note that you must have UmlGraph installed. You find here more information.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<reportSets>
<reportSet>
<id>html</id>
<reports>
<report>javadoc</report>
</reports>
</reportSet>
<reportSet>
<id>doccheck</id>
<configuration>
<doclet>gr.spinellis.umlgraph.doclet.UmlGraph</doclet>
<docletArtifact>
<groupId>gr.spinellis.umlgraph</groupId>
<artifactId>umlgraph</artifactId>
<version>4.6</version>
</docletArtifact>
<additionalparam>-attributes</additionalparam>
<additionalparam>-enumerations</additionalparam>
<additionalparam>-enumconstants</additionalparam>
<additionalparam>-operations</additionalparam>
<additionalparam>-qualify</additionalparam>
<additionalparam>-types</additionalparam>
<additionalparam>-visibility</additionalparam>
<additionalparam>
-d ${project.build.directory}/site/doccheck
</additionalparam>
<destDir>doccheck</destDir>
<name>DocCheck</name>
<description>DocCheck documentation.</description>
</configuration>
<reports>
<report>javadoc</report>
</reports>
</reportSet>
</reportSets>
</plugin>
Info from our SCM repository.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-changelog-plugin</artifactId>
</plugin>
There is also project report with information about developer, scm and so on.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<report>cim</report>
<report>dependencies</report>
<report>issue-tracking</report>
<report>index</report>
<report>summary</report>
<report>scm</report>
<report>project-team</report>
</reports>
</reportSet>
</reportSets>
</plugin>
Do you use another reporting plugin? Fill free to share it into comments.
Bye
I found some interesting feature in springframework. Spring offers you to map sql error code to exception type. By default there is in springframework distribution file called sql-error-codes.xml which has definition for common databases ( DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase).
This keys are taken form java.sql.DatabaseMetaData databaseProductName property. Each driver should provide proper implementation. This post will provide you the way how to add another databases or extends definition to more sql codes.
As we read javadoc, the default file is bundled in spring.jar and it’s location is “org/springframework/jdbc/support/sql-error-codes.xml" so first solution is to simple extract this file, add what we want, and than put it back. This solution has many drawbacks e.g. when we upgrade we must repeat our steps once again. If we keep reading javadoc we read
Reads the default file in this package if not overridden by a file in the root of the class path (for example in the “/WEB-INF/classes” directory).
So the second option is to provide file called ‘sql-error-codes.xml’ in the root path. Lets start coding.
Our App.java is very simple.
public class App {
public static void main(String[] args) {
SQLErrorCodesFactory errorCodes = SQLErrorCodesFactory.getInstance();
SQLErrorCodes errorCode = errorCodes.getErrorCodes(“MySQL”);
}
}
If we run this in debug mode we can see something like this. This is default SQL error codes definition for MySQL database. Now we add the ‘sql-error-codes.xml’. I’ve just copied MySQL bean from default file and added some codes to badSqlGrammarCodes. This time the result look like this:
And here is execution log, interesting lines in bold:
Sep 2, 2008 9:54:46 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
Sep 2, 2008 9:54:46 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [sql-error-codes.xml]
Sep 2, 2008 9:54:46 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'MySQL':
replacing [Generic bean: ... defined in class path resource [org/springframework/jdbc/support/sql-error-codes.xml]]
with [Generic bean: ... defined in class path resource [sql-error-codes.xml]]
Sep 2, 2008 9:54:46 PM org.springframework.jdbc.support.SQLErrorCodesFactory
INFO: Found custom sql-error-codes.xml file at the root of the classpath
Sep 2, 2008 9:54:46 PM org.springframework.jdbc.support.SQLErrorCodesFactory
INFO: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
The point to remember is that our definition didn’t override all the definition but only definition with the same bean id. This is not so clear in SQLErrorCodesFactory javadoc, which suggest that we override all beans.
We can use this mechanism if we have different database, or if we want to extend or change sql code mapping to exception. The application is really simple but you can
download it, and try by yourself.
Bye.
My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. I started my journey with Java 1.1 with Thread and JDBC programing in 1998 as I worked for merlin.pl. In 1999 I've passed Java Programer Certificate for Java 1.2, and was solution architect of merlin.pl infrastructure when we was moving from pl/sql to J2EE. It was great performance optimization with 10 times more req/sec than in requirements and 85 times faster as original solution.
Currently I work as Expert Software Development Java at F.Hoffmann-La Roche. The company was founded in 1896 and today, Roche employs over 80.000 people. After work I'm involved in activities related to Scala/Lift, Ruby/Rails/Merb, Python/Django. This is because I try to be pragmatic also I'm focused on application performance and tuning with success in my daily work.
My Yoda's motto: Do, or do not. There is no try.