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.

pátek 16. října 2009

Eclipse: Java Decompiler - Really Nice Plugin

Why the method throws an exception? What happens when I pass a null parameter? Hot the hell is the class designed? These question I have often asked when I had was working with a library without sources. But now I have found a really nice Eclipse plugin http://java.decompiler.free.fr/jd-eclipse/. The update site is http://java.decompiler.free.fr/jd-eclipse/update.

Now I may press F3 button and see the decompiled source immediately. I just miss a possibility to debug the code without source.

středa 16. září 2009

Linux: How to Convert Binary File into Byte Array

Sometimes, a programmer needs to embed a content of a binary file into a program, usually as an array of bytes. For instance, I needed to embed an icon into a Java code, to be really sure the icon is always available for the program. So I have saved an icon in a GIF format and then used the od command. But the result was an unusable list of octal numbers. After a minute with playing, I have found a feasible solution
od -A n -v -t d1 image.gif | sed -e 's/^ *//g' -e 's/  */,/g' -e 's/$/,/g'
I have copied a result into my Java code as
private static final byte[] MISSING_ICON_BYTES = new byte[] { /* copy result here */ };
public static final ImageIcon MISSING_ICON = new ImageIcon(MISSING_ICON_BYTES);