pátek 3. prosince 2010

Java: A Simple Assert Class for Design By Contract Checks

Very often, the programmer are lazy to include design by contract checks into the code. That means argument checking in the beginning of the method, checking of a wrong state in the method body, etc. The Java assert construct cannot be used for this purpose, since it is switched off by default (one has tu run java -ea to switch on the Java asserts). To simplify the design by contract checks, I have made three simple static methods:

/**
 * A static class for simple run-time checks. The methods from this class may (and should) be used to check arguments
 * and code for design-by-contract.
 * <p>
 * These are the methods for lazy programmers. If you like to write more complicated messages (and God bless you for
 * that), use the classic {@code if (...) throw ... } way.
 * <p>
 * It is not possible to switch off these checks, do not use them for any memory or CPU consuming operations.
 * 
 * @author xmedeko
 * 
 */
public class Assert {

 /**
  * Check, that the condition is {@code true}.
  * <p>
  * Usage:
  * 
  * <pre>
  * Assert.state(i &lt;= maxIter, &quot;iteration number exceeded&quot;);
  * </pre>
  * 
  * @param check
  * @param message
  *            An error message.
  */
 public static void state(boolean check, String message) {
  if (check)
   return;
  throw new IllegalStateException(message);
 }

 /**
  * Check, that the object is not null. 
  * <p>
  * Usage:
  * 
  * <pre>
  * Assert.notNull(entity, &quot;entity&quot;);
  * </pre>
  * 
  * 
  * @param o
  * @param message
  *            An error message.
  */
 public static void notNull(Object o, String message) {
  if (o != null)
   return;
  throw new NullPointerException(message);
 }

 /**
  * Check, that the argument fullfills the given condition.
  * <p>
  * Usage:
  * 
  * <pre>
  * Assert.argument(from &gt; 0, &quot;from has to be greater then zero&quot;);
  * </pre>
  * 
  * @param check
  * @param message
  *            An error message.
  */
 public static void argument(boolean check, String message) {
  if (check)
   return;
  throw new IllegalArgumentException(message);
 }

 /** No instances. */
 private Assert() {
  throw new UnsupportedOperationException();
 }

}

Note, to check argument for not null value, do not use
Assert.argument(obj != null, "obj is null");
but use instead:
Assert.argument(obj != null, "obj");

Also, do not put any String concatenation or other operation into the argument. These are the methods for lazy programmers. If you like to write more complicated messages (and God bless you for that), use the classic if (...) throw ... way.

Update 29.3.2011: I have just found out that Spring frameworks has org.springframework.util.Assert class, too.
Update 30.7.2012: Google Guava has a very similar solution in the class Preconditions.

čtvrtek 2. prosince 2010

ZK Calendar Colors

Unfortunately, colours for the ZK Calendar has to be specified in the Java. They cannot be set in a CSS file. Because writing hexadecimal colour codes directly in the code is error prone, I have created a simple enum constant with a set of ZK Calendar colors:

public enum ZkCalendarsColor  {
 GREY("#B4BAB7", "#72706F"),
 GREEN("#4CB052", "#0D7813"),
 KHAKI("#BFBF4D", "#88880E"),
 PURPLE("#B373B3", "#7A367A"),
 RED("#D96666", "#A32929"),
 BLUE("#668CD9", "#3467CE");
 
 private String contentColor;
 private String headerColor;
 
 private ZkCalendarsColor(String contentColor, String headerColor) {
  this.contentColor = contentColor;
  this.headerColor = headerColor;
 }

 public String getContentColor() {
  return contentColor;
 }

 public String getHeaderColor() {
  return headerColor;
 }
}

čtvrtek 14. října 2010

Fixing Line Terminators for Cygwin CVS

I have to use a Windows operating system (unfortunately :-)) and CVS in my current job. Thanks to the Cygwin, I can work with Windows almost as effectively as in Linux. The CVS (CVSNT) installation in Windows and other CVS tools installed in on my Windows box (TortoiseCVS, Eclipse) stores the CVS files with CRLF line terminators (EOLs). The Cygwin CVS client requires the classic UNIX LT terminator. Fortunatelly, the Windows CVS clients can read the UNIX-way text files, too. So, I have creates a simple bash script to forx this problem:
#!/bin/sh
find . -type f -path '*/CVS/*' -print0 | xargs -0 dos2unix 
Hope, someone may find it useful.

úterý 31. srpna 2010

JasperReports PDF Font Mapping

Are your font bold and italic font properties lost when you export your JasperReports report into the PDF? Do you wander, why PDF preview in the iReport has all fonts right? Check this link out, mate: http://sites.google.com/site/xmedeko/code/misc/jasperreports-pdf-font-mapping

čtvrtek 5. srpna 2010

JBoss and HSQLDB 2.0.0

