Project

General

Profile

Actions

The Vaadin Add-on

The Vaadin add-on provides access to Vaadin libraries from Maven repositories and some supporting functions for best use of Vaadin in conjunction with Z2:

  • The ContextClassLoader (source, javadoc) utility helps loading your Vaadin application class from the right context.
  • An extensibility utility. The ExtensionComponentsUtil (source, javadoc) help you to split a Vaadin web application across modules.

The sample Sample-Vaadin-Spring-Hibernate makes use of both features.

Repository

z2-addons-vaadin

Version map

add-on version Vaddin version
2.2 6.8.4
2.3 7.1.2
2.4 7.1.2
2.5 7.1.2
2.6 7.1.2
2.7 7.1.2
2.8 7.1.2

More details on the ContextClassLoader

In non-modular application environments like the typical Java Web Application Server, you will typically use a build tool like ANT or Maven to package all libraries and resources that are required to run your Web application into one Web application archive (WAR). At runtime, a Web container like Apache Tomcat serves all code-like resources of the Web app from one classloading scope.

Toolkits like Vaadin that need to load user classes by name now need to decide what class loader to refer to for loading of user-defined classes, such as the actual application class in the Vaadin case. The right and de-facto standard choice is to use the context class loader associated with the current thread. Unfortunately Vaadin uses the class loader that loaded the Vaadin classes however.

In modular environments, such as Z2 but also OSGi, the Vaadin implementation will typically be shared among many Web applications. In that case, the application may see the Vaadin types but not vice versa.

In order to make Vaadin work as expected, add the following init parameter to the Vaadin servlet config in the web.xml file of your Web app:

<init-param>
    <param-name>ClassLoader</param-name>
    <param-value>com.my.package.ContextClassLoader</param-value>
</init-param>
Read on here:

More details on the ExtensionComponentsUtil

While the context class loader from above is a necessity to use Vaadin, the extension components util (ECU) is a great but not strictly required addition. It implements the following formula

(Vaadin is all Java) + (Z2 for modular Java applications) => Modular Vaadin user interfaces on Z2

More specifically, when applications get bigger, user interface implementations, like other parts of your implementation, have a tendency to become too big to maintain as single, monolithic modules. Using the ECU you can rather easily break your user interface implementation into modules that may have their completely private set of dependencies. This does not only allow to split the code base into well-managable pieces, it does also allow to cleanly separate concerns and to implement general user interface extensibility.

Doing it

The extension component, that will be retrieved by the to-be-extended Vaadin UI must declare the extension point it belongs to and some priority, e.g. like this:

com.zfabrik.vaadin.extension.point=com.acme.sample.admin.mainTab
com.zfabrik.vaadin.extension.order=100

The extension point id is used to find it (see below). The order is used to sort extensions before returning them as an ordered collection to the consumer. The latter can be useful, for example, to define the order of tab panels in a tab layout.

From a consumer perspective, i.e. from the retrieving party, the component implements the IExtensionComponentFactory (source, javadoc) interface.

Using the ExtensionsComponentUtil the consuming side can then simply retrieve and bind extension components like this:

// retrieve all Vaadin components for extension point "com.acme.sample.admin.mainTab" 
for (Component c : ExtensionComponentsUtil.getComponentyByExtensionForApplication(application,"com.acme.sample.admin.mainTab")) {
   // and add them to this.
   addComponent(c);
}

For the providing side, two possibilities exist:

For once, a component may be declared of type com.vaadin.ui.Component (implemented by ExtensionComponentResource). In that case, the class specified via the component.className property can either implement directly Vaadin's Component interface or the more explicit provider interface IExtensionComponent (javadoc).

The former is simpler and safes one implementation class. In many cases, Vaadin Component implementations require the current Vaadin application at creation time. For convenience, if the implementation class specified has a single argument constructor accepting a Vaadin application instance, that constructor will be used with preference and the application object of the retrieving user interface will be passed.

In the latter case, an extension component implementation may check for pre-conditions on the application object passed into IExtensionComponent.html#createNewInstanceForApplication before actually instantiating a control instance.

A complete extension component declaration looks like this:

#
# A Vaadin UI extension 
#
com.zfabrik.component.type=com.vaadin.ui.Component

#
# Coordinates:
#
com.zfabrik.vaadin.extension.point=com.acme.admin.mainTab
com.zfabrik.vaadin.extension.order=100

#
# The implementation class
#
component.className=com.acme.admin.impl.UsersTab

Alternatively to using the custom Vaadin Component type above, a component of any origin (e.g. a Spring bean) should simply implement IExtensionComponentFactory (javadoc) and adhere to its contract.

Updated by Henning Blohm about 4 years ago · 12 revisions