Home > Guides > Plugin Developers Guide > OSGi Plugin |
This plugin provides support for starting an instance of Apache Felix inside a web application, and scanning installed bundles for Struts configuration. An admin bundle is also provided. It can be used with Glassfish 3 as well (Glassfish 3 based on Apache Felix as well), but in such a way struts.osgi.host
must be defined.
There are two ways of organizing bundles. If third party bundles will not be used, then the application bundles can just be placed under /WEB-INF/classes/bundles
. Bundles in this dir will be started in run level 2, the Apache Felix framework's bundles will be loaded in run level 1. If third parties bundles will be used, or you need to start bundles on different run level, create sub dirs under /WEB-INF/classes/bundles
with numeric names (starting from "2" because "1" is reserved for Felix), which correspond to the run level number. For example bundles under /WEB-INF/classes/bundles/2
will be started in run level 2, and bundles under /WEB-INF/classes/bundles/3
will be started in run level 3.
Add these lines to MANIFEST.MF:
Struts2-Enabled: true Export-Package: com.mycompany.myapp.actions Bundle-Version: 1.0.0 Bundle-SymbolicName: foo.actions Import-Package: com.opensymphony.xwork2
Now the jar is ready to be deployed. Drop the jar into the /WEB-INF/classes/bundles
directory and it will automatically be installed when the application starts up.
If you want to use the Spring as the object factory for your actions, then follow these steps:
/META-INF/spring
in the bundle jar file/spring
(they must be in the classpath, if you are using maven, put thme under /src/resources/spring) in the application/WEB-INF/classes/bundles
in your applicationcom.springsource.org.aopalliance-1.0.0.jar com.springsource.org.apache.commons.logging-1.1.1.jar org.springframework.aop-2.5.5.A.jar org.springframework.beans-2.5.5.A.jar org.springframework.context-2.5.5.A.jar org.springframework.core-2.5.5.A.jar org.springframework.osgi.core-1.1.2.A.jar org.springframework.osgi.extender-1.1.2.A.jar org.springframework.osgi.io-1.1.2.A.jar org.springframework.osgi.web-1.1.2.A.jar org.springframework.web-2.5.5.A.jar
If you are going to use Velocity results, then add Velocity and Common Digester jars to your application. Using maven:
The Convention plugin will discover actions in bundles in the same way that it discovers them in normal applications. The Convention plugin expects result templates to be (by default) stored under /WEB-INF/content. When packaging actions inside bundles, there won't be a WEB-INF folder, so you must let Convention know where the templates are located. There are two ways of doing so(assuming the templates are under /content):
1. Set the templates location constant in struts.xml (in the application struts.xml, not a bundled struts.xml)
2. Using the ResultPath annotation
The OSGi plugins defines the osgi
interceptor and osgiStack
(defaultStack
plus the osgi
interceptor) in the package osgi-default
. This interceptor will check the action and if it implements org.apache.struts2.osgi.interceptor.BundleContextAware
, it will invoke setBundleContext(BundleContext bundleContext) on the action, passing the BundleContext of the OSGi container. The interceptor also checks if the class implements org.apache.struts2.osgi.interceptor.ServiceAware<T>
, if it does, setServices(List<T> services) will be called, where T is the type of a service published in the OSGi container. For example, lets assume an installed bundle publishes a service with the interface BookPriceLookup
, to get all the instances of this service, an action would look like:
An admin bundle is distributed with struts, which provides a simple interface to list the installed bundles. Using this interface the bundles can be stopped, started and updated (reloaded from the file system). This interface also provides information on the installed bundles, like OSGi metadata, and a list of packages and actions loaded from each bundle. An interactive AJAX shell is also available, which is just a web interface to the Apache Felix Shell. To use this bundle, just copy the jar file to /bundles (same place where the application bundles are installed) and open http://localhost:PORT/CONTEXT/osgi/admin/ (replace PORT and context)
When a bundle is started, the OSGi plugin will check for the header Struts2-Enabled
in it. If it is set to "true", the bundle will be scanned for XML config and Convention config. When a bundle is stopped, any actions that were loaded from it will be removed from the runtime configuration.
The following settings can be customized. See the developer guide.
Setting | Description | Default | Possible Values |
---|---|---|---|
| The alias of the ObjectFactory to wrap | | Any configured alias |
The following setting must be set as context parameters in web.xml, because they are used by the StrutsOsgiListener, for example:
If you are running your application on Glassfish 3 (which already contains Apache Felix) you must specify struts.osgi.host
, like below:
Setting | Description | Default | Possible Values |
---|---|---|---|
| Delete all installed bundles when the container starts | true | true or false |
| Run level to start the container | 3 | >=3 |
| Log level for Apache Felix | 1 (Error) | 1 = error, 2 = warning, 3 = information, and 4 = debug |
Jar files can be turned into bundles using the Maven Bundle Plugin like: