Java Reference
In-Depth Information
UNINSTALLED , an IllegalStateException is thrown. As with the other lifecycle opera-
tions, a bundle shouldn't attempt to uninstall itself.
Listing 3.9 Bundle uninstall command
package org.foo.shell;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
public class UninstallCommand extends BasicCommand {
public void exec(String id) throws Exception {
Bundle bundle = getBundle(id);
bundle.uninstall();
}
}
That's it. You've created a telnet-based shell bundle that you can use in any OSG i
framework. But there is one fly in the ointment. Most of the shell commands require
the bundle identifier to perform their action, but how does the shell user know which
identifier to use? You need some way to inspect the state of the framework's installed
bundle set. You'll create a command for that next.
3.3.3
Inspecting framework state
You need one more command to display information about the bundles currently
installed in the framework. The next listing shows a simple implementation of a
bundles command.
Listing 3.10 Bundle information example
package org.foo.shell;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
public class BundlesCommand extends BasicCommand {
public void exec(String args, PrintStream out, PrintStream err)
throws Exception {
Bundle[] bundles = m_context.getBundles();
out.println(" ID State Name");
for (Bundle bundle : bundles) {
printBundle(
bundle.getBundleId(), getStateString(bundle.getState()),
(String) bundle.getHeaders().get(Constants.BUNDLE_NAME),
bundle.getLocation(), bundle.getSymbolicName(), out);
}
}
private String getStateString(int state) {
switch (state) {
 
Search WWH ::




Custom Search