Example usage for java.net JarURLConnection getEntryName

List of usage examples for java.net JarURLConnection getEntryName

Introduction

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

Prototype

public String getEntryName() 

Source Link

Document

Return the entry name for this connection.

Usage

From source file:io.tempra.AppServer.java

public static void copyJarResourceToFolder(JarURLConnection jarConnection, File destDir) {

    try {/*w w w . j a  v a2  s. com*/
        JarFile jarFile = jarConnection.getJarFile();

        /**
         * Iterate all entries in the jar file.
         */
        for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {

            JarEntry jarEntry = e.nextElement();
            String jarEntryName = jarEntry.getName();
            String jarConnectionEntryName = jarConnection.getEntryName();

            /**
             * Extract files only if they match the path.
             */
            if (jarEntryName.startsWith(jarConnectionEntryName)) {

                String filename = jarEntryName.startsWith(jarConnectionEntryName)
                        ? jarEntryName.substring(jarConnectionEntryName.length())
                        : jarEntryName;
                File currentFile = new File(destDir, filename);

                if (jarEntry.isDirectory()) {
                    currentFile.mkdirs();
                } else {
                    InputStream is = jarFile.getInputStream(jarEntry);
                    OutputStream out = FileUtils.openOutputStream(currentFile);
                    IOUtils.copy(is, out);
                    is.close();
                    out.close();
                }
            }
        }
    } catch (IOException e) {
        // TODO add logger
        e.printStackTrace();
    }

}

From source file:com.github.ithildir.liferay.mobile.go.GoSDKBuilder.java

protected void copyJarResource(JarURLConnection jarConnection, File destinationDir) throws IOException {

    String jarConnectionEntryName = jarConnection.getEntryName();
    JarFile jarFile = jarConnection.getJarFile();

    Enumeration<JarEntry> enu = jarFile.entries();

    while (enu.hasMoreElements()) {
        JarEntry jarEntry = enu.nextElement();
        String jarEntryName = jarEntry.getName();

        if (jarEntryName.startsWith(jarConnectionEntryName)) {
            String fileName = jarEntryName;

            if (fileName.startsWith(jarConnectionEntryName)) {
                fileName = fileName.substring(jarConnectionEntryName.length());
            }/*from  w  w  w. ja va2 s .  com*/

            File file = new File(destinationDir, fileName);

            if (jarEntry.isDirectory()) {
                file.mkdirs();
            } else {
                InputStream is = null;

                try {
                    is = jarFile.getInputStream(jarEntry);

                    FileUtils.copyInputStreamToFile(is, file);
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        }
    }
}

From source file:JarUtils.java

/** Given a URL check if its a jar url(jar:<url>!/archive) and if it is,
 extract the archive entry into the given dest directory and return a file
 URL to its location. If jarURL is not a jar url then it is simply returned
 as the URL for the jar./*from  ww w  .  java  2  s  .com*/
        
 @param jarURL the URL to validate and extract the referenced entry if its
   a jar protocol URL
 @param dest the directory into which the nested jar will be extracted.
 @return the file: URL for the jar referenced by the jarURL parameter.
 * @throws IOException 
 */
public static URL extractNestedJar(URL jarURL, File dest) throws IOException {
    // This may not be a jar URL so validate the protocol 
    if (jarURL.getProtocol().equals("jar") == false)
        return jarURL;

    String destPath = dest.getAbsolutePath();
    URLConnection urlConn = jarURL.openConnection();
    JarURLConnection jarConn = (JarURLConnection) urlConn;
    // Extract the archive to dest/jarName-contents/archive
    String parentArchiveName = jarConn.getJarFile().getName();
    // Find the longest common prefix between destPath and parentArchiveName
    int length = Math.min(destPath.length(), parentArchiveName.length());
    int n = 0;
    while (n < length) {
        char a = destPath.charAt(n);
        char b = parentArchiveName.charAt(n);
        if (a != b)
            break;
        n++;
    }
    // Remove any common prefix from parentArchiveName
    parentArchiveName = parentArchiveName.substring(n);

    File archiveDir = new File(dest, parentArchiveName + "-contents");
    if (archiveDir.exists() == false && archiveDir.mkdirs() == false)
        throw new IOException(
                "Failed to create contents directory for archive, path=" + archiveDir.getAbsolutePath());
    String archiveName = jarConn.getEntryName();
    File archiveFile = new File(archiveDir, archiveName);
    File archiveParentDir = archiveFile.getParentFile();
    if (archiveParentDir.exists() == false && archiveParentDir.mkdirs() == false)
        throw new IOException(
                "Failed to create parent directory for archive, path=" + archiveParentDir.getAbsolutePath());
    InputStream archiveIS = jarConn.getInputStream();
    FileOutputStream fos = new FileOutputStream(archiveFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] buffer = new byte[4096];
    int read;
    while ((read = archiveIS.read(buffer)) > 0) {
        bos.write(buffer, 0, read);
    }
    archiveIS.close();
    bos.close();

    // Return the file url to the extracted jar
    return archiveFile.toURL();
}

From source file:com.technophobia.substeps.report.DefaultExecutionReportBuilder.java

public void copyJarResourcesRecursively(final File destination, final JarURLConnection jarConnection)
        throws IOException {
    final JarFile jarFile = jarConnection.getJarFile();
    for (final JarEntry entry : Collections.list(jarFile.entries())) {
        if (entry.getName().startsWith(jarConnection.getEntryName())) {
            final String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
            if (!entry.isDirectory()) {
                InputStream entryInputStream = null;
                try {
                    entryInputStream = jarFile.getInputStream(entry);
                    FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName));
                } finally {
                    IOUtils.closeQuietly(entryInputStream);
                }//from  www. j  av a2  s . co  m
            } else {
                new File(destination, fileName).mkdirs();
            }
        }
    }
}

