Vaadin OSGi Support

Vaadin applications can be deployed on an OSGi compatible servlet container.

An OSGi application typically consists of multiple bundles that can be deployed separately.

To deploy Vaadin applications as OSGi bundles, static resources must be published using the appropriate APIs.

The application is typically packaged as a JAR file, and needs to have a valid OSGi bundle manifest which can be created e.g. by the bnd-maven-plugin or Apache Felix maven-bundle-plugin. All the dependencies of the application should be available as OSGi bundles.

Minimal Vaadin Project For OSGi

Vaadin application for OSGi should be a valid bundle, i.e. it should be packaged as a .jar file, and it should have a proper OSGi manifest inside. The easiest way to convert regular maven-based Vaadin application into a valid OSGi bundle consists of five steps:

  • Change packaging type to jar in your pom.xml:
  1. <packaging>jar</packaging>
  • Change the scope for all vaadin dependencies from default to provided, like this:

XML

  1. <dependency>
  2. <groupId>com.vaadin</groupId>
  3. <artifactId>vaadin-core</artifactId>
  4. <scope>provided</scope>
  5. </dependency>
  • Add OSGi-related dependencies to the project

XML

  1. <groupId>com.vaadin</groupId>
  2. <artifactId>flow-osgi</artifactId>
  3. <version>${flow.version}</version>
  4. <scope>provided</scope>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.osgi</groupId>
  8. <artifactId>osgi.core</artifactId>
  9. <version>6.0.0</version>
  10. <scope>provided</scope>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.osgi</groupId>
  14. <artifactId>osgi.annotation</artifactId>
  15. <version>6.0.1</version>
  16. <scope>provided</scope>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.osgi</groupId>
  20. <artifactId>osgi.cmpn</artifactId>
  21. <version>6.0.0</version>
  22. <scope>provided</scope>
  23. </dependency>
  • Setup necessary plugins for building the project:

XML

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>biz.aQute.bnd</groupId>
  5. <artifactId>bnd-maven-plugin</artifactId>
  6. <version>3.3.0</version>
  7. <executions>
  8. <execution>
  9. <goals>
  10. <goal>bnd-process</goal>
  11. </goals>
  12. </execution>
  13. </executions>
  14. </plugin>
  15. <plugin>
  16. <groupId>org.apache.maven.plugins</groupId>
  17. <artifactId>maven-jar-plugin</artifactId>
  18. <version>3.0.2</version>
  19. <configuration>
  20. <archive>
  21. <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
  22. </archive>
  23. </configuration>
  24. </plugin>
  25. ...
  26. </plugins>
  27. </build>
  • Add bundle script (bnd.bnd) into the project root folder:

text

  1. Bundle-Name: ${project.name}
  2. Bundle-Version: ${project.version}
  3. Bundle-SymbolicName: ${project.groupId}.${project.artifactId}
  4. Export-Package: com.example.osgi.myapplication
  5. Import-Package: *
  6. Vaadin-OSGi-Extender: true
Note
The last line in the manifest tells Vaadin OSGi integration to scan all classes in the bundle and discover routes.

Publishing a Servlet With OSGi

It’s a developer responsibility to register a VaadinServlet in the servlet container (inside OSGi container). There are many ways to do it. One way is to use HTTP Whiteboard specification.

Java

  1. @Component(immediate = true)
  2. public class VaadinServletRegistration {
  3. private static class FixedVaadinServlet extends VaadinServlet {
  4. @Override
  5. public void init(ServletConfig servletConfig) throws ServletException {
  6. super.init(servletConfig);
  7. getService().setClassLoader(getClass().getClassLoader());
  8. }
  9. @Override
  10. protected DeploymentConfiguration createDeploymentConfiguration(Properties initParameters) {
  11. // npm mode is not currently supported
  12. initParameters.setProperty("compatibilityMode", "true");
  13. return super.createDeploymentConfiguration(initParameters);
  14. }
  15. }
  16. @Activate
  17. void activate(BundleContext ctx) {
  18. Hashtable<String, Object> properties = new Hashtable<>();
  19. properties.put(
  20. HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ASYNC_SUPPORTED,
  21. "true");
  22. properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN,
  23. "/*");
  24. ctx.registerService(Servlet.class, new FixedVaadinServlet(),
  25. properties);
  26. }
  27. }
