Add UML diagrams to the JavaDoc

It is possible to add UML diagrams to the JavaDoc generated during the build phase. The first thing to do is to install Graphviz which is an open source graph visualization software. After installation, add the bin folder (D:\Graphviz\bin for instance) to the PATH environment variable. Then configure the pom.xml : Here i chose to … Read more

Modularity and plug-ability features in Servlet 3.0 specification with Tomcat 9

Modularity and plug-ability features in Servlet 3.0 specification is also called web fragments. The JSR 315 (Java Servlet 3.0 specification) is already 9 years old. The final release dates back to december 10, 2009. https://jcp.org/en/jsr/detail?id=315 Web fragments are one of the main features of this JSR. A web fragment is an XML file named web-fragment.xml … Read more

Variable which is “effectively final” in Java 8

Java 8 has an interesting feature that I learnt recently. A variable can become “effectively final”. Very useful. For instance, the following class would not compile in Java 1.7. It would return the following error : “Cannot refer to the non-final local variable myVocabulary defined in an enclosing scope”. It would require to add the … Read more

How is the cyclomatic complexity of an algorithm calculated ?

When checking the issues detected by Sonar, i noticed a few issues related to cyclomatic complexity. The usual message is clear : The Cyclomatic Complexity of this method “checkSomething” is 22 which is greater than 12 authorized. Indeed, the method checkSomething has way too many if conditions : Question : how is the cyclomatic complexity … Read more

[GWT] How to print a widget or a page

To print a widget or an entire page with GWT, you can use JSNI and the JavaScript print function. A good practice is to put all the content you want to print inside a DIV tag, for instance : An id is assigned to that DIV tag (line 10) and a button is added to … Read more

[JPA 2] Criteria API and MetaModel

I want to mention the Criteria API, a very cool API which in my opinion is not used as much as it should be. The developers who implement the specification (JSR 317: Java Persistence 2.0) do an impressive work. Thanks to them we have an API that makes it possible to write type safe queries in an object oriented way.
Usually Java developers write queries using JPQL or they write native queries. The fact is that the API Criteria has a (small) learning curve : you have to explore the API and study some basic queries examples before writing your own queries. The first time you use it, it does not seem as intuitive as JPQL.

The Criteria API is particularly convenient when writing queries which do not have a static where clause. For instance in the case of a web page where the user can make a research based on optional criterias. Then the generated query is not always the same.

The Criteria API has its advantages and its disadvantages : it produces typesafe queries that can be checked at compile time but on the other hand queries can be a bit hard to read (apparently unlike QueryDSL or EasyCriteria).
Another advantage is that it can help to avoid SQL injection since the user input is validated or escaped by the JDBC driver (which is not the case with native queries).

To create typesafe queries, one uses the canonical metamodel class associated to an entity (an idea originally proposed by Gavin King, as far as i know). A possible definition of a metamodel class could be that one : it is a class that provides meta information about a managed entity. By default, it has the same name as the entity plus an underscore. For instance if an entity is called Employee then the metamodel class is called Employee_. It is annotated with the annotation javax.persistence.StaticMetamodel.
Fortunately you do not have to write them, you can generate them using an annotation processor, through Eclipse or a Maven plugin for instance.
I chose to generate the metamodel classes with the help of the Hibernate Metamodel Generator (an annotation processor) and the maven-processor-plugin maven plugin, at each build so that they are updated whenever the entities are modified. It is a good way to keep the metamodel classes up to date.

<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.0.5</version>
<dependencies>
<!-- Annotation processor to generate the JPA 2.0 metamodel classes for typesafe criteria queries -->
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>1.2.0.Final</version>
  </dependency>
</dependencies>
<executions>
  <execution>
    <id>process</id>
    <goals>
      <goal>process</goal>
    </goals>
    <phase>generate-sources</phase>
    <configuration>
      <outputDirectory>${project.basedir}/src/main/.metamodel/</outputDirectory>
      <processors>
        <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
      </processors>
    </configuration>
  </execution>
</executions>
</plugin>

