use of java.util.jar.JarInputStream in project karaf by apache.
the class BlueprintDeploymentListenerTest method testCustomManifest.
public void testCustomManifest() throws Exception {
File f = File.createTempFile("smx", ".jar");
try {
OutputStream os = new FileOutputStream(f);
BlueprintTransformer.transform(getClass().getClassLoader().getResource("test.xml"), os);
os.close();
InputStream is = new FileInputStream(f);
JarInputStream jar = new JarInputStream(is);
jar.getManifest().write(System.err);
is.close();
} finally {
f.delete();
}
}
use of java.util.jar.JarInputStream in project karaf by apache.
the class Kar method extract.
/**
* Extract a kar from a given URI into a repository dir and resource dir
* and populate shouldInstallFeatures and featureRepos
*
* @param repoDir directory to write the repository contents of the kar to
* @param resourceDir directory to write the resource contents of the kar to
*/
public void extract(File repoDir, File resourceDir) {
InputStream is = null;
JarInputStream zipIs = null;
FeatureDetector featureDetector = new FeatureDetector();
this.featureRepos = new ArrayList<>();
this.shouldInstallFeatures = true;
try {
is = karUri.toURL().openStream();
repoDir.mkdirs();
if (!repoDir.isDirectory()) {
throw new RuntimeException("The KAR file " + karUri + " is already installed");
}
LOGGER.debug("Uncompress the KAR file {} into directory {}", karUri, repoDir);
zipIs = new JarInputStream(is);
boolean scanForRepos = true;
Manifest manifest = zipIs.getManifest();
if (manifest != null) {
Attributes attr = manifest.getMainAttributes();
String featureStartSt = (String) attr.get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_START));
if ("false".equals(featureStartSt)) {
shouldInstallFeatures = false;
}
String featureReposAttr = (String) attr.get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_REPOS));
if (featureReposAttr != null) {
featureRepos.add(new URI(featureReposAttr));
scanForRepos = false;
}
}
ZipEntry entry = zipIs.getNextEntry();
while (entry != null) {
if (entry.getName().startsWith("repository/")) {
String path = entry.getName().substring("repository/".length());
File destFile = new File(repoDir, path);
extract(zipIs, entry, destFile);
if (scanForRepos && featureDetector.isFeaturesRepository(destFile)) {
Map map = new HashMap<>();
String uri = Parser.pathToMaven(path, map);
if (map.get("classifier") != null && ((String) map.get("classifier")).equalsIgnoreCase("features"))
featureRepos.add(URI.create(uri));
else
featureRepos.add(destFile.toURI());
}
}
if (entry.getName().startsWith("resources/")) {
String path = entry.getName().substring("resources/".length());
File destFile = new File(resourceDir, path);
extract(zipIs, entry, destFile);
}
entry = zipIs.getNextEntry();
}
} catch (Exception e) {
throw new RuntimeException("Error extracting kar file " + karUri + " into dir " + repoDir + ": " + e.getMessage(), e);
} finally {
closeStream(zipIs);
closeStream(is);
}
}
use of java.util.jar.JarInputStream in project karaf by apache.
the class EncryptableConfigAdminPropertyPlaceholderTest method getBundleDescriptor.
private BundleDescriptor getBundleDescriptor(String path, TinyBundle bundle) throws Exception {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
StreamUtils.copy(bundle.build(), fos);
fos.close();
JarInputStream jis = new JarInputStream(new FileInputStream(file));
Map<String, String> headers = new HashMap<>();
for (Map.Entry entry : jis.getManifest().getMainAttributes().entrySet()) {
headers.put(entry.getKey().toString(), entry.getValue().toString());
}
return new BundleDescriptor(getClass().getClassLoader(), "jar:" + file.toURI().toString() + "!/", headers);
}
use of java.util.jar.JarInputStream in project karaf by apache.
the class EncryptablePropertyPlaceholderTest method getBundleDescriptor.
private BundleDescriptor getBundleDescriptor(String path, TinyBundle bundle) throws Exception {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
StreamUtils.copy(bundle.build(), fos);
fos.close();
JarInputStream jis = new JarInputStream(new FileInputStream(file));
Map<String, String> headers = new HashMap<>();
for (Map.Entry entry : jis.getManifest().getMainAttributes().entrySet()) {
headers.put(entry.getKey().toString(), entry.getValue().toString());
}
return new BundleDescriptor(getClass().getClassLoader(), "jar:" + file.toURI().toString() + "!/", headers);
}
use of java.util.jar.JarInputStream in project karaf by apache.
the class FileUtil method downloadSource.
public static void downloadSource(PrintStream out, PrintStream err, URL srcURL, String dirStr, boolean extract) {
// Get the file name from the URL.
String fileName = (srcURL.getFile().lastIndexOf('/') > 0) ? srcURL.getFile().substring(srcURL.getFile().lastIndexOf('/') + 1) : srcURL.getFile();
try {
out.println("Connecting...");
File dir = new File(dirStr);
if (!dir.exists()) {
err.println("Destination directory does not exist.");
}
File file = new File(dir, fileName);
OutputStream os = new FileOutputStream(file);
URLConnection conn = srcURL.openConnection();
int total = conn.getContentLength();
InputStream is = conn.getInputStream();
if (total > 0) {
out.println("Downloading " + fileName + " ( " + total + " bytes ).");
} else {
out.println("Downloading " + fileName + ".");
}
byte[] buffer = new byte[4096];
int count = 0;
for (int len = is.read(buffer); len > 0; len = is.read(buffer)) {
count += len;
os.write(buffer, 0, len);
}
os.close();
is.close();
if (extract) {
JarInputStream jis = new JarInputStream(new FileInputStream(file));
out.println("Extracting...");
unjar(jis, dir);
jis.close();
file.delete();
}
} catch (Exception ex) {
err.println(ex);
}
}
Aggregations