Do it online

Online programming editors/debuggers are getting popular. Here are the couple of them which I use from time to time:

  • IDEOne – a broad choice of programming languages (there is even BrainF**k, however, there is no XSLT)
  • jsFiddle – a nice IDE for JavaScript.
  • jsBin – another one for JavaScript
  • XSLTCake – useful IDE for XSLT. It is still ‘beta’, but it offers three types of XSLT processing (JavaScript, web service or .NET 4.0)
  • XML Playground – another cool IDE for XSL templates. Unfortunately it doesn’t support XSLT 2.0
  • Rubular – IDE for checking regular expression written in Ruby
  • XSLTTest – Small and neat application for XSLT. Supports XSLT 2.0.

Useful XSLT snippet with translate()

Let’s say there is a following function written in XSLT 2.0:

    <xsl:function name="str:convertBits">
        <xsl:param name="cpnNumber" />

        <xsl:choose>
            <xsl:when test="$cpnNumber = 1">
                <xsl:value-of>8</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 2">
                <xsl:value-of>4</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 3">
                <xsl:value-of>2</xsl:value-of>
            </xsl:when>
            <xsl:when test="$cpnNumber = 4">
               <xsl:value-of>1</xsl:value-of>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of>0</xsl:value-of>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

Quite long, isn’t it?

This is how it can be shortened:

number(translate(xs:string($cpnNumbers[1]), '1234567890', '8421000000'))

How to change XML to lower case using XSLT multi-pass?

Today I’ve resolved another issue using XSLT multi-pass technique presented in my previous post. I think this be might useful in future, so I post it here. The XSLT translates the input XML to lower case (it changes only node names, it doesn’t affect data inside elements or attributes). After that, it applies the actual logic.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="firstPassResult">
<xsl:apply-templates select="/" mode="firstPass"/>
</xsl:variable>
<xsl:template match="@*|node()" mode="firstPass">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="firstPass">
<xsl:element name="{translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz')}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
</xsl:template>
<xsl:template match="//boss" mode="secondPass">
<xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>

The sample XML file (only to prove that this technique works):

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Boss>John</Boss>
<BoSs>Henry</BoSs>
<BOSS>Nathan</BOSS>
<boss>igor</boss>
</Root>

It worked under Altova XMLSpy and produced the following output:

<?xml version="1.0" encoding="UTF-8"?>JohnHenryNathanigor

How to get rid of empty attributes in XSLT output?

Some time ago, I faced an issue related to XSLT stylesheets. It was while we were developing quite complex XSLT template whose purpose was to convert one  XML  into completely different another one. During the implementation, it turned out that the output XML contains a lot unnecessary empty attributes, e.g.:

<field attr="" />

It was actually possible to get rid of them with <xsl:if> or <xsl:choose> instructions. However, it would require one conditional instruction per one <xsl:attribute…/> instruction. This would make the XSLT file too complex. So I needed other approach, that would act after the basic transform. And this is achievable by usage of

multi-pass XSLT transforms.

Let’s say that we have the following input XML:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<database>
<record firstName=\"john\" lastName=\"smith\">writer</record>
</database>

We would like to have the above XML converted by this XSLT transform:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes" exclude-result-prefixes="xs xsl"/>

    <xsl:template match="/database/record">
        <output>
            <xsl:attribute name="firstName">
                <xsl:value-of select="@firstName"/>
            </xsl:attribute>
            <xsl:attribute name="lastName">
                <xsl:value-of select="@firstName"/>
            </xsl:attribute>
            <xsl:attribute name="middleName">
                <xsl:value-of select="@middleName"/>
            </xsl:attribute>
            <xsl:attribute name="dob">
                <xsl:value-of select="@dob"/>
            </xsl:attribute>
            <xsl:text>recordFound</xsl:text>
        </output>
    </xsl:template>
</xsl:stylesheet>

The output looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<output firstName="john" lastName="john" middleName="" dob="">recordFound</output>

As you can see there are two empty attributes: middlename and dob. And this is where we can think about multi-pass transforms. We would like to have another transform applied on the results of the this XSLT.

