Java Reference
In-Depth Information
The “Service”
Let's first examine the Java code for the service's interface. It describes a service whose sole function is to
take as inputs a target language and a name and to return as output a greeting.
package com.apress.springenterpriserecipes.osgi.helloworld.service;
public interface GreeterService {
String greet(String language, String name);
The implementation's similarly plain. It hard-codes the greetings for three languages and satisfies
the interface.
package com.apress.springenterpriserecipes.osgi.helloworld.service;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class GreeterServiceImpl implements GreeterService {
private Map<String, String> salutation;
public GreeterServiceImpl() {
salutation = new HashMap<String, String>();
salutation.put(Locale.ENGLISH.toString(), "Hello, %s");
salutation.put(Locale.FRENCH.toString(), "Bonjour, %s");
salutation.put(Locale.ITALIAN.toString(), "Buongiorno, %s");
* @param language Can be any language you want, so long as that language is one of
* <code>Locale.ENGLISH.toString()</code>,
* <code>Locale.ITALIAN.toString()</code>, or
* <code>Locale.FRENCH.toString()</code>.
* :-)
* @param name the name of the person you'd like to address
* @return the greeting, in the language you want, tailored to the name you specified
public String greet(String language, String name) {
if (salutation.containsKey(language))
return String.format(salutation.get(language), name);
throw new RuntimeException(String.format("The language you specified “+
doesn't exist", language));
Search WWH ::

Custom Search