Example usage for java.net JarURLConnection getManifest

List of usage examples for java.net JarURLConnection getManifest

Introduction

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

Prototype

public Manifest getManifest() throws IOException 

Source Link

Document

Returns the Manifest for this connection, or null if none.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("jar:file:/c://my.jar!/");
    JarURLConnection conn = (JarURLConnection) url.openConnection();

    Manifest manifest = conn.getManifest();
    System.out.println(manifest);
}

From source file:com.stgmastek.core.comm.main.StartCoreCommunication.java

static void getBundleDetails() {
    strBundleDetailsArray_ = new String[] { "Unknown", "Unknown", "Unknown", Integer.MIN_VALUE + "",
            Integer.MIN_VALUE + "", Integer.MIN_VALUE + "" };
    String localFile = StartCoreCommunication.class.getProtectionDomain().getCodeSource().getLocation()
            .toString();/*from   ww w .  ja  v a2 s .c om*/
    localFile = localFile.concat("!/");
    String tmpString = "jar:";
    String localJarFileString = tmpString.concat(localFile);
    URL localJarFileURL;
    try {
        localJarFileURL = new URL(localJarFileString);
        JarURLConnection localJarFile = (JarURLConnection) localJarFileURL.openConnection();
        Manifest mf = localJarFile.getManifest();
        Attributes attributes = mf.getMainAttributes();
        strBundleDetailsArray_[0] = (String) attributes.getValue("Bundle-Name");
        strBundleDetailsArray_[1] = (String) attributes.getValue("Bundle-Version");
        strBundleDetailsArray_[2] = (String) attributes.getValue("Bundled-On");
        strBundleDetailsArray_[3] = (String) attributes.getValue("Major-Version");
        strBundleDetailsArray_[4] = (String) attributes.getValue("Minor-Version");
        strBundleDetailsArray_[5] = (String) attributes.getValue("Build-Number");
    } catch (MalformedURLException e) {
        // do nothing
    } catch (FileNotFoundException fnfe) {
        // do nothing
    } catch (IOException ioe) {
        // do nothing
    }
}

From source file:net.sf.freecol.FreeCol.java

/**
 * Extract the package version from the class.
 *
 * @param juc The <code>JarURLConnection</code> to extract from.
 * @return A value of the package version attribute.
 *///from   w ww  .ja va2 s. c o m
private static String readVersion(JarURLConnection juc) throws IOException {
    Manifest mf = juc.getManifest();
    return (mf == null) ? null : mf.getMainAttributes().getValue("Package-Version");
}

From source file:jdbc.pool.CConnectionPoolManager.java

/**
 * Stores the version number array of the JDBCPool in the variable {@link #VERSION}.
 * /*from   w w w .  ja va  2 s .com*/
 * String[0] element is the product name.
 * String[1] element is the version number.
 * String[2] element is the date on which the JDBCPool was released.
 * 
 */
private void getVersion() {
    String localFile = getClass().getProtectionDomain().getCodeSource().getLocation().toString();
    localFile = localFile.concat("!/");
    String tmpString = "jar:";
    String localJarFileString = tmpString.concat(localFile);
    URL localJarFileURL;
    try {
        localJarFileURL = new URL(localJarFileString);
        JarURLConnection localJarFile = (JarURLConnection) localJarFileURL.openConnection();
        Manifest mf = localJarFile.getManifest();
        Attributes attributes = mf.getMainAttributes();
        VERSION[0] = (String) attributes.getValue("Bundle-Name");
        VERSION[1] = (String) attributes.getValue("Bundle-Version");
        VERSION[2] = (String) attributes.getValue("Bundled-On");
    } catch (MalformedURLException e) {
        //do nothing
    } catch (IOException e) {
        //do nothing
    }

    //        URL url = getURL();
    //        if (url != null)
    //        {
    //            try {
    //                String file = url.getFile();
    //                if (file.lastIndexOf(".jar") > -1) {
    //                    url = new URL(file.substring(0, file.lastIndexOf(".jar")+4));
    //                } else {
    //                    logger_.warn("Illegal Execution. Execution is not as distributed.");
    //                    return;
    //                }
    //                JarInputStream jis = null;
    //                try {
    //                    jis = new JarInputStream(url.openStream());
    //                    Manifest manifest = jis.getManifest();
    //                    Attributes attributes = manifest.getMainAttributes();
    //                    VERSION[0] = (String) attributes.getValue("Bundle-Name");
    //                    VERSION[1] = (String) attributes.getValue("Bundle-Version");
    //                    VERSION[2] = (String) attributes.getValue("Bundled-On");
    //                } finally {
    //                    if (jis != null) {
    //                        jis.close();
    //                    }
    //                }
    //            } catch (Exception e) {
    //                //Do nothing
    //            }
    //            return;
    //        } else {
    //            logger_.debug("Unable to find the class.");
    //            logger_.warn("Illegal Execution. Execution is not as distributed.");
    //        }
    //        return;
}