Now, let’s consider what should be done to remove empty attributes. This is very to accomplish using identity transforms. This is simple variant of identity transform that removes empty attributes:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:template match="node()|@*" >
        <xsl:copy>
             <xsl:apply-templates select="@*[.!='']" />
             <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

What is left is merging our both  XSLTs. The following code is the final XSLT transformation. We are using modes that are available XSLT 2.0. We store the result of our business operation in the variable $firstPassResult and we perform the removal of empty attributes on it.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">    
    <xsl:output indent="yes" exclude-result-prefixes="xs xsl"/>
    <xsl:variable name="firstPassResult">
        <xsl:apply-templates select="/" mode="firstPass" />
    </xsl:variable>

    <xsl:template match="/database/record" mode="firstPass">
        <output>
            <xsl:attribute name="firstName">
                <xsl:value-of select="@firstName"/>
            </xsl:attribute>
            <xsl:attribute name="lastName">
                <xsl:value-of select="@firstName"/>
            </xsl:attribute>
            <xsl:attribute name="middleName">
                <xsl:value-of select="@middleName"/>
            </xsl:attribute>
            <xsl:attribute name="dob">
                <xsl:value-of select="@dob"/>
            </xsl:attribute>
            <xsl:text>recordFound</xsl:text>
        </output>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates select="$firstPassResult"
            mode="secondPass" />
    </xsl:template>

    <xsl:template match="node()|@*" mode="secondPass">
        <xsl:copy>
             <xsl:apply-templates select="@*[.!='']" mode="secondPass"/>
             <xsl:apply-templates select="node()" mode="secondPass"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Problem: MS Office Communicator 2005 fails to install

In my current company, I’m forced to use IM which is MS Office Communicator 2005. Accidentally (or perhaps my tortured subconsciousness told me do so;), I uninstalled this thing. Unfortunately, re-installation of this application wasn’t so easy as it seemed to be. When I ran installer it exited saying that Communicator could not be installed. That was really strange. After a couple of hours, I’ve finally realized that configuration of my workstation doesn’t really differ from the configuration of a typical WinXP machine, so the problem may be in some service pack or some security update. And that was it. A little creature named “Security Update KB974571” was the reason causing the failure (it was enough to uninstall it and reboot the computer). I wasted about 4 hours of my life which could be spent on coding.

SQL Server 2005 issue.

A few days ago I was forced to install and use SQL Server database on my system. A few days later I had to install MS VS .NET 2008. After that, it turned out that the standard utility SQL Server Configuration Manager does not seem to work correctly. It shows a message box that contains the following error:
Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can only manage SQL Server 2005 servers with SQL Server Configuration Manager.
Nie można znaleźć określonego modułu. [0x8007007e]

Unfortunately, I could not find any working solution of this problem (perhaps it is so because my knowledge of Microsoft’s database is less than miserable ) Some pages suggested running Mof-compiler program with *.mof file from SQL Server installation directory but it does not work in my case.

EDIT: After some time I figured out what should done to solve this problem. It is simple. It just requires copying framedyn.dll file from folder: “C:\WINDOWS\system32\wbem” to folder: “C:\WINDOWS\system32”.

Hibernate in OSGi bundle

This is my third post about OSGi technology. This time we’re going to do something more advanced. We will create Hibernate-based application working in OSGi bundles. It can be used as a basis for more complex multi-layered SOA killer-app ;-)

In a few words… We will create a Maven project that will consist of two modules. I think that it would be nice to have some kind of a starter for bigger Hibernate applications. That’s why I’m going to separate Hibernate libraries from the mappings and configuration. Both modules are to be packaged as bundles:

  • osgi-hibernate-classes – will be a wrapper bundle for Hibernate JAR and all its dependencies
  • osgi-hibernate-mysql-test – will contain configuration, mappings the application itself

Both these projects will have a common parent POM file.

