Java Reference
In-Depth Information
public void stop(BundleContext context) {
m_binding.stop();
}
...
}
...
public interface Binding {
public void start();
public void stop() throws InterruptedException;
}
This class implements the OSG i BundleActivator interface. When the bundle is
installed and started, the framework constructs an instance of this activator class and
invokes the start() method. When the bundle is stopped, the framework invokes the
stop() method. The start() method is the starting point for your bundle, sort of like
the static main() method in standard Java. After it returns, your bundle is expected to
function until the stop() method is invoked at some later point. The stop() method
should undo anything done by the start() method.
We need to mention a few technical but potentially important details about the
handling of the BundleActivator instance:
The activator instance on which start() is called is the same instance on which
stop() is called.
After stop() is called, the activator instance is discarded and isn't used again.
If the bundle is subsequently restarted after being stopped, a new activator
instance is created, and the start() and stop() methods are invoked on it as
appropriate.
As you can see, the rest of the activator isn't complicated. In the start() method, you
get the port on which the bundle listens for connection requests and the number of
allowed concurrent connections. You also create a TelnetBinding , which does the
work of listening on a socket for user input and processes it; the details of creating the
telnet binding are omitted here for reasons of simplicity and brevity. The next step is
to start the binding, which creates a new Thread to run the shell. How this happens is
left to the binding, which you start next E .
The point about the binding starting its own thread is important because the acti-
vator methods shouldn't do much work. This is best practice as with most callback pat-
terns, which are supposed to return quickly, allowing the framework to carry on
managing other bundles. But it's also important to point out that the OSG i specifica-
tion doesn't mandate you start a new thread if your application's startup doesn't war-
rant it—the ball is in your court.
F o r t h e a c t i v a t o r stop() method, all you do is tell the binding to stop listening to user
input and cease to execute. You should make sure it does stop by waiting until its thread
is finished; the binding method waits for its thread to stop. Sometimes, you may have spe-
cial cases for certain situations because, as you'll see later, the shell thread itself may call
Search WWH ::




Custom Search