org.tkit.jee.base.util.ManifestUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tkit-quarkus-portal-mvc Show documentation
Show all versions of tkit-quarkus-portal-mvc Show documentation
1000kit mvc library for the Quarkus framework
/*
* Copyright (c) 2011, 1000kit.org, and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.tkit.jee.base.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.text.MessageFormat;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The manifest utility class.
*
* @author Andrej Petras
*/
public class ManifestUtil {
/**
* The logger for this class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(ManifestUtil.class);
/**
* Load the manifest of the JAR file for the class.
*
* @param clazz the class
* @return the manifest of the JAR file for the class.
*/
public static Manifest readManifest(Class clazz) {
ProtectionDomain domain = clazz.getProtectionDomain();
if (domain != null) {
CodeSource codeSource = domain.getCodeSource();
if (codeSource != null) {
URL url = codeSource.getLocation();
if (url.toExternalForm().contains("WEB-INF")) {
String urlString = url.toExternalForm();
urlString = urlString.substring(0, urlString.lastIndexOf("WEB-INF"));
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
LOGGER.error("Error creating url for String: " + urlString, e);
}
}
if (url != null) {
InputStream manifestStream = null;
try {
manifestStream = urlToStream(new URL(url + "/" + JarFile.MANIFEST_NAME));
return new Manifest(manifestStream);
} catch (MalformedURLException e1) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(e1.getMessage(), e1);
}
} catch (IOException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(e.getMessage(), e);
}
} finally {
if (manifestStream != null) {
try {
manifestStream.close();
} catch (IOException e) {
LOGGER.error(MessageFormat.format("Error closing stream: {0}", e.getMessage()), e);
}
}
}
JarInputStream jis = null;
try {
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
if (urlConnection instanceof JarURLConnection) {
JarURLConnection jarUrlConnection = (JarURLConnection) urlConnection;
return jarUrlConnection.getManifest();
} else {
jis = new JarInputStream(urlConnection.getInputStream());
return jis.getManifest();
}
} catch (IOException e) {
LOGGER.error(MessageFormat.format("Error reading META-INF/MANIFEST.MF file: {0}", e.getMessage()), e);
} finally {
if (jis != null) {
try {
jis.close();
} catch (IOException e) {
LOGGER.error(MessageFormat.format("Error closing stream: {0}", e.getMessage()), e);
}
}
}
}
}
}
return null;
}
/**
* Creates the input stream to URL.
*
* @param url the URL.
* @return the corresponding stream to the URL.
* @throws IOException if the method fails.
*/
private static InputStream urlToStream(URL url) throws IOException {
if (url != null) {
URLConnection connection = url.openConnection();
try {
connection.setUseCaches(false);
} catch (IllegalArgumentException e) {
LOGGER.error(e.getLocalizedMessage(), e);
}
return connection.getInputStream();
} else {
return null;
}
}
}