📄 extensionvalidator.java
字号:
}
}
// Locate the Manifests for all bundled JARs
NamingEnumeration ne = null;
try {
if (dirContext != null) {
ne = dirContext.listBindings("WEB-INF/lib/");
}
while ((ne != null) && ne.hasMoreElements()) {
Binding binding = (Binding)ne.nextElement();
if (!binding.getName().toLowerCase().endsWith(".jar")) {
continue;
}
Resource resource = (Resource)dirContext.lookup
("/WEB-INF/lib/" + binding.getName());
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);
}
/**
* Return an instance of the ExtensionValidator.
* The ExtensionValidator is a singleton.
*/
public static ExtensionValidator getInstance() throws IOException {
if (validator == null) {
validator = new ExtensionValidator();
}
return validator;
}
// -------------------------------------------------------- Private Methods
/**
* Validates a <code>ArrayList</code> of <code>ManifestResource</code>
* objects. This method requires an application name (which is the
* context root of the application at runtime).
*
* <code>false</false> is returned if the extension dependencies
* represented by any given <code>ManifestResource</code> 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 <code>ManifestResource</code> 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 = (ManifestResource)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 = (Extension)
containerAvailableExtensions.get(extId);
if (targetExt.isCompatibleWith(requiredExt)) {
requiredExt.setFulfilled(true);
}
} else {
// Failure
log.info(sm.getString(
"extensionValidator.extension-not-found-error",
appName, mre.getResourceName(),
requiredExt.getExtensionName()));
passes = false;
failureCount++;
}
}
}
if (!passes) {
log.info(sm.getString(
"extensionValidator.extension-validation-error", 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
* <code>MainfestResource</code> objects will be added to a
* <code>HashMap</code> 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 <code>ManifestResource</code> 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 = (ManifestResource)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);
}
}
}
}
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 {
Manifest manifest = getManifest(new FileInputStream(jarFile));
if (manifest != null) {
ManifestResource mre
= new ManifestResource(jarFile.getAbsolutePath(),
manifest,
ManifestResource.SYSTEM);
if (containerManifestResources == null) {
containerManifestResources = new ArrayList();
}
containerManifestResources.add(mre);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -