com.hotels.bdp.waggledance.manifest.ManifestAttributes Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2016-2019 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hotels.bdp.waggledance.manifest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Read and make available all the attributes held in the specified class' META_INF/MANIFEST.MF file. The attributes are
* made available via a getter and the toString method can for instance be used on application start to report build
* date, build user and version number of Maven builds.
*
* This class does the best effort to find the correct manifest in the classpath.
*
*/
public class ManifestAttributes {
private static final Logger LOG = LoggerFactory.getLogger(ManifestAttributes.class);
static final String META_INF_MANIFEST_MF = "META-INF/MANIFEST.MF";
private static final String JAR_PROTOCOL = "jar:";
private static final String FILE_PROTOCOL = "file:";
private final String attributesString;
private Attributes attributes = new Attributes();
public ManifestAttributes(Class> mainClass) {
this(mainClass.getProtectionDomain());
}
ManifestAttributes(ProtectionDomain protectionDomain) {
StringBuilder attributesStringBuilder = new StringBuilder();
try (InputStream manifestStream = openManifestStream(protectionDomain)) {
if (manifestStream != null) {
attributes = new Manifest(manifestStream).getMainAttributes();
for (Entry