Vaadin Add-on » History » Version 10
Henning Blohm, 08.07.2013 09:57
1 | 1 | Henning Blohm | h1. The Vaadin Add-on |
---|---|---|---|
2 | |||
3 | The Vaadin add-on provides the Vaadin libraries and some minimal supporting functionality: |
||
4 | |||
5 | 2 | Henning Blohm | * The *ContextClassLoader* ("source":https://redmine.z2-environment.net/projects/z2-addons/repository/z2-addons-vaadin/revisions/master/entry/com.vaadin/java/src.api/com/zfabrik/vaadin/ContextClassLoader.java, "javadoc":http://www.z2-environment.net/javadoc/com.vaadin!2Fjava/api/com/zfabrik/vaadin/ContextClassLoader.html) utility helps loading your Vaadin application class from the right context. |
6 | 4 | Henning Blohm | * An extensibility utility. The *ExtensionComponentsUtil* ("source":https://redmine.z2-environment.net/projects/z2-addons/repository/z2-addons-vaadin/revisions/master/entry/com.zfabrik.vaadin/java/src.api/com/zfabrik/vaadin/ExtensionComponentsUtil.java, "javadoc":http://www.z2-environment.net/javadoc/com.zfabrik.vaadin!2Fjava/api/com/zfabrik/vaadin/ExtensionComponentsUtil.html) help you to split a Vaadin web application across modules. |
7 | 1 | Henning Blohm | |
8 | 10 | Henning Blohm | The sample [[Sample-Vaadin-Spring-Hibernate]] makes use of both features. |
9 | |||
10 | 1 | Henning Blohm | h2. More details on the ContextClassLoader |
11 | |||
12 | 4 | Henning Blohm | 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. |
13 | |||
14 | 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. |
||
15 | |||
16 | 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. |
||
17 | |||
18 | 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: |
||
19 | |||
20 | <pre><code class="xml"> |
||
21 | <init-param> |
||
22 | <param-name>ClassLoader</param-name> |
||
23 | <param-value>com.my.package.ContextClassLoader</param-value> |
||
24 | </init-param> |
||
25 | </code></pre> |
||
26 | |||
27 | Read on here: |
||
28 | * "web.xml":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/web/WebContent/WEB-INF/web.xml of Vaadin sample app |
||
29 | * "Ticket":http://dev.vaadin.com/ticket/9809 with Vaadin |
||
30 | * A "blog article":http://www.z2-environment.net/blog/2012/07/for-techies-protecting-java-in-a-modular-world-context-classloaders/ on class loader topics |
||
31 | 1 | Henning Blohm | |
32 | h2. More details on the ExtensionComponentsUtil |
||
33 | 5 | Henning Blohm | |
34 | 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 |
||
35 | |||
36 | 6 | Henning Blohm | (Vaadin is all Java) + (Z2 for modular Java applications) => Modular Vaadin user interfaces on Z2 |
37 | 5 | Henning Blohm | |
38 | 1 | Henning Blohm | 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. |
39 | 6 | Henning Blohm | |
40 | h3. Doing it |
||
41 | |||
42 | 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: |
||
43 | |||
44 | <pre><code class="bash"> |
||
45 | com.zfabrik.vaadin.extension.point=com.acme.sample.admin.mainTab |
||
46 | com.zfabrik.vaadin.extension.order=100 |
||
47 | </code></pre> |
||
48 | |||
49 | 9 | Henning Blohm | 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. |
50 | 6 | Henning Blohm | |
51 | 8 | Henning Blohm | From a consumer perspective, i.e. from the retrieving party, the component implements the *IExtensionComponentFactory* ("source":https://redmine.z2-environment.net/projects/z2-addons/repository/z2-addons-vaadin/revisions/master/entry/com.zfabrik.vaadin/java/src.api/com/zfabrik/vaadin/IExtensionComponentFactory.java, "javadoc":http://www.z2-environment.net/javadoc/com.zfabrik.vaadin!2Fjava/api/com/zfabrik/vaadin/IExtensionComponentFactory.html) interface. |
52 | 6 | Henning Blohm | |
53 | 8 | Henning Blohm | Using the *ExtensionsComponentUtil* the consuming side can then simply retrieve and bind extension components like this: |
54 | 6 | Henning Blohm | |
55 | <pre><code class="java"> |
||
56 | // retrieve all Vaadin components for extension point "com.acme.sample.admin.mainTab" |
||
57 | for (Component c : ExtensionComponentsUtil.getComponentyByExtensionForApplication(application,"com.acme.sample.admin.mainTab")) { |
||
58 | 1 | Henning Blohm | // and add them to this. |
59 | addComponent(c); |
||
60 | } |
||
61 | </code></pre> |
||
62 | |||
63 | 8 | Henning Blohm | For the providing side, two possibilities exist: |
64 | 1 | Henning Blohm | |
65 | 8 | Henning Blohm | For once, a component may be declared of type *com.vaadin.ui.Component* (implemented by "ExtensionComponentResource":http://www.z2-environment.net/javadoc/com.zfabrik.vaadin!2Fjava/impl/com/zfabrik/impl/vaadin/ExtensionComponentResource.html). 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":http://www.z2-environment.net/javadoc/com.zfabrik.vaadin!2Fjava/api/com/zfabrik/vaadin/provider/IExtensionComponent.html). |
66 | |||
67 | 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. |
||
68 | |||
69 | In the latter case, an extension component implementation may check for pre-conditions on the application object passed into "IExtensionComponent.html#createNewInstanceForApplication(com.vaadin.Application)":http://www.z2-environment.net/javadoc/com.zfabrik.vaadin!2Fjava/api/com/zfabrik/vaadin/provider/IExtensionComponent.html#createNewInstanceForApplication%28com.vaadin.Application%29 before actually instantiating a control instance. |
||
70 | 1 | Henning Blohm | |
71 | 9 | Henning Blohm | A complete extension component declaration looks like this: |
72 | |||
73 | <pre><code class="bash"> |
||
74 | # |
||
75 | # A Vaadin UI extension |
||
76 | # |
||
77 | com.zfabrik.component.type=com.vaadin.ui.Component |
||
78 | |||
79 | # |
||
80 | # Coordinates: |
||
81 | # |
||
82 | com.zfabrik.vaadin.extension.point=com.acme.admin.mainTab |
||
83 | com.zfabrik.vaadin.extension.order=100 |
||
84 | |||
85 | # |
||
86 | # The implementation class |
||
87 | # |
||
88 | component.className=com.acme.admin.impl.UsersTab |
||
89 | </code></pre> |
||
90 | 8 | Henning Blohm | |
91 | |||
92 | Alternatively to using the custom Vaadin Component type above, a component of any origin (e.g. a Spring bean) should simply implement *IExtensionComponentFactory* ("javadoc":http://www.z2-environment.net/javadoc/com.zfabrik.vaadin!2Fjava/api/com/zfabrik/vaadin/IExtensionComponentFactory.html) and adhere to its contract. |