Java Reference
In-Depth Information
Figure 2.11 The Fancy Foods web page, before and after deleting the French-language translations from
the load directory
be started and stopped like bundles, so the fragment will be shown as INSTALLED .
Refresh the web bundle with refresh [web bundle number] and the fragment will
attach to the bundle and move to the RESOLVED state.
Check the web page again, and the greeting should be shown in French. Delete
the fancyfoods.web.nls.fr fragment JAR from the load directory, and try the web
page again—back to English (figure 2.11)!
Although internationalization is the most popular use for fragments, it's not the
only one. Anything can be put into a fragment, including Java classes. But including
classes in fragments for pluggability isn't the best implementation. OSG i provides
higher-level ways of achieving pluggability through services.
2.3
Decoupling with dependency injection
Despite working in two languages, the web application you've written is pretty limited
so far. Not only does it not do anything beyond issuing a greeting, it doesn't even say
hello in a particularly dynamic way. What if you wanted to change the greeting
depending on the time of day, or take the opportunity to highlight items which were
on special offer?
All of this logic could be coded into SayHello.java , but that's not a flexible or
dynamic architecture. (Remember—recompiling is no fun.) What you'd like is the
ability to publish, or register, extra content providers that are made available to the
web page without it having to hardcode lookups.
As you've seen, code can be separated out into fragments that are installed indepen-
dently. This is slightly more modular and dynamic than lumping everything into one
JAR . But a fragment's host must be specified at build time, which isn't modular. As you've
seen, they also aren't that dynamic, because the host bundle needs to be refreshed to
attach and detach them. This makes them a useful mechanism for extending or modi-
fying bundles, but not great for reuse or dynamic behavior changes.
If you're familiar with OSG i services, you might be thinking right now that OSG i ser-
vices are an excellent way of providing chunks of function that can appear and disappear
Search WWH ::




Custom Search