Java Reference
In-Depth Information
don't change much. In this regard, they may not be the best choice for the shell, but it
depends on what you want to achieve; for example, it makes it difficult to change the
shell's port dynamically. For now, we'll keep things simple, so this is sufficient.
3.3.2
Deploying bundles
Each bundle installed into the framework is represented by a Bundle object and can be
identified by its bundle identifier, location, or symbolic name. For most of the shell
commands you'll implement, you'll use the bundle identifier to retrieve a Bundle
object, because the bundle identifier is nice and concise. Most of the commands accept
a bundle identifier as a parameter, so let's look at how you can use it and the bundle
context to access Bundle objects associated with other bundles. As part of the design,
you create an abstract BasicCommand class to define a shared method, getBundle() , to
retrieve bundles by their identifier, as shown here:
protected volatile BundleContext m_context;
...
public Bundle getBundle(String id) {
Bundle bundle = m_context.getBundle(Long.parseLong(id.trim()));
if (bundle == null) {
throw new IllegalArgumentException("No such bundle.");
}
return bundle;
}
All you do is call BundleContext.getBundle() on the con-
text object with the parsed bundle identifier, which is
passed in as a String . The only special case you need to
worry about is when no bundle with the given identifier
exists. In such a case, you throw an exception.
Install
INSTALL COMMAND
With this basic functionality in place, you can start the first
command. The next listing shows the implementation of
an install command, and figure 3.8 reminds you which
portion of the bundle lifecycle is involved.
Installed
Figure 3.8 The install-
related portion of the bundle
lifecycle state diagram
Listing 3.5 Bundle install command
package org.foo.shell;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
public class InstallCommand extends BasicCommand {
public void exec(String args, PrintStream out, PrintStream err)
throws Exception {
Bundle bundle = m_context.installBundle(args);
out.println("Bundle: " + bundle.getBundleId());
}
}
Search WWH ::




Custom Search