Categories
Java RS Library

Apache ServiceMix, Camel and XSL Transformation

This topic cost me a few hours. It is basically how to achieve XML transformation when your original XML file contains a DTD reference that cannot be resolved from your system.

My DTD reference looked like this:

<!DOCTYPE root-element SYSTEM "../dtd/core.dtd">

and the route was simply:

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<route>
			<from uri="file:data/inbox"/>
			<to uri="xslt://xslt/transformation.xsl"/>
			<to uri="file:data/outbox"/>
		</route>
	</camelContext>

The Camel transformation was always complaining about the missing DTD because it was looking for it in the filesystem instead of the bundle.

There exist many hints on the net but none of them really helped (e.g. using a CatalogResolver). So I decided to tamper my Camel route and adding a bean inbetween:

	<bean id="dtdTransformer" class="de.example.xslt.DtdTransformer"/>
 
	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<route>
			<from uri="file:data/inbox"/>
			<bean ref="dtdTransformer"/>
			<to uri="xslt://xslt/bundesliga.xsl"/>
			<to uri="file:data/outbox"/>
		</route>
	</camelContext>

This DtdTransformer is a simple class having one method, transform():

	public static final String DOCTYPE_LINE = "<!DOCTYPE root PUBLIC \"-//PRIVATE//RS//EN\" \"###URI###\">";
 
	public String transform(String body) throws Exception {
		URL url = FileFinder.find(getClass(), "dtd/core.dtd");
		if (url != null) {
			body = body.replaceAll("<!DOCTYPE[^>]*>", DOCTYPE_LINE).replaceAll("###URI###", url.toURI().toString());
		}
		return body;
	}

The URI will be replaced by looking up the DTD file at runtime, here by using my FileFinder class. Then it continues with replacing the special marker ###URI### by the real URI from the file found.

Although the solution sounds so simple, it took me quite a while to find out that the DOCTYPE references needs to be a URI and not a file or URL reference.

Categories
CSV Java

CSV/Excel Utility Package 2.5.0 released

The new version 2.5.0 delivers the package for usage in OSGI containers and improves handling of blank lines and calculated cells in an Excel sheet (this code was contributed by Andrej Czapszys). A complete change log is available through the new Maven Site

You can download the new version here or visit the Homepage of the utility where you will find some examples on how to use it.

The Maven coordinates are:

   <dependency>
      <groupid>eu.ralph-schuster</groupid>
      <artifactid>csv</artifactid>
      <version>2.5.0</version>
   </dependency>
Categories
Java RS Library

RS Library 1.0.0 released

Finally it’s done. I decided to release the first version of all those classes that I implemented and collected throughout the last years, the RS Library. A complete documentation can be found either through Javadocs or the appropriate module homepages:

The Maven coordinates are:

   <dependency>
      <groupid>eu.ralph-schuster</groupid>
      <artifactid>$module</artifactid>
      <version>1.0.0</version>
   </dependency>
Categories
Java

Eclipse E4: Using the TranslationService

This post demonstrates how the TranslationService from Eclipse/E4 can be used in real world applications. First, you need the translation files, bundle.properties (minimum) and e.g. bundle_de.properties for your special language:

myapp.key1 = A text.
myapp.key2 = Another text.

Store these files in OSGI-INF/l10n folder of your plugin. This way, the Eclipse/E4 translation service will find it.

Next, I recommend to have a static helper class that will retrieve the translation service from Eclipse/E4 and returns the translated keys. It could look like this:

public class E4Utils {
 
	public static final TranslationService TRANSLATIONS = getTopContext().get(TranslationService.class);
 
	/**
	 * Finds the top Eclipse context.
	 * @return the eclipse context.
	 */
	public static IEclipseContext getTopContext() {
		return E4Workbench.getServiceContext();
 
	}
 
	/**
	 * Translate the given key.
	 * @param key key to translate
	 * @param contributorUri the contributor URI of the translation
	 * @param args variable replacements (will replace {0}, {1},... placeholders)
	 */
	public static String translate(String key, String contributorUri, Object... args) {
		if (key == null) return "";
		if (key.charAt(0) != '%') key = '%'+key;
		String rc = TRANSLATIONS.translate(key, contributorUri);
		if ((args == null) || (args.length == 0)) return rc;
		return MessageFormat.format(rc, args);
	}
}

Thie helper class provides a method translate() which you pass in the key, the contributor URI of your plugin and any additional arguments. This method does a few things for me. It first checks whether the key starts with ‘%’ which is a requirement for the TranslationService. If this ‘%’ is missing, it will be added automatically. Also it will apply the MessageFormat pattern after translation so you can use more sophisticated translation patterns.

The key is very obvious the string that you mentioned in the bundle.properties file. The contributor URI is of form platform:/plugin/symbolicPluginName. The symbolic plugin name can be looked up in the MANIFEST file. To ease the usage of your helper class, create a Plugin class that adds all information required:

public class Plugin {
 
