001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015import static org.apache.juneau.internal.IOUtils.*;
016
017import java.io.*;
018import java.net.*;
019import java.util.*;
020import java.util.jar.*;
021
022import org.apache.juneau.*;
023
024/**
025 * Utility class for working with Jar manifest files.
026 *
027 * <p>
028 * Copies the contents of a {@link Manifest} into an {@link ObjectMap} so that the various convenience methods on that
029 * class can be used to retrieve values.
030 */
031public class ManifestFile extends ObjectMap {
032
033   private static final long serialVersionUID = 1L;
034
035   /**
036    * Create an instance of this class from a manifest file on the file system.
037    *
038    * @param f The manifest file.
039    * @throws IOException If a problem occurred while trying to read the manifest file.
040    */
041   public ManifestFile(File f) throws IOException {
042      Manifest mf = new Manifest();
043      try (FileInputStream fis = new FileInputStream(f)) {
044         mf.read(fis);
045         load(mf);
046      } catch (IOException e) {
047         throw new IOException("Problem detected in MANIFEST.MF.  Contents below:\n" + read(f), e);
048      }
049   }
050
051   /**
052    * Create an instance of this class from a {@link Manifest} object.
053    *
054    * @param f The manifest to read from.
055    */
056   public ManifestFile(Manifest f) {
057      load(f);
058   }
059
060   /**
061    * Finds and loads the manifest file of the jar file that the specified class is contained within.
062    *
063    * @param c The class to get the manifest file of.
064    * @throws IOException If a problem occurred while trying to read the manifest file.
065    */
066   public ManifestFile(Class<?> c) throws IOException {
067      String className = c.getSimpleName() + ".class";
068      String classPath = c.getResource(className).toString();
069      if (! classPath.startsWith("jar")) {
070         return;
071      }
072      String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) +  "/META-INF/MANIFEST.MF";
073      try {
074         Manifest mf = new Manifest(new URL(manifestPath).openStream());
075         load(mf);
076      } catch (MalformedURLException e) {
077         throw new IOException(e);
078      } catch (IOException e) {
079         e.printStackTrace();
080      }
081   }
082
083   private void load(Manifest mf) {
084      for (Map.Entry<Object,Object> e : mf.getMainAttributes().entrySet())
085         put(e.getKey().toString(), e.getValue().toString());
086   }
087}