Java Reference
In-Depth Information
PrintWriter html = response.getWriter();
html.append("<html>");
html.append("Hello valued customer!<br/>");
try {
InitialContext ctx = new InitialContext();
String jndiName = "osgi:service/" +
CurrentOffers.class.getName();
CurrentOffers offers =
(CurrentOffers) ctx.lookup(jndiName);
html.append("<table>");
List<SpecialOffer> currentOffers =
offers.getCurrentOffers();
for (SpecialOffer offer : currentOffers) {
writeRowForOffer(html, offer);
}
html.append("</table>");
} catch (NamingException e) {
html.append("We have no special offers today. " +
"Try again tomorrow.");
}
html.append("</html>");
}
private void writeRowForOffer(PrintWriter html,
SpecialOffer offer) {
html.append("<tr>");
String description = offer.getDescription();
Food offerFood = offer.getOfferFood();
html.append("<td>" + offerFood.getName() + "</td>");
html.append("<td>" + offerFood.getPrice() + "</td>");
html.append("<td>" + description + "</td>");
html.append("</tr>");
}
}
The osgi:service/ namespace provides access to services in the OSG i Service Registry.
Services are registered under their interface name, which makes them easy to find. If no
services implementing the SpecialOffer interface are registered, a NamingException
will be thrown. In this case that's not a problem—special offers aren't compulsory! (The
sharp-eyed among you will notice that we've abandoned internationalization again to
keep the sample shorter.)
Implementing SayHello JNDI has added some new dependencies, and that needs to
be reflected in the manifest. The fancyfood.offers , fancyfoods.food , and javax
.naming packages need to be imported:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: fancyfoods.web
Bundle-Version: 1.0.0
Bundle-ClassPath: WEB-INF/classes
Web-ContextPath: /fancyfoods.web
Import-Package: fancyfoods.offers;version="[1.0, 2.0)",
Search WWH ::




Custom Search