Java Reference
In-Depth Information
We now install our
calculator-service
bundle. We first copy the
JAR
file to the
FELIX_HOME
/bundle folder. Next, we use the
install
command:
-> install file:bundle/calculator-service.jar
A
BundleID
will be assigned to our bundle. We use that
BundleID
to manage our bundle.
Start the service with the following command
-> start [BundleID]
where
[BundleID]
is a placeholder for your bundle
ID
.
We've installed the bundle and started it. The bundle is now running and
exports the service as an
API
. Next, we create a sample client application for our
CalculatorService
.
16.2.1
The sample application
The idea behind the client application is to demonstrate how to make another bundle
that uses the first one, the
CalculatorService
. We start by implementing the
Bundle-
Activator
to override the behavior on start and stop. See listing 16.5.
Listing 16.5
ClientBundleActivator
implementation
[...]
import
com.manning.junitbook.ch16.service.CalculatorService;
B
C
D
public class
ClientBundleActivator implements BundleActivator {
public void
start(BundleContext context)
throws
Exception {
ServiceReference reference = context.getServiceReference(
CalculatorService.class.getName());
E
if
( reference !=
null
) {
CalculatorService calculator = (CalculatorService)
context.getService(reference);
F
BufferedReader in =
new
BufferedReader(
new
InputStreamReader(System.in));
System.out.println("Enter operation (add or multiply):");
String operation = in.readLine();
System.out.println("Enter several numbers
separated with a space:");
String line = in.readLine();
G
H
double
[] numbers = calculator.parseUserInput(line);
if
(operation.equals("add")) {
calculator.printResult(calculator.add(numbers));
}
else if
(operation.equals("multiply")) {
calculator.printResult(calculator.multiply(numbers));
}
else
{
I
J