use of java.util.jar.JarInputStream in project aries by apache.
the class BundleContextMock method installBundle.
/**
* Asks to install an OSGi bundle from the given location.
*
* @param location the location of the bundle on the file system.
* @return the installed bundle.
* @throws BundleException
*/
public Bundle installBundle(String location) throws BundleException {
try {
URI uri = new URI(location.replaceAll(" ", "%20"));
File baseDir = new File(uri);
Manifest man = null;
//check if it is a directory
if (baseDir.isDirectory()) {
man = new Manifest(new FileInputStream(new File(baseDir, "META-INF/MANIFEST.MF")));
} else //if it isn't assume it is a jar file
{
InputStream is = new FileInputStream(baseDir);
JarInputStream jis = new JarInputStream(is);
man = jis.getManifest();
jis.close();
if (man == null) {
throw new BundleException("Null manifest");
}
}
return createBundle(man, location);
} catch (IOException e) {
throw new BundleException(e.getMessage(), e);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
throw new BundleException(e.getMessage(), e);
}
}
use of java.util.jar.JarInputStream in project aries by apache.
the class Util method getClassLoaderViaBundleClassPath.
private static ClassLoader getClassLoaderViaBundleClassPath(Bundle b, URL url) {
try {
JarInputStream jis = null;
try {
jis = new JarInputStream(url.openStream());
JarEntry je = null;
while ((je = jis.getNextJarEntry()) != null) {
String path = je.getName();
if (path.endsWith(".class")) {
ClassLoader cl = getClassLoaderFromClassResource(b, path);
if (cl != null)
return cl;
}
}
} finally {
if (jis != null)
jis.close();
}
} catch (IOException e) {
BaseActivator.activator.log(LogService.LOG_ERROR, "Problem loading class from embedded jar file: " + url + " in bundle " + b.getSymbolicName(), e);
}
return null;
}
use of java.util.jar.JarInputStream in project aries by apache.
the class JarCreator method createJarFromFile.
private static void createJarFromFile(String fileName, String version) throws Exception {
JarOutputStream jos = null;
FileOutputStream fos = null;
ZipEntry entry = null;
try {
// let's get hold of jars on disk
File jarFile = new File(fileName);
JarInputStream jarInput = new JarInputStream(jarFile.toURL().openStream());
Manifest manifest = jarInput.getManifest();
//update manifest, Bundle-Version
Attributes attr = manifest.getMainAttributes();
attr.putValue(Constants.BUNDLE_VERSION, version);
if (fileName.indexOf("helloIsolationRef") < 0) {
attr.putValue(Constants.EXPORT_PACKAGE, "org.apache.aries.subsystem.example.helloIsolation;uses:=\"org.osgi.util.tracker,org.osgi.framework\";version=" + version);
}
int lastSlash = fileName.lastIndexOf("/");
// trim the path
fileName = fileName.substring(lastSlash + 1);
int loc = fileName.indexOf("-");
String jarFilePath = fileName.substring(0, loc + 1) + version + ".jar";
if (fileName.indexOf("helloIsolationRef") < 0) {
File directory = new File(System.getProperty("user.home") + "/.m2/repository/org/apache/aries/subsystem/example/org.apache.aries.subsystem.example.helloIsolation/" + version + "/");
if (!directory.exists()) {
directory.mkdir();
}
fos = new FileOutputStream(directory.getAbsolutePath() + "/" + jarFilePath);
} else {
File directory = new File(System.getProperty("user.home") + "/.m2/repository/org/apache/aries/subsystem/example/org.apache.aries.subsystem.example.helloIsolationRef/" + version + "/");
if (!directory.exists()) {
directory.mkdir();
}
fos = new FileOutputStream(directory.getAbsolutePath() + "/" + jarFilePath);
}
jos = new JarOutputStream(fos, manifest);
//Copy across all entries from the original jar
int val;
while ((entry = jarInput.getNextEntry()) != null) {
jos.putNextEntry(entry);
byte[] buffer = new byte[4096];
while ((val = jarInput.read(buffer)) != -1) jos.write(buffer, 0, val);
}
jos.closeEntry();
jos.finish();
System.out.println("finishing creating jar file: " + jarFilePath + " in local m2 repo");
} finally {
if (jos != null) {
jos.close();
}
if (fos != null) {
fos.close();
}
}
}
use of java.util.jar.JarInputStream in project zm-mailbox by Zimbra.
the class ZimletFile method initialize.
@SuppressWarnings("unchecked")
private void initialize(String name) throws IOException, ZimletException {
if (!name.matches(ZimletUtil.ZIMLET_NAME_REGEX)) {
//so there is no need to try and load a zimlet with a bad name
throw ZimletException.INVALID_ZIMLET_NAME();
}
if (name.endsWith(ZIP_SUFFIX)) {
name = name.substring(0, name.length() - 4);
}
mDescFile = name + XML_SUFFIX;
mEntries = new HashMap<String, ZimletEntry>();
if (mBaseStream != null) {
mCopy = ByteUtil.getContent(mBaseStream, 0);
JarInputStream zis = new JarInputStream(new ByteArrayInputStream(mCopy));
Manifest mf = zis.getManifest();
if (mf != null) {
mDescFile = mf.getMainAttributes().getValue("Zimlet-Description-File");
}
ZipEntry entry = zis.getNextEntry();
while (entry != null) {
if (entry.getSize() > 0)
mEntries.put(entry.getName().toLowerCase(), new ZimletRawEntry(zis, entry.getName(), (int) entry.getSize()));
zis.closeEntry();
entry = zis.getNextEntry();
}
zis.close();
} else if (mBase.isDirectory()) {
addFileEntry(mBase, "");
} else {
JarFile jar = new JarFile(mBase);
Manifest mf = jar.getManifest();
if (mf != null) {
mDescFile = mf.getMainAttributes().getValue("Zimlet-Description-File");
}
Enumeration entries = jar.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (entry.getSize() > 0)
mEntries.put(entry.getName().toLowerCase(), new ZimletZipEntry(jar, entry));
}
}
initZimletDescription();
mZimletName = mDesc.getName();
}
use of java.util.jar.JarInputStream in project aries by apache.
the class WarToWabConverterImpl method generateManifest.
private void generateManifest() throws IOException {
if (wabManifest != null) {
// WAB manifest is already generated
return;
}
JarInputStream jarInput = null;
try {
jarInput = new JarInputStream(input.getInputStream());
Manifest manifest = jarInput.getManifest();
if (isBundle(manifest)) {
wabManifest = updateBundleManifest(manifest);
} else {
scanForDependencies(jarInput);
// Add the new properties to the manifest byte stream
wabManifest = updateManifest(manifest);
}
} finally {
try {
if (jarInput != null)
jarInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Aggregations