/*
找出含有rose标帜的目录或jar包
773743
private List<ResourceRef> findRoseResources() {
        final List<ResourceRef> resources;
        try {
            resources = RoseScanner.getInstance().getJarOrClassesFolderResources();
        } catch (IOException e) {
            throw new ApplicationContextException(
                    "error on getJarResources/getClassesFolderResources", e);
        }
        return resources;
    }
713657
public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            System.out.println("There should be an argument: path to a jar.");
            System.exit(0);
        }
        String jarInput = args[0];
        extract(new File(jarInput), new File("/tmp/junit-ekstazi-agent.jar"),
                new String[] { Names.EKSTAZI_PACKAGE_BIN }, new String[] {});
        for (String name : extractClassNames("/tmp/junit-ekstazi-agent.jar")) {
            System.out.println(name);
        }
    }
510694
public static void extractDirFromJar(String jarpath, String dir, String destdir) {
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(jarpath);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries != null && entries.hasMoreElements()) {
                ZipEntry ze = entries.nextElement();
                if (!ze.isDirectory() && ze.getName().startsWith(dir)) {
                    InputStream in = zipFile.getInputStream(ze);
                    try {
                        File file = new File(destdir, ze.getName());
                        if (!file.getParentFile().mkdirs()) {
                            if (!file.getParentFile().isDirectory()) {
                                throw new IOException("Mkdirs failed to create " +
                                        file.getParentFile().toString());
                            }
                        }
                        OutputStream out = new FileOutputStream(file);
                        try {
                            byte[] buffer = new byte[8192];
                            int i;
                            while ((i = in.read(buffer)) != -1) {
                                out.write(buffer, 0, i);
                            }
                        } finally {
                            out.close();
                        }
                    } finally {
                        if (in != null)
                            in.close();
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn("No " + dir + " from " + jarpath + "!\n" + e.getMessage());
        } finally {
            if (zipFile != null)
                try {
                    zipFile.close();
                } catch (Exception e) {
                    LOG.warn(e.getMessage());
                }

        }
    }
252147
function _jsifyPath(path) {
        // Iterate in parallel over objects and labels.
        var objects = _jsifyCollection(path.objects());
        var labelSets = _jsifyCollection(path.labels()); // TODO: Java.List<Set<String>>
        var zipped = _.zip(objects, labelSets);
        var arr = _.map(zipped, function (pair) {
            var object = pair[0];
            var labels = pair[1];
            labels.sort();
            return { object: jsify(object), labels: labels };
        });
        return arr;
    }
759594
private static URL getPersistenceRootUrl(URL url)
    {
        String f = url.getFile();
        f = parseFilePath(f);

        URL jarUrl = url;
        try
        {
            if (AllowedProtocol.isJarProtocol(url.getProtocol()))
            {
                jarUrl = new URL(f);
                if (jarUrl.getProtocol() != null
                        && AllowedProtocol.FILE.name().equals(jarUrl.getProtocol().toUpperCase())
                        && StringUtils.contains(f, " "))
                {
                    jarUrl = new File(f).toURI().toURL();
                }
            }
            else if (AllowedProtocol.isValidProtocol(url.getProtocol()))
            {
                if (StringUtils.contains(f, " "))
                {
                    jarUrl = new File(f).toURI().toURL();
                }
                else
                {
                    jarUrl = new File(f).toURL();
                }
            }
        }
        catch (MalformedURLException mex)
        {
            log.error("Error during getPersistenceRootUrl(), Caused by: {}.", mex);
            throw new IllegalArgumentException("Invalid jar URL[] provided!" + url);
        }

        return jarUrl;
    }
467240
private static String jarFileName() {
        // do this first so we can access extractor, ok to invoke more than once
        createExtractor();
        // get <name> from path/<name>.jar
        String fullyQualifiedFileName = extractor.container.getName();
        int lastSeparator = fullyQualifiedFileName.lastIndexOf(File.separatorChar);
        String simpleFileName = fullyQualifiedFileName.substring(lastSeparator + 1);
        int dotIdx = simpleFileName.lastIndexOf('.');
        if (dotIdx != -1) {
            return simpleFileName.substring(0, simpleFileName.lastIndexOf('.'));
        }
        return simpleFileName;
    }
1370966
private URL findJarForResource(URL url) {
        // 如果URL的文件部分本身是一个URL，那么该URL可能指向JAR
        try {
            long startTime = System.currentTimeMillis();
            while (true) {
                url = new URL(url.getFile());
                if (System.currentTimeMillis() - startTime > MAX_LIMIT) {
                    break;
                }
            }
        } catch (MalformedURLException expected) {
            // This will happen at some point and serves as a break in the loop
        }

        // Look for the .jar extension and chop off everything after that
        StringBuilder jarUrl = new StringBuilder(url.toExternalForm());
        int index = jarUrl.lastIndexOf(".jar");
        if (index >= 0) {
            jarUrl.setLength(index + 4);
        } else {
            return null;
        }

        // Try to open and test it
        try {
            URL testUrl = new URL(jarUrl.toString());
            if (this.isJar(testUrl)) {
                return testUrl;
            }

            // WebLogic fix: check if the URL's file exists in the filesystem.
            jarUrl.replace(0, jarUrl.length(), testUrl.getFile());
            File file = new File(jarUrl.toString());

            // File name might be URL-encoded
            if (!file.exists()) {
                file = new File(URLEncoder.encode(jarUrl.toString(), "UTF-8"));
            }

            if (file.exists()) {
                testUrl = file.toURI().toURL();
                if (isJar(testUrl)) {
                    return testUrl;
                }
            }
        } catch (MalformedURLException e) {
            log.warn("Invalid JAR URL: " + jarUrl);
        } catch (UnsupportedEncodingException e) {
            throw new ZealotException("Unsupported encoding?  UTF-8?  That's unpossible.");
        }

        return null;
    }
143421
public static URL[] getClassPathJars(final Bundle bundle) {
		final List<URL> urls = new ArrayList<>();
		final String bundleClasspath = (String) bundle.getHeaders().get(
				"Bundle-ClassPath");
		if (bundleClasspath != null) {
			String[] segments = bundleClasspath.split(",");
			for (String segment : segments) {
				final URL url = bundle.getEntry(segment);
				if (url != null) {
					if (url.toExternalForm().endsWith("jar")) {
						LOG.debug("Using url: " + url);
						try {
							URL jarUrl = new URL("jar:" + url.toExternalForm()
									+ "!/");
							urls.add(jarUrl);
						} catch (MalformedURLException ignore) {
							LOG.debug(ignore.getMessage());
						}
					}
				}
			}
		}
		LOG.debug("Bundle-ClassPath URLs: " + urls);
		// adds the depending bundles to the "classloader" space
		urls.addAll(getLocationsOfBundlesInClassSpace(bundle));

		return urls.toArray(new URL[urls.size()]);
	}
718542
List<String> buildClassPath(String appClassPath) throws IOException {
    String sparkHome = getSparkHome();

    Set<String> cp = new LinkedHashSet<>();
    addToClassPath(cp, appClassPath);

    addToClassPath(cp, getConfDir());

    boolean prependClasses = !isEmpty(getenv("SPARK_PREPEND_CLASSES"));
    boolean isTesting = "1".equals(getenv("SPARK_TESTING"));
    if (prependClasses || isTesting) {
      String scala = getScalaVersion();
      List<String> projects = Arrays.asList(
        "common/kvstore",
        "common/network-common",
        "common/network-shuffle",
        "common/network-yarn",
        "common/sketch",
        "common/tags",
        "common/unsafe",
        "core",
        "examples",
        "graphx",
        "launcher",
        "mllib",
        "repl",
        "resource-managers/mesos",
        "resource-managers/yarn",
        "sql/catalyst",
        "sql/core",
        "sql/hive",
        "sql/hive-thriftserver",
        "streaming"
      );
      if (prependClasses) {
        if (!isTesting) {
          System.err.println(
            "NOTE: SPARK_PREPEND_CLASSES is set, placing locally compiled Spark classes ahead of " +
            "assembly.");
        }
        for (String project : projects) {
          addToClassPath(cp, String.format("%s/%s/target/scala-%s/classes", sparkHome, project,
            scala));
        }
      }
      if (isTesting) {
        for (String project : projects) {
          addToClassPath(cp, String.format("%s/%s/target/scala-%s/test-classes", sparkHome,
            project, scala));
        }
      }

      // Add this path to include jars that are shaded in the final deliverable created during
      // the maven build. These jars are copied to this directory during the build.
      addToClassPath(cp, String.format("%s/core/target/jars/*", sparkHome));
      addToClassPath(cp, String.format("%s/mllib/target/jars/*", sparkHome));
    }

    // Add Spark jars to the classpath. For the testing case, we rely on the test code to set and
    // propagate the test classpath appropriately. For normal invocation, look for the jars
    // directory under SPARK_HOME.
    boolean isTestingSql = "1".equals(getenv("SPARK_SQL_TESTING"));
    String jarsDir = findJarsDir(getSparkHome(), getScalaVersion(), !isTesting && !isTestingSql);
    if (jarsDir != null) {
      addToClassPath(cp, join(File.separator, jarsDir, "*"));
    }

    addToClassPath(cp, getenv("HADOOP_CONF_DIR"));
    addToClassPath(cp, getenv("YARN_CONF_DIR"));
    addToClassPath(cp, getenv("SPARK_DIST_CLASSPATH"));
    return new ArrayList<>(cp);
  }
127793
public List<String> selectJars(String targetPath) {
        List<String> jarPaths = new ArrayList<String>();

        if (targetPath == null) {
            return jarPaths;
        }

        File targetDir = new File(targetPath);
        if (!UtilImpl_FileUtils.exists(targetDir)) {
            return jarPaths;
        }

        File[] targetFiles = UtilImpl_FileUtils.listFiles(targetDir);
        if (targetFiles != null) {
            for (File nextTargetFile : targetFiles) {
                String nextTargetName = nextTargetFile.getName();
                if (nextTargetName.toUpperCase().endsWith(".JAR")) {
                    String nextTargetPath = targetPath + "/" + nextTargetName;
                    jarPaths.add(nextTargetPath);
                }
            }
        }

        return jarPaths;
    }
1203766
private static boolean isJarURL(final URL rootDirResource) {

        final String protocol = rootDirResource.getProtocol();

        /**
         * Determine whether the given URL points to a resource in a jar file, that is, has protocol "jar", "zip",
         * "wsjar" or "code-source".
         * <p>
         * "zip" and "wsjar" are used by BEA WebLogic Server and IBM WebSphere, respectively, but can be treated like
         * jar files. The same applies to "code-source" URLs on Oracle OC4J, provided that the path contains a jar
         * separator.
         * *
         */
        return "jar".equals(protocol) || "zip".equals(protocol) || "wsjar".equals(protocol)
                || ("code-source".equals(protocol) && rootDirResource.getPath().contains(JAR_URL_SEPARATOR));

    }