From source file:com.stacksync.desktop.Environment.java

public void copyResourcesFromJar(JarURLConnection jarConnection, File destDir) {

    try {/*  w w w  . j  av  a 2  s .com*/
        JarFile jarFile = jarConnection.getJarFile();

        /**
         * Iterate all entries in the jar file.
         */
        for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {

            JarEntry jarEntry = e.nextElement();
            String jarEntryName = jarEntry.getName();
            String jarConnectionEntryName = jarConnection.getEntryName();

            /**
             * Extract files only if they match the path.
             */
            if (jarEntryName.startsWith(jarConnectionEntryName)) {

                String filename = jarEntryName.startsWith(jarConnectionEntryName)
                        ? jarEntryName.substring(jarConnectionEntryName.length())
                        : jarEntryName;
                File currentFile = new File(destDir, filename);

                if (jarEntry.isDirectory()) {
                    currentFile.mkdirs();
                } else {
                    InputStream is = jarFile.getInputStream(jarEntry);
                    OutputStream out = FileUtils.openOutputStream(currentFile);
                    IOUtils.copy(is, out);
                    is.close();
                    out.close();
                }
            }
        }
    } catch (IOException e) {
        // TODO add logger
        e.printStackTrace();
    }

}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.frontdesk.web.data.SessionBean.java

/** Return a List with all the available bankruptcy functions by using dynamic class loading */
public List getAvailableBankruptcyFunctions() {
    try {/*from www.  j a va2  s .  c  o m*/
        List bankruptcyFunctions = new ArrayList();
        String pckgname = "edu.caltechUcla.sselCassel.projects.jMarkets.shared.functions";
        String name = "/edu/caltechUcla/sselCassel/projects/jMarkets/shared/functions";

        // Get a File object for the package
        URL url = SessionBean.class.getResource(name);
        URI uri = new URI(url.getPath());

        URL ori_url = new URL(url.getProtocol() + ":" + uri.getPath());

        File directory = new File(ori_url.getFile());

        if (directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (int i = 0; i < files.length; i++) {
                // we are only interested in .class files
                if (files[i].endsWith(".class")) {
                    // removes the .class extension
                    String classname = files[i].substring(0, files[i].length() - 6);

                    try {
                        // Try to create an instance of the object
                        Object o = Class.forName(pckgname + "." + classname).newInstance();

                        if (o instanceof BankruptcyFunction && classname.endsWith("Function")) {
                            BankruptcyBean bankruptcyBean = new BankruptcyBean();
                            String bankruptcyName = classname.substring(0, classname.length() - 8);
                            bankruptcyBean.setName(bankruptcyName);

                            bankruptcyFunctions.add(bankruptcyBean);
                        }
                    } catch (ClassNotFoundException e) {
                        log.warn("Error loading bankruptcy function class", e);
                    } catch (InstantiationException e) {
                        //log.warn("Loaded verifier class does not have a default constructor!" + MSConstants.newline + e);
                    } catch (IllegalAccessException e) {
                        log.warn("Loaded bankruptcy function class is not public!", e);
                    }
                }
            }
        }

        //check the jar file if the verifiers are not in the file system
        else {
            try {
                JarURLConnection conn = (JarURLConnection) url.openConnection();
                String starts = conn.getEntryName();
                JarFile jfile = conn.getJarFile();
                Enumeration e = jfile.entries();
                while (e.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) e.nextElement();
                    String entryname = entry.getName();
                    if (entryname.startsWith(starts) && (entryname.lastIndexOf('/') <= starts.length())
                            && entryname.endsWith(".class")) {
                        String classname = entryname.substring(0, entryname.length() - 6);
                        if (classname.startsWith("/"))
                            classname = classname.substring(1);
                        classname = classname.replace('/', '.');
                        try {
                            // Try to create an instance of the object
                            Object o = Class.forName(classname).newInstance();

                            if (o instanceof BankruptcyFunction && classname.endsWith("Function")) {
                                String cname = classname.substring(classname.lastIndexOf('.') + 1);

                                BankruptcyBean bankruptcyBean = new BankruptcyBean();
                                String bankruptcyName = cname.substring(0, cname.length() - 8);
                                bankruptcyBean.setName(bankruptcyName);

                                bankruptcyFunctions.add(bankruptcyBean);
                            }
                        } catch (ClassNotFoundException cnfex) {
                            log.warn("Error loading bankruptcy function class", cnfex);
                        } catch (InstantiationException iex) {
                            // We try to instanciate an interface
                            // or an object that does not have a
                            // default constructor
                        } catch (IllegalAccessException iaex) {
                            log.warn("Loaded bankruptcy function class is not public!", iaex);
                        }
                    }
                }
            } catch (IOException e) {
                log.warn("Unknown IO Error", e);
            }
        }

        return bankruptcyFunctions;
    } catch (Exception e) {
        log.error("Failed to return a list of bankruptcy functions", e);
    }
    return null;
}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.frontdesk.web.data.SessionBean.java