Parent POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.lukaszbaran.simpleosgi.db</groupId>
	<artifactId>parent-pom</artifactId>
	<version>1.0</version>
	<packaging>pom</packaging>
	<name>Hibernate on OSGi</name>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<modules>
		<module>osgi-hibernate-classes</module>
		<module>osgi-hibernate-mysql-test</module>
	</modules>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.6</version>
		</dependency>
		<dependency>
			<groupId>sax</groupId>
			<artifactId>sax</artifactId>
			<version>2.0.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-annotations</artifactId>
			<version>3.3.0.ga</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-commons-annotations</artifactId>
			<version>3.3.0.ga</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>jsr250-api</artifactId>
			<version>1.0</version>
		</dependency>
	</dependencies>
</project>

osgi-hibernate-classes module

This wrapper bundle is very simple, as it won’t have any Java code. It will only consist of POM file which should be located in osgi-hibernate-classes sub-directory and look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.lukaszbaran.simpleosgi.db</groupId>
		<artifactId>parent-pom</artifactId>
		<version>1.0</version>
		<relativePath>../pom.xml</relativePath>
	</parent>
	<groupId>com.lukaszbaran.simpleosgi.db</groupId>
	<artifactId>osgi-hibernate-classes</artifactId>
	<packaging>bundle</packaging>
	<version>1.0</version>
	<name>osgi-hibernate-classes</name>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<extensions>true</extensions>
				<version>1.4.3</version>
				<configuration>
					<instructions>
						<manifestLocation>META-INF</manifestLocation>
						<Import-Package>
							org.apache.commons.logging,
							javax.sql,
							javax.naming*,
							org.w3c.*,
							org.xml.sax,
							org.xml.sax.ext,
							org.xml.sax.helpers,
							!*
						</Import-Package>
						<Embed-Transitive>true</Embed-Transitive>
						<Embed-Dependency>*</Embed-Dependency>
						<DynamicImport-Package>
							com.lukaszbaran.simpleosgi.db.test.entities.*
 						</DynamicImport-Package>
						<_exportcontents>
							org.hibernate*
						</_exportcontents>
					</instructions>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate</artifactId>
			<version>3.2.6.ga</version>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
</project>

The most important part of this POM file is the configuration of maven-bundle-plugin. What we want to achieve is to have Hibernate JARs embedded in an OSGi bundle. There are several ways of achieving our goal. It means that the solution presented here is not the ultimate and best solution. But it’s the working one! Here, we are putting all the JARs necessary for Hibernate into a bundle. The JARs are not be unpacked and they will appear in the main path inside of the bundle. This is achieved by the two sections:

	<Embed-Transitive>true</Embed-Transitive>
	<Embed-Dependency>*</Embed-Dependency>

The * character in Emded-Dependency tells plugin to include all dependencies from POM file (of course, it includes dependencies from our parent POM). The Embed-Transitive section causes that all the transitive dependencies will be included as well.

Another important point is Import-Package. This is where we declare what other (external) packages will be referenced by our code. In other words, all external dependencies have to be listed here.

OK, now comes the most tricky part in this tutorial – the packages that our wrapper will be exposing to other bundles. In this case we want to expose Hibernate to other bundles. To achieve this we can use Export-Package, but it may complicate things as we don’t want to have the classes inlined in the bundle [inlined means here *.class files taken out of JAR and put in the bundle’]. That’s why we’re using little documented feature of the maven-bundle-plugin called <_exportcontents> – it will place all the sub-packages of given packages in exported packages section in MANIFEST file.

Another thing worth mentioning is the way the Hibernate dependency is declared in POM dependencies part. We manually exlude one of its transitive dependencies – commons-logging, as we will be using this dependency from FUSE ESB’s bundles. This reduces the final size of the resulting bundle. Btw, I’m pretty sure that it is possible to make this bundle even smaller..

osgi-hibernate-mysql-test module

