Considerations for using web applications and bundle fragments in web application bundles

There are a few specifics to consider when you develop a web-enabled OSGi application, especially if your application uses the Servlet 3.0 or Contexts and Dependency Injection (CDI) capabilities of the Java™ Platform, Enterprise Edition 6 (Java EE 6) specification.

Files in the root directory

Any files that are in the root directory of a web application can be viewed or downloaded using a browser. To prevent your bundles and bundle fragments from being accessed in this way, you should keep your .class files in a subdirectory such as WEB-INF/classes, and your .jar files in a subdirectory such as WEB-INF/lib, rather than in the root of the bundle.

Fragment attach order

You can extend bundles by adding bundle fragments. However you cannot control the order in which bundle fragments are attached.

To support the Java Servlet 3.0 specification, this implementation lets you define a web application through annotations in bundles and bundle fragments, as well as (or instead of) through a web.xml file for the application. The configuration for a web application is built by scanning its class path for annotations, and the order of components on the class path depends upon the fragment attach order.

Because you cannot control the order in which bundle fragments are attached, you cannot predict the order of items on the class path. This means that the design for your web application must not rely on bundle fragments being attached in a particular order.

Class path ordering in web fragments

The Java Servlet 3.0 specification lets you use web fragments to define the class path order. For OSGi applications, the bundle class path is the primary mechanism for specifying the ordering of components. If you use the Java Servlet 3.0 ordering capabilities for web fragments, you must ensure that the resulting class path order matches the bundle class path.

Web application bundles and CDI

For Contexts and Dependency Injection (CDI) to work for a directory or Java archive (JAR) file on a bundle class path, you must add the file META-INF/beans.xml within that directory or JAR file. For example, consider the following bundle class path:
BundleClassPath:WEB-INF/myclasses,WEB-INF/yourclasses.jar,
WEB-INF/hisclasses,WEB-INF/herclasses.jar
For CDI to work with a class WEB-INF/myclasses/a.class, you need to add the file WEB-INF/myclasses/META-INF/beans.xml. Similarly, for CDI to work with a class WEB-INF/herclasses.jar/b.class, you need to add the file WEB-INF/herclasses.jar/META-INF/beans.xml.
Note: If you convert a web application as described in Converting a web application archive file to an OSGi web application bundle, and the web archive (WAR) file has classes in the WEB-INF/classes directory and has a WEB-INF/beans.xml file, then the conversion process automatically adds the file WEB-INF/classes/META-INF/beans.xml to the web application bundle (WAB) file that is hosted.