Monday, February 28, 2011

XML validation: fail-slow and capture all the errors

see here for the whole story:

Validating a XML document capturing ALL the validation errors is a bit roundabouthish:

first you must create a ErrorHandler:

public static class XMLValidationErrorHandler implements ErrorHandler {
  List errors = new ArrayList();

  public void warning(SAXParseException ex) {
   errors.add(new ValidationError(ex.getLocalizedMessage()));
   logger.error("ErrorHandler warning " + ex.getLocalizedMessage());

  public void error(SAXParseException ex) {
   errors.add(new ValidationError(ex.getLocalizedMessage()));
   logger.error("ErrorHandler error " + ex.getLocalizedMessage());

  public void fatalError(SAXParseException ex) {
   errors.add(new ValidationError(ex.getLocalizedMessage()));
   logger.error("ErrorHandler fatalError " + ex.getLocalizedMessage());

  public List getErrors() {
   return errors;

and then assign it to the unmarshaller:
List errors = new ArrayList();
byte[] bytes = xmlAsString.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
Source paramSource = new StreamSource(bais);

Schema schema = unmarshaller.getSchema(); 
Validator validator = schema.newValidator();

XMLValidationErrorHandler errorHandler = new XMLValidationErrorHandler();

errors = errorHandler.getErrors();

I really feel that things should be easier than that...

Saturday, February 26, 2011

Quick Java code to convert a Java object into XML

I keep having to do it and I keep forgetting...

private static String objectToXML(Object object, Class clazz) throws JAXBException,
 PropertyException {

 JAXBContext jc = JAXBContext.newInstance(clazz);
 Marshaller marshaller = jc.createMarshaller();
 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
 ByteArrayOutputStream baos = new ByteArrayOutputStream();   
 marshaller.marshal(object, baos);
 return new String(baos.toByteArray());

XML Streaming Parsing: a desert populated by skinny animals

Having to implement a XML Streaming parser is no fun these days.

On one hand, you have a solid technology which is JAXB. Wonderful. But it doesn't handle streaming. So if you have a 1 GB document to parse, you are screwed.

On the other hand, you have SAX and StAX.... well I have played a few hours with StAX, and I can't really see the improvement over SAX, apart from the silly "pull" approach over the "push" approach... so what? Basically you still have to handle individual atomic events (startElement, characters, stopElement) and manually reconstruct your Java XmlEntity from them.... painful if you have complex Entities...

StAX is useful only if you need to parse bits and pieces of information here and there, without having to parse the whole thing. Otherwise, FIASCO.

Finally I resorted to using the excellent java.util.Scanner class to locate the start of an element, and read until the end of the element into a String, then use JAXB to parse the String into a XmlEntity.... it works like a charm.

you just need to annotate your entity as @XmlRootElement - no worries you can have multiple XmlRootElement in your file, and you can annotate even a static class.

I see here a big need for a streaming technology that can still make use of JAXB... it seems not too difficult to implement... or some adjustments on StAX so that it can call JAXB...

Friday, February 25, 2011

javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: bla

org.hibernate.PropertyValueException is a nice animal, containing a lot of useful info:


so instead of doing yourself the validation on all the properties to be persisted, you can delegate Hibernate.

The problem with Hibernate is that it's a fail-fast validation: the first invalid property (null or referencing an non existing entity) will throw an exception.

If you want to capture ALL the validation errors, then you need to do it individually testing each property.

Thursday, February 24, 2011

Hibernate generating IDs from a Sequence

on the Entity:

@SequenceGenerator(name="PRODUCT_PROID_GENERATOR", sequenceName="PRO_ID_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PRODUCT_PROID_GENERATOR")
@Column(name="PRO_ID", unique=true, nullable=false, precision=12)
private long proId;

and on the DB:


funnily the values assigned are:

here a decent explanation about hilo :

so adding allocationSize=1 to @SequenceGenerator brings the behaviour back to normal

Finding a file in Unit Tests

A common problem is if you need to access a file in a JUnit test in Eclipse and also in the unit tests run in Hudson.
For Eclipse, if you do

File dir = new File(".");

this directory will point to the root directory of the current project.
So all relative paths should refer to this root directory.

In Hudson, the mapping is not quite the same.

So in my Unit tests I use:

import org.unitils.core.Unitils;
import org.unitils.dbunit.datasetfactory.DataSetResolver;
import org.unitils.dbunit.datasetfactory.impl.DefaultDataSetResolver;

        static DataSetResolver datasetResolver;

 public static void initUnitiles() {
  Unitils unitilsInstance = Unitils.getInstance();
  datasetResolver = new DefaultDataSetResolver();

   public void testIcoli() {

        File file = datasetResolver.resolve(MyTest.class, "sampleFile.xml");


and I place the sampleFile.xml in the same directory and the MyTest class...

Go look in the DefaultDataSetResolver code to see how the magic is done... it's quite easy anyway, just do

URL myURL = MyTestClass.getResource("myfile.xml");
File myFile = toFile(myURL);

(where toFile is an Apache Commons function).

Wednesday, February 23, 2011

Mapping with Dozer

I can't praise enough this little gem.

Just do:
MapperIF mapper = DozerBeanMapperSingletonWrapper.getInstance();

put dozerBeanMapping.xml in your classpath and in it you map:

  MM/dd/yyyy HH:mm


If you receive an error:

net.sf.dozer.util.mapping.MappingException: org.xml.sax.SAXException: Parsing Error

Message: Document root element "mappings", must match DOCTYPE root "null".

then you are screwed and probably give up using Dozer. I Hate Xml.

Tuesday, February 22, 2011

Java how to read a file line by line

I keep forgetting and I keep having to google it up...

public class MyFileReader {
 File file;
 FileReader fr;
 BufferedReader br = null;

 public MyFileReader(File f) throws FileNotFoundException {
  file = f;
  fr = new FileReader(file);
  br = new BufferedReader(fr);

 public void readAll() throws IOException {
  String line;
  while ( (line = br.readLine()) != null ) {



 public void close() {

How to write a file:

   FileWriter fstream = new FileWriter("out.txt");
        BufferedWriter out = new BufferedWriter(fstream);
    out.write("I Love Silvio (just kidding)");

Dump an object

I believe this code can be sometimes useful to debug...
it dumps the value of all methods of an object starting with "get", "is" or "has"...

 public String dumpObject(Object theObject) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
  StringBuilder result = new StringBuilder();
  for (Method method : theObject.getClass().getDeclaredMethods()) {
   String methodName = method.getName();
   if (methodName.startsWith("get") || methodName.startsWith("has") ||methodName.startsWith("is") && method.getParameterTypes().length == 0) {
    result.append(methodName).append("=").append(method.invoke(theObject, null));
  return result.toString();

I am sure the same can be done with some Apache commons utility....

Monday, February 21, 2011


I had a quick look at Vaadin, it looks really the Holy Grail of Web Development... no more JSPs and tag libraries...

we shall see if the myth will survive impact with reality....

Here a good tutorial

Saturday, February 19, 2011

Grails BeanBuilder to generate DB test data

It's such a pain to maintain XML to feed the Database with test data with Unitils.

I am looking into Groovy-based DSLs to do the same. Talk about Spock, EasyB and other techniques. In the meantime, the BeanBuilder seems a winner for the simplicity to generate XML expressing it as embedded beans. NO MORE XML. If you find yourself coding XML, it can only mean you are doing something very wrong.

Java Code Geeks

I have just added this blog to my google reader
It's REALLY interesting!

use this button to add it to your Google reader:

Add to Google

Agile poll result

most voters are quite comfortable about Agile, although there is still some skepticism or unawareness about the methodology. Honestly I was expecting much more adversity.

I have worked on a project where "Agile" was invoked as a pretext to be just lazy and bullshitters, so I have grown some hostility against Agile. In normal projects, where people are honest and competent, being Agile and XP (open to communication, short iterations, constant verification, automation, peer programming...) is the best way to deliver quickly some working code.


Presentation here.

Using LoggingStopWatch (start and stop) is a cleaner way of logging performance records.

Later you can process the perf log fils with LogParser to display statistics. You can even generate graphics.

AsyncCoalescingStatisticsAppender allows buffering of log events.

JmxAttributeStatisticsAppender allows you to monitor performance through JConsole

You can use @Profiled annotation to monitor a method
Also, you can weave perf4j monitoring with Spring or AspectJ.

Thursday, February 17, 2011

DataSet annotation in Unitils

a cool feature is to initialize the DB before each test.

Since most data are common to all tests, it pays to factor them out to a shared DataSet (xml file)

Unitils supports this feature and this is the syntax:


Wednesday, February 16, 2011

JpaUnitils.assertMappingWithDatabaseConsistent() is your friend

This baby detects the differences between your schema and your entities, and generates the DDL to adjust your schema to the Entities (of course if you use a schema-first approach you would run the DDL on the Entities, not on the schema!)


Funnily enough, I have discovered that running the same tests using different versions of Oracle DB (Oracle XE and the "real" Oracle DB) can give different results concerning the DB Metadata, speficically for the error:

junit.framework.AssertionFailedError: Found mismatches between Java objects and database tables. Applying following DDL statements to the database should resolve the problem:

(list of DDL commands follows)

Tuesday, February 15, 2011

Unitils: @InjectIntoByType and @TestedObject

AlertService alertService = new AlertService();

Mock mockSchedulerService;
Mock mockMessageService;

the target="alertService" is not required, since there is only one TestedObject.

instead of passing the Mock objects in the AlertService constructor, we can simply inject them using InjectIntoByType.

What I love is that no XML is required. XML sucks because it's too loosely connected with code.

See AlertServiceAnnotatedTest in for a working example.

Again, it's a pain to get all the required jars in the classpath... welcome to Java world :o(

Eclipse unable to create a DBConnection

The custom JDBC URL is:


In SQL Developer you can specify a custom URL;

In Eclipse, no way, you must enter the individual fields and no matter what, it keeps appending a ":" after 389

The only workaround is to create a dummy connection, then edit the SID in the "Database Connections" "properties".... a terrible hack for a terrible product (Eclipse)

You should set this:

PORT NUMBER=//oid.acme


a simple and practical example on how it works. Javadocs say that "it is strongly recommended that all serializable classes explicitly declare serialVersionUID values"...

IMHO if you declare the serialVersionUID and don't change it when you modify the class, it's USELESS. So either you have an automated mechanism to change it every time the Class is checked in, otherwise simply DON'T DECLARE IT and let the JVM work a little harder.

If this really becomes a performance issue (and you will discover it when you profile your application) then it will take you 10 minutes to add it to all your classes.

Remember to add @SuppressWarnings("serial")

Monday, February 14, 2011

JPA mapping boolean to CHAR(1) yes_no

After having googled around and found this and this, I have decided for the simplest possible solution: use a char in the Entity and expose it with a boolean getter/setter - anyway JPA uses "field" access to the Entity, so she is not disturbed by my setters and getters.

private char sysparam = 'N';

public void setSysparam(boolean sysparam) {
 this.sysparam = sysparam ? 'Y' : 'N';

public boolean getSysparam() {
 return sysparam == 'Y';

Incidentally all my boolean fields are NOT NULL, so I use directly a boolean rather than a Boolean.

It's a shame that JPA doesn't cover something so basic. Everybody knows that Oracle doesn't support a BOOLEAN data type; so I expect everyone is having the same problem.

Comments on Tables and Columns

One can stick a comment to a COLUMN or TABLE

This is an excellent candidate to "annotate" a schema and having an automated tool process it and generate all the Entities and DAOs, using for instance the class to introspect a schema....

One can ask: why don't you create a metamodel and generate the DDL from it, rather than trying to express everything in DDL?
Here is why: DDL is too essential to risk missing some feature by inventing a metamodel. I would rather have the DDL as the FOUNDATION of my application, rather than an artifact gnerated by some script. I think DBAs would agree with me.

Sunday, February 13, 2011

Small Groovy Script to filter a file

Groovy makes life sooo easy...

this small script prints only lines who do not start with file:///

I know it's much simpler in grep, but try doing something more complex with grep and awk and you will waste the entire day...

package com.pierre

datafile = new File('C:/pierre/download/myfile.txt') 
outdatafile = new File('C:/pierre/download/myfile.txt.out')
PrintWriter pw = new PrintWriter(outdatafile)

 line -> 
 if (!line.startsWith("file:///")) {

Friday, February 11, 2011

JPA Entity: extending vs embedding

JPA defines 2 annotations: @Embeddable and @Embedded
This is a great way to COMPOSE objects - rather than inheriting.

Suppose you have to incorporate in your entities several features:
tracking, logical deletion, id....

one solution is to stick all the co,,on features in a base abstract Entity.
The problem arises when not ALL Entities use ALL the features...; then you should introduce several base classes... this could soon become unmanageable.

The alternative is to Embed only the Embeddable entities that you want to incorporate.

The problem is that it's not easy to determine which Embeddable Entities are embedded in a given Entity.... while when using abstract classes or interfaces you can use instanceof.

Unitils mocks: easy and cool

I am not a fan of EasyMock, I think mockito is far superior.

Yet I found Unitils mocks - which are based on EasyMock - quite easy to use.

Here a sample Eclipse project to illustrate the mocks - it's basically takes from Unitils tutorial

The only hassle is putting together all the required jars (adjust .classpath file)

Thursday, February 10, 2011

Oracle: drop table if exists

something quite annoying in Oracle is that if you do

drop table MYTABLE;

and the table doesn't exist, you get an error.

A workaround can be:


Wednesday, February 9, 2011

Primary keys and Unique keys

Today some philosophical debate on whether one should absolutely have a primary key or not, and if one can use Foreign Key to point to a column which is not a Primary Key.

The answer is that, basically, ANY column can be used as a Primary Key (=something that can be used to identify a record without ambiguity) as long as it's UNIQUE and NOT NULLABLE.
Primary Key is just a convenient term for the developer, but carries no exclusive property of its own.

Specifically on Oracle you might find that the INDEX associated to a PK is CLUSTERED, while for a Unique Key it is not clustered.

Jar Finder home grown

I republish here my beloved utility to dump the content of all ZIP and JAR files....

I change and use it often, so better have it as a post, rather than in SVN.

package com.pierre.jar;

import java.util.Enumeration;

public class JarSearcher {
 private static final String BASEDIR = "C:\\ac,e\\repository\\central\\libs";
 //private static final String BASEDIR = "C:\\Temp";
 static FileWriter fstream ;
 static BufferedWriter out;

 public static void main(String[] args) throws Exception {
  fstream = new FileWriter("c:\\out.txt");
  out = new BufferedWriter(fstream);
  JarSearcher jarSearcher = new JarSearcher();
  jarSearcher.findTest(BASEDIR, "");

 private void findTest(String dirString, String text) throws ZipException, IOException {
  File dir = new File(dirString);
  for (File f : dir.listFiles()) {
   if (f.isDirectory()) {
    findTest(f.getAbsolutePath(), text);
   else {
    String name = f.getName();
    if (name.endsWith(".jar") || name.endsWith(".zip")) {
     System.out.println("SEARCHING " + f.getAbsolutePath());
     out.write("===" + f.getAbsolutePath() + "===\n");
     try {
      ZipFile zipFile = new ZipFile(f);
      Enumeration e =  zipFile.entries();
      while (e.hasMoreElements()) {
       ZipEntry nextElement = e.nextElement();
       InputStream is = zipFile.getInputStream(nextElement);
       if (text.length() == 0 || findInInputStream(is, text)) {
        System.out.println("FOUND ENTRY " + nextElement.getName() + " in file " + f.getAbsolutePath());
        out.write(nextElement.getName() + "\n");
     catch (Exception e) {
    else {


 private boolean findInInputStream(InputStream is, String text) throws IOException {
  boolean result = false;
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  String line = null;
  while ( (line = br.readLine()) != null && result == false) {
   if (line.contains(text)) {
    result = true;
  return result;

Build path entry is missing: org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6

Build path entry is missing: org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6

go to the project's .classpath file,
you find:

classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"

make sure that JavaSE-1.6 is actually registered as a JVM in your Eclipse.

Otherwise you might have to change it, using for instance J2SE-1.5

Oracle DB: difference between date and timestamp

basically: use DATE in "normal" cases, and TIMESTAMP when computing time intervals is important.

in SQLDeveloper:

create table TESTDATES (
column1 date,
column2 timestamp(6)

first do this to display dates in all their splendor:

alter session set NLS_DATE_FORMAT='DD/MM/YYYY HH24:MI:SS';

If you use the JPA wizard in Eclipse, you will find that DATE is mapped to a java.util.Date (with a @Temporal annotation), while TIMESTAMP is mapped to a java.sql.Timestamp

@Temporal( TemporalType.DATE)
private Date column1;

private Timestamp column2;

All you wanted to know about JPA

it's a really good source of information... I love wikified information, so easy to navigate.

Tuesday, February 8, 2011

Funny DBMAINTAIN issue

WARNING: Executed scripts table "DBMAINTAIN"."DBMAINTAIN_SCRIPTS" doesn't exist yet or is invalid. A new one is created automatically.
Exception in thread "main" java.lang.AbstractMethodError: oracle.jdbc.driver.OracleDatabaseMetaData.getDatabaseMajorVersion()I

I am using database.driverClassName=oracle.jdbc.driver.OracleDriver
and I put DBMAINTAIN_JDBC_DRIVER=C:/Oracle2/Middleware/oracle_common/oui/jlib/classes12.jar

Possibly changing the jar file could solve the issue.
Yet I found out that creating manually the DBMAINTAIN_SCRIPTS table solves the issue:


Monday, February 7, 2011

Domain-Driven Design book

I hear good things about this book.... it's in my to-read list.

Here  the "Quickly" version

A quote from the preface.... how true!

"We, in the enterprise development community, especially the web development community, have been tainted by years of hype that took us away from proper object oriented software development."

All that SOA hype has generated too many "spaghetti service buses".

useful concepts:

Layers: UI (=jsp), Application (=actions), Domain, Infrastructure (=persistence)

Ubiquitous Language
Entity (has identity, trackable)
Value Object (no identity associated, throwaway objects)
Service (holds cross-entities logic)

Module (contains and partitions model)

Yet another BPEL book for the coming Spring: BPEL Cookbook

The author is co-author of the other book on BPEL from PacktPub.

Saturday, February 5, 2011

Wonderful Grails

I am learning Grails over this weekend..... It's touching in its simplicity.

remember you can change your Tomcat port number

grails -Dserver.port=8090 run-app

Tutorial here


grails create-app cmdb

Friday, February 4, 2011

Rooms for rent in Brussels - chambres a louer a Bruxelles

some useful links here:

Bruxelles is quite cheap - at least compared to Roma, London or Paris.


This thing seems very useful at applying incremental patches to a (multiple) DB.


an incremental script can be executed only once, unless you specify fromScratch - which destroys the DB and recreates it again.

repeatableScripts can be executed as many times as you want

you can pack scripts in a jar (ant createScriptArchive).

supports multiple databases, using @ you can switch

supports out-of-sequence scripts, using #patch

.... to be continued ....

Thursday, February 3, 2011

Failed while installing Axis2 Web Services Core 1.1

I am using Eclipse
Version: 3.6.1
Build id: M20100909-0800

I try to add Axis2 facet to a Web Service project, I get:

Unable to add the follwing facets to project : Axis2 Web Services Core, Axis2 Web Services Extensions

Failed while installing Axis2 Web Services Core 1.1

at org.eclipse.wst.common.project.facet.core.internal.FacetedProject.callDelegate(
... 8 more

In fact this turns out to be a BUG

See also

To configure the Axis2 runtime:

in Windows/Preferences/Web Services/Axis2 Preferences set the runtime location

Windows/Preferences/Web Services/Server and Runtime/Apache Axis2

Dynamic Web Module should be set to 2.5 - they say.

Guys, this is ridiculous, in Eclipse 3.6 one cannot develop Axis2 Web Services. Simply OBSCENE.

java.lang.IllegalStateException: EMF has not been initialized yet!

have you remembered to setup your JTA Datasource in the persistence.xml ? Remember to provide the JNDI name for the DS, not the logical name.

Is it pointing to a valid DS in WebLogic?
Probably the EntityManagerFactory didn't start because of invalid DS. Check the logs.

Thanks Dimitri for pointing this out :o)

Wednesday, February 2, 2011

Web Frameworks comparison

here a brilliant and thorough research on the topic

read slide 7 (or 8?) on Struts...