}
// remove system bundle.
all.remove(Activator.getTargetBC().getBundle(0));
ZipOutputStream out = null;
StartLevel sl = (StartLevel)slTracker.getService();
File jarunpackerFile = new File("../tools/jarunpacker/out/jarunpacker/jarunpacker.jar");
URL jarunpackerURL = null;
try {
jarunpackerURL = getClass().getResource("/jarunpacker.jar");
} catch (Exception ignored) {
}
InputStream jarunpacker_in = null;
try {
if(file.getName().endsWith(".jar")) {
if(jarunpackerURL != null) {
jarunpacker_in = jarunpackerURL.openStream();
} else if(jarunpackerFile.exists()) {
jarunpacker_in = new FileInputStream(jarunpackerFile);
}
if(jarunpacker_in != null) {
// Construct a string version of a manifest
StringBuffer sb = new StringBuffer();
sb.append("Manifest-Version: 1.0\n");
sb.append("Main-class: org.knopflerfish.tools.jarunpacker.Main\n");
sb.append("jarunpacker-optbutton: base\n");
sb.append("jarunpacker-destdir: .\n");
sb.append("knopflerfish-version: " + base + "\n");
sb.append("jarunpacker-opendir: " + base + "\n");
// Convert the string to a input stream
InputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
Manifest mf = new Manifest(is);
out = new JarOutputStream(new FileOutputStream(file), mf);
} else {
out = new JarOutputStream(new FileOutputStream(file));
}
} else if(file.getName().endsWith(".zip")) {
out = new ZipOutputStream(new FileOutputStream(file));
}
StringBuffer xargs = new StringBuffer();
int levelMax = -1;
int bid = 0;
int lastLevel = -1;
for(Iterator it = all.iterator(); it.hasNext(); ) {
Bundle b = (Bundle)it.next();
String loc = b.getLocation();
bid++;
URL srcURL = new URL(loc);
String name = Util.shortLocation(loc);
ZipEntry entry = new ZipEntry(base + "/" + name);
int level = -1;
try {
level = sl.getBundleStartLevel(b);
} catch (Exception ignored) { }
levelMax = Math.max(level, levelMax);
if(level != -1 && level != lastLevel) {
xargs.append("-initlevel " + level + "\n");
lastLevel = level;
}
xargs.append("-install file:" + name + "\n");
out.putNextEntry(entry);
InputStream in = null;
try {
in = srcURL.openStream();
int n = 0;
while ((n = in.read(buf)) != -1) {
out.write(buf, 0, n);
}
} finally {
try {
in.close();
} catch (Exception ignored) { }
}
}
bid = 0;
for(Iterator it = all.iterator(); it.hasNext(); ) {
Bundle b = (Bundle)it.next();
bid++;
if(b.getState() == Bundle.ACTIVE) {
xargs.append("-start " + bid + "\n");
}
}
if(levelMax != -1) {
xargs.append("-startlevel " + levelMax + "\n");
}
ZipEntry entry = new ZipEntry(base + "/" + "init.xargs");
out.putNextEntry(entry);
out.write(xargs.toString().getBytes());
entry = new ZipEntry(base + "/" + "framework.jar");
out.putNextEntry(entry);
InputStream in = null;
File fwFile = new File("framework.jar");
if(fwFile.exists()) {
try {
in = new FileInputStream(fwFile);
int n = 0;
while ((n = in.read(buf)) != -1) {
out.write(buf, 0, n);
}
} finally {
try {
in.close();
} catch (Exception ignored) { }
}
} else {
Activator.log.warn("No framework.jar file found");
}
// Copy jarunpacker files, if availbale
if(jarunpacker_in != null) {
JarInputStream jar_in = null;
try {
jar_in = new JarInputStream(new BufferedInputStream(jarunpacker_in));
ZipEntry srcEntry;
while(null != (srcEntry = jar_in.getNextEntry())) {
// Skip unused files from jarunpacker
if(srcEntry.getName().startsWith("META-INF") ||
srcEntry.getName().startsWith("OSGI-OPT")) {
continue;
}
ZipEntry destEntry = new ZipEntry(srcEntry.getName());
out.putNextEntry(destEntry);
long nTotal = 0;
int n = 0;
while (-1 != (n = jar_in.read(buf, 0, buf.length))) {
out.write(buf, 0, n);
nTotal += n;
}
}
} finally {
try { jar_in.close(); } catch (Exception ignored) { }
}
} else {
Activator.log.warn("No jarunpacker available");
}
// end of jarunpacker copy
} catch (Exception e) {
showErr("Failed to write to " + file, e);
Activator.log.error("Failed to write to " + file, e);
} finally {
try { out.close(); } catch (Exception ignored) { }
}
String txt =
"Saved deploy archive as\n\n" +