/** Return a List with all the available payoff functions by using dynamic class loading */
public List getAvailablePayoffFunctions() {
    try {// w  ww .  j  a  v a2 s.c o m
        List payoffFunctions = new ArrayList();
        String pckgname = "edu.caltechUcla.sselCassel.projects.jMarkets.shared.functions";
        String name = "/edu/caltechUcla/sselCassel/projects/jMarkets/shared/functions";

        // Get a File object for the package
        URL url = SessionBean.class.getResource(name);

        URI uri = new URI(url.getPath());

        URL ori_url = new URL(url.getProtocol() + ":" + uri.getPath());

        File directory = new File(ori_url.getFile());

        if (directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (int i = 0; i < files.length; i++) {
                // we are only interested in .class files
                if (files[i].endsWith(".class")) {
                    // removes the .class extension
                    String classname = files[i].substring(0, files[i].length() - 6);

                    try {
                        // Try to create an instance of the object
                        Object o = Class.forName(pckgname + "." + classname).newInstance();

                        if (o instanceof PayoffFunction && classname.endsWith("Function")) {
                            PayoffBean payoffBean = new PayoffBean();
                            String payoffName = classname.substring(0, classname.length() - 8);
                            payoffBean.setName(payoffName);

                            payoffFunctions.add(payoffBean);
                        }
                    } catch (ClassNotFoundException e) {
                        log.warn("Error loading payoff function class", e);
                    } catch (InstantiationException e) {
                        //log.warn("Loaded verifier class does not have a default constructor!" + MSConstants.newline + e);
                    } catch (IllegalAccessException e) {
                        log.warn("Loaded payoff function class is not public!", e);
                    }
                }
            }
        }

        //check the jar file if the verifiers are not in the file system
        else {
            try {
                JarURLConnection conn = (JarURLConnection) url.openConnection();
                String starts = conn.getEntryName();
                JarFile jfile = conn.getJarFile();
                Enumeration e = jfile.entries();
                while (e.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) e.nextElement();
                    String entryname = entry.getName();
                    if (entryname.startsWith(starts) && (entryname.lastIndexOf('/') <= starts.length())
                            && entryname.endsWith(".class")) {
                        String classname = entryname.substring(0, entryname.length() - 6);
                        if (classname.startsWith("/"))
                            classname = classname.substring(1);
                        classname = classname.replace('/', '.');
                        try {
                            // Try to create an instance of the object
                            Object o = Class.forName(classname).newInstance();

                            if (o instanceof PayoffFunction && classname.endsWith("Function")) {
                                String cname = classname.substring(classname.lastIndexOf('.') + 1);

                                PayoffBean payoffBean = new PayoffBean();
                                String payoffName = cname.substring(0, cname.length() - 8);
                                payoffBean.setName(payoffName);

                                payoffFunctions.add(payoffBean);
                            }
                        } catch (ClassNotFoundException cnfex) {
                            log.warn("Error loading payoff function class", cnfex);
                        } catch (InstantiationException iex) {
                            // We try to instanciate an interface
                            // or an object that does not have a
                            // default constructor
                        } catch (IllegalAccessException iaex) {
                            log.warn("Loaded payoff function class is not public!", iaex);
                        }
                    }
                }
            } catch (IOException e) {
                log.warn("Unknown IO Error", e);
            }
        }

        return payoffFunctions;
    } catch (Exception e) {
        log.error("Failed to return a list of payoff functions", e);
    }
    return null;
}