org.apache.catalina.util.ExtensionValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2016 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2004 The Apache Software Foundation
*
* 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.
*/
// Portions Copyright 2018-2022 Payara Foundation and/or affiliates
package org.apache.catalina.util;
import org.apache.catalina.LogFacade;
import org.apache.catalina.core.StandardContext;
import org.apache.naming.resources.Resource;
import javax.naming.Binding;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Ensures that all extension dependies are resolved for a WEB application
* are met. This class builds a master list of extensions available to an
* applicaiton and then validates those extensions.
*
* See http://java.sun.com/j2se/1.4/docs/guide/extensions/spec.html for
* a detailed explanation of the extension mechanism in Java.
*
* @author Greg Murray
* @author Justyna Horwat
* @version $Revision: 1.3 $ $Date: 2006/03/12 01:27:08 $
*
*/
public final class ExtensionValidator {
private static final Logger log = LogFacade.getLogger();
private static final ResourceBundle rb = log.getResourceBundle();
private static volatile HashMap containerAvailableExtensions = null;
private static ArrayList containerManifestResources =
new ArrayList();
// ----------------------------------------------------- Static Initializer
/**
* This static initializer loads the container level extensions that are
* available to all web applications. This method scans all extensions
*
* The System Class-Path is also scanned for jar files that may contain
* available extensions.
*/
static {
// check for container level optional packages
String systemClasspath = System.getProperty("java.class.path");
StringTokenizer strTok = new StringTokenizer(systemClasspath,
File.pathSeparator);
// build a list of jar files in the classpath
while (strTok.hasMoreTokens()) {
String classpathItem = strTok.nextToken();
if (classpathItem.toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
File item = new File(classpathItem);
if (item.exists()) {
try {
addSystemResource(item);
} catch (IOException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.FAILED_LOAD_MANIFEST_RESOURCES_EXCEPTION),
item);
log.log(Level.SEVERE, msg, e);
}
}
}
}
}
// --------------------------------------------------------- Public Methods
/**
* Runtime validation of a Web Applicaiton.
*
* This method uses JNDI to look up the resources located under a
* DirContext
. It locates Web Application MANIFEST.MF
* file in the /META-INF/ directory of the application and all
* MANIFEST.MF files in each JAR file located in the WEB-INF/lib
* directory and creates an ArrayList
of
* ManifestResorce objects. These objects are then passed
* to the validateManifestResources method for validation.
*
* @param dirContext The JNDI root of the Web Application
* @param context The context from which the Logger and path to the
* application
*
* @return true if all required extensions satisfied
*/
public static synchronized boolean validateApplication(
DirContext dirContext,
StandardContext context)
throws IOException {
String appName = context.getPath();
ArrayList appManifestResources =
new ArrayList();
ManifestResource appManifestResource = null;
// If the application context is null it does not exist and
// therefore is not valid
if (dirContext == null) return false;
// Find the Manifest for the Web Applicaiton
InputStream inputStream = null;
try {
NamingEnumeration wne = dirContext.listBindings("/META-INF/");
Binding binding = (Binding) wne.nextElement();
if (binding.getName().toUpperCase(Locale.ENGLISH).equals("MANIFEST.MF")) {
Resource resource = (Resource)dirContext.lookup
("/META-INF/" + binding.getName());
inputStream = resource.streamContent();
Manifest manifest = new Manifest(inputStream);
inputStream.close();
inputStream = null;
String resourceName = "Web Application Manifest"; // Can we do it like this?
ManifestResource mre = new ManifestResource
(resourceName,
manifest, ManifestResource.WAR);
appManifestResources.add(mre);
}
} catch (NamingException | NoSuchElementException nex) {
// Application does not contain a MANIFEST.MF file
}
finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Throwable t) {
// Ignore
}
}
}
// Locate the Manifests for all bundled JARs
NamingEnumeration ne = null;
try {
ne = dirContext.listBindings("WEB-INF/lib/");
while ((ne != null) && ne.hasMoreElements()) {
Binding binding = (Binding)ne.nextElement();
if (!binding.getName().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
continue;
}
Object obj =
dirContext.lookup("/WEB-INF/lib/" + binding.getName());
if (!(obj instanceof Resource)) {
// Probably a directory named xxx.jar - ignore it
continue;
}
Resource resource = (Resource) obj;
Manifest jmanifest = getManifest(resource.streamContent());
if (jmanifest != null) {
ManifestResource mre = new ManifestResource(
binding.getName(),
jmanifest,
ManifestResource.APPLICATION);
appManifestResources.add(mre);
}
}
} catch (NamingException nex) {
// Jump out of the check for this application because it
// has no resources
}
return validateManifestResources(appName, appManifestResources);
}
// -------------------------------------------------------- Private Methods
/**
* Validates a ArrayList
of ManifestResource
* objects. This method requires an application name (which is the
* context root of the application at runtime).
*
* false is returned if the extension dependencies
* represented by any given ManifestResource
objects
* is not met.
*
* This method should also provide static validation of a Web Applicaiton
* if provided with the necessary parameters.
*
* @param appName The name of the Application that will appear in the
* error messages
* @param resources A list of ManifestResource
objects
* to be validated.
*
* @return true if manifest resource file requirements are met
*/
private static boolean validateManifestResources(String appName,
ArrayList resources) {
boolean passes = true;
int failureCount = 0;
HashMap availableExtensions = null;
Iterator it = resources.iterator();
while (it.hasNext()) {
ManifestResource mre = it.next();
ArrayList requiredList = mre.getRequiredExtensions();
if (requiredList == null) {
continue;
}
// build the list of available extensions if necessary
if (availableExtensions == null) {
availableExtensions = buildAvailableExtensionsMap(resources);
}
// load the container level resource map if it has not been built
// yet
if (containerAvailableExtensions == null) {
containerAvailableExtensions
= buildAvailableExtensionsMap(containerManifestResources);
}
// iterate through the list of required extensions
Iterator rit = requiredList.iterator();
while (rit.hasNext()) {
Extension requiredExt = (Extension)rit.next();
String extId = requiredExt.getUniqueId();
// check the applicaion itself for the extension
if (availableExtensions != null
&& availableExtensions.containsKey(extId)) {
Extension targetExt = (Extension)
availableExtensions.get(extId);
if (targetExt.isCompatibleWith(requiredExt)) {
requiredExt.setFulfilled(true);
}
// check the container level list for the extension
} else if (containerAvailableExtensions != null
&& containerAvailableExtensions.containsKey(extId)) {
Extension targetExt =
containerAvailableExtensions.get(extId);
if (targetExt.isCompatibleWith(requiredExt)) {
requiredExt.setFulfilled(true);
}
} else {
// Failure
if (log.isLoggable(Level.INFO)) {
log.log(Level.INFO, LogFacade.EXTENSION_NOT_FOUND_INFO,
new Object[] {appName, mre.getResourceName(),
requiredExt.getExtensionName()});
}
passes = false;
failureCount++;
}
}
}
if (!passes) {
if (log.isLoggable(Level.INFO)) {
log.log(Level.INFO, LogFacade.FAILED_FIND_EXTENSION_INFO,
new Object[] {appName, failureCount});
}
}
return passes;
}
/*
* Build this list of available extensions so that we do not have to
* re-build this list every time we iterate through the list of required
* extensions. All available extensions in all of the
* MainfestResource
objects will be added to a
* HashMap
which is returned on the first dependency list
* processing pass.
*
* The key is the name + implementation version.
*
* NOTE: A list is built only if there is a dependency that needs
* to be checked (performance optimization).
*
* @param resources A list of ManifestResource
objects
*
* @return HashMap Map of available extensions
*/
private static HashMap buildAvailableExtensionsMap(
ArrayList resources) {
HashMap availableMap = null;
Iterator it = resources.iterator();
while (it.hasNext()) {
ManifestResource mre = it.next();
HashMap map = mre.getAvailableExtensions();
if (map != null) {
Iterator values = map.values().iterator();
while (values.hasNext()) {
Extension ext = (Extension) values.next();
if (availableMap == null) {
availableMap = new HashMap<>();
availableMap.put(ext.getUniqueId(), ext);
} else if (!availableMap.containsKey(ext.getUniqueId())) {
availableMap.put(ext.getUniqueId(), ext);
}
}
}
List listExtensions = mre.getRequiredExtensions();
if (listExtensions != null) {
for (Extension extension : listExtensions) {
if (availableMap == null) {
availableMap = new HashMap<>();
}
availableMap.put(extension.getUniqueId(), extension);
}
}
}
return availableMap;
}
/**
* Return the Manifest from a jar file or war file
*
* @param inStream Input stream to a WAR or JAR file
* @return The WAR's or JAR's manifest
*/
private static Manifest getManifest(InputStream inStream)
throws IOException {
Manifest manifest = null;
JarInputStream jin = null;
try {
jin = new JarInputStream(inStream);
manifest = jin.getManifest();
jin.close();
jin = null;
} finally {
if (jin != null) {
try {
jin.close();
} catch (Throwable t) {
// Ignore
}
}
}
return manifest;
}
/*
* Checks to see if the given system JAR file contains a MANIFEST, and adds
* it to the container's manifest resources.
*
* @param jarFile The system JAR whose manifest to add
*/
private static void addSystemResource(File jarFile) throws IOException {
try (FileInputStream fio = new FileInputStream(jarFile)) {
Manifest manifest = getManifest(fio);
if (manifest != null) {
ManifestResource mre
= new ManifestResource(jarFile.getAbsolutePath(),
manifest,
ManifestResource.SYSTEM);
containerManifestResources.add(mre);
}
}
}
}