Example usage for java.net JarURLConnection getJarFile

List of usage examples for java.net JarURLConnection getJarFile

Introduction

In this page you can find the example usage for java.net JarURLConnection getJarFile.

Prototype

public abstract JarFile getJarFile() throws IOException;

Source Link

Document

Return the JAR file for this connection.

Usage

From source file:com.plugin.excel.xsd.node.store.impl.XsdNodeParserManagerImpl.java

private List<String> getXsdPaths(String directoryLocation) throws IOException {

    List<String> files = new ArrayList<String>();

    Enumeration<URL> e = this.getClass().getClassLoader().getResources(directoryLocation);
    while (e.hasMoreElements()) {
        java.net.URL url = e.nextElement();
        java.net.JarURLConnection urlcon = (java.net.JarURLConnection) (url.openConnection());
        java.util.jar.JarFile jar = urlcon.getJarFile();
        {//from   www  .  ja  v a  2  s  . c  o m
            java.util.Enumeration<java.util.jar.JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                String entry = entries.nextElement().getName();
                if (entry.startsWith(directoryLocation)) {
                    files.add(entry);
                }
            }
        }
    }

    return files;
}

From source file:org.tobarsegais.webapp.ContentServlet.java

@Override
protected long getLastModified(HttpServletRequest req) {
    String path = req.getPathInfo();
    if (path == null) {
        path = req.getServletPath();//  ww  w  .  j a v a2 s .  com
    }
    int index = path.indexOf(PLUGINS_ROOT);
    if (index != -1) {
        path = path.substring(index + PLUGINS_ROOT.length() - 1);
    }
    Map<String, String> bundles = (Map<String, String>) getServletContext().getAttribute("bundles");
    try {
        for (index = path.indexOf('/'); index != -1; index = path.indexOf('/', index + 1)) {
            String key = path.substring(0, index);
            if (key.startsWith("/")) {
                key = key.substring(1);
            }
            if (bundles.containsKey(key)) {
                key = bundles.get(key);
            }
            URL resource = getServletContext()
                    .getResource(ServletContextListenerImpl.BUNDLE_PATH + "/" + key + ".jar");
            if (resource == null) {
                continue;
            }
            URL jarResource = new URL("jar:" + resource + "!/");
            URLConnection connection = jarResource.openConnection();
            if (!(connection instanceof JarURLConnection)) {
                continue;
            }
            JarURLConnection jarConnection = (JarURLConnection) connection;
            JarFile jarFile = jarConnection.getJarFile();
            try {
                int endOfFileName = path.indexOf('#', index);
                endOfFileName = endOfFileName == -1 ? path.length() : endOfFileName;
                String fileName = path.substring(index + 1, endOfFileName);
                JarEntry jarEntry = jarFile.getJarEntry(fileName);
                if (jarEntry == null) {
                    continue;
                }
                return jarEntry.getTime();
            } finally {
                //jarFile.close();
            }
        }
    } catch (IOException e) {
        // ignore
    }
    return -1;
}

From source file:org.tobarsegais.webapp.ContentServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String path = req.getPathInfo();
    if (path == null) {
        path = req.getServletPath();/*from w ww  .jav  a 2 s . c o  m*/
    }
    int index = path.indexOf(PLUGINS_ROOT);
    if (index != -1) {
        path = path.substring(index + PLUGINS_ROOT.length() - 1);
    }
    Map<String, String> bundles = (Map<String, String>) getServletContext().getAttribute("bundles");
    for (index = path.indexOf('/'); index != -1; index = path.indexOf('/', index + 1)) {
        String key = path.substring(0, index);
        if (key.startsWith("/")) {
            key = key.substring(1);
        }
        if (bundles.containsKey(key)) {
            key = bundles.get(key);
        }
        URL resource = getServletContext()
                .getResource(ServletContextListenerImpl.BUNDLE_PATH + "/" + key + ".jar");
        if (resource == null) {
            continue;
        }
        URL jarResource = new URL("jar:" + resource + "!/");
        URLConnection connection = jarResource.openConnection();
        if (!(connection instanceof JarURLConnection)) {
            continue;
        }
        JarURLConnection jarConnection = (JarURLConnection) connection;
        JarFile jarFile = jarConnection.getJarFile();
        try {
            int endOfFileName = path.indexOf('#', index);
            endOfFileName = endOfFileName == -1 ? path.length() : endOfFileName;
            String fileName = path.substring(index + 1, endOfFileName);
            JarEntry jarEntry = jarFile.getJarEntry(fileName);
            if (jarEntry == null) {
                continue;
            }
            long size = jarEntry.getSize();
            if (size > 0 && size < Integer.MAX_VALUE) {
                resp.setContentLength((int) size);
            }
            resp.setContentType(getServletContext().getMimeType(fileName));
            InputStream in = null;
            OutputStream out = resp.getOutputStream();
            try {
                in = jarFile.getInputStream(jarEntry);
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                out.close();
            }
            return;
        } finally {
            //jarFile.close();
        }
    }
    resp.sendError(404);
}

From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java

private static void readJarDirectoryEntries(URL location, String basePath, Set<String> resources)
        throws IOException {
    JarURLConnection conn = (JarURLConnection) location.openConnection();
    JarFile jarfile = null;//ww w  .  jav a2s  . c o m
    jarfile = conn.getJarFile();

    Enumeration<JarEntry> entries = jarfile.entries();
    while (entries != null && entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String name = entry.getName();

        if (entry.isDirectory() && StringUtils.startsWith(name, basePath)) {
            resources.add(name);
        }
    }
}

From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java

private static void readJarEntries(URL location, String basePath, Map<String, URL> resources)
        throws IOException {
    JarURLConnection conn = (JarURLConnection) location.openConnection();
    JarFile jarfile = null;/*from ww  w  .  j  a  v a2s.c o  m*/
    jarfile = conn.getJarFile();

    Enumeration<JarEntry> entries = jarfile.entries();
    while (entries != null && entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String name = entry.getName();

        if (entry.isDirectory() || !name.startsWith(basePath) || name.length() == basePath.length()) {
            continue;
        }

        name = name.substring(basePath.length());

        if (name.contains("/")) {
            continue;
        }

        URL resource = new URL(location, name);
        resources.put(name, resource);
    }
}

From source file:org.mule.tools.schemadocs.SchemaDocsMain.java

protected void readFromJar(URL jarUrl, List resources) throws IOException {
    JarURLConnection jarConnection = (JarURLConnection) jarUrl.openConnection();
    Enumeration entries = jarConnection.getJarFile().entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = (JarEntry) entries.nextElement();
        String name = new File(entry.getName()).getName();
        if (name.startsWith(MULE) && name.endsWith(XSD)) {
            logger.debug("entry: " + entry);
            resources.add(new URL(jarUrl, entry.getName()));
        }/*from   w ww  .ja  v  a2s.  c  om*/
    }
}

From source file:fr.gael.dhus.server.http.webapp.WebApplication.java

