List of usage examples for java.util.jar JarFile getName
public String getName()
From source file:org.springframework.boot.devtools.restart.ChangeableUrls.java
private static List<URL> getUrlsFromManifestClassPathAttribute(URL jarUrl, JarFile jarFile) throws IOException { Manifest manifest = jarFile.getManifest(); if (manifest == null) { return Collections.emptyList(); }/*from w ww . j ava2 s . co m*/ String classPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH); if (!StringUtils.hasText(classPath)) { return Collections.emptyList(); } String[] entries = StringUtils.delimitedListToStringArray(classPath, " "); List<URL> urls = new ArrayList<>(entries.length); List<URL> nonExistentEntries = new ArrayList<>(); for (String entry : entries) { try { URL referenced = new URL(jarUrl, entry); if (new File(referenced.getFile()).exists()) { urls.add(referenced); } else { nonExistentEntries.add(referenced); } } catch (MalformedURLException ex) { throw new IllegalStateException("Class-Path attribute contains malformed URL", ex); } } if (!nonExistentEntries.isEmpty()) { System.out.println("The Class-Path manifest attribute in " + jarFile.getName() + " referenced one or more files that do not exist: " + StringUtils.collectionToCommaDelimitedString(nonExistentEntries)); } return urls; }
From source file:net.ymate.platform.core.beans.impl.DefaultBeanLoader.java
private List<Class<?>> __doFindClassByJar(String packageName, JarFile jarFile, IBeanFilter filter) throws Exception { List<Class<?>> _returnValue = new ArrayList<Class<?>>(); if (!__doCheckExculedFile(new File(jarFile.getName()).getName())) { Enumeration<JarEntry> _entriesEnum = jarFile.entries(); for (; _entriesEnum.hasMoreElements();) { JarEntry _entry = _entriesEnum.nextElement(); // ??? '/' '.'?.class???'$'?? String _className = _entry.getName().replaceAll("/", "."); if (_className.endsWith(".class") && _className.indexOf('$') < 0) { if (_className.startsWith(packageName)) { _className = _className.substring(0, _className.lastIndexOf('.')); Class<?> _class = __doLoadClass(_className); __doAddClass(_returnValue, _class, filter); }/* ww w . java2 s. com*/ } } } return _returnValue; }
From source file:org.wso2.carbon.integration.common.utils.FileManager.java
public void copyJarFile(String sourceFileLocationWithFileName, String destinationDirectory) throws URISyntaxException, IOException { File sourceFile = new File(getClass().getResource(sourceFileLocationWithFileName).toURI()); File destinationFileDirectory = new File(destinationDirectory); JarOutputStream jarOutputStream = null; InputStream inputStream = null; try {//from w w w . ja va2 s. com JarFile jarFile = new JarFile(sourceFile); String fileName = jarFile.getName(); String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator)); File destinationFile = new File(destinationFileDirectory, fileNameLastPart); jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile)); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { try { JarEntry jarEntry = entries.nextElement(); inputStream = jarFile.getInputStream(jarEntry); //jarOutputStream.putNextEntry(jarEntry); //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName())); byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, bytesRead); } } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { log.warn("Fail to close jarOutStream"); } } if (jarOutputStream != null) { try { jarOutputStream.flush(); jarOutputStream.closeEntry(); } catch (IOException e) { log.warn("Error while closing jar out stream"); } } if (jarFile != null) { try { jarFile.close(); } catch (IOException e) { log.warn("Error while closing jar file"); } } } } } finally { if (jarOutputStream != null) { try { jarOutputStream.close(); } catch (IOException e) { log.warn("Fail to close jarOutStream"); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { log.warn("Error while closing input stream"); } } } }
From source file:org.ow2.bonita.runtime.AbstractClassLoader.java
private void releaseConnection(URL url) { if (LOG.isLoggable(Level.INFO)) { LOG.info("Releasing class loader: " + this); }//from w ww .j ava 2 s . c om try { final URLConnection conn = url.openConnection(); if (LOG.isLoggable(Level.INFO)) { LOG.info("Getting connection of url: " + url + ", conn=" + conn); } final String fileURLConnectionClassName = "sun.net.www.protocol.file.FileURLConnection"; if (conn instanceof JarURLConnection) { JarFile jarfile = ((JarURLConnection) conn).getJarFile(); if (LOG.isLoggable(Level.INFO)) { LOG.info("Closing jar file: " + jarfile.getName()); } jarfile.close(); } else if (conn.getClass().getName().equals(fileURLConnectionClassName)) { if (LOG.isLoggable(Level.INFO)) { LOG.info("Closing connection (" + fileURLConnectionClassName + ": " + conn); } Method close = conn.getClass().getMethod("close", (Class[]) null); close.invoke(conn, (Object[]) null); } } catch (Exception e) { if (LOG.isLoggable(Level.WARNING)) { LOG.warning("Error while releasing classloader: " + this + ": " + Misc.getStackTraceFrom(e)); } e.printStackTrace(); } close(); }
From source file:UnpackedJarFile.java
public static void copyToPackedJar(JarFile inputJar, File outputFile) throws IOException { if (inputJar.getClass() == JarFile.class) { // this is a plain old jar... nothign special copyFile(new File(inputJar.getName()), outputFile); } else if (inputJar instanceof NestedJarFile && ((NestedJarFile) inputJar).isPacked()) { NestedJarFile nestedJarFile = (NestedJarFile) inputJar; JarFile baseJar = nestedJarFile.getBaseJar(); String basePath = nestedJarFile.getBasePath(); if (baseJar instanceof UnpackedJarFile) { // our target jar is just a file in upacked jar (a plain old directory)... now // we just need to find where it is and copy it to the outptu copyFile(((UnpackedJarFile) baseJar).getFile(basePath), outputFile); } else {/* w w w.ja v a 2 s.c om*/ // out target is just a plain old jar file directly accessabel from the file system copyFile(new File(baseJar.getName()), outputFile); } } else { // copy out the module contents to a standalone jar file (entry by entry) JarOutputStream out = null; try { out = new JarOutputStream(new FileOutputStream(outputFile)); byte[] buffer = new byte[4096]; Enumeration entries = inputJar.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); InputStream in = inputJar.getInputStream(entry); try { out.putNextEntry(new ZipEntry(entry.getName())); try { int count; while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } } finally { out.closeEntry(); } } finally { close(in); } } } finally { close(out); } } }
From source file:org.nuxeo.osgi.JarBundleFile.java
public JarBundleFile(JarFile jarFile) { this.jarFile = jarFile; try {//from w w w.j a v a2 s. co m urlBase = "jar:" + new File(jarFile.getName()).toURI().toURL() + "!/"; } catch (MalformedURLException e) { log.error("Failed to convert bundle location to an URL: " + jarFile.getName() + ". Bundle getEntry will not work.", e); } }
From source file:org.hyperic.hq.plugin.tomcat.TomcatServerDetector.java
private boolean isCorrectVersion(String versionJar) { boolean correctVersion = false; try {// w ww. j av a 2s . co m JarFile jarFile = new JarFile(versionJar); log.debug("[isInstallTypeVersion] versionJar='" + jarFile.getName() + "'"); Attributes attributes = jarFile.getManifest().getMainAttributes(); jarFile.close(); String tomcatVersion = attributes.getValue("Specification-Version"); String expectedVersion = getTypeProperty("tomcatVersion"); if (expectedVersion == null) { expectedVersion = getTypeInfo().getVersion(); } log.debug("[isInstallTypeVersion] tomcatVersion='" + tomcatVersion + "' (" + expectedVersion + ")"); correctVersion = tomcatVersion.equals(expectedVersion); } catch (IOException e) { log.debug("Error getting Tomcat version (" + e + ")", e); } return correctVersion; }
From source file:org.apache.apex.common.util.JarHelper.java
/** * Adds dependent jar-files from manifest to the target list of jar-files * @param jarFile Jar file/*from w ww . j a v a2s . c o m*/ * @param set Set of target jar-files * @throws IOException */ public void getDependentJarsFromManifest(JarFile jarFile, Set<String> set) throws IOException { String value = jarFile.getManifest().getMainAttributes().getValue(APEX_DEPENDENCIES); if (!StringUtils.isEmpty(value)) { Path folderPath = Paths.get(jarFile.getName()).getParent(); for (String jar : value.split(",")) { File file = folderPath.resolve(jar).toFile(); if (file.exists()) { set.add(file.getPath()); logger.debug("The file {} was added as a dependent of the jar {}", file.getPath(), jarFile.getName()); } else { logger.warn("The dependent file {} of the jar {} does not exist", file.getPath(), jarFile.getName()); } } } }
From source file:org.hyperic.hq.plugin.jboss7.JBossDetectorBase.java
final String getVersion(Map<String, String> args) { String version = "not found"; File jbossAsServerJar = null; String mp = args.get("mp"); List<String> modulesPAths = Arrays.asList(mp.split(File.pathSeparator)); log.debug("[getVersion] modulesPAths=" + modulesPAths); for (String path : modulesPAths) { Collection<File> files = listFileTree(new File(path)); for (File file : files) { String name = file.getName(); if (name.startsWith("jboss-as-server") && name.endsWith(".jar")) { jbossAsServerJar = file; }/*from ww w . j av a2s .com*/ } } if (jbossAsServerJar != null) { try { JarFile jarFile = new JarFile(jbossAsServerJar); log.debug("[getVersion] jboss-as-server.jar = '" + jarFile.getName() + "'"); Attributes attributes = jarFile.getManifest().getMainAttributes(); jarFile.close(); version = attributes.getValue("JBossAS-Release-Version"); } catch (IOException e) { log.debug("[getVersion] Error getting JBoss version (" + e + ")", e); } } else { log.debug("[getVersion] 'jboss-as-server.*.jar' not found."); } return version; }
From source file:com.qualogy.qafe.web.css.util.CssProvider.java
private Map<String, InputStream> findResourceInFile(File resourceFile, String fileNamePattern) throws IOException { Map<String, InputStream> result = new TreeMap<String, InputStream>(); if (resourceFile.canRead() && resourceFile.getAbsolutePath().endsWith(".jar")) { JarFile jarFile = new JarFile(resourceFile); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry singleEntry = entries.nextElement(); if (singleEntry.getName().matches(fileNamePattern)) { result.put(jarFile.getName() + "/" + singleEntry.getName(), jarFile.getInputStream(singleEntry)); }/*from w w w . ja va2s .com*/ } } return result; }