List of usage examples for java.util.jar JarFile getInputStream
public synchronized InputStream getInputStream(ZipEntry ze) throws IOException
From source file:org.commonjava.web.test.fixture.JarKnockouts.java
public void rewriteJar(final File source, final File targetDir) throws IOException { targetDir.mkdirs();/*from w w w .ja va 2 s.c o m*/ final File target = new File(targetDir, source.getName()); JarFile in = null; JarOutputStream out = null; try { in = new JarFile(source); final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target)); out = new JarOutputStream(fos, in.getManifest()); final Enumeration<JarEntry> entries = in.entries(); while (entries.hasMoreElements()) { final JarEntry entry = entries.nextElement(); if (!knockout(entry.getName())) { final InputStream stream = in.getInputStream(entry); out.putNextEntry(entry); copy(stream, out); out.closeEntry(); } } } finally { closeQuietly(out); if (in != null) { try { in.close(); } catch (final IOException e) { } } } }
From source file:org.xchain.tools.monitoring.MonitoringMojo.java
private void mergeWarMonitoringInfo(MonitoringInfo monitoringInfo, File file) throws MojoExecutionException { JarFile artifactJar = null; JarEntry monitoringInfoEntry = null; InputStream in = null;/*from w w w. jav a2s.c o m*/ try { getLog().info("Getting monitoring info from file " + file.toString()); artifactJar = new JarFile(file); monitoringInfoEntry = artifactJar.getJarEntry("WEB-INF/classes/META-INF/monitoring-info.xml"); if (monitoringInfoEntry != null) { in = artifactJar.getInputStream(monitoringInfoEntry); // digest the xml file and get all of the entries. Digester digester = new Digester(); digester.push(monitoringInfo); digester.addRuleSet(new MonitoringInfoRuleSet()); WithDefaultsRulesWrapper wrapper = new WithDefaultsRulesWrapper(digester.getRules()); wrapper.addDefault(new LoggingRule()); digester.setRules(wrapper); digester.parse(in); } else { getLog().info("Monitoring info file not found in " + file.toString()); } } catch (SAXException se) { throw new MojoExecutionException("Could not parse a monitoring-info.xml file.", se); } catch (IOException ioe) { throw new MojoExecutionException("Could not open jar file.", ioe); } finally { if (in != null) { try { in.close(); } catch (IOException ioe) { getLog().warn("Could not close a jar entry input stream.", ioe); } } try { artifactJar.close(); } catch (IOException ioe) { getLog().warn("Could not close a jar.", ioe); } } }
From source file:rubah.runtime.classloader.RubahClassloader.java
@Override public InputStream getResourceAsStream(String name) { JarFile jarFile = null; try {/*from ww w .ja v a2 s . co m*/ VersionManager u = VersionManager.getInstance(); Version v = u.getRunningVersion(); File f = VersionManager.getInstance().getJarFile(v); jarFile = new JarFile(f); JarEntry entry = jarFile.getJarEntry(name); if (entry != null) { try { byte[] ret = IOUtils.toByteArray(jarFile.getInputStream(entry)); return new ByteArrayInputStream(ret); } catch (IOException e) { throw new Error(e); } } } catch (IOException e) { throw new Error(e); } finally { if (jarFile != null) try { jarFile.close(); } catch (IOException e) { // Don't care } } return super.getResourceAsStream(name); }
From source file:com.ebay.cloud.cms.metadata.dataloader.MetadataDataLoader.java
private void loadMetaClassesFromPath(String pathName) { try {/*from w ww . java 2 s . co m*/ URL url = MetadataDataLoader.class.getResource(pathName); URI uri = url.toURI(); BasicDBList metas = new BasicDBList(); if (uri.isOpaque()) { JarURLConnection connection = (JarURLConnection) url.openConnection(); JarFile jar = connection.getJarFile(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.getName().startsWith(pathName.substring(1)) && entry.getName().endsWith(".json")) { InputStream is = jar.getInputStream(entry); readMetaClass(is, metas); } } } else { File dir = new File(url.toURI()); Collection<File> files = FileUtils.listFiles(dir, new String[] { "json" }, true); for (File f : files) { InputStream is = new FileInputStream(f); readMetaClass(is, metas); } } loadMetaClasses(metas); } catch (Exception e) { logger.error("error in loading metadata: ", e); } }
From source file:com.googlecode.onevre.utils.ServerClassLoader.java
/** * * @see java.lang.ClassLoader#findLibrary(java.lang.String) *//* w ww .j a va 2s. com*/ protected String findLibrary(String libname) { try { String name = System.mapLibraryName(libname + "-" + System.getProperty("os.arch")); URL url = getResourceURL(name); log.info("Loading " + name + " from " + url); if (url != null) { File jar = cachedJars.get(url); JarFile jarFile = new JarFile(jar); JarEntry entry = jarFile.getJarEntry(name); File library = new File(localLibDirectory, name); if (!library.exists()) { InputStream input = jarFile.getInputStream(entry); FileOutputStream output = new FileOutputStream(library); byte[] buffer = new byte[BUFFER_SIZE]; int totalBytes = 0; while (totalBytes < entry.getSize()) { int bytesRead = input.read(buffer); if (bytesRead < 0) { throw new IOException("Jar Entry too short!"); } output.write(buffer, 0, bytesRead); totalBytes += bytesRead; } output.close(); input.close(); jarFile.close(); } return library.getAbsolutePath(); } } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:org.gofleet.context.GoClassLoader.java
private void findModulesInPath(ArrayList<File> res, String module_suffix, final java.lang.String path) { try {/*w w w .ja v a 2 s . co m*/ LOG.trace("findModulesInPath(" + path + ")"); final java.io.File object = new java.io.File(path); if (object.isDirectory()) { LOG.trace("Directory found: " + object.getAbsolutePath()); for (java.lang.String entry : object.list()) { final java.io.File thing = new java.io.File( object.getCanonicalPath() + System.getProperty("file.separator") + entry); if (thing.isFile() && thing.getName().endsWith(module_suffix)) res.add(object); else findModulesInPath(res, module_suffix, thing.getCanonicalPath()); } } else if (object.isFile() && object.getName().endsWith(module_suffix)) { LOG.info("Module found: " + object.getAbsolutePath()); res.add(object); } else if (object.isFile() && object.getName().endsWith(".jar")) { LOG.debug("Jar found: " + object.getAbsolutePath()); FileInputStream fis = new FileInputStream(object); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry zipEntry; while ((zipEntry = zis.getNextEntry()) != null) { if (zipEntry.getName().endsWith(module_suffix)) { JarFile jarFile = new JarFile(object); JarEntry entry = jarFile.getJarEntry(zipEntry.getName()); InputStream inputStream = jarFile.getInputStream(entry); File f = File.createTempFile("gofleet-", ".module"); f.deleteOnExit(); OutputStream out = new FileOutputStream(f); byte buf[] = new byte[1024]; int len; while ((len = inputStream.read(buf)) > 0) out.write(buf, 0, len); out.close(); inputStream.close(); LOG.info("Module found: " + zipEntry.getName()); res.add(f); } } zis.close(); fis.close(); } } catch (Throwable t) { LOG.error(t, t); } }
From source file:org.gofleet.context.GoClassLoader.java
private void findFileInPath(ArrayList<String> res, String cadena, final java.lang.String path) { try {//from w w w .jav a2 s. c om LOG.trace("findFileInPath(" + path + ")"); final java.io.File object = new java.io.File(path); if (object.isDirectory()) { LOG.trace("Directory found: " + object.getAbsolutePath()); for (java.lang.String entry : object.list()) { final java.io.File thing = new java.io.File( object.getCanonicalPath() + System.getProperty("file.separator") + entry); if (thing.isFile() && thing.getName().contains(cadena)) res.add(object.getCanonicalPath()); else findFileInPath(res, cadena, thing.getCanonicalPath()); } } else if (object.isFile() && object.getName().contains(cadena)) { LOG.debug("File found: " + object.getAbsolutePath()); res.add(object.getCanonicalPath()); } else if (object.isFile() && object.getName().endsWith(".jar")) { LOG.debug("Jar found: " + object.getAbsolutePath()); FileInputStream fis = new FileInputStream(object); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry zipEntry; while ((zipEntry = zis.getNextEntry()) != null) { if (zipEntry.getName().contains(cadena)) { JarFile jarFile = new JarFile(object); JarEntry entry = jarFile.getJarEntry(zipEntry.getName()); InputStream inputStream = jarFile.getInputStream(entry); System.out.println(object.getAbsolutePath() + "/" + zipEntry.getName()); File f = File.createTempFile(zipEntry.getName(), ""); f.deleteOnExit(); OutputStream out = new FileOutputStream(f); byte buf[] = new byte[1024]; int len; while ((len = inputStream.read(buf)) > 0) out.write(buf, 0, len); out.close(); inputStream.close(); LOG.debug("File found: " + zipEntry.getName()); res.add(f.getCanonicalPath()); } } zis.close(); fis.close(); } } catch (Throwable t) { LOG.error(t, t); } }
From source file:org.b3log.latke.servlet.RequestProcessors.java
/** * Scans classpath (lib directory) to discover request processor classes. *///from w ww . ja v a 2 s. c om private static void discoverFromLibDir() { final String webRoot = AbstractServletListener.getWebRoot(); final File libDir = new File( webRoot + File.separator + "WEB-INF" + File.separator + "lib" + File.separator); @SuppressWarnings("unchecked") final Collection<File> files = FileUtils.listFiles(libDir, new String[] { "jar" }, true); final ClassLoader classLoader = RequestProcessors.class.getClassLoader(); try { for (final File file : files) { if (file.getName().contains("appengine-api") || file.getName().startsWith("freemarker") || file.getName().startsWith("javassist") || file.getName().startsWith("commons") || file.getName().startsWith("mail") || file.getName().startsWith("activation") || file.getName().startsWith("slf4j") || file.getName().startsWith("bonecp") || file.getName().startsWith("jsoup") || file.getName().startsWith("guava") || file.getName().startsWith("markdown") || file.getName().startsWith("mysql") || file.getName().startsWith("c3p0")) { // Just skips some known dependencies hardly.... LOGGER.log(Level.INFO, "Skipped request processing discovery[jarName={0}]", file.getName()); continue; } final JarFile jarFile = new JarFile(file.getPath()); final Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { final JarEntry jarEntry = entries.nextElement(); final String classFileName = jarEntry.getName(); if (classFileName.contains("$") // Skips inner class || !classFileName.endsWith(".class")) { continue; } final DataInputStream classInputStream = new DataInputStream(jarFile.getInputStream(jarEntry)); final ClassFile classFile = new ClassFile(classInputStream); final AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) classFile .getAttribute(AnnotationsAttribute.visibleTag); if (null == annotationsAttribute) { continue; } for (Annotation annotation : annotationsAttribute.getAnnotations()) { if ((annotation.getTypeName()).equals(RequestProcessor.class.getName())) { // Found a request processor class, loads it final String className = classFile.getName(); final Class<?> clz = classLoader.loadClass(className); LOGGER.log(Level.FINER, "Found a request processor[className={0}]", className); final Method[] declaredMethods = clz.getDeclaredMethods(); for (int i = 0; i < declaredMethods.length; i++) { final Method mthd = declaredMethods[i]; final RequestProcessing requestProcessingMethodAnn = mthd .getAnnotation(RequestProcessing.class); if (null == requestProcessingMethodAnn) { continue; } addProcessorMethod(requestProcessingMethodAnn, clz, mthd); } } } } } } catch (final Exception e) { LOGGER.log(Level.SEVERE, "Scans classpath (lib directory) failed", e); } }
From source file:org.dbmaintain.script.repository.impl.ArchiveScriptLocation.java
/** * @param scriptLocation The location of the jar file, not null * @return The properties as a properties map *///from w ww .j a v a 2s . co m @Override protected Properties getCustomProperties(File scriptLocation) { InputStream configurationInputStream = null; try { JarFile jarFile = createJarFile(scriptLocation); ZipEntry configurationEntry = jarFile.getEntry(LOCATION_PROPERTIES_FILENAME); if (configurationEntry == null) { // no custom config found in meta-inf folder, skipping return null; } Properties configuration = new Properties(); configurationInputStream = jarFile.getInputStream(configurationEntry); configuration.load(configurationInputStream); return configuration; } catch (IOException e) { throw new DbMaintainException("Error while reading configuration file " + LOCATION_PROPERTIES_FILENAME + " from jar file " + scriptLocation, e); } finally { closeQuietly(configurationInputStream); } }
From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.server.IntactSolrHomeBuilder.java
public void install(File solrWorkingDir) throws IOException { if (log.isInfoEnabled()) log.info("Installing Intact SOLR Home at: " + solrWorkingDir); // copy resource directory containing solr-home and war file File solrHomeToCreate = new File(solrWorkingDir, "home"); File solrWarToCreate = new File(solrWorkingDir, "solr.war"); // only copy solr-home when solr-home does not exist if (!solrHomeToCreate.exists() && getSolrHomeDir() == null) { solrHomeToCreate.mkdirs();/*from w w w.jav a 2 s . com*/ File solrHomeToCopy = new File(IntactSolrHomeBuilder.class.getResource("/home").getFile()); // is in the resources if (solrHomeToCopy.exists()) { FileUtils.copyDirectory(solrHomeToCopy, solrHomeToCreate); if (!solrWarToCreate.exists() && getSolrWar() == null) { try (InputStream solrWarToCopy = IntactSolrHomeBuilder.class.getResourceAsStream("/solr.war")) { FileUtils.copyInputStreamToFile(solrWarToCopy, solrWarToCreate); } } } // is in the jar in the dependencies else { String originalName = IntactSolrHomeBuilder.class.getResource("/home").getFile(); String jarFileName = originalName.substring(0, originalName.indexOf("!")).replace("file:", ""); JarFile jarFile = new JarFile(jarFileName); Enumeration<JarEntry> jarEntries = jarFile.entries(); // write while (jarEntries.hasMoreElements()) { JarEntry entry = jarEntries.nextElement(); // solr war file if (entry.getName().endsWith("solr.war") && !solrWarToCreate.exists() && getSolrHomeDir() == null) { InputStream inputStream = jarFile.getInputStream(entry); try { FileUtils.copyInputStreamToFile(inputStream, solrWarToCreate); } finally { inputStream.close(); } } else if (entry.toString().startsWith("home")) { File fileToCreate = new File(solrWorkingDir, entry.toString()); if (entry.isDirectory()) { fileToCreate.mkdirs(); continue; } try (InputStream inputStream = jarFile.getInputStream(entry)) { FileUtils.copyInputStreamToFile(inputStream, fileToCreate); } } } } setSolrHomeDir(solrHomeToCreate); setSolrWar(solrWarToCreate); } // only copy solr.war when solr.war does not exist else if (!solrWarToCreate.exists() && getSolrWar() == null) { File solrHomeToCopy = new File(IntactSolrHomeBuilder.class.getResource("/home").getFile()); // is in the resources if (solrHomeToCopy.exists()) { try (InputStream solrWarToCopy = IntactSolrHomeBuilder.class.getResourceAsStream("/solr.war")) { FileUtils.copyInputStreamToFile(solrWarToCopy, new File(solrWorkingDir + "/solr.war")); } } // is in the jar in the dependencies else { String originalName = IntactSolrHomeBuilder.class.getResource("/home").getFile(); String jarFileName = originalName.substring(0, originalName.indexOf("!")).replace("file:", ""); JarFile jarFile = new JarFile(jarFileName); Enumeration<JarEntry> jarEntries = jarFile.entries(); // write while (jarEntries.hasMoreElements()) { JarEntry entry = jarEntries.nextElement(); // solr war file if (entry.getName().endsWith("solr.war")) { File fileToCreate = new File(solrWorkingDir, entry.toString()); try (InputStream inputStream = jarFile.getInputStream(entry)) { FileUtils.copyInputStreamToFile(inputStream, fileToCreate); } } } } setSolrHomeDir(solrHomeToCreate); setSolrWar(solrWarToCreate); } if (log.isDebugEnabled()) { log.debug("\nIntact Solr Home: {}\nSolr WAR: {}", getSolrHomeDir().toString(), getSolrWar().toString()); } }