	public static final String SYMBOLIC_NAME = "rc.rcpplugins.e4";
	public static final String CONTRIBUTOR_URI = "platform:/plugin/"+SYMBOLIC_NAME;
 
	/**
	 * Translate the given key.
	 * @param key key to translate
	 * @param args variable replacements (will replace {0}, {1},... placeholders)
	 * @return translated value
	 */
	public static String translate(String key, Object... args) {
		return E4Utils.translate(key, CONTRIBUTOR_URI, args);
	}
}

Now we can use the translation service at any class in our Eclipse/E4 project. e.g:

	button.setToolTipText(Plugin.translate("myapp.key1"));

I implemented this scheme in all my E4 plugins now (with E4Utils as a central helper in a base plugin) and rapidly translated a medium-sized Eclipse/E4 application within a few hours.

PS: I will publish the base helpers soon as Open Source Eclipse/E4 plugins so you can just install them in your Eclipse instance.

Categories
Java Linux

JavaMail: Sending mails fails

Ever stumbled across this?

javax.mail.SendFailedException: Sending failed;
   nested exception is:
      javax.mail.MessagingException: 501 5.0.0 HELO requires domain address

I wasn’t able for a long time to find the solution in the net although many people reported having this problem. The only hint you find is “There was an invalid entry in /etc/hosts”. But noone said exactly what the “invalid entry” was.

I myself found that there was no invalid entry but the configured hostname of the machine (issue “hostname” in a shell) cannot be resolved by DNS. The result is the given error message from the mail server.

What is happening? JavaMail resolves its own hostname by asking for the local hostname. The SMTP protocol requires the sending domain while handshaking. The server will try to resolve the domain by its DNS. If the hostname sent cannot be resolved then the server responds with “HELO requires domain address” as stated above.

I hope that this issue now becomes clearer to all of you.

PS: Solution is make sure your hostname can be resolved by DNS. Fix your hostname if it’s wrong or add the DNS entry.

Categories
Java Tomcat

Undocumented Java WebStart Features

Many of you might use Oracle’s JNLP Download Servlet, delivered with JDK in jnlp-servlet.jar. One of the nice features of this servlet is the automatic replacement of variables at runtime, e.g. codebase and context. However, the existing documentation does not reveal all of these replacements. While searching for a solution where I need to put the server name into the JNLP file, I also examined the source code of JnlpDownloadServlet. And voilá: the feature is already there but not documented.

Here is the complete list of replacements that the servlet does for you while delivering a JNLP:

  • $$name: will be replaced by the name of the URL file requested (without path, e.g. myfile.jnlp)
  • $$hostname: will be replaced by the servername as given in the HTTP(S) request
  • $$codebase: will be replaced by the codebase (request without URL file: http://my.server/appname/somedir/)
  • $$site: will be replaced by the protocol, server and port, e.g. http://my.server:8080

I hope you’ll find that useful next time you look for a solution to deliver WAR files to individual servers with the specific servername in its JNLP file.

Categories
CSV Java

CSV/Excel Utility Package V2.0.2 released

The package has undergone some minor bugfixing andJUnit Test was introduced for CSV files. Thanks to eldn for his help making this release.

You can download it here or visit the Homepage of the utility where you will find some examples on how to use it.

Categories
Java

Log4j Configuration Problem

It seems that some documentation that is hanging around the net contains incorrect information about correct configuration of the log4j system.

Take care that you specify the properties filename as an URL rather than simple plain path location, e.g

java -Dlog4j.configuration=file:///my/path/to/log4j.properties ...

Log4j should find your configuration then. So make sure you include file:// there… 😉

Categories
CSV Java

CSV Utility Package 2.0 Beta

Some work has been spent over the last weeks to upgrade the stable CSV Utility Package. Of course, the new version contains all the useful existing functionality. The most beneficial improvement is the introduction of ExcelReader and ExcelWriter classes that behave like the CSV pendants. This means that you do not have to care about the underlying output format when you want to read or write table-structured data. The most important changes are:

  • Introduction of a TableReader and TableWriter interface
  • Introduction of Excel implementation
  • Aligning CSV and Excel implementations to these interfaces
  • Introduction of a Factory class in order to select correct implementation (CSV or Excel)

The Beta version still requires some work in terms of documentation and code beautifying. However, the CSV part is already frozen and you can use it to upgrade your existing projects to the new utility version. The Excel part still faces some changes while improving usability.

Please be aware also that Java package structure needed to be changed to integrate the new classes and interfaces. Additionally, new 3rd party libraries were integrated (Apache POI and JavaBeans Activation).

Download the Beta Version or browse the existing API Documentation. Bugs and/or enhancements can be requested via Bugzilla.

Categories
Java Templating

Templating Java Package 1.0.1 released

A new minor release of Templating Java Package has just been released. It fixes a major bug when replacing markers with values that itself contain strings looking like regular expressions. All such expressions are masked now to prevent an exception.

Download the release here.