Now, let’s prepare a sample application that will use of the wrapper bundle. We’ll use MySQL table which could like this:

 CREATE TABLE IF NOT EXISTS `honey` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(250) default NULL,
  `taste` varchar(250) default NULL,
  PRIMARY KEY  (`id`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

What we need is POJO representation of the honey table, Hibernate configuration and the code that will make use of it.

The project structure will look like this:

│   pom.xml
│
└───src
    ├───main
    │   ├───java
    │   │   └───com
    │   │       └───lukaszbaran
    │   │           └───simpleosgi
    │   │               └───db
    │   │                   └───test
    │   │                       ├───entities
    │   │                       │       Honey.java
    │   │                       │
    │   │                       └───osgi
    │   │                               Activator.java
    │   │                               BundleUtils.java
    │   │                               HibernateApplication.java
    │   │
    │   └───resources
    │           hibernate.cfg.xml
    │           Honey.hbm.xml
    │           log4j.properties
    │
    └───test
        └───java

The POM file for the project will be really simple:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.lukaszbaran.simpleosgi.db</groupId>
		<artifactId>parent-pom</artifactId>
		<version>1.0</version>
		<relativePath>../pom.xml</relativePath>
	</parent>
	<groupId>com.lukaszbaran.simpleosgi.db</groupId>
	<artifactId>osgi-hibernate-mysql-test</artifactId>
	<packaging>bundle</packaging>
	<version>1.0</version>
	<name>osgi-hibernate-mysql-test</name>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<extensions>true</extensions>
				<version>1.4.3</version>
				<configuration>
					<instructions>
						<manifestLocation>META-INF</manifestLocation>
						<Import-Package>
							org.osgi.framework,
							org.hibernate,
							org.hibernate.cfg,
							org.hibernate.classic,
							javax.*,
							org.w3c.dom,
							org.xml.sax,
							org.apache.log4j
						</Import-Package>
						<Private-Package>
							com.lukaszbaran.simpleosgi.db.test.osgi.*
						</Private-Package>
						<Export-Package>
							com.lukaszbaran.simpleosgi.db.test.entities.*
						</Export-Package>
						<Bundle-Activator>
							com.lukaszbaran.simpleosgi.db.test.osgi.Activator
						</Bundle-Activator>
						<DynamicImport-Package>
							org.hibernate.proxy.*
						</DynamicImport-Package>
						<Require-Bundle>
							com.lukaszbaran.simpleosgi.db.osgi-hibernate-classes
						</Require-Bundle>
					</instructions>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate</artifactId>
			<version>3.2.6.ga</version>
		</dependency>
		<dependency>
			<groupId>org.apache.felix</groupId>
			<artifactId>org.osgi.core</artifactId>
			<version>1.0.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
	</dependencies>
</project>

The configuration of Bundle plugin may seem a little bit complicated but in fact it’s just a compilation of previous tutorials. We’re declaring that we require our wrapper bundle named: com.lukaszbaran.simpleosgi.db.osgi-hibernate-classes. The only package exported from this bundle is com.lukaszbaran.simpleosgi.db.test.entities – it is where we have our Honey POJO.

This POJO looks like this (it’s really nihil novum sub sole):

package com.lukaszbaran.simpleosgi.db.test.entities;
public class Honey {
	private Integer id;
	private String name;
	private String taste;
 	public Honey() {}
 
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getTaste() {
		return taste;
	}

	public void setTaste(String taste) {
		this.taste = taste;
	}
	
	@Override
	public String toString() {
		return "id=" + getId() + " \tname=" + getName() + " \ttaste=" + getTaste();
	}
}

The Hibernate mapping (Honey.hbm.xml):

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
	<class name="com.lukaszbaran.simpleosgi.db.test.entities.Honey" table="honey">
		<id name="id" column="id" type="java.lang.Integer">
			<generator class="increment"/>
		</id>
		<property name="name" column="name" type="java.lang.String"/>
		<property name="taste" column="taste" type="java.lang.String"/>
	</class>
</hibernate-mapping>

hibernate.cfg.xml file:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">
			com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">
			jdbc:mysql://localhost/firsthibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">password</property>
		<property name="hibernate.connection.pool_size">10</property>
		<property name="show_sql">true</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
	</session-factory>
</hibernate-configuration>

Eventually, we have three Java classes that form our application:

Activator.java this class should only store reference to the bundle in BundleUtils instance, however, I’ve decided to run the test method here:

package com.lukaszbaran.simpleosgi.db.test.osgi;
import org.apache.log4j.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
	private static final Logger LOGGER = Logger.getLogger(Activator.class);
	public void start(BundleContext arg0) throws Exception {
		LOGGER.info("Activator start() START");
		
		// because we need to access to the current bundle elsewhere
		if (arg0 != null) {
			BundleUtils.getInstance().setBundle(arg0.getBundle());
		}

		// perform some test connection
		HibernateApplication app = new HibernateApplication();
		app.test();
		LOGGER.info("Activator start() END");
	}
	public void stop(BundleContext arg0) throws Exception {
		LOGGER.info("Activator stop()");
	}
}