<!--  add sources (classes generated inside the .metamodel folder) to the build -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
  <execution>
    <id>add-source</id>
    <phase>generate-sources</phase>
    <goals>
      <goal>add-source</goal>
    </goals>
    <configuration>
      <sources>
        <source>${basedir}/src/main/.metamodel</source>
      </sources>
    </configuration>
  </execution>
</executions>
</plugin>

And here is the kind of dynamic query one can create :

Read more

Read a properties file external to a webapp

Externalizing part of the configuration of a webapp in a .properties file that is located outside of that webapp (outside the WAR, the EAR …) is a frequent requirement and I initially thought that the PropertyPlaceholderConfigurer class, provided by the Spring framework and which is an implementation of the BeanFactoryPostProcessor interface, would help me to … Read more

M2E problem : Plugin execution not covered by lifecycle configuration

I am adding to the blogosphere of development-related blogs another post about the “Plugin execution not covered by lifecycle configuration” error. The M2E Plugin 1.0.0 is integrated into Eclipse Indigo (3.7). Apparently, M2E connectors, which are a bridge between Maven and Eclipse, require to provide a connector for every plugin that is used in the … Read more

Coding my first Android application

I finally got the time to develop my first small Android application.
Technical environment :
– Eclipse Indigo
– Android SDK 2.3.3 (Gingerbread)
– An Android phone with Android SDK 2.3.4

I tried to go a little bit further than the traditional “Hello World” sample.
The application, called TeamManagement, is still a simple one : type in a name of a project member and it will display what his role is and when he arrived in the project. That’s it. I explored some nice features : relative layout, autocompletion, popup, menu at the bottom, show/remove image etc.

Main

The project inside Eclipse has the following structure :

The file /MyProject/res/values/strings.xml is where the name of the application is set.

<string name="app_name">TeamManagement 1.0</string>

I chose to organize the layout programatically, instead of declaratively (main.xml) and created a single activity called MyProjectActivity :

Read more

Java puzzlers

This week I attended the very first and very well organized conference What’s Next ? in Paris and one of the speakers was Neil Gafter, co-author of the book “Java™ Puzzlers: Traps, Pitfalls, and Corner Cases” (2005).
He shared with us 2 or 3 puzzlers which I will share here too :
Question 1) What will the following program print ?

import java.util.Random;

public class Rhymes {
    private static Random rnd = new Random();

    public static void main(String[] args) {
        StringBuffer word = null;
        switch(rnd.nextInt(2)) {
            case 1: word = new StringBuffer('P');
            case 2: word = new StringBuffer('G');
            default: word = new StringBuffer('M');
        }
        word.append('a');
        word.append('i');
        word.append('n');
        System.out.println(word);
    }
}

Read more

My book on EJB 3 is now on sale

I received an email from the editor informing me that the book is now printed and will be available in bookstores next week (from July 5th, 2010). It is already available for sale on Amazon and ENI. An online version is also available. Here is the cover of the book :

First glance at Scala

Today I took a first glance at Scala at the Paris JUG monthly meeting. Scala is a programming language which runs on the Java Virtual Machine.
I have not compiled a single line of code in Scala yet but I saw enough code to get a decent idea of it.
When you think Scala, think about the functional programming paradigme. A function can be a value.
The first speaker, Sader Drobi, showed us a few examples of code to illustrate the advantages of passing a function as a parameter.

Read more

DAO design pattern in a session bean ?

I have recently been challenged by a colleague with a .Net background about the way I sometimes use session beans in web applications. Indeed, I like to implement DAOs as session beans. Some people argue that it’s best not to directly implement CRUD methods in session beans. The main reason being that they prefer to … Read more

Escaping quotes in Java

Here is a simple tip to replace a quote with a backslash and a quote : ‘ ==> \’ public static String echappeApostrophe(String s) { String temp; temp = s.replaceAll(“‘”,”\\\\’”); return temp; } I needed 4 backslashes To better understand what is happening here, i printed out a few values : String a = “\’”; … Read more