From source file:org.hyperic.hq.product.ProductPluginManager.java

private void logPluginManifest(String jarName) {
    if (log.isDebugEnabled()) {
        URL url;//from  w  w  w.  j av a  2s .c o m
        try {
            url = new URL("jar", "", "file:" + jarName + "!/");
            JarURLConnection jarConn = (JarURLConnection) url.openConnection();
            Map attributeMap = jarConn.getManifest().getMainAttributes();
            if (!attributeMap.isEmpty()) {
                StringBuilder manifestLog = new StringBuilder(
                        "\n--- Manifest entries for: " + url.toString() + " ---\n");
                Iterator iter = attributeMap.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    manifestLog.append(entry.getKey() + " - " + entry.getValue() + "\n");
                }
                log.debug(manifestLog.toString());
            }
        } catch (Exception e) {
            log.debug("Manifest retrieval had an exception (continuing): " + e.getMessage());
        }
    }
}

From source file:org.kuali.kra.test.infrastructure.ApplicationServer.java

/**
 * The jetty server's jsp compiler does not have access to the classpath artifacts to compile the jsps.
 * This method takes the current webapp classloader and creates one containing all of the
 * classpath artifacts on the test's classpath.
 *
 * See http://stackoverflow.com/questions/17685330/how-do-you-get-embedded-jetty-9-to-successfully-resolve-the-jstl-uri
 *
 * @param current the current webapp classpath
 * @return a classloader to replace it with
 * @throws IOException if an error occurs creating the classloader
 *//*from   w ww.  ja v a  2s.  c om*/
private static ClassLoader createClassLoaderForJasper(ClassLoader current) throws IOException {
    // Replace classloader with a new classloader with all URLs in manifests
    // from the parent loader bubbled up so Jasper looks at them.
    final ClassLoader parentLoader = current.getParent();
    if (current instanceof WebAppClassLoader && parentLoader instanceof URLClassLoader) {
        final LinkedList<URL> allURLs = new LinkedList<URL>(
                Arrays.asList(((URLClassLoader) parentLoader).getURLs()));

        for (URL url : ((LinkedList<URL>) allURLs.clone())) {
            try {
                final URLConnection conn = new URL("jar:" + url.toString() + "!/").openConnection();
                if (conn instanceof JarURLConnection) {
                    final JarURLConnection jconn = (JarURLConnection) conn;
                    final Manifest jarManifest = jconn.getManifest();
                    final String[] classPath = ((String) jarManifest.getMainAttributes().getValue("Class-Path"))
                            .split(" ");

                    for (String cpurl : classPath) {
                        allURLs.add(new URL(url, cpurl));
                    }
                }
            } catch (IOException | NullPointerException e) {
                //do nothing
            }
        }
        LOG.info("Creating new classloader for Application Server");
        return new WebAppClassLoader(new URLClassLoader(allURLs.toArray(new URL[] {}), parentLoader),
                ((WebAppClassLoader) current).getContext());
    }
    LOG.warn("Cannot create new classloader for app server " + current);
    return current;
}

From source file:org.languagetool.JLanguageTool.java

/**
 * Returns the build date or {@code null} if not run from JAR.
 *///w  ww . j a  v  a 2 s  . c  o m
@Nullable
private static String getBuildDate() {
    try {
        URL res = JLanguageTool.class.getResource(JLanguageTool.class.getSimpleName() + ".class");
        if (res == null) {
            // this will happen on Android, see http://stackoverflow.com/questions/15371274/
            return null;
        }
        Object connObj = res.openConnection();
        if (connObj instanceof JarURLConnection) {
            JarURLConnection conn = (JarURLConnection) connObj;
            Manifest manifest = conn.getManifest();
            return manifest.getMainAttributes().getValue("Implementation-Date");
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not get build date from JAR", e);
    }
}