Note
FixedVaadinServlet class is used here as a workaround for the Classloader bug. Once it’s fixed there will be no need in it.
Note
When you have more than one bundle created by Vaadin, note that you should not have multiple ‍‍‍VaadinServlet registrations with the same servlet pattern. So, you should either use a unique pattern for each bundle or create VaadinServlet in only one bundle. In the latter case, keep in mind that for the other bundles to work, it is required that the bundle containing the servlet is active.

Publishing Static Resources With OSGi

If your project has resources which are supposed to be available as static web resources then you should register them. In case you are using standalone servlet container you are usually using a webapp folder which is configured to be a static web resources folder for the web server. But there is no any dedicated webapp folder for OSGi bundles. Instead you should register your resource via the way provided by Vaadin OSGi integration. To do that implement either OsgiVaadinStaticResource or OsgiVaadinContributor as an OSGi service. Here the resource packaged in the jar file with /META-INF/resources/frontend/my-component.html is registered to be available by URL "http://localhost:8080/frontend/my-component.html":

Java

  1. @Component
  2. public class MyComponentResource implements OsgiVaadinStaticResource {
  3. public String getPath(){
  4. return "/META-INF/resources/frontend/my-component.html";
  5. }
  6. public String getAlias(){
  7. return "/frontend/my-component.html";
  8. }
  9. }

Classes discovering

Vaadin discovers a number of classes to delegate them some functionality. E.g. classes annotated with @Route annotation are used in the routing functionality (see Defining Routes with @Route). There are many other cases which requires classes discovering functionality (see also Router Exception Handling, Creating PWA with Flow). It doesn’t happen out of the box in OSGi container for every bundle. To avoid scanning all classes in all bundles Vaadin uses Vaadin-OSGi-Extender manifest header as a marker for those bundles that needs to be scanned. So if you have a bundle which contains routes or other classes whose functionality relies on inheritance or annotation presence you should mark this bundle using Vaadin-OSGi-Extender manifest header (so normally every Vaadin application bundle should have this manifest header otherwise routes declared in this bundle won’t be discovered):

text

  1. ....
  2. Export-Package: com.example.osgi.myapplication
  3. Import-Package: *
  4. Vaadin-OSGi-Extender: true
  5. ....

Deployment to OSGi container.

In order to have your application running under OSGi container, you need to have Vaadin Flow bundles deployed, and then the application bundle can be deployed and started. Please note that there are many transitive dependencies which are also need to be deployed. Bundle won’t be activated if all its dependencies are not deployed and activated (it might be that some OSGi containers may deploy transitive dependencies along with the bundle deployment). Here is a minimal list of required Vaadin Flow bundles:

  • flow-server-X.Y.Z.jar

  • flow-client-X.Y.Z.jar

  • flow-html-components-X.Y.Z.jar

  • flow-data-X.Y.Z.jar

  • flow-osgi-X.Y.Z.jar

This is not a full list of all required bundles. The full list is too long and may vary due to transitive dependencies. Here are some of the required external dependencies (the versions are omitted):

  • jsoup

  • gentyref-x.y.z.vaadin1.jar

  • gwt-elemental-x.y.z.vaadin2.jar

  • ph-css

  • …​.

Please note that some of the dependencies are repackaged by Vaadin because original jars are not OSGi compatible (like gwt-elemental). Other dependencies require some OSGi features which needs to be deployed at runtime but they don’t depend on them during compilation. This is the case with ph-css bundle. It depends on ph-commons (which should be deployed also of course) but the latter bundle requires ServiceLoader OSGi implementation. You will need to deploy the bundle which contains this implementation suitable for your OSGi container. Also Vaadin OSGi support uses OSGi Compendium API (which allows registering an OSGi service using declarative services annotations). If your OSGI container doesn’t have it out of the box, you have to deploy an implementation bundle to support the Compendium API.

