List of usage examples for java.util.jar JarEntry isDirectory
public boolean isDirectory()
From source file:org.webical.plugin.classloading.PluginClassLoader.java
/** * Reads in all classes in a jar file for later reference * @param name the full name and path of the jar file * @throws PluginException //from w ww . j a v a 2s.c o m */ public void readJarFile(File jarFile) throws PluginException { //Firstly check input if (jarFile == null) { log.error("Cannot load jar without a reference..."); throw new PluginException("Cannot load jar without a reference..."); } JarInputStream jis; JarEntry je; if (!jarFile.exists()) { log.error("Jar does not exist: " + jarFile.getAbsolutePath()); throw new PluginException("Jar does not exist: " + jarFile.getAbsolutePath()); } if (!jarFile.canRead()) { log.error("Jar is not readable: " + jarFile.getAbsolutePath()); throw new PluginException("Jar is not readable: " + jarFile.getAbsolutePath()); } if (log.isDebugEnabled()) log.debug("Loading jar file " + jarFile); FileInputStream fis = null; try { fis = new FileInputStream(jarFile); jis = new JarInputStream(fis); } catch (IOException ioe) { log.error("Can't open jar file " + jarFile, ioe); if (fis != null) { try { fis.close(); } catch (IOException e) { log.error("Could not close filehandle to: " + jarFile.getAbsolutePath(), e); throw new PluginException("Could not close filehandle to: " + jarFile.getAbsolutePath(), e); } } throw new PluginException("Can't open jar file " + jarFile, ioe); } //Loop over the jarfile entries try { while ((je = jis.getNextJarEntry()) != null) { String jarEntryName = je.getName(); //Skip the META-INF dir if (jarEntryName.startsWith(META_INF)) { continue; } if (jarEntryName.endsWith(FileUtils.CLASS_FILE_EXTENSION)) { //Extract java class loadClassBytes(jis, ClassUtils.fileToFullClassName(jarEntryName)); } else if (je.isDirectory()) { //Extract directory unpackDirectory(jarEntryName, jarFile); } else { //it could be an image or audio file so let's extract it and store it somewhere for later reference try { extractJarResource(jis, jarEntryName, jarFile); } catch (Exception e) { log.error("Cannot cache jar resource: " + jarEntryName + " from jar file: " + jarFile.getAbsolutePath(), e); throw new PluginException("Cannot cache jar resource: " + jarEntryName + " from jar file: " + jarFile.getAbsolutePath(), e); } } jis.closeEntry(); } } catch (IOException ioe) { log.error("Badly formatted jar file: " + jarFile.getAbsolutePath(), ioe); throw new PluginException("Badly formatted jar file: " + jarFile.getAbsolutePath(), ioe); } finally { if (jis != null) { try { jis.close(); } catch (IOException e) { log.error("Could not close connection to jar: " + jarFile.getAbsolutePath(), e); throw new PluginException("Could not close connection to jar: " + jarFile.getAbsolutePath(), e); } } } }
From source file:org.apache.catalina.loader.WebappClassLoader.java
/** * Find specified resource in local repositories. * * @return the loaded resource, or null if the resource isn't found *//*from w w w.j a v a 2 s .c o m*/ protected ResourceEntry findResourceInternal(String name, String path) { if (!started) { log.info(sm.getString("webappClassLoader.stopped")); return null; } if ((name == null) || (path == null)) return null; ResourceEntry entry = (ResourceEntry) resourceEntries.get(name); if (entry != null) return entry; int contentLength = -1; InputStream binaryStream = null; int jarFilesLength = jarFiles.length; int repositoriesLength = repositories.length; int i; Resource resource = null; for (i = 0; (entry == null) && (i < repositoriesLength); i++) { try { String fullPath = repositories[i] + path; Object lookupResult = resources.lookup(fullPath); if (lookupResult instanceof Resource) { resource = (Resource) lookupResult; } // Note : Not getting an exception here means the resource was // found if (securityManager != null) { PrivilegedAction dp = new PrivilegedFindResource(files[i], path); entry = (ResourceEntry) AccessController.doPrivileged(dp); } else { entry = findResourceInternal(files[i], path); } ResourceAttributes attributes = (ResourceAttributes) resources.getAttributes(fullPath); contentLength = (int) attributes.getContentLength(); entry.lastModified = attributes.getLastModified(); if (resource != null) { try { binaryStream = resource.streamContent(); } catch (IOException e) { return null; } // Register the full path for modification checking // Note: Only syncing on a 'constant' object is needed synchronized (allPermission) { int j; long[] result2 = new long[lastModifiedDates.length + 1]; for (j = 0; j < lastModifiedDates.length; j++) { result2[j] = lastModifiedDates[j]; } result2[lastModifiedDates.length] = entry.lastModified; lastModifiedDates = result2; String[] result = new String[paths.length + 1]; for (j = 0; j < paths.length; j++) { result[j] = paths[j]; } result[paths.length] = fullPath; paths = result; } } } catch (NamingException e) { } } if ((entry == null) && (notFoundResources.containsKey(name))) return null; JarEntry jarEntry = null; synchronized (jarFiles) { openJARs(); for (i = 0; (entry == null) && (i < jarFilesLength); i++) { jarEntry = jarFiles[i].getJarEntry(path); if (jarEntry != null) { entry = new ResourceEntry(); try { entry.codeBase = getURL(jarRealFiles[i]); String jarFakeUrl = getURI(jarRealFiles[i]).toString(); jarFakeUrl = "jar:" + jarFakeUrl + "!/" + path; entry.source = new URL(jarFakeUrl); entry.lastModified = jarRealFiles[i].lastModified(); } catch (MalformedURLException e) { return null; } contentLength = (int) jarEntry.getSize(); try { entry.manifest = jarFiles[i].getManifest(); binaryStream = jarFiles[i].getInputStream(jarEntry); } catch (IOException e) { return null; } // Extract resources contained in JAR to the workdir if (!(path.endsWith(".class"))) { byte[] buf = new byte[1024]; File resourceFile = new File(loaderDir, jarEntry.getName()); if (!resourceFile.exists()) { Enumeration entries = jarFiles[i].entries(); while (entries.hasMoreElements()) { JarEntry jarEntry2 = (JarEntry) entries.nextElement(); if (!(jarEntry2.isDirectory()) && (!jarEntry2.getName().endsWith(".class"))) { resourceFile = new File(loaderDir, jarEntry2.getName()); // No need to check mkdirs result because an // IOException will occur anyway resourceFile.getParentFile().mkdirs(); FileOutputStream os = null; InputStream is = null; try { is = jarFiles[i].getInputStream(jarEntry2); os = new FileOutputStream(resourceFile); while (true) { int n = is.read(buf); if (n <= 0) { break; } os.write(buf, 0, n); } } catch (IOException e) { // Ignore } finally { try { if (is != null) { is.close(); } } catch (IOException e) { } try { if (os != null) { os.close(); } } catch (IOException e) { } } } } } } } } if (entry == null) { synchronized (notFoundResources) { notFoundResources.put(name, name); } return null; } if (binaryStream != null) { byte[] binaryContent = new byte[contentLength]; try { int pos = 0; while (true) { int n = binaryStream.read(binaryContent, pos, binaryContent.length - pos); if (n <= 0) break; pos += n; } binaryStream.close(); } catch (IOException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } entry.binaryContent = binaryContent; // The certificates are only available after the JarEntry // associated input stream has been fully read if (jarEntry != null) { entry.certificates = jarEntry.getCertificates(); } } } // Add the entry in the local resource repository synchronized (resourceEntries) { // Ensures that all the threads which may be in a race to load // a particular class all end up with the same ResourceEntry // instance ResourceEntry entry2 = (ResourceEntry) resourceEntries.get(name); if (entry2 == null) { resourceEntries.put(name, entry); } else { entry = entry2; } } return entry; }
From source file:org.allcolor.yahp.converter.CClassLoader.java
/** * analyse the content of the given jar file * /*from www . j a va 2 s.c o m*/ * @param jarFile * the jar to analise */ private final void readDirectories(final URL jarFile) { JarInputStream jarIn = null; try { if (!jarFile.getPath().endsWith(".jar")) { return; } if (CClassLoader.sl(CClassLoader.DEBUG)) { CClassLoader.log("opening jar : " + jarFile.toExternalForm(), CClassLoader.DEBUG); } jarIn = new JarInputStream(jarFile.openStream()); JarEntry jarEntry = null; while ((jarEntry = jarIn.getNextJarEntry()) != null) { if (jarEntry.isDirectory()) { continue; } final URL url = new URL("yahpjarloader://" + CBASE64Codec.encode(jarFile.toExternalForm().getBytes("utf-8")).replaceAll("\n", "") + "/" + jarEntry.getName()); if (CClassLoader.sl(CClassLoader.DEBUG)) { CClassLoader.log("found entry : " + url.toString(), CClassLoader.DEBUG); } if (jarEntry.getName().endsWith(".class")) { if (!this.classesMap.containsKey(jarEntry.getName())) { if (!this.booResourceOnly) { this.classesMap.put(jarEntry.getName(), url); } } if (this.resourcesMap.containsKey(jarEntry.getName())) { final Object to = this.resourcesMap.get(jarEntry.getName()); if (to instanceof URL) { final URL uo = (URL) to; final List l = new ArrayList(); l.add(uo); l.add(url); this.resourcesMap.put(jarEntry.getName(), l); } else if (to instanceof List) { final List uo = (List) to; uo.add(url); this.resourcesMap.put(jarEntry.getName(), uo); } } else { this.resourcesMap.put(jarEntry.getName(), url); } } else if (jarEntry.getName().startsWith("native/")) { String system = jarEntry.getName().substring(7); system = system.substring(0, system.indexOf('/')); if (!this.dllMap.containsKey(system)) { this.dllMap.put(system, url); } if (this.resourcesMap.containsKey(jarEntry.getName())) { final Object to = this.resourcesMap.get(jarEntry.getName()); if (to instanceof URL) { final URL uo = (URL) to; final List l = new ArrayList(); l.add(uo); l.add(url); this.resourcesMap.put(jarEntry.getName(), l); } else if (to instanceof List) { final List uo = (List) to; uo.add(url); this.resourcesMap.put(jarEntry.getName(), uo); } } else { this.resourcesMap.put(jarEntry.getName(), url); } } else { if (this.resourcesMap.containsKey(jarEntry.getName())) { final Object to = this.resourcesMap.get(jarEntry.getName()); if (to instanceof URL) { final URL uo = (URL) to; final List l = new ArrayList(); l.add(uo); l.add(url); this.resourcesMap.put(jarEntry.getName(), l); } else if (to instanceof List) { final List uo = (List) to; uo.add(url); this.resourcesMap.put(jarEntry.getName(), uo); } } else { this.resourcesMap.put(jarEntry.getName(), url); } } } if (CClassLoader.sl(CClassLoader.DEBUG)) { CClassLoader.log("opening jar : " + jarFile.getFile().toString() + " done.", CClassLoader.DEBUG); } } catch (final MalformedURLException mue) { mue.printStackTrace(); if (CClassLoader.sl(CClassLoader.FATAL)) { CClassLoader.log(mue.getMessage(), CClassLoader.FATAL); } } catch (final IOException ioe) { ioe.printStackTrace(); if (CClassLoader.sl(CClassLoader.FATAL)) { CClassLoader.log(ioe.getMessage(), CClassLoader.FATAL); } } catch (final Exception e) { e.printStackTrace(); if (CClassLoader.sl(CClassLoader.FATAL)) { CClassLoader.log(e.getMessage(), CClassLoader.FATAL); } } finally { try { jarIn.close(); jarIn = null; } catch (final Exception e) { } } }