Java Class Loader loadManifest(Class theClass)

Here you can find the source of loadManifest(Class theClass)

Description

load Manifest

License

Open Source License

Parameter

Parameter Description
theClass A Class in the bundle you wish to load the manifest from.

Return

The loaded .

Declaration

public static final Manifest loadManifest(Class<?> theClass) throws IOException, URISyntaxException 

Method Source Code

//package com.java2s;
/*//from  w w w  . jav a 2 s.c  o  m
 * Copyright (c) 1998-2017 by Richard A. Wilkes. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, version 2.0. If a copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, version 2.0.
 */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.jar.Manifest;

public class Main {
    private static final String MANIFEST_FILE = "META-INF/MANIFEST.MF";

    /**
     * @param theClass A {@link Class} in the bundle you wish to load the manifest from.
     * @return The loaded {@link Manifest}.
     */
    public static final Manifest loadManifest(Class<?> theClass) throws IOException, URISyntaxException {
        URI uri = theClass.getProtectionDomain().getCodeSource().getLocation().toURI();
        File file = new File(uri.getPath());
        URL url = file.isDirectory() ? uri.resolve(MANIFEST_FILE).toURL()
                : new URL("jar:" + uri.toASCIIString() + "!/" + MANIFEST_FILE); //$NON-NLS-1$ //$NON-NLS-2$
        try (InputStream in = setupConnection(url).getInputStream()) {
            return new Manifest(in);
        }
    }

    /**
     * @param uri The URI to setup a connection for.
     * @return A {@link URLConnection} configured with a 10 second timeout for connecting and
     *         reading data.
     */
    public static final URLConnection setupConnection(String uri) throws IOException {
        return setupConnection(new URL(uri));
    }

    /**
     * @param url The URL to setup a connection for.
     * @return A {@link URLConnection} configured with a 10 second timeout for connecting and
     *         reading data.
     */
    public static final URLConnection setupConnection(URL url) throws IOException {
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(10000);
        return connection;
    }
}

Related

  1. loadClassesImplementsTheInterface(String packageName, Class interfaceClazz)
  2. loadClassifiers(String dataDirectoryName)
  3. loadConfig(String path, ClassLoader classLoader)
  4. loadForClass(final Class clazz, final String filename)
  5. loadManifest(Class manifestFileClass)
  6. loadManifestFrom(Class c)
  7. loadProperties(final Properties properties, final String fileName, final ClassLoader cl)
  8. loadStyleSheet(Class type)
  9. loadTextFile(Class relToThisClass, String relFileName)