Note
There exists an OSGi base starter project that is ready to use and it declares all bundles which needs to be deployed to the OSGi container as provided dependencies in the dedicated profile. Those bundles are copied into the specific folder using maven-dependency-plugin and auto-deployed from there. As a result all required bundles are deployed to the OSGi container. See https://github.com/vaadin/base-starter-flow-osgi.

In your project you will most likely want to use some ready-made Vaadin components like Vaadin Button. In this case you should deploy vaadin-button-flow bundle as a dependency. Please note that all Vaadin Flow components are OSGi compatible bundles but they depend on webjars with the client side web component resources which are not OSGi compatible unfortunately. See the next section about this topic.

Make webjar resource working in OSGi.

Normally every Flow component has a client side part which is distributed as a webjar. Webjars contain only web resources and they are not OSGi compatible. It means that webjar is not a bundle and cannot be deployed to an OSGi container. As a result you won’t get Flow component working without additional setup. We suggest a solution for repackaging webjar resources into the application bundle. Here is the code snippet of the project configuration which we use to repackage the webjars:

XML

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-dependency-plugin</artifactId>
  4. <executions>
  5. <execution>
  6. <id>unpack-dependencies</id>
  7. <phase>generate-resources</phase>
  8. <goals>
  9. <goal>unpack-dependencies</goal>
  10. </goals>
  11. <configuration>
  12. <includes>**/webjars/**</includes>
  13. </configuration>
  14. </execution>
  15. </executions>
  16. </plugin>
  17. <plugin>
  18. <artifactId>maven-antrun-plugin</artifactId>
  19. <version>1.7</version>
  20. <executions>
  21. <execution>
  22. <id>copy-frontend</id>
  23. <phase>generate-resources</phase>
  24. <configuration>
  25. <tasks>
  26. <mkdir
  27. dir="${project.build.directory}/generated-resources/frontend/bower_components"></mkdir>
  28. <copy
  29. todir="${project.build.directory}/generated-resources/frontend/bower_components">
  30. <fileset
  31. dir="${project.build.directory}/dependency/META-INF/resources/webjars/" />
  32. </copy>
  33. </tasks>
  34. </configuration>
  35. <goals>
  36. <goal>run</goal>
  37. </goals>
  38. </execution>
  39. </executions>
  40. </plugin>
  41. <plugin>
  42. <groupId>org.codehaus.mojo</groupId>
  43. <artifactId>build-helper-maven-plugin</artifactId>
  44. <version>3.0.0</version>
  45. <executions>
  46. <execution>
  47. <id>add-resource</id>
  48. <phase>generate-resources</phase>
  49. <goals>
  50. <goal>add-resource</goal>
  51. </goals>
  52. <configuration>
  53. <resources>
  54. <resource>
  55. <directory>${project.build.directory}/generated-resources</directory>
  56. <targetPath></targetPath>
  57. </resource>
  58. </resources>
  59. </configuration>
  60. </execution>
  61. </executions>
  62. </plugin>

This code snippet unpacks all dependencies and extracts webjars folder from them. Then it copies the resulting resources to the dedicated folder to create the appropriate structure for them and the folder is added as a resource folder. In the result the folder will be packaged in the jar archive and the resources will be available in the jar bundle starting from the archive root. It makes them automatically available as ServletContext resources.

The resulting frontend resources should also be available as web resources. See Publishing Static Resources With OSGi section how static resources may be registered via OsgiVaadinStaticResource/OsgiVaadinContributor. But HTTP Whiteboard specification allows to do the same easier:

Java

  1. @Component(service = FrontendResources.class)
  2. @HttpWhiteboardResource(pattern = "/frontend/*", prefix = "/frontend")
  3. public class FrontendResources {
  4. }
Tip
This is done in our OSGi base starter project https://github.com/vaadin/base-starter-flow-osgi. You may check out the code.