/** * Find all .tld files in the given jar. * * @param uri the uri to jar file * @return the collection of tlds as url references * @throws IOException if unable to scan the jar file */ public Collection<URL> getTlds (URI uri) throws IOException { HashSet<URL> tlds = new HashSet<URL>(); String jarUri = uriJarPrefix(uri, "!/"); URL url = new URL(jarUri); JarURLConnection jarConn = (JarURLConnection) url.openConnection(); jarConn.setUseCaches(Resource.getDefaultUseCaches()); JarFile jarFile = jarConn.getJarFile(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry e = entries.nextElement(); String name = e.getName(); if (name.startsWith("META-INF") && name.endsWith(".tld")) { tlds.add(new URL(jarUri + name)); } } if (!Resource.getDefaultUseCaches()) jarFile.close(); return tlds; }
throws IOException { URLConnection con = rootDirURL.openConnection(); JarFile jarFile; String jarFileUrl; jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); JarEntry jarEntry = jarCon.getJarEntry(); rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); closeJarFile = !jarCon.getUseCaches(); String urlFile = rootDirURL.getFile(); try { int separatorIndex = urlFile.indexOf(ResourceUtils.WAR_URL_SEPARATOR); jarFile = new JarFile(urlFile); jarFileUrl = urlFile; rootEntryPath = ""; for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) { JarEntry entry = entries.nextElement(); String entryPath = entry.getName(); if (entryPath.startsWith(rootEntryPath)) { String relativePath = entryPath.substring(rootEntryPath.length()); jarFile.close();
public static void copyJarResourcesRecursively(File destination, JarURLConnection jarConnection) { JarFile jarFile; try { jarFile = jarConnection.getJarFile(); } catch (Exception e) { _.die("Failed to get jar file)"); Enumeration<JarEntry> em = jarFile.entries(); while (em.hasMoreElements()) { JarEntry entry = em.nextElement(); if (entry.getName().startsWith(jarConnection.getEntryName())) { String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName()); if (!fileName.equals("/")) { // exclude the directory InputStream entryInputStream = null; try { entryInputStream = jarFile.getInputStream(entry); FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName)); } catch (Exception e) { if (entryInputStream != null) { try { entryInputStream.close(); } catch (Exception e) {
/** * Gets the {@code JarEntry} object of the entry referenced by this {@code * JarURLConnection}. * * @return the referenced {@code JarEntry} object or {@code null} if no * entry name is specified. * @throws IOException * if an error occurs while getting the file or file-entry. */ public JarEntry getJarEntry() throws IOException { if (!connected) { connect(); } if (entryName == null) { return null; } // The entry must exist since the connect succeeded return getJarFile().getJarEntry(entryName); }
private static List<JavaFileObject> browseJar(URL packageFolderURL) { List<JavaFileObject> result = new ArrayList<>(); try { String jarUri = packageFolderURL.toExternalForm().split("!")[0]; JarURLConnection jarConn = (JarURLConnection) packageFolderURL.openConnection(); String rootEntryName = jarConn.getEntryName(); int rootEnd = rootEntryName.length() + 1; Enumeration<JarEntry> entryEnum = jarConn.getJarFile().entries(); while (entryEnum.hasMoreElements()) { JarEntry jarEntry = entryEnum.nextElement(); String name = jarEntry.getName(); if (name.startsWith(rootEntryName) && name.indexOf('/', rootEnd) == -1 && name.endsWith(CLASS_FILE)) { String binaryName = name.replaceAll("/", ".").replaceAll(CLASS_FILE + "$", ""); result.add(new CustomJavaFileObject(URI.create(jarUri + "!/" + name), JavaFileObject.Kind.CLASS, binaryName)); } } } catch (Exception e) { throw new RuntimeException(packageFolderURL + " is not a JAR file", e); } return result; } }
URLConnection uc = url.openConnection(); final URL jarURL = connection.getJarFileURL(); if (jarURL.getProtocol().equals("file")) { uc = null; String file = jarURL.getFile(); return new File(file).lastModified(); } else { if (connection.getEntryName() != null) { LOGGER.log(WARNING, "Accessing modification date of {0} file, which is an entry in JAR file. " + "The access protocol is not file:, falling back to the default logic (risk of file descriptor leak).", return uc.getLastModified();
URLConnection urlConnection = resourceURL != null ? resourceURL.openConnection() : null; if (urlConnection instanceof JarURLConnection) { JarFile jarFile = ((JarURLConnection)urlConnection).getJarFile(); JarEntry jarEntry = ((JarURLConnection)urlConnection).getJarEntry(); String jarFileName = jarFile.getName(); String jarEntryName = jarEntry.getName(); if (!jarEntryName.endsWith("/")) { jarEntryName += "/"; if (jarEntry.isDirectory() || jarFile.getJarEntry(jarEntryName) != null) { InputStream is = urlConnection != null ? urlConnection.getInputStream() : null; OutputStream os = null; if (is == null) { byte[] buffer = new byte[64 * 1024]; int length; while ((length = is.read(buffer)) != -1) { os.write(buffer, 0, length); is.close(); if (os != null) { os.close();
URLConnection urlConn = url.openConnection(); JarURLConnection jarUrlConn; if (!(urlConn instanceof JarURLConnection)) { jarUrlConn.setUseCaches(false); jarFile = jarUrlConn.getJarFile(); if (is != null) { try { is.close(); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); if (jarFile != null) { try { jarFile.close(); } catch (Throwable t) { ExceptionUtils.handleThrowable(t);
File directory = new File(url.getFile()); JarURLConnection conn = (JarURLConnection) url.openConnection(); JarFile jarFile = conn.getJarFile(); Enumeration<JarEntry> e = jarFile.entries(); while (e.hasMoreElements()) { JarEntry entry = e.nextElement(); String entryname = entry.getName(); if (!entry.isDirectory() && entryname.endsWith(".class")) { String classname = entryname.substring(0, entryname.length() - 6); if (classname.startsWith("/")) {
public interface Factory { SPARQLSyntaxTest createSPARQLSyntaxTest(String testURI, String testName, String testAction, boolean positiveTest); }
final URLConnection urlCon = jarBaseEntryUrl.openConnection(); if (!test_emulateNoJarURLConnections && urlCon instanceof JarURLConnection) { final JarURLConnection jarCon = (JarURLConnection) urlCon; jarFile = jarCon.getJarFile(); baseEntryPath = normalizeJarEntryPath(jarCon.getEntryName(), true); if (baseEntryPath == null) { throw newFailedToExtractEntryPathException(jarBaseEntryUrl); final String jarBaseEntryUrlEF = jarBaseEntryUrl.toExternalForm(); final int jarEntrySepIdx = jarBaseEntryUrlEF.indexOf(JAR_URL_ENTRY_PATH_START); if (jarEntrySepIdx == -1) { jarBaseEntryUrlEF.substring(jarEntrySepIdx + JAR_URL_ENTRY_PATH_START.length()), true); File rawJarContentAsFile = urlToFileOrNull(new URL(rawJarContentUrlEF)); jarFile = rawJarContentAsFile != null ? new JarFile(rawJarContentAsFile) : null; + jarBaseEntryUrl); for (Enumeration/*<JarEntry>*/ entries = jarFile.entries(); entries.hasMoreElements(); ) { final JarEntry curEntry = (JarEntry) entries.nextElement(); final String curEntryPath = normalizeJarEntryPath(curEntry.getName(), false); if (curEntryPath.startsWith(baseEntryPath) && curEntryPath.endsWith(".tld")) { final String curEntryBaseRelativePath = curEntryPath.substring(baseEntryPath.length()); in.close();
JarURLConnection remoteConnection = (JarURLConnection) solrHomeJar.openConnection(); long lastModified = remoteConnection.getLastModified(); URL remoteURL = new URL(solrHomeJar.toString().substring(4, solrHomeJar.toString().length()-2)); writeStreamToFile(localJar, remoteURL.openStream()); JarURLConnection connection = (JarURLConnection) jarUrl.openConnection(); JarFile jarFile = connection.getJarFile(); Enumeration<JarEntry> jarEntries = jarFile.entries(); while (jarEntries.hasMoreElements()) { JarEntry entry = jarEntries.nextElement(); if (!entry.toString().startsWith("META-INF")) { File fileToCreate = new File(solrWorkingDir, entry.toString()); if (entry.isDirectory()) { fileToCreate.mkdirs(); continue; InputStream inputStream = jarFile.getInputStream(entry); inputStream.close();
JarFile jar = null; try { jarURL = new URL("file:/" + file.getCanonicalPath()); jarURL = new URL("jar:" + jarURL.toExternalForm() + "!/"); JarURLConnection conn = (JarURLConnection) jarURL.openConnection(); jar = conn.getJarFile(); } catch (Exception e) { Enumeration<JarEntry> e = jar.entries(); while (e.hasMoreElements()) { JarEntry entry = e.nextElement(); if (entry.isDirectory()) { if (entry.getName().toUpperCase().equals("META-INF/")) { continue; map.put(new URL(jarURL.toExternalForm() + entry.getName()), packageNameFor(entry)); } catch (MalformedURLException murl) {
final JarURLConnection jarConnection) throws IOException { final JarFile jarFile = jarConnection.getJarFile(); for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { final JarEntry entry = e.nextElement(); if (entry.getName().startsWith(jarConnection.getEntryName())) { final String filename = StringUtils.removeStart(entry.getName(), // jarConnection.getEntryName()); final InputStream entryInputStream = jarFile.getInputStream(entry); if(!FileUtils.copyStream(entryInputStream, f)){ return false; entryInputStream.close(); } else { if (!FileUtils.ensureDirectoryExists(f)) { while ((len = is.read(buf)) > 0) { os.write(buf, 0, len); is.close(); os.close(); return true;
final ClassLoader loader = Thread.currentThread().getContextClassLoader(); final Enumeration<URL> resourceEnum = loader.getResources(packageName); while (resourceEnum.hasMoreElements()) { final URL url = resourceEnum.nextElement(); final boolean isVfs = url.getProtocol() != null && url.getProtocol().startsWith("vfs"); if ("file".equals(url.getProtocol()) || isVfs) { final File dir = toFile(url); if (dir.isDirectory()) { jarFile = toFile(((JarURLConnection) url.openConnection()).getJarFileURL()); } catch (ClassCastException cce) { try { loadJarContent((JarURLConnection) (url.openConnection()), packageName, streams); } else { streams.add(url.openConnection().getInputStream());
System.out.println("Loading JAR from: " + url.toString()); JarURLConnection URLcon = (JarURLConnection)(url.openConnection()); jar = URLcon.getJarFile(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) JarEntry entry = (JarEntry) entries.nextElement(); if (entry.isDirectory() || !entry.getName().toLowerCase().endsWith(".xml")) continue; try inputStream = jar.getInputStream(entry); if (inputStream != null) inputStream.close();
List<Class> classes = new ArrayList<>(); Enumeration<URL> resources = classLoader.getResources(packagePath); if (!resources.hasMoreElements() && LOG.isDebugEnabled()) { LOG.debug("No resources found under package path: {}", packagePath); URLConnection con = url.openConnection(); if (con instanceof JarURLConnection) { JarURLConnection jarCon = (JarURLConnection) con; JarFile jarFile = jarCon.getJarFile(); jarFile.stream() .filter(entry -> { String name = entry.getName(); return name.startsWith(packagePath) && name.endsWith(ClassUtils.CLASS_EXTENSION) && name.indexOf('$') == -1; }) .forEach(entry -> { try (InputStream inputStream = jarFile.getInputStream(entry)) { scanInputStream(annotation, inputStream, classes); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Ignoring JAR entry [" + entry.getName() + "] due to I/O error: " + e.getMessage(), e); LOG.debug("Ignoring JAR entry [" + entry.getName() + "]. Class not found: " + e.getMessage(), e);
@RequestHandler(RequestCmd.OBJECT_CHECK_RESOURCE_FILE) public Pack checkJarFile(Pack param) { MapPack p = (MapPack) param; String resource = p.getText("resource"); MapPack m = new MapPack(); try { URL url = new URL(resource); JarURLConnection connection = (JarURLConnection) url.openConnection(); File file = new File(connection.getJarFileURL().toURI()); if (file.exists() == false) { m.put("error", "Cannot find jar file."); } else { if (file.canRead()) { m.put("name", file.getName()); m.put("size", file.length()); } else { m.put("error", "Cannot read jar file."); } } } catch (Exception e) { m.put("error", e.toString()); } return m; } @RequestHandler(RequestCmd.OBJECT_DOWNLOAD_JAR)
private static URL getParentURL(URL url) throws IOException { URL fileURL = ((JarURLConnection) url.openConnection()).getJarFileURL(); String file = fileURL.getFile(); String parentFile = new File(file).getParent(); parentFile = parentFile.replace(File.separatorChar, '/'); if (parentFile.charAt(0) != '/') { parentFile = "/" + parentFile; } URL parentURL = new URL(fileURL.getProtocol(), fileURL .getHost(), fileURL.getPort(), parentFile); return parentURL; }
URL jarURL = ((JarURLConnection) conn).getJarFileURL(); if (jarURL.getProtocol().equals("file")) { return new File(jarURL.getFile()).lastModified(); } else { jarConn = jarURL.openConnection(); return jarConn.getLastModified(); } catch (IOException e) { return -1; } finally { try { if (jarConn != null) jarConn.getInputStream().close(); } catch (IOException e) { } long lastModified = conn.getLastModified(); if (lastModified == -1L && url.getProtocol().equals("file")) {