HibernateApplication.java configures Hibernate library using the config files that are embedded in the bundle and it invokes simple database access test:

package com.lukaszbaran.simpleosgi.db.test.osgi;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.lukaszbaran.simpleosgi.db.test.entities.Honey;
public class HibernateApplication {
	private static final Logger LOGGER = Logger.getLogger(HibernateApplication.class);
	public HibernateApplication() {	}

	@SuppressWarnings("unchecked")
	public void test() throws Exception {
		LOGGER.info("Application test() START");
		Session session = null;
		try {
			Configuration cfg = new Configuration();
			cfg.configure(BundleUtils.getInstance().loadResourceURL("/hibernate.cfg.xml"));
			cfg.addDocument(BundleUtils.getInstance().loadResourceXML("/Honey.hbm.xml"));
			SessionFactory sessionFactory = cfg.buildSessionFactory();
			session = sessionFactory.openSession();
			List<Honey> list = session.createCriteria(Honey.class).list();
			for (Iterator<Honey> iter = list.iterator(); iter.hasNext();) {
				Honey element = iter.next();
				LOGGER.debug(element);
			}
			session.close();
		} catch (Exception bhe) {
			LOGGER.error("Exception caught: ", bhe);
			throw bhe;
		} 
	}
}

BundleUtils.java is an ugly singleton class (it really should be done other way as I guess it is against the notion of OSGi) but this code was not the most important part of the show. We only needed to store a reference to a bundle somewhere. Besides that, the class provides methods to access bundle resources:

package com.lukaszbaran.simpleosgi.db.test.osgi;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.osgi.framework.Bundle;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class BundleUtils {
	private static BundleUtils instance = null; 
	private Bundle bundle;
        private final DocumentBuilder builder;
	private BundleUtils() {
	    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    factory.setNamespaceAware(true);
	    try {
	        builder = factory.newDocumentBuilder();
	    } catch (ParserConfigurationException ex) {
	    	throw new RuntimeException("Unable to instantiate DocumentBuilder.", ex);
	    }		
	}
	public static synchronized BundleUtils getInstance() {
		if (instance == null)
			instance = new BundleUtils();
		return instance;
	}
	public Bundle getBundle() {
		return bundle;
	}
	public void setBundle(Bundle bundle) {
		this.bundle = bundle;
	}
	public Document loadResourceXML(final String resourceName) {
	    URL url = loadResourceURL(resourceName);
		try {
			InputStream is = url.openStream();
			return builder.parse(is);
		} catch (SAXException e) {
	    	throw new RuntimeException("Unable to parse resource.", e);
		} catch (IOException e) {
	    	throw new RuntimeException("IO exception while parsing resource.", e);
		}
	}
	public URL loadResourceURL(final String resourceName) {
	    URL url = null;
		if (bundle == null) { 
			url = this.getClass().getResource(resourceName);
		} else {
			url = bundle.getResource(resourceName);
		}
		if (url == null) {
			throw new RuntimeException("Unable to access resource.");
		}
		return url;
	}		
}

osgi-hibernate.zip contains all the source code presented here.

Summary

Of course, you can now build those two bundles and deploy them into FUSE ESB container (I guess it would be better to start with deploying Hibernate wrapper bundle in the first place). If your MySQL database is properly configured and if there was anything in the honey table, you would see some results in data/log/servicemix.log file.