1124271
function (urls) {

            if (this._typeUtils.isString(urls)) {
                urls = [urls];
            }

            var _getPackages = function (urls, urlMap, packages) {
                if (urls.length === 0) {
                    // everything has been found, leave back the recursive loop
                    return;
                }
                for (var key in urlMap) {
                    var value = urlMap[key];
                    if (typeof(value) == 'string') {
                        value = "/" + value;
                        // Check if something match
                        for (var i = 0, ii = urls.length; i < ii; i++) {
                            var file = urls[i];
                            if (value.substr(0, file.length) == file) {
                                packages[value] = true;
                                // remove found item from the url list, for performance reason
                                urls.slice(i, i);
                                break;
                            }
                        }
                    } else {
                        _getPackages(urls, urlMap[key], packages);
                    }
                }
            };

            // Clone urls array
            var urlClone = [];
            for (var i = 0, ii = urls.length; i < ii; i++) {
                urlClone.push(urls[i]);
            }

            var packages = {};
            _getPackages(urls, this._urlMap, packages);

            // return the keys only
            var keys = [];
            for (var key in packages) {
                keys.push(key);
            }
            return keys;
        }
321927
private static Set<Class<?>> findEnumClassesWithFile(final String packageName, final Path dir) {
		Set<Class<?>> classes = new HashSet<>();
		try (Stream<Path> stream = Files.walk(dir)) {
			stream.filter(entry -> entry.getFileName().toString().endsWith(".class")).forEach(file -> {
				StringJoiner joiner = new StringJoiner(".", packageName + ".", "");
				dir.relativize(file).forEach(p -> joiner.add(p.toString()));
				String className = joiner.toString().replaceAll(".class$", "");
				loadEnum(className).ifPresent(classes::add);
			});
		} catch (IOException e) {
			LOG.error(e.getMessage(), e);
		}

		return classes;
	}
