Java Reference
In-Depth Information
OSGi and JNDI
Retrieving the OSGi bundle context from the
ServletContext
is the most direct way
to interact with the OSGi environment. Many existing servlets use JNDI to discover
Java EE services—wouldn't it be great if a bridge existed between these two worlds?
Such a bridge does exist in the R4.2 Enterprise specification, so rest assured that
you can use this mechanism to access services. The technical details of how this
interaction works are beyond the scope of this topic; refer to the specification for
more information (
www.osgi.org/Download/Release4V42
).
15.1.3
Standard WARs: the Web URL Handler
As a convenience for users who wish to migrate web applications to
OSG
i but don't wish
to undertake the effort of converting a
WAR
file to a
WAB
, the Web Applications speci-
fication provides a utility mechanism to convert a
WAR
file to a
WAB
at execution time:
the Web
URL
Handler. It uses the
OSG
i
URL
Handlers Service to turn
WAR
s into
WAB
s.
URL Handlers Service specification
The OSGi URL Handlers Service specification provides a service-based approach for
bundles to offer custom stream and content handlers associated with URLs. The normal
approach for dealing with stream and content handlers in Java is to set a
URLStream-
HandlerFactory
and/or a
ContentHandlerFactory
on
URL
and
URLConnection
, re-
spectively. Unfortunately, these are singletons, so they can't be shared and they aren't
dynamic. The URL Handlers specification addresses both of these issues.
The URL Handlers Service works by setting the
URLStreamHandlerFactory
and
ContentHandlerFactory
objects once; it then uses the whiteboard pattern to dis-
cover services offered by bundles implementing custom stream and content han-
dlers. When the URL Handlers service receives a request for a specific protocol or
content type, it delegates the request to the appropriate underlying service to perform
the processing.
To u s e t h e We b
URL
Handler, all you need to do is prefix any existing
URL
pointing to
a
WAR
file with the
webbundle
protocol when installing the
WAR
file into the frame-
work. For example, you could use your shell's install command like this:
install webbundle:http://www.acme.com/acme.war?Bundle-SymoblicName=
com.example&Web-ContextPath=acme
The Web
URL
Handler converts the referenced
WAR
file into a
WAB
on the fly prior to
the
OSG
i framework installing. The Web
URL
Handler makes a best-effort attempt to
convert a
WAR
to a
WAB
, but in certain circumstances you may have to give it extra
hints to help the process go smoothly. In this example above, you specify a
Bundle-
SymbolicName
as a parameter in the query portion of the
URL
. The Web
URL
Handler
also supports a number of other parameters that affect the outcome of the conver-
sion; these parameters are listed in table 15.2.