use of java.util.jar.JarInputStream in project sling by apache.
the class BundlesInstaller method getBundleSymbolicName.
public String getBundleSymbolicName(File bundleFile) throws IOException {
String name = null;
final JarInputStream jis = new JarInputStream(new FileInputStream(bundleFile));
try {
final Manifest m = jis.getManifest();
if (m == null) {
fail("Manifest is null in " + bundleFile.getAbsolutePath());
}
name = m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
} finally {
jis.close();
}
return name;
}
use of java.util.jar.JarInputStream in project processdash by dtuma.
the class WebAppContextDashboard method scanJarForFiles.
private List<String> scanJarForFiles(JarResource jarResource, String[] suffixes) {
String jarUrl = jarResource.toString();
int pos = jarUrl.indexOf("!/");
if (pos == -1)
return Collections.EMPTY_LIST;
List<String> result = new ArrayList<String>();
String jarFileUrl = jarUrl.substring(4, pos);
JarInputStream jarIn = null;
try {
jarIn = new JarInputStream(new URL(jarFileUrl).openStream());
ZipEntry e;
while ((e = jarIn.getNextEntry()) != null) {
String name = e.getName();
if (filenameEndsWithSuffix(name, suffixes)) {
if (name.startsWith("Templates/"))
name = name.substring(9);
else
name = "/" + name;
result.add(name);
}
}
} catch (Exception e) {
Log.error("Unable to scan '" + jarFileUrl + "' for files", e);
} finally {
FileUtils.safelyClose(jarIn);
}
return result;
}
use of java.util.jar.JarInputStream in project processdash by dtuma.
the class TemplateLoader method searchJarForTemplates.
private static JarSearchResult searchJarForTemplates(DashHierarchy templates, String jarURL, DataRepository data) {
boolean foundTemplates = false;
try {
debug("searching for templates in " + jarURL);
URL jarFileUrl = new URL(jarURL);
JarInputStream jarFile = new JarInputStream((jarFileUrl).openStream());
ZipEntry file;
String filename;
while ((file = jarFile.getNextEntry()) != null) {
filename = file.getName().toLowerCase();
if (filename.equals(MCF_PROCESS_XML)) {
String baseURL = "jar:" + jarURL + "!/";
InputStream mcfXmlData = MCFManager.getInstance().registerMcf(baseURL, jarFile, null, true);
if (mcfXmlData != null) {
String n = MCF_PROCESS_XML + " (in " + jarURL + ")";
loadXMLProcessTemplate(templates, data, n, null, file.getTime(), mcfXmlData, true);
jarFile.close();
return JarSearchResult.Mcf;
}
}
if (startsWithIgnoreCase(filename, TEMPLATE_DIR)) {
if (filename.lastIndexOf('/') != 9)
continue;
} else if (startsWithIgnoreCase(filename, WEB_INF_DIR)) {
if (!filename.equalsIgnoreCase(WEB_INF_XML_FILE))
continue;
} else {
continue;
}
if (filename.endsWith(XML_TEMPLATE_SUFFIX) || filename.endsWith(XML_TEMPLATE_FILE)) {
debug("loading template: " + filename);
String n = file.getName() + " (in " + jarURL + ")";
loadXMLProcessTemplate(templates, data, n, jarFileUrl, file.getTime(), jarFile, false);
foundTemplates = true;
} else if (filename.endsWith(TEMPLATE_SUFFIX)) {
debug("loading template: " + filename);
loadProcessTemplate(templates, jarFile, false);
foundTemplates = true;
} else if (filename.endsWith(DATAFILE_SUFFIX)) {
try {
debug("loading data: " + filename);
data.addGlobalDefinitions(jarFile, false);
} catch (Exception e) {
logger.severe("unable to load global process data from " + file.getName() + " in " + jarURL + ": " + e);
}
}
}
jarFile.close();
} catch (IOException ioe) {
logger.severe("error looking for templates in " + jarURL);
ioe.printStackTrace(System.out);
}
return foundTemplates ? JarSearchResult.Template : JarSearchResult.None;
}
use of java.util.jar.JarInputStream in project bnd by bndtools.
the class bnd method _grep.
@Description("Grep the manifest of bundles/jar files. ")
public void _grep(grepOptions opts) throws Exception {
List<String> args = opts._arguments();
String s = args.remove(0);
Pattern pattern = Glob.toPattern(s);
if (pattern == null) {
messages.InvalidGlobPattern_(s);
return;
}
if (args.isEmpty()) {
args = new ExtList<String>(getBase().list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
}));
}
Set<String> headers = opts.headers();
if (headers == null)
headers = new TreeSet<String>();
if (opts.exports())
headers.add(Constants.EXPORT_PACKAGE);
if (opts.bsn())
headers.add(Constants.BUNDLE_SYMBOLICNAME);
if (opts.imports())
headers.add(Constants.IMPORT_PACKAGE);
Instructions instructions = new Instructions(headers);
for (String fileName : args) {
File file = getFile(fileName);
if (!file.isFile()) {
messages.NoSuchFile_(file);
continue;
}
try (JarInputStream in = new JarInputStream(IO.stream(file))) {
Manifest m = in.getManifest();
for (Object header : m.getMainAttributes().keySet()) {
Attributes.Name name = (Name) header;
if (instructions.isEmpty() || instructions.matches(name.toString())) {
String h = m.getMainAttributes().getValue(name);
QuotedTokenizer qt = new QuotedTokenizer(h, ",;=");
for (String value : qt.getTokenSet()) {
Matcher matcher = pattern.matcher(value);
while (matcher.find()) {
int start = matcher.start() - 8;
if (start < 0)
start = 0;
int end = matcher.end() + 8;
if (end > value.length())
end = value.length();
out.printf("%40s : %20s ...%s[%s]%s...\n", fileName, name, value.substring(start, matcher.start()), value.substring(matcher.start(), matcher.end()), value.substring(matcher.end(), end));
}
}
}
}
}
}
}
use of java.util.jar.JarInputStream in project sling by apache.
the class DeploymentPackageInstaller method getManifest.
/**
* Read the manifest from supplied input stream, which is closed before return.
*/
private Manifest getManifest(final InputStream ins) throws IOException {
Manifest result = null;
if (ins != null) {
JarInputStream jis = null;
try {
jis = new JarInputStream(ins);
result = jis.getManifest();
} finally {
// since closing the jar stream closes the input stream
if (jis != null) {
try {
jis.close();
} catch (final IOException ignore) {
}
} else {
try {
ins.close();
} catch (final IOException ignore) {
}
}
}
}
return result;
}
Aggregations