HSQLBD 2.0 GA has been released on 7 June 2010 with a lot of new cool features. I have needed some of them for my JBoss 5.1.0GA, so I have just replaced the library common/lib/hsqldb.jar and voilà, it runs almost without any problem (Hibernate/JPA usage) as before. I have made just one small change, see bellow. Even the HSQLDB Database Manager starts from JMX console. My personal feeling is that the version 2.0 is a little bit faster than 1.8.0, but I have not made any benchmarks.

The HSQLDB 2.0 upgrade notes:
  1. In my import.sql I had to change all timestamp formats from '2010-08-06 8:00:00' to '2010-08-06 08:00:00', i.e. I had to zero pad the hours into two decimal places.

středa 21. července 2010

ZK 3.6 CSRF Protection

Unfortunately, ZK Web Framework version 3.6 and earlier lack any CSRF protection. i have created a very basic event filter to tackle this issue. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/zk-web-framework/zk-3-6-csrf-protection. It is just a very first version, leave me a comment, if you have some suggestion.

Update 15th Sep 2010: Improved protection and logging.

středa 14. července 2010

ZK - Converting Null Values

A common problem in database programming is to display some meaningful labels instead of NULL values in the list of database rows. Fortunately, ZK Web Framework, offers a comfortable TypeConverter class to address this issue. I have created a general type converter NullToLabelConverter to convert NULL values into any localised text. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/zk-web-framework/nulltolabelconverter.

středa 19. května 2010

ZK Logging in ZUL zscript Code

I have created a very simple class to simplify getting Java logger for the ZUL file (see ZK Web Framework). It is targeted especially for the JBoss logging org.jboss.logging.Logger, but can be easily changed for java.util.logging or any other logging library. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/zk-web-framework/zul-logging.

středa 7. dubna 2010

ZK and Very Long Strings

When I was working with ZK Web Framework, I have needed to display just a beginning of a very long String in a listbox. I have created a new type converter StringTrimConverter, which trims a string to the specified length. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/zk-web-framework/stringtrimconverter.

čtvrtek 11. března 2010

JPA: Mapping from Enum to Char(1)

Very often, enumerations are stored in SQL databases as char(1) columns. However, JPA annotation Enumerated allows mapping from Java Enum type into javax.persistence.EnumType.STRING (i.e. SQL type VARCHAR) or javax.persistence.EnumType.ORDINAL (i.e. SQL type INTERGER) only.

I have read a few rather complicated advices, how to make a mapping from general Java Enum type into SQL char(1) in Hibernate. After a while, I have found out, that when all constant names in the Enum have length just character, one can make an easy JPA mapping:
@Basic(optional = false)
@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "char(1) default 'A'")
private FruitType fruit_type = FruitType.A;
So, the FruitType may be:
public enum FruitType {
  /** Apples */
  A,
  /** Apricots */
  B,
  /** Cherries */
  C,
  /** Pears */
  P;
}

Well, it has a disadvantage, that "apricots" are represented by FruitType.B. But all modern IDE (like Eclipse) shows Javadoc when the developer types the code. Beside the simplicity, the other great advantage is, that the application developer knows that the "apricots" are represented by 'B' in the database. So, it is easier for the developer to write JPQL/HQL/SQL queries. When you a complicated JPA/Hibernate mapping from some type FruitTypeLong.APPRICOT to the 'B' in database, then it will be just confusing after some time to write JPQL/HQL/SQL queries.

čtvrtek 11. února 2010

Java Web Container: Hunting Redeploy Memory Leaks

Using a ThreadLocal in a web application (WAR) may simplify the development, but one must have a very deep understanding of the web container (e.g. Tomcat) internals and Java class loading to avoid memory leaks when WAR is redeployed. See


The problem might be hidden deep in any library used by the application. To check, if the redeploy memory leak occurs is simple with Eclipse Memory Analyzer (MAT). Just redeploy the WAR several times, make a heap dump (e.g. by jmap or jconsole). Then open the heap dump in MAT and click the link Duplicate Classes: List classes loaded by more than one class loader. If there are some duplicated classes, then you have a memory leak.

Tip: how to clean ThreadLocals in a servlet: http://www.dewavrin.info/?p=196

středa 20. ledna 2010

Eclipse: An error occurred while collecting items to be installed

I have tried to update my Eclipse 3.5 Galileo plugins, and all what I have seen was:
An error occurred while collecting items to be installed
No repository found containing: org.eclipse.net4j.jms.api/osgi.bundle/1.0.0.v200806180305
I could not find the solution for a couple of hours. Finally, BINGO!, reinstalling EclipseLink packages solved it.

Just go to Help -> Install New Software ->, choose Galile update site Galileo - http://download.eclipse.org/releases/galileo and search for all installed EclipseLink packages. (You must uncheck Hide items that are already installed also.) The installed packages have grey icon. Just check them, press Next, and then Finish. Restart the Eclipse and then try to update plugins again.