Search in sources :

Example 31 with JarURLConnection

use of java.net.JarURLConnection in project JessMA by ldcsaa.

the class PackageHelper method scanPackageNamesByJar.

/**
 * 在 jar 文件中扫描子包
 *
 * @param url				: jar 文件的 URL
 * @param basePackagePath	: jar 文件中的包路径
 * @param filter			: 包过滤器,参考:{@link PackageHelper.PackageFilter}
 * @param names				: 扫描结果集合
 */
public static final void scanPackageNamesByJar(URL url, String basePackagePath, final PackageFilter filter, Set<String> names) {
    basePackagePath = adjustBasePackagePath(basePackagePath);
    try {
        JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String name = entry.getName();
            if (!entry.isDirectory() || !name.startsWith(basePackagePath))
                continue;
            if (name.endsWith(PATH_SEP_STR))
                name = name.substring(0, name.length() - 1);
            if (name.equals(basePackagePath))
                continue;
            String packageName = name.replace(PATH_SEP_CHAR, PACKAGE_SEP_CHAR);
            if (filter == null || filter.accept(packageName))
                names.add(packageName);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 32 with JarURLConnection

use of java.net.JarURLConnection in project JessMA by ldcsaa.

the class PackageHelper method scanResourceNamesByJar.

/**
 * 在 jar 文件中扫描资源文件
 *
 * @param url				: jar 文件的 URL
 * @param basePackagePath	: jar 文件中的包路径
 * @param recursive			: 是否扫描子目录
 * @param filter			: 资源文件过滤器,参考:{@link PackageHelper.ResourceFilter}
 * @param names				: 扫描结果集合
 */
public static final void scanResourceNamesByJar(URL url, String basePackagePath, final boolean recursive, final ResourceFilter filter, Set<String> names) {
    basePackagePath = adjustBasePackagePath(basePackagePath);
    try {
        JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String name = entry.getName();
            int nameIndex = name.lastIndexOf(PATH_SEP_CHAR);
            if (entry.isDirectory() || !name.startsWith(basePackagePath))
                continue;
            if (!recursive && nameIndex != basePackagePath.length())
                continue;
            String packagePath = (nameIndex != -1 ? name.substring(0, nameIndex) : EMPTY_STR);
            String simpleName = (nameIndex != -1 ? name.substring(nameIndex + 1) : name);
            if (filter != null && !filter.accept(packagePath, simpleName))
                continue;
            names.add(name);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 33 with JarURLConnection

use of java.net.JarURLConnection in project fabric8 by jboss-fuse.

the class LogQuerySupport method jarIndex.

protected String jarIndex(URL url) throws IOException {
    StringBuilder buffer = new StringBuilder();
    JarURLConnection uc = (JarURLConnection) url.openConnection();
    return jarIndex(uc.getJarFile());
}
Also used : JarURLConnection(java.net.JarURLConnection)

Example 34 with JarURLConnection

use of java.net.JarURLConnection in project xwiki-platform by xwiki.

the class FilesystemResourceReferenceCopier method getJARFile.

private File getJARFile(String resourceName) throws IOException {
    // Get the JAR URL by looking up the passed resource name to extract the location of the JAR
    URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(resourceName);
    // "url($!services.webjars.url(...))". In this case ignore it and don't copy the resource to the file system.
    if (resourceURL != null) {
        JarURLConnection connection = (JarURLConnection) resourceURL.openConnection();
        URL jarURL = connection.getJarFileURL();
        File file;
        try {
            file = new File(jarURL.toURI());
        } catch (URISyntaxException e) {
            file = new File(jarURL.getPath());
        }
        return file;
    } else {
        LOGGER.debug("Cannot construct JAR File for resource [{}] which couldn't be found in the context " + "ClassLoader.", resourceName);
        return null;
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) URISyntaxException(java.net.URISyntaxException) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

Example 35 with JarURLConnection

use of java.net.JarURLConnection in project liferay-ide by liferay.

the class BladeCLI method execute.

public static String[] execute(String args) {
    Project project = new Project();
    Java javaTask = new Java();
    javaTask.setProject(project);
    javaTask.setFork(true);
    javaTask.setFailonerror(true);
    Properties properties = System.getProperties();
    boolean needToCopy = true;
    File temp = new File(properties.getProperty("user.home"), ".liferay-ide");
    File bladeJar = new File(temp, "blade.jar");
    ClassLoader bladeClassLoader = BladeCLI.class.getClassLoader();
    URL url = bladeClassLoader.getResource("/libs/blade.jar");
    try (InputStream in = bladeClassLoader.getResourceAsStream("/libs/blade.jar")) {
        JarURLConnection jarUrlConnection = (JarURLConnection) url.openConnection();
        JarEntry jarEntry = jarUrlConnection.getJarEntry();
        Long bladeJarTimestamp = jarEntry.getTime();
        if (bladeJar.exists()) {
            Long destTimestamp = bladeJar.lastModified();
            if (destTimestamp < bladeJarTimestamp) {
                bladeJar.delete();
            } else {
                needToCopy = false;
            }
        }
        if (needToCopy) {
            FileUtil.writeFile(bladeJar, in);
            bladeJar.setLastModified(bladeJarTimestamp);
        }
    } catch (IOException ioe) {
    }
    javaTask.setJar(bladeJar);
    javaTask.setArgs(args);
    DefaultLogger logger = new DefaultLogger();
    project.addBuildListener(logger);
    StringBufferOutputStream out = new StringBufferOutputStream();
    logger.setOutputPrintStream(new PrintStream(out));
    logger.setMessageOutputLevel(Project.MSG_INFO);
    javaTask.executeJava();
    List<String> lines = new ArrayList<>();
    Scanner scanner = new Scanner(out.toString());
    while (scanner.hasNextLine()) {
        String nextLine = scanner.nextLine();
        lines.add(nextLine.replaceAll(".*\\[null\\] ", ""));
    }
    scanner.close();
    boolean hasErrors = false;
    StringBuilder errors = new StringBuilder();
    for (String line : lines) {
        if (line.startsWith("Error")) {
            hasErrors = true;
        } else if (hasErrors) {
            errors.append(line);
        }
    }
    return lines.toArray(new String[0]);
}
Also used : Java(org.apache.tools.ant.taskdefs.Java) PrintStream(java.io.PrintStream) Scanner(java.util.Scanner) InputStream(java.io.InputStream) JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Project(org.apache.tools.ant.Project) File(java.io.File) DefaultLogger(org.apache.tools.ant.DefaultLogger)

Aggregations

JarURLConnection (java.net.JarURLConnection)222 URL (java.net.URL)160 JarFile (java.util.jar.JarFile)129 IOException (java.io.IOException)120 JarEntry (java.util.jar.JarEntry)105 File (java.io.File)92 URLConnection (java.net.URLConnection)89 ArrayList (java.util.ArrayList)32 InputStream (java.io.InputStream)27 MalformedURLException (java.net.MalformedURLException)25 URISyntaxException (java.net.URISyntaxException)21 Enumeration (java.util.Enumeration)17 Manifest (java.util.jar.Manifest)16 FileInputStream (java.io.FileInputStream)12 CodeSource (java.security.CodeSource)12 LinkedHashSet (java.util.LinkedHashSet)11 URI (java.net.URI)10 Attributes (java.util.jar.Attributes)10 ZipEntry (java.util.zip.ZipEntry)9 FileNotFoundException (java.io.FileNotFoundException)8