public Set<String> listResources(String subdir) { try { Set<String> result = new HashSet<String>(); if (resourceURL != null) { String protocol = resourceURL.getProtocol(); if (protocol.equals("jar")) { String resPath = resourceURL.getPath(); int pling = resPath.lastIndexOf("!"); URL jarURL = new URL(resPath.substring(0, pling)); String resDirInJar = resPath.substring(pling + 2); String prefix = resDirInJar + subdir + "/"; // System.out.printf("BaseMod.listResources: looking for names starting with %s\n", // prefix); JarFile jar = new JarFile(new File(jarURL.toURI())); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { String name = entries.nextElement().getName(); if (name.startsWith(prefix) && !name.endsWith("/") && !name.contains("/.")) { // System.out.printf("BaseMod.listResources: name = %s\n", name); result.add(name.substring(prefix.length())); } } } else throw new RuntimeException("Resource URL protocol " + protocol + " not supported"); } return result; } catch (Exception e) { throw new RuntimeException(e); } }
protected void addPathFile(final File pathComponent) throws IOException { if (!this.pathComponents.contains(pathComponent)) { this.pathComponents.addElement(pathComponent); } if (pathComponent.isDirectory()) { return; } final String absPathPlusTimeAndLength = pathComponent.getAbsolutePath() + pathComponent.lastModified() + "-" + pathComponent.length(); String classpath = AntClassLoader.pathMap.get(absPathPlusTimeAndLength); if (classpath == null) { JarFile jarFile = null; try { jarFile = new JarFile(pathComponent); final Manifest manifest = jarFile.getManifest(); if (manifest == null) { return; } classpath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH); } finally { if (jarFile != null) { jarFile.close(); } } if (classpath == null) { classpath = ""; } AntClassLoader.pathMap.put(absPathPlusTimeAndLength, classpath); } if (!"".equals(classpath)) { final URL baseURL = AntClassLoader.FILE_UTILS.getFileURL(pathComponent); final StringTokenizer st = new StringTokenizer(classpath); while (st.hasMoreTokens()) { final String classpathElement = st.nextToken(); final URL libraryURL = new URL(baseURL, classpathElement); if (!libraryURL.getProtocol().equals("file")) { this.log( "Skipping jar library " + classpathElement + " since only relative URLs are supported by this" + " loader", 3); } else { final String decodedPath = Locator.decodeUri(libraryURL.getFile()); final File libraryFile = new File(decodedPath); if (!libraryFile.exists() || this.isInPath(libraryFile)) { continue; } this.addPathFile(libraryFile); } } } }
private Manifest getJarManifest(final File container) throws IOException { if (container.isDirectory()) { return null; } final JarFile jarFile = this.jarFiles.get(container); if (jarFile == null) { return null; } return jarFile.getManifest(); }
private Certificate[] getCertificates(final File container, final String entry) throws IOException { if (container.isDirectory()) { return null; } final JarFile jarFile = this.jarFiles.get(container); if (jarFile == null) { return null; } final JarEntry ent = jarFile.getJarEntry(entry); return (Certificate[]) ((ent == null) ? null : ent.getCertificates()); }
public synchronized void cleanup() { final Enumeration<JarFile> e = this.jarFiles.elements(); while (e.hasMoreElements()) { final JarFile jarFile = e.nextElement(); try { jarFile.close(); } catch (IOException ex) { } } this.jarFiles = new Hashtable<File, JarFile>(); if (this.project != null) { this.project.removeBuildListener(this); } this.project = null; }
public byte[] getBytes(String className) { try { Tracer.mark(); String realName = className.replace(".", "/"); realName += ".class"; JarEntry je = jf.getJarEntry(realName); InputStream is = jf.getInputStream(je); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[4096]; while (is.available() > 0) { int read = is.read(buff); baos.write(buff, 0, read); } is.close(); return baos.toByteArray(); } catch (Exception e) { } finally { Tracer.unmark(); } return null; }
public String verify(JarFile jar, String... algorithms) throws IOException { if (algorithms == null || algorithms.length == 0) algorithms = new String[] {"MD5", "SHA"}; else if (algorithms.length == 1 && algorithms[0].equals("-")) return null; try { Manifest m = jar.getManifest(); if (m.getEntries().isEmpty()) return "No name sections"; for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) { JarEntry je = e.nextElement(); if (MANIFEST_ENTRY.matcher(je.getName()).matches()) continue; Attributes nameSection = m.getAttributes(je.getName()); if (nameSection == null) return "No name section for " + je.getName(); for (String algorithm : algorithms) { try { MessageDigest md = MessageDigest.getInstance(algorithm); String expected = nameSection.getValue(algorithm + "-Digest"); if (expected != null) { byte digest[] = Base64.decodeBase64(expected); copy(jar.getInputStream(je), md); if (!Arrays.equals(digest, md.digest())) return "Invalid digest for " + je.getName() + ", " + expected + " != " + Base64.encodeBase64(md.digest()); } else reporter.error("could not find digest for " + algorithm + "-Digest"); } catch (NoSuchAlgorithmException nsae) { return "Missing digest algorithm " + algorithm; } } } } catch (Exception e) { return "Failed to verify due to exception: " + e.getMessage(); } return null; }
protected URL getResourceURL(final File file, final String resourceName) { try { JarFile jarFile = this.jarFiles.get(file); if (jarFile == null && file.isDirectory()) { final File resource = new File(file, resourceName); if (resource.exists()) { try { return AntClassLoader.FILE_UTILS.getFileURL(resource); } catch (MalformedURLException ex) { return null; } } } else { if (jarFile == null) { if (!file.exists()) { return null; } jarFile = new JarFile(file); this.jarFiles.put(file, jarFile); jarFile = this.jarFiles.get(file); } final JarEntry entry = jarFile.getJarEntry(resourceName); if (entry != null) { try { return new URL("jar:" + AntClassLoader.FILE_UTILS.getFileURL(file) + "!/" + entry); } catch (MalformedURLException ex) { return null; } } } } catch (Exception e) { final String msg = "Unable to obtain resource from " + file + ": "; this.log(msg + e, 1); System.err.println(msg); e.printStackTrace(); } return null; }
private InputStream getResourceStream(final File file, final String resourceName) { try { JarFile jarFile = this.jarFiles.get(file); if (jarFile == null && file.isDirectory()) { final File resource = new File(file, resourceName); if (resource.exists()) { return new FileInputStream(resource); } } else { if (jarFile == null) { if (!file.exists()) { return null; } jarFile = new JarFile(file); this.jarFiles.put(file, jarFile); jarFile = this.jarFiles.get(file); } final JarEntry entry = jarFile.getJarEntry(resourceName); if (entry != null) { return jarFile.getInputStream(entry); } } } catch (Exception e) { this.log( "Ignoring Exception " + e.getClass().getName() + ": " + e.getMessage() + " reading resource " + resourceName + " from " + file, 3); } return null; }
public CommandData parseCommandData(ArtifactData artifact) throws Exception { File source = new File(artifact.file); if (!source.isFile()) throw new FileNotFoundException(); CommandData data = new CommandData(); data.sha = artifact.sha; data.jpmRepoDir = repoDir.getCanonicalPath(); JarFile jar = new JarFile(source); try { reporter.trace("Parsing %s", source); Manifest m = jar.getManifest(); Attributes main = m.getMainAttributes(); data.name = data.bsn = main.getValue("Bundle-SymbolicName"); String version = main.getValue("Bundle-Version"); if (version == null) data.version = Version.LOWEST; else data.version = new Version(version); data.main = main.getValue("Main-Class"); data.description = main.getValue("Bundle-Description"); data.title = main.getValue("JPM-Name"); reporter.trace("name " + data.name + " " + data.main + " " + data.title); DependencyCollector path = new DependencyCollector(this); path.add(artifact); DependencyCollector bundles = new DependencyCollector(this); if (main.getValue("JPM-Classpath") != null) { Parameters requires = OSGiHeader.parseHeader(main.getValue("JPM-Classpath")); for (Map.Entry<String, Attrs> e : requires.entrySet()) { path.add(e.getKey(), e.getValue().get("name")); // coordinate } } else if (!artifact.local) { // No JPM-Classpath, falling back to // server's revision // Iterable<RevisionRef> closure = // library.getClosure(artifact.sha, // false); // System.out.println("getting closure " + artifact.url + " " + // Strings.join("\n",closure)); // if (closure != null) { // for (RevisionRef ref : closure) { // path.add(Hex.toHexString(ref.revision)); // } // } } if (main.getValue("JPM-Runbundles") != null) { Parameters jpmrunbundles = OSGiHeader.parseHeader(main.getValue("JPM-Runbundles")); for (Map.Entry<String, Attrs> e : jpmrunbundles.entrySet()) { bundles.add(e.getKey(), e.getValue().get("name")); } } reporter.trace("collect digests runpath"); data.dependencies.addAll(path.getDigests()); reporter.trace("collect digests bundles"); data.runbundles.addAll(bundles.getDigests()); Parameters command = OSGiHeader.parseHeader(main.getValue("JPM-Command")); if (command.size() > 1) reporter.error("Only one command can be specified"); for (Map.Entry<String, Attrs> e : command.entrySet()) { data.name = e.getKey(); Attrs attrs = e.getValue(); if (attrs.containsKey("jvmargs")) data.jvmArgs = attrs.get("jvmargs"); if (attrs.containsKey("title")) data.title = attrs.get("title"); if (data.title != null) data.title = data.name; } return data; } finally { jar.close(); } }