/** @see java.net.URLStreamHandler#openConnection(URL) */ protected URLConnection openConnection(URL u) throws IOException { try { URL otherURL = new URL(u.getFile()); String host = otherURL.getHost(); String protocol = otherURL.getProtocol(); int port = otherURL.getPort(); String file = otherURL.getFile(); String query = otherURL.getQuery(); // System.out.println("Trying to load: " + u.toExternalForm()); String internalFile = null; if (file.indexOf("!/") != -1) { internalFile = file.substring(file.indexOf("!/") + 2); file = file.substring(0, file.indexOf("!/")); } URL subURL = new URL(protocol, host, port, file); File newFile; if (cacheList.containsKey(subURL.toExternalForm())) { newFile = cacheList.get(subURL.toExternalForm()); } else { InputStream is = (InputStream) subURL.getContent(); String update = subURL.toExternalForm().replace(":", "_"); update = update.replace("/", "-"); File f = File.createTempFile("pack", update); f.deleteOnExit(); FileOutputStream fostream = new FileOutputStream(f); copyStream(new BufferedInputStream(is), fostream); // Unpack newFile = File.createTempFile("unpack", update); newFile.deleteOnExit(); fostream = new FileOutputStream(newFile); BufferedOutputStream bos = new BufferedOutputStream(fostream, 50 * 1024); JarOutputStream jostream = new JarOutputStream(bos); Unpacker unpacker = Pack200.newUnpacker(); long start = System.currentTimeMillis(); unpacker.unpack(f, jostream); printMessage( "Unpack of " + update + " took " + (System.currentTimeMillis() - start) + "ms"); jostream.close(); fostream.close(); cacheList.put(subURL.toExternalForm(), newFile); } if (internalFile != null) { URL directoryURL = new URL("jar:" + newFile.toURL().toExternalForm() + "!/" + internalFile); printMessage("Using DirectoryURL:" + directoryURL); return directoryURL.openConnection(); } else return newFile.toURL().openConnection(); } catch (Exception ex) { ex.printStackTrace(); } printMessage("Returning null for: " + u.toExternalForm()); return null; }
static { Attributes m = null; final URL loc = Prop.LOCATION; if (loc != null) { final String jar = loc.getFile(); try { final ClassLoader cl = JarManifest.class.getClassLoader(); final Enumeration<URL> list = cl.getResources("META-INF/MANIFEST.MF"); while (list.hasMoreElements()) { final URL url = list.nextElement(); if (!url.getFile().contains(jar)) continue; final InputStream in = url.openStream(); try { m = new Manifest(in).getMainAttributes(); break; } finally { in.close(); } } } catch (final IOException ex) { Util.errln(ex); } } MAP = m; }
private List<String> searchDependencyEngineConfigFiles( String prefixPackageName, Class<?> contentLoaderClass) { List<String> fileNames = new ArrayList<String>(); Enumeration<?> enumFiles = findConfigFileUrls(prefixPackageName, contentLoaderClass); while (enumFiles != null && enumFiles.hasMoreElements()) { URL url = (URL) enumFiles.nextElement(); if (url == null || url.getFile() == null || url.getFile().length() <= 1) continue; String fileName = getFileName(url.getPath()); if (url.getProtocol() != null && url.getProtocol().equalsIgnoreCase("jar")) { String elementInJar = getJarFileEntryName(url.getPath()); JarFile jarFile = openJar(url); List<JarEntry> entries = Collections.list(jarFile.entries()); for (JarEntry entry : entries) { String entryName = entry.getName(); if (entryName.startsWith(elementInJar)) { if (!entry.isDirectory() && entryName.matches(dependencyEngineConfigFileRegex)) { fileNames.add(fileName + "!" + "/" + entry.toString()); } } } } else { List<String> list = searchDependencyEngineConfigFiles(new File(fileName)); if (list != null && list.size() > 0) { fileNames.addAll(list); } } } return fileNames; }
private Map<String, Properties> getBundleDefaultProperties(long bundleId) throws IOException { Bundle bundle = bundleContext.getBundle(bundleId); // Find all property files in main bundle directory Enumeration<URL> enumeration = bundle.findEntries("", "*.properties", false); Map<String, Properties> allDefaultProperties = new LinkedHashMap<>(); if (enumeration != null) { while (enumeration.hasMoreElements()) { InputStream is = null; URL url = enumeration.nextElement(); try { is = url.openStream(); Properties defaultBundleProperties = new Properties(); defaultBundleProperties.load(is); if (!url.getFile().isEmpty()) { // We want to store plain filename, without unnecessary slash prefix allDefaultProperties.put(url.getFile().substring(1), defaultBundleProperties); } } catch (IOException e) { LOGGER.error("Error while reading or retrieving default properties", e); } finally { IOUtils.closeQuietly(is); } } } return allDefaultProperties; }
/** * Opens a stream from a public and system ID. * * @param publicID the public ID, which may be null * @param systemID the system ID, which is never null * @throws java.net.MalformedURLException if the system ID does not contain a valid URL * @throws java.io.FileNotFoundException if the system ID refers to a local file which does not * exist * @throws java.io.IOException if an error occurred opening the stream */ public Reader openStream(final String publicID, final String systemID) throws MalformedURLException, FileNotFoundException, IOException { URL url = new URL(currentReader.systemId, systemID); if (url.getRef() != null) { final String ref = url.getRef(); if (url.getFile().length() > 0) { url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile()); url = new URL("jar:" + url + '!' + ref); } else { url = StdXMLReader.class.getResource(ref); } } currentReader.publicId = publicID; currentReader.systemId = url; final StringBuilder charsRead = new StringBuilder(); final Reader reader = this.stream2reader(url.openStream(), charsRead); if (charsRead.length() == 0) { return reader; } final String charsReadStr = charsRead.toString(); final PushbackReader pbreader = new PushbackReader(reader, charsReadStr.length()); for (int i = charsReadStr.length() - 1; i >= 0; i--) { pbreader.unread(charsReadStr.charAt(i)); } return pbreader; }
protected Set<Class> processJarUrl(URL url, String basepath, Class clazz) throws IOException { Set<Class> set = new HashSet<Class>(); String path = url.getFile().substring(5, url.getFile().indexOf("!")); JarFile jar = new JarFile(path); for (Enumeration entries = jar.entries(); entries.hasMoreElements(); ) { JarEntry entry = (JarEntry) entries.nextElement(); if (entry.getName().startsWith(basepath) && entry.getName().endsWith(".class")) { try { String name = entry.getName(); // Ignore anonymous // TODO RM what about the other anonymous classes like $2, $3 ? if (name.contains("$1")) { continue; } URL classURL = classLoader.getResource(name); ClassReader reader = new ClassReader(classURL.openStream()); ClassScanner visitor = getScanner(clazz); reader.accept(visitor, 0); if (visitor.isMatch()) { Class c = loadClass(visitor.getClassName()); if (c != null) { set.add(c); } } } catch (Exception e) { if (logger.isDebugEnabled()) { Throwable t = ExceptionHelper.getRootException(e); logger.debug(String.format("%s: caused by: %s", e.toString(), t.toString())); } } } } return set; }
public static Properties loadProperties(String fileName) { diag_println(DIAG_OFF, ".props : ", fileName); Properties props = new Properties(); if (notNullOrEmpty(fileName)) { InputStream in = null; try { JarFile jarFile = null; if (isJarUri(fileName)) { URL url = new URL(fileName); url = new URL(url.getFile()); String[] parts = url.getFile().split("!/"); jarFile = new JarFile(new File(parts[0])); JarEntry jarEntry = jarFile.getJarEntry(parts[1]); in = jarFile.getInputStream(jarEntry); } else { File file = new File(fileName); if (file.isFile()) { in = new FileInputStream(file); } else { in = PluginUtils.class.getResourceAsStream(fileName); } } props.load(in); if (jarFile != null) jarFile.close(); } catch (Exception e) { Log.log(Log.ERROR, PluginUtils.class + ".loadProperties", e); } finally { IOUtilities.closeQuietly((Closeable) in); } } return props; }
/** * Comparison that does not consider Ref. * * @param url1 the url1 * @param url2 the url2 * @return true, if successful */ public static boolean sameNoRefURL(URL url1, URL url2) { return Objects.equals(url1.getHost(), url2.getHost()) && Objects.equals(url1.getProtocol(), url2.getProtocol()) && (url1.getPort() == url2.getPort()) && Objects.equals(url1.getFile(), url2.getFile()) && Objects.equals(url1.getUserInfo(), url2.getUserInfo()); }
/** * Gets a resource as InputStream. * * @param fileName The path and name of the file relative from the working directory. * @return The resource as InputStream. * @throws FileNotFoundException Thrown if the resource cannot be located. */ public static File getResourceAsFile(final String fileName, ClassLoader classLoader) throws FileNotFoundException { URL url = classLoader.getResource(resolveFileName(fileName)); // Maybe it's in a WAR file if (url == null) { url = classLoader.getResource(resolveFileName("WEB-INF/classes/" + fileName)); } File file = null; if (url != null) { logger.debug("Resource found: " + url.getFile()); try { // Decode necessary for windows paths file = new File(URLDecoder.decode(url.getFile(), "cp1253")); } catch (UnsupportedEncodingException e) { logger.warn(e); } } if (file == null) { logger.debug("Resource not found, getting file."); file = new File(fileName); if (!file.exists()) { throw new FileNotFoundException("File '" + fileName + "' not found."); } } return file; }
private URL targetURL(URL base, String name) throws MalformedURLException { StringBuffer sb = new StringBuffer(base.getFile().length() + name.length()); sb.append(base.getFile()); sb.append(name); String file = sb.toString(); return new URL(base.getProtocol(), base.getHost(), base.getPort(), file, null); }
public static File webget(String urlString, String filename) throws MalformedURLException, IOException, FileNotFoundException { InputStream in = null; FileOutputStream out = null; URL url = new URL(urlString); File destFile = null; if (filename == null) { String inputStr = url.getFile(); destFile = new File(inputStr.replaceAll(".*[\\/](.*)$", "$1")); } else if (new File(filename).isDirectory()) { String inputStr = url.getFile(); destFile = new File(filename + "/" + inputStr.replaceAll(".*[\\/](.*)$", "$1")); } else { destFile = new File(filename); } URLConnection conn = url.openConnection(); long len = conn.getContentLength(); String contentLen; if (len < 0) { contentLen = "Unknown Length"; } else { contentLen = len + " byte" + ((len > 1) ? "s" : ""); } System.out.println("Downloading " + destFile.getName() + " " + contentLen); in = url.openStream(); out = new FileOutputStream(destFile); copyStream(in, out, len); System.out.println("Download completed."); return destFile; }
public static File toFile(final URL url) { if ("jar".equals(url.getProtocol())) { try { final String spec = url.getFile(); int separator = spec.indexOf('!'); /* * REMIND: we don't handle nested JAR URLs */ if (separator == -1) throw new MalformedURLException("no ! found in jar url spec:" + spec); return toFile(new URL(spec.substring(0, separator++))); } catch (MalformedURLException e) { throw new IllegalStateException(e); } } else if ("file".equals(url.getProtocol())) { String path = decode(url.getFile()); if (path.endsWith("!")) { path = path.substring(0, path.length() - 1); } return new File(path); } else { throw new IllegalArgumentException("Unsupported URL scheme: " + url.toExternalForm()); } }
private String getFileWithNormalizedPath(URL url) throws MalformedURLException { String file; if (hasNormalizablePathPattern.matcher(url.getPath()).find()) { // only normalize the path if there is something to normalize // to avoid needless work try { file = url.toURI().normalize().toURL().getFile(); // URI.normalize() does not normalize leading dot segments, // see also http://tools.ietf.org/html/rfc3986#section-5.2.4 int start = 0; while (file.startsWith("/../", start)) { start += 3; } if (start > 0) { file = file.substring(start); } } catch (URISyntaxException e) { file = url.getFile(); } } else { file = url.getFile(); } // if path is empty return a single slash if (file.isEmpty()) { file = "/"; } return file; }
private static File extract(URL url) throws IOException { File localFile; if ("file".equals(url.getProtocol())) localFile = new File(URLDecoder.decode(url.getFile(), "UTF-8")); else { File f = new File(System.getProperty("user.home")); f = new File(f, ".jnaerator"); f = new File(f, "extractedLibraries"); if (!f.exists()) f.mkdirs(); localFile = new File(f, new File(url.getFile()).getName()); URLConnection c = url.openConnection(); if (localFile.exists() && localFile.lastModified() > c.getLastModified()) { c.getInputStream().close(); } else { System.out.println("Extracting " + url); InputStream in = c.getInputStream(); OutputStream out = new FileOutputStream(localFile); int len; byte[] b = new byte[1024]; while ((len = in.read(b)) > 0) out.write(b, 0, len); out.close(); in.close(); } } return localFile; }
private static Instance[] initializeInstances(String dataFile, String labelFile) { // DataSetReader dsr = new CSVDataSetReader(new File("").getAbsolutePath() + "/src/opt/test/" + // dataFile); // DataSetReader lsr = new CSVDataSetReader(new File("").getAbsolutePath() + "/src/opt/test/" + // labelFile); URL d_path = IndepenentComponentAnalysisMyDataTest.class.getResource(dataFile); File df = new File(d_path.getFile()); URL l_path = IndepenentComponentAnalysisMyDataTest.class.getResource(dataFile); File lf = new File(l_path.getFile()); DataSetReader dsr = new CSVDataSetReader(df.toString()); DataSetReader lsr = new CSVDataSetReader(lf.toString()); DataSet ds; DataSet labs; try { ds = dsr.read(); labs = lsr.read(); Instance[] instances = ds.getInstances(); Instance[] labels = labs.getInstances(); // for(int i = 0; i < instances.length; i++) { // instances[i].setLabel(new Instance(labels[i].getData().get(0))); // //instances[i].setLabel(new Instance(labels[i].getData())); // } return instances; } catch (Exception e) { System.out.println("Failed to read input file"); return null; } }
@Override public Path resolve(Path path) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl != null) { String spath = path.toString(); String substituted = FILE_SEP == '/' ? spath : spath.replace(FILE_SEP, '/'); URL url = cl.getResource(substituted); if (url != null) { if (FILE_SEP == '/') { // *nix - a bit quicker than pissing around with URIs String sfile = url.getFile(); if (sfile != null) { return Paths.get(url.getFile()); } } else { // E.g. windows // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4701321 try { URI uri = url.toURI(); if (uri.isOpaque()) { return Paths.get(url.getPath()); } else { return Paths.get(uri); } } catch (Exception exc) { throw new VertxException(exc); } } } } return path; }
/** * Returns the groovy-all-*.jar that is used in the Eclipse project. We know there should only be * one specified in the header for org.codehaus.groovy right now. * * @return Returns the names of the jars that are exported by the org.codehaus.groovy project. * @throws BundleException */ public static URL getExportedGroovyAllJar() { try { Bundle groovyBundle = CompilerUtils.getActiveGroovyBundle(); if (groovyBundle == null) { throw new RuntimeException("Could not find groovy bundle"); } Enumeration<URL> enu = groovyBundle.findEntries("lib", "groovy-all-*.jar", false); if (enu == null) { // in some versions of the plugin, the groovy-all jar is in the base directory of the // plugins enu = groovyBundle.findEntries("", "groovy-all-*.jar", false); } while (enu.hasMoreElements()) { URL jar = enu.nextElement(); if (jar.getFile().indexOf("-sources") == -1 && jar.getFile().indexOf("-javadoc") == -1 && jar.getFile().indexOf("-eclipse") == -1) { // remove the "reference:/" protocol jar = resolve(jar); return jar; } } } catch (IOException e) { throw new RuntimeException(e); } throw new RuntimeException("Could not find groovy all jar"); }
private String getCsvFilePath() { ClassLoader classLoader = getClass().getClassLoader(); URL csvUrl = classLoader.getResource("category_mapping.csv"); if (csvUrl == null || csvUrl.getFile() == null) { throw new RuntimeException("Bad path to category_mapping.csv"); } return csvUrl.getFile(); }
public static String findNameOfFileInURL(URL u) { if (u.getQuery() != null) { Matcher m = p.matcher(u.getQuery()); if (m.find()) { return m.group(2); } } return u.getFile().substring(u.getFile().lastIndexOf('/') + 1); }
@Test public void testAdvanced() throws IOException { final URL url = getClass().getResource("coffeeScript/advanced"); final File testFolder = new File(url.getFile(), "test"); final File expectedFolder = new File(url.getFile(), "expected"); WroTestUtils.compareFromDifferentFoldersByExtension( testFolder, expectedFolder, "js", processor); }
@Test public void shouldTransformFilesFromFolder() throws IOException { final URL url = getClass().getResource("dustjs"); final File testFolder = new File(url.getFile(), "test"); final File expectedFolder = new File(url.getFile(), "expected"); WroTestUtils.compareFromDifferentFoldersByExtension( testFolder, expectedFolder, "js", processor); }
/** * Downloads the given file specified via url to the given canonicalDestination. * * @param urlSource String * @param urlDestination String * @throws Exception */ @Override public void downloadFile(String urlSource, String urlDestination) throws Exception { // sanity check if (urlSource == null || urlSource.length() == 0 || urlDestination == null || urlDestination.length() == 0) { throw new IllegalArgumentException( "downloadFile(): urlSource or urlDestination argument is null..."); } // URLs for given parameters URL source = new URL(urlSource); URL destination = new URL(urlDestination); // we have a compressed file if (GzipUtils.isCompressedFilename(urlSource)) { // downlod to temp destination File tempDestinationFile = org.apache.commons.io.FileUtils.getFile( org.apache.commons.io.FileUtils.getTempDirectory(), new File(source.getFile()).getName()); if (LOG.isInfoEnabled()) { LOG.info("downloadFile(), " + urlSource + ", this may take a while..."); } org.apache.commons.io.FileUtils.copyURLToFile(source, tempDestinationFile); if (LOG.isInfoEnabled()) { LOG.info("downloadFile(), gunzip: we have compressed file, decompressing..."); } // decompress the file gunzip(tempDestinationFile.getCanonicalPath()); if (LOG.isInfoEnabled()) { LOG.info("downloadFile(), gunzip complete..."); } // move temp/decompressed file to final destination File destinationFile = new File(destination.getFile()); if (destinationFile.exists()) { org.apache.commons.io.FileUtils.forceDelete(destinationFile); } org.apache.commons.io.FileUtils.moveFile( org.apache.commons.io.FileUtils.getFile( GzipUtils.getUncompressedFilename(tempDestinationFile.getCanonicalPath())), destinationFile); // lets cleanup after ourselves - remove compressed file tempDestinationFile.delete(); } // uncompressed file, download directry to urlDestination else { if (LOG.isInfoEnabled()) { LOG.info("downloadFile(), " + urlSource + ", this may take a while..."); } org.apache.commons.io.FileUtils.copyURLToFile( source, org.apache.commons.io.FileUtils.getFile(destination.getFile())); } }
@Test public void testFromFolder() throws IOException { final ResourcePostProcessor processor = new MultiLineCommentStripperProcessor(); final URL url = getClass().getResource("multiline"); final File testFolder = new File(url.getFile(), "test"); final File expectedFolder = new File(url.getFile(), "expected"); WroTestUtils.compareFromDifferentFoldersByExtension(testFolder, expectedFolder, "*", processor); }
@Test public void testFromFolder() throws Exception { final ResourcePostProcessor processor = new RhinoLessCssProcessor(); final URL url = getClass().getResource("lesscss"); final File testFolder = new File(url.getFile(), "test"); final File expectedFolder = new File(url.getFile(), "expected"); WroTestUtils.compareFromDifferentFoldersByExtension( testFolder, expectedFolder, "css", processor); }
/** Ignored because it fails when running the test from command line. */ @Test public void testFromFolder() throws Exception { final ResourcePreProcessor processor = new WroManagerProcessor(); final URL url = getClass().getResource("wroManager"); final File testFolder = new File(url.getFile(), "test"); final File expectedFolder = new File(url.getFile(), "expected"); WroTestUtils.compareFromDifferentFoldersByExtension( testFolder, expectedFolder, "js", processor); }
/* this code is workaround for subtle bug/feature in JDK1.3.1 and 1.4, related to loading applets behind proxy */ protected PermissionCollection getPermissions(CodeSource codesource) { PermissionCollection sysPerms = null; Policy policy = (Policy) AccessController.doPrivileged( new PrivilegedAction() { public Object run() { return Policy.getPolicy(); } }); if (policy != null) sysPerms = policy.getPermissions(new CodeSource(null, null)); else sysPerms = new Permissions(); final PermissionCollection perms = sysPerms; if (base != null && base.getHost() != null) perms.add(new SocketPermission(base.getHost() + ":1-", "accept,connect,resolve")); URL url = codesource.getLocation(); if (url.getProtocol().equals("file")) { String path = url.getFile().replace('/', File.separatorChar); if (!path.endsWith(File.separator)) { int endIndex = path.lastIndexOf(File.separatorChar); if (endIndex != -1) { path = path.substring(0, endIndex + 1) + "-"; perms.add(new FilePermission(path, "read")); } } perms.add(new SocketPermission("localhost", "connect,accept")); AccessController.doPrivileged( new PrivilegedAction() { public Object run() { try { String host = InetAddress.getLocalHost().getHostName(); perms.add(new SocketPermission(host, "connect,accept")); } catch (UnknownHostException uhe) { } return null; } }); if (base.getProtocol().equals("file")) { String bpath = base.getFile().replace('/', File.separatorChar); if (bpath.endsWith(File.separator)) { bpath += "-"; } perms.add(new FilePermission(bpath, "read")); } } // for (Enumeration e=perms.elements();e.hasMoreElements();) // System.err.println("p="+e.nextElement()); return perms; }
/** * Searches the class path and local paths to find the config file. * * @param path path to find. if it starts with / then it's absolute path. * @return File or null if not found at all. */ public static File findConfigFile(String path) { ClassLoader cl = PropertiesUtil.class.getClassLoader(); URL url = cl.getResource(path); if (url != null) { return new File(url.getFile()); } url = ClassLoader.getSystemResource(path); if (url != null) { return new File(url.getFile()); } File file = new File(path); if (file.exists()) { return file; } String newPath = "conf" + (path.startsWith(File.separator) ? "" : "/") + path; url = ClassLoader.getSystemResource(newPath); if (url != null) { return new File(url.getFile()); } url = cl.getResource(newPath); if (url != null) { return new File(url.getFile()); } newPath = "conf" + (path.startsWith(File.separator) ? "" : File.separator) + path; file = new File(newPath); if (file.exists()) { return file; } newPath = System.getenv("CATALINA_HOME"); if (newPath == null) { newPath = System.getenv("CATALINA_BASE"); } if (newPath == null) { newPath = System.getProperty("catalina.home"); } if (newPath == null) { return null; } file = new File(newPath + File.separator + "conf" + File.separator + path); if (file.exists()) { return file; } return null; }
@Before public void setUp() { try { URL res = this.getClass().getClassLoader().getResource("toolspecs"); // use the file toolspec xml as the input file too (for this test) toolspecsDir = res.getFile(); repo = new LocalToolRepository(res.getFile()); } catch (IOException ex) { fail(ex.getMessage()); } }
public HelloServlet() { super(); URL url = HelloServlet.class.getResource("HelloServlet.class"); if (url != null) { System.out.println("]]]]]]]]]]]]]]]]]] HelloServlet :" + url); System.out.println("]]]]]]]]]]]]]]]]]] HelloServlet :" + url.getFile()); System.out.println("]]]]]]]]]]]]]]]]]] HelloServlet :" + new File(url.getFile()).exists()); System.out.println("]]]]]]]]]]]]]]]]]] HelloServlet :" + url.toExternalForm()); } else { } }
/** * Create a script from an URL. * * @param url representing a script source code. * @param parameters execution arguments. * @throws InvalidScriptException if the creation fails. */ public Script(URL url, Serializable[] parameters) throws InvalidScriptException { this.scriptEngineLookup = FileUtils.getExtension(url.getFile()); try { storeScript(url); } catch (IOException e) { throw new InvalidScriptException("Unable to read script : " + url.getPath(), e); } this.id = url.toExternalForm(); this.parameters = parameters; this.scriptName = url.getFile(); }