private static List<URL> getUrls(String path) throws MalformedURLException { ImmutableList.Builder<URL> urlList = ImmutableList.builder(); File driverPath = new File(path); if (!driverPath.isDirectory()) { urlList.add(Paths.get(path).toUri().toURL()); return urlList.build(); } File[] files = driverPath.listFiles((dir, name) -> { return name.endsWith(".jar"); }); if (files == null) { return urlList.build(); } for (File file : files) { // Does not handle nested directories if (file.isDirectory()) { continue; } urlList.add(Paths.get(file.getAbsolutePath()).toUri().toURL()); } return urlList.build(); }
@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attributes) { Path relative = Paths.get(base.relativize(dir.toUri()).getPath()); if (excludeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) { return FileVisitResult.SKIP_SUBTREE; } if (isParentIncluded.getOrDefault(dir.getParent(), false) || includeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) { isParentIncluded.put(dir, true); return FileVisitResult.CONTINUE; } return FileVisitResult.CONTINUE; }
private URL toURL(String s) throws MalformedURLException { s = s.trim(); URL url = null; if (!s.startsWith("jar:") && !s.startsWith("http:") && !s.startsWith("https:")) { try { url = Paths.get(s).toUri().toURL(); } catch (Exception e) { logger.error(e.getMessage()); } } if (url == null) { url = new URL(s); } return url; } }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { Path relative = Paths.get(base.relativize(file.toUri()).getPath()); if (excludeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) { return FileVisitResult.CONTINUE; } if ((isParentIncluded.getOrDefault(file.getParent(), false) || includeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) && (file.toFile().getCanonicalPath().endsWith(".swift") && file.toFile().canRead())) { fileNames.add(file.toFile().getCanonicalPath()); } return FileVisitResult.CONTINUE; }
private List<URL> buildClasspath(List<String> jars) throws IOException { List<URL> urls = new ArrayList<>(); for (String jar : jars) { Path jarPath = Paths.get(jar); if (Files.isDirectory(jarPath)) { try (Stream<Path> stream = Files.list(jarPath)) { List<Path> files = stream .filter((path) -> Files.isRegularFile(path)) .collect(Collectors.toList()); for (Path file : files) { URL url = file.toUri().toURL(); urls.add(url); } } } else { URL url = jarPath.toUri().toURL(); urls.add(url); } } return urls; }
static URI propertyUrlToUri(String wildFlyConfig) { if (File.separator.equals("\\") && wildFlyConfig.contains("\\")) { // we are on the windows and path is for windows File f = new File(wildFlyConfig); return f.toPath().toUri(); } else { try { URI uri = new URI(wildFlyConfig); if (!uri.isAbsolute()) { // URI does not include schema if (uri.getPath().charAt(0) != File.separatorChar && uri.getPath().charAt(0) != '/') { // relative path String userDir = System.getProperty("user.dir").replace(File.separatorChar, '/'); return Paths.get(userDir, uri.getPath()).toUri(); } else { // absolute path return Paths.get(uri.getPath()).toUri(); } } return uri; } catch (URISyntaxException e) { // no config file there return null; } } }
public JrtfsCodeBase(ICodeBaseLocator codeBaseLocator, @Nonnull String fileName) { super(codeBaseLocator); this.fileName = fileName; URL url; try { url = Paths.get(fileName).toUri().toURL(); URLClassLoader loader = new URLClassLoader(new URL[] { url }); fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap(), loader); root = fs.getPath("modules"); packageToModuleMap = createPackageToModuleMap(fs); } catch (IOException e) { e.printStackTrace(); } }
protected void fillHadoopConfiguration(Map topologyConf, String configKey, Configuration configuration) { Map<String, Object> config = (Map<String, Object>) topologyConf.get(configKey); LOG.info("TopoConf {}, got config {}, for configKey {}", ConfigUtils.maskPasswords(topologyConf), ConfigUtils.maskPasswords(config), configKey); if (config != null) { List<String> resourcesToLoad = new ArrayList<>(); for (Map.Entry<String, Object> entry : config.entrySet()) { if (entry.getKey().equals(CONFIG_KEY_RESOURCES)) { resourcesToLoad.addAll((List<String>) entry.getValue()); } else { configuration.set(entry.getKey(), String.valueOf(entry.getValue())); } } LOG.info("Resources to load {}", resourcesToLoad); // add configs from resources like hdfs-site.xml for (String pathStr : resourcesToLoad) { configuration.addResource(new Path(Paths.get(pathStr).toUri())); } } LOG.info("Initializing UGI with config {}", configuration); UserGroupInformation.setConfiguration(configuration); }
private static ClassLoader createBootstrapClassLoader() { //Get list of files in bootstrap folder final List<URL> urls = new ArrayList<>(); try { Files.list(Paths.get("lib/bootstrap")).forEach(p -> { try { urls.add(p.toUri().toURL()); } catch (final MalformedURLException mef) { LOGGER.warn("Unable to load " + p.getFileName() + " due to " + mef, mef); } }); } catch (IOException ioe) { LOGGER.warn("Unable to access lib/bootstrap to create bootstrap classloader", ioe); } //Create the bootstrap classloader return new URLClassLoader(urls.toArray(new URL[0]), Thread.currentThread().getContextClassLoader()); }
/** * Returns a zip file system * @param zipFilename to construct the file system from * @param create true if the zip file should be created * @return a zip file system * @throws IOException */ private static FileSystem createZipFileSystem(String zipFilename, boolean create) throws IOException { // convert the filename to a URI final Path path = Paths.get(zipFilename); if(Files.notExists(path.getParent())) { Files.createDirectories(path.getParent()); } final URI uri = URI.create("jar:file:" + path.toUri().getPath()); final Map<String, String> env = new HashMap<>(); if (create) { env.put("create", "true"); } return FileSystems.newFileSystem(uri, env); }