Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, October 21, 2009

Character Encoding UTF-8 with JPA/Hibernate, MySql and Tomcat

I'm writing a little Java application using JPA (Hibernate implementation) and Spring. The application will run on Tomcat and uses MySql as the RDBMS.

The problem I had today was with the good old character encoding: I was able to store German Umlaut characters (üöä) properly in MySql, but whenever I retrieved them, they would be scrambled - regardless of whether I displayed the result on a web page or just printed it to the console.

So, the problem is: how to consistently set UTF-8 as the character encoding of choice throughout the whole stack:

  • for MySql as well as for any session coming through the JDBC driver in order to ensure that any entity created by Hibernate/JPA uses UTF-8
  • for Tomcat to make sure that any data served uses UTF-8

I know that you'll find a lot of material on the solution for each individual piece of software in my tech stack across the net. However, I still think it's worth to post this solution as I did not find all elements of it in one place (and don't want to search again the next time :-)

Here's what I did:

1. Ensure that MySql runs on UTF-8 as default: in the MySql configuration file my.cnf add the following in the section for mysqld:

[mysqld]
...
default-character-set=utf8
default-collation=utf8_general_ci
...

2. Configure your MySql JDBC driver connection as follows (obviously hostname, port and schema are probably different in your configuration :-):

jdbc:mysql://localhost:3306/test?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8

When configuring the above driver URL in your Spring XML context definition, don't forget to escape the Ampersand as you will get parsing errors otherwise.

3. Configure Tomcat for UTF-8 by adding the following line to your : catalina.bat or catalina.sh:

JAVA_OPTS="$JAVA_OPTS -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8"

Versions I am using: MySql 5.0, Tomcat 6.0.20, Spring 2.5.6, Java 6, MySql Connector 5.1.6

Happy hacking!

Sunday, April 6, 2008

How to launch an OSGi runtime in your application with Equinox

Here's a useful code snippet for launching an OSGi runtime from within your application. This is useful where you want to use OSGi in your own application as a plug-in mechanism.

I use Equinox, the Eclipse OSGi implementation.


import org.eclipse.osgi.baseadaptor.BaseAdaptor;
import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
import org.eclipse.osgi.framework.internal.core.OSGi;

...

/**
* Launch the OSGi framework.
*
* @param installArea Directory path to where the OSGi bundles remain.
* @param configArea Direcotory path to where OSGi should store its configuration data.
*/
public void launchOsgi( String installArea, String configArea )
{
System.setProperty( "osgi.install.area", installArea );
System.setProperty( "osgi.configuration.area", configArea );
FrameworkAdaptor adaptor = new BaseAdaptor( null );
OSGi osgiInstance = new OSGi( adaptor );
osgiInstance.launch();
}

...


The installation area points to a location in the file system where you want to make your OSGi runtime believe it is "installed", i.e. where it would look for bundles if you used the install file:mybundle_1.0.0.jar command.

The config area points to the location where you want your OSGi runtime to store configuration information about bundles you install while it is running. As you probably know, OSGi stores information about every bundle you install such that upon the next restart, you will not have to install all bundles from scratch again.

Instead of setting system properties, which is admittedly a bit cumbersome, you could pass parameters to the BaseAdaptor constructor, but I havent figured out the parameter names yet :-)

From the OSGi instance you have created you can use osgiInstance.getBundleContext() to install and manage bundles.

That's basically all you need to build an OSGi-based plugin mechanism into your application.

Well, almost. Of course you will need to write some code to detect new bundles in the install area or other locations (at start time of your application or dynamically at runtime of your application). You could use functionality like Peter Kriens' FileInstall bundle, for example. Equinox has something similar but I have not looked at it yet :-)

I might cover this topic in one of my future posts.