protected void extractJarFile(URL url, String configuration_folder, String dest_folder) throws IOException {
    final JarURLConnection connection = (JarURLConnection) url.openConnection();
    if (connection != null) {
        Enumeration<JarEntry> entries = connection.getJarFile().entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory() && entry.getName().equals(configuration_folder)) {
                InputStream in = connection.getJarFile().getInputStream(entry);
                try {
                    File file = new File(dest_folder);
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }//from www .j ava2  s . c  om
                    OutputStream out = new FileOutputStream(file);
                    try {
                        IOUtils.copy(in, out);
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    }
}

From source file:org.rhq.cassandra.schema.UpdateFolder.java

/**
 * Loads the initial set of update files based on the input folder.
 *
 * @return list of update files/*  w  w  w .  j  a v  a 2 s . c om*/
 * @throws Exception
 */
private List<UpdateFile> loadUpdateFiles() throws Exception {
    List<UpdateFile> files = new ArrayList<UpdateFile>();
    InputStream stream = null;

    try {
        URL resourceFolderURL = this.getClass().getClassLoader().getResource(folder);

        if (resourceFolderURL.getProtocol().equals("file")) {
            stream = this.getClass().getClassLoader().getResourceAsStream(folder);
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

            String updateFile;
            while ((updateFile = reader.readLine()) != null) {
                files.add(new UpdateFile(folder + "/" + updateFile));
            }
        } else if (resourceFolderURL.getProtocol().equals("jar")) {
            URL jarURL = this.getClass().getClassLoader().getResources(folder).nextElement();
            JarURLConnection jarURLCon = (JarURLConnection) (jarURL.openConnection());
            JarFile jarFile = jarURLCon.getJarFile();
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                String entry = entries.nextElement().getName();
                if (entry.startsWith(folder) && !entry.equals(folder) && !entry.equals(folder + "/")) {
                    files.add(new UpdateFile(entry));
                }
            }
        } else if (resourceFolderURL.getProtocol().equals("vfs")) {
            URLConnection conn = resourceFolderURL.openConnection();
            VirtualFile virtualFolder = (VirtualFile) conn.getContent();
            for (VirtualFile virtualChild : virtualFolder.getChildren()) {
                if (!virtualChild.isDirectory()) {
                    files.add(new UpdateFile(virtualChild.getPathNameRelativeTo(virtualFolder.getParent())));
                }
            }
        } else {
            // In the event we get another protocol that we do not recognize, throw an
            // exception instead of failing silently.
            throw new RuntimeException(
                    "The URL protocol [" + resourceFolderURL.getProtocol() + "] is not " + "supported");
        }

        Collections.sort(files, new Comparator<UpdateFile>() {
            @Override
            public int compare(UpdateFile o1, UpdateFile o2) {
                return o1.compareTo(o2);
            }
        });
    } catch (Exception e) {
        log.error("Error reading the list of update files.", e);
        throw e;
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception e) {
                log.error("Error closing the stream with the list of update files.", e);
                throw e;
            }
        }
    }

    return files;
}

From source file:uniko.west.topology.bolts.TweetIndexBolt.java

@Override
public void prepare(@SuppressWarnings("rawtypes") Map stormConf, TopologyContext context,
        OutputCollector collector) {/*from w  w w  .  j  av a2 s .c  om*/
    this.collector = collector;
    try {
        File topologyJarFile = new File(
                TweetIndexBolt.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
        this.pathToLocalWordMap = topologyJarFile.getParent() + "/topic-model/wordmap.txt";
        new File(new File(pathToLocalWordMap).getParent()).mkdirs();

        FileLoader.getFile(urlToWordMap, pathToLocalWordMap);

        this.dictionary = new Dictionary();
        this.dictionary.readWordMap(pathToLocalWordMap);
    } catch (URISyntaxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // for language detection
    String dirname = "profiles/";
    Enumeration<URL> en;
    try {
        en = Detector.class.getClassLoader().getResources(dirname);
        List<String> profiles = new ArrayList<>();
        if (en.hasMoreElements()) {
            URL url = en.nextElement();
            JarURLConnection urlcon = (JarURLConnection) url.openConnection();
            try (JarFile jar = urlcon.getJarFile();) {
                Enumeration<JarEntry> entries = jar.entries();
                while (entries.hasMoreElements()) {
                    String entry = entries.nextElement().getName();
                    if (entry.startsWith(dirname)) {
                        try (InputStream in = Detector.class.getClassLoader().getResourceAsStream(entry);) {
                            profiles.add(IOUtils.toString(in));
                        }
                    }
                }
            }
        }

        DetectorFactory.loadProfile(profiles);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (LangDetectException e) {
        e.printStackTrace();
    }

}

From source file:fr.gael.dhus.server.http.web.WebApplication.java

protected void extractJarFolder(URL url, String configuration_folder, String dest_folder) throws IOException {
    final JarURLConnection connection = (JarURLConnection) url.openConnection();
    if (connection != null) {
        Enumeration<JarEntry> entries = connection.getJarFile().entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory() && entry.getName().startsWith(configuration_folder)) {
                InputStream in = connection.getJarFile().getInputStream(entry);
                try {
                    File file = new File(dest_folder, entry.getName().substring(configuration_folder.length()));
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }//from  w  w w. j  av  a2s . c om
                    OutputStream out = new FileOutputStream(file);
                    try {
                        IOUtils.copy(in, out);
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    }
}