652933
public static String getJarDir(Class<?> clazz) {
        return decodeUrl(new File(clazz.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent());
    }
1116419
function formatAsElegantPath(path, sep) {
    const packages = path.split(sep);

    // prettier-ignore
    const result = packages.map(item => item.trim()).join(' -> ');
    return result;
}
434143
private URL[] generateClassPathUrls() throws MojoExecutionException {
        List<URL> urls = new ArrayList<URL>();
        URL url;
        try {
            for (Object element : getCompileClasspath()) {
                String path = (String) element;
                if (path.endsWith(".jar")) {
                    url = new URL("jar:" + new File(path).toURI().toString() + "!/");
                } else {
                    url = new File(path).toURI().toURL();
                }
                urls.add(url);
            }
        } catch (MalformedURLException e) {
            throw new MojoExecutionException("Could not set up classpath", e);
        }
        return urls.toArray(new URL[urls.size()]);
    }
1117640
static String readFileFromJarfile(String fulljarpath) throws IOException {
        assert (fulljarpath.contains(".jar!"));

        String[] paths = fulljarpath.split("!");
        if (paths[0].startsWith("file:"))
            paths[0] = paths[0].substring("file:".length());
        paths[1] = paths[1].substring(1);

        return readFileFromJarfile(paths[0], paths[1]);
    }