001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.store.util;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.net.URL;
023    import java.util.jar.Manifest;
024    
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    
028    /**
029     * Utility to read a <code>MANIFEST.MF</code>.
030     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
031     */
032    public final class ManifestUtil
033    {
034            private static final Logger logger = LoggerFactory.getLogger(ManifestUtil.class);
035    
036            private ManifestUtil() { }
037    
038            /**
039             * Read the <code>MANIFEST.MF</code> of the JAR file containing the given class.
040             * @param clazz a class located in the JAR whose MANIFEST.MF should be read.
041             * @return the manifest; never <code>null</code>.
042             * @throws IOException if reading the manifest fails.
043             */
044            public static Manifest readManifest(Class<?> clazz)
045            throws IOException
046            {
047                    if (clazz == null)
048                            throw new IllegalArgumentException("clazz must not be null!");
049    
050                    Manifest manifest = new Manifest();
051    
052                    String referenceClassRelativePath = "/" + clazz.getName().replace('.', '/') + ".class";
053                    URL referenceClassURL = clazz.getResource(referenceClassRelativePath);
054                    logger.trace("readManifest: referenceClassURL={}", referenceClassURL);
055    
056                    String referenceClassURLBase = referenceClassURL.toExternalForm();
057                    if (!referenceClassURLBase.endsWith(referenceClassRelativePath))
058                            throw new IllegalStateException("referenceClassURL does not end on \"" + referenceClassRelativePath + "\": " + referenceClassURLBase);
059    
060                    referenceClassURLBase = referenceClassURLBase.substring(0, referenceClassURLBase.length() - referenceClassRelativePath.length());
061                    logger.trace("readManifest: referenceClassURLBase={}", referenceClassURLBase);
062    
063                    String manifestResourceName = "/META-INF/MANIFEST.MF";
064                    URL manifestResourceUrl = new URL(referenceClassURLBase + manifestResourceName);
065    
066                    InputStream in = manifestResourceUrl.openStream();
067                    try {
068                            manifest.read(in);
069                    } catch (IOException x) {
070                            throw new IOException("Cannot read resource: " + manifestResourceUrl, x);
071                    } finally {
072                            in.close();
073                    }
074    
075                    return manifest;
076            }
077    }