- VolkomenJuist.nl - http://www.volkomenjuist.nl/blog -

Wicket mounting in an OSGi environment

This page describes how to do page mounting in Wicket in an OSGi environment. A resource/page/etc. can be registered to Wicket’s WebApplication via a couple of ‘mount’ methods. This example only shows how to mount a page (to keep it simple) but can be extended for IRequestMapper/resource/package as well.

First of all I created an interface which provides all pages (per bundle) to mount.

public interface UrlMountProvider {
	Map<String, Class<? extends Page>> getPageMounts();

Now each bundle can provide it’s own implementation. Just create a bean and export it.

<bean id="portalUrlMountProvider" class="my.company.PortalUrlMountProvider"/>
<service ref="portalUrlMountProvider" interface="my.company.UrlMountProvider" />

An example of a provider:

public class PortalUrlMountProvider extends UrlMountProvider {

	public Map<String, Class<? extends Page>> getPageMounts() {
		Map<String, Class<? extends Page>> mounts = Maps.newHashMap();
		mounts.put("today", PortalPage.class);
		return mounts;

All those providers should be registered somewhere so I created another interface like:

public interface UrlMountRegistrar {
    void mount(WebApplication webApplication);

The implementation looks like (without the setter and afterPropertiesSet method):

public final class UrlMountRegistrarImpl implements UrlMountRegistrar, InitializingBean {
	private List<UrlMountProvider> m_urlProviders;

	public void mount(WebApplication webApplication) {
		for (UrlMountProvider urlProvider : m_urlProviders) {
		    for (Entry<String, Class<? extends Page>> entry : urlProvider.getPageMounts().entrySet()) {
		      webApplication.mountPage(entry.getKey(), entry.getValue());

Now create another bean/list which will ‘collect’ all UrlMountProvider’s which are exported by each bundle which I showed earlier and wire the ‘urlMountRegistrar’ in your Wicket application bean:

<osgi:list id="urlMountProviders" cardinality="0..N" interface="my.company.UrlMountProvider"/>
<bean id="urlMountRegistrar" class="my.company.impl.UrlMountRegistrarImpl" p:urlMountProviders-ref="urlMountProviders" />

<bean id="wicketApplication" class="my.company.DesktopApplication"

The only thing left is to call the mount(WebApplication) method of the UrlMountRegistrarImpl class. This should be done when the application is initialized. Currently we are using Wicket 1.5.7 and it has a nice feature -> Listeners.

So register a listener in your Applcation class (‘init’ method) like:

getApplicationListeners().add(new IApplicationListener() {

			public void onBeforeDestroyed(Application application) {
				LOG.warn("Wicket application is about to destroy");

			public void onAfterInitialized(Application application) {
				LOG.info("Wicket application is initialized");
				WebApplication webApplication = (WebApplication) application;
Buffer [1]