Java Reference
In-Depth Information
These are the same basic steps the generic bundle launcher uses, as we'll introduce in
the following subsections by breaking the example into short code snippets. The com-
plete source code for the generic launcher is in the launcher/ directory of the topic's
companion code.
13.2.1
Determining which bundles to install
As you've seen throughout the topic, the generic bundle launcher installs and starts
all bundles contained in a directory specified as a command line argument. The
launcher is composed of a single class, called Main , which is declared in the following
code snippet.
Listing 13.3 Main class declaration for generic bundle launcher
public class Main {
private static Framework fwk;
public static void main(String[] args) throws Exception {
if (args.length < 1 || !new File(args[0]).isDirectory()) {
System.out.println("Usage: <bundle-directory>");
} else {
File[] files = new File(args[0]).listFiles();
Arrays.sort(files);
List jars = new ArrayList();
for (int i = 0; i < files.length; i++)
if (files[i].getName().toLowerCase().endsWith(".jar"))
jars.add(files[i]);
...
The static member variable holds the framework instance you're going to create. You
verify that a directory was specified as a command line argument. If a directory was
specified, you get the files contained in it and save all files ending with .jar into a list to
be processed later.
13.2.2
Shutting down cleanly
You can't always guarantee that the launcher process will exit normally, so it's a good
idea to try to ensure your framework instance cleanly shuts down. Depending on the
framework implementation, you can end up with a corrupted bundle cache if you
don't shut down cleanly. The following listing adds a shutdown hook to the JVM pro-
cess to cleanly shut down your framework instance.
Listing 13.4 Using a shutdown hook to cleanly stop the framework
...
if (jars.isEmpty()) {
System.out.println("No bundles to install.");
} else {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
 
Search WWH ::




Custom Search