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 {

	@Override
	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;

	@Override
	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"
	p:urlMountRegistrar-ref="urlMountRegistrar"
/>

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() {

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

			@Override
			public void onAfterInitialized(Application application) {
				LOG.info("Wicket application is initialized");
				WebApplication webApplication = (WebApplication) application;
				m_urlMountRegistrar.mount(webApplication);
			}
		});

2 comments to Wicket mounting in an OSGi environment

  • Malix

    Hi Stefanovich,
    Thanks for your awesome post.
    May I know the environment you are running?
    Are you using OSGi as a container, or OSGi is within Web Application?

    Would love to try it out as you described.

    Thanks,
    Malix

  • Hi Malix

    Thanks for your comment!

    Currently we are using Virgo.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>