📄 tldconfig.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.catalina.startup;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.servlet.ServletException;
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.util.StringManager;
import org.apache.tomcat.util.digester.Digester;
import org.xml.sax.InputSource;
/**
* Startup event listener for a <b>Context</b> that configures the properties
* of that Context, and the associated defined servlets.
*
* @author Craig R. McClanahan
* @author Jean-Francois Arcand
* @author Costin Manolache
*/
public final class TldConfig {
// Names of JARs that are known not to contain any TLDs
private static HashSet<String> noTldJars;
private static org.apache.juli.logging.Log log=
org.apache.juli.logging.LogFactory.getLog( TldConfig.class );
/*
* Initializes the set of JARs that are known not to contain any TLDs
*/
static {
noTldJars = new HashSet<String>();
// Bootstrap JARs
noTldJars.add("bootstrap.jar");
noTldJars.add("commons-daemon.jar");
noTldJars.add("tomcat-juli.jar");
// Main JARs
noTldJars.add("annotations-api.jar");
noTldJars.add("catalina.jar");
noTldJars.add("catalina-ant.jar");
noTldJars.add("catalina-ha.jar");
noTldJars.add("catalina-tribes.jar");
noTldJars.add("el-api.jar");
noTldJars.add("jasper.jar");
noTldJars.add("jasper-el.jar");
noTldJars.add("jasper-jdt.jar");
noTldJars.add("jsp-api.jar");
noTldJars.add("servlet-api.jar");
noTldJars.add("tomcat-coyote.jar");
noTldJars.add("tomcat-dbcp.jar");
// i18n JARs
noTldJars.add("tomcat-i18n-en.jar");
noTldJars.add("tomcat-i18n-es.jar");
noTldJars.add("tomcat-i18n-fr.jar");
noTldJars.add("tomcat-i18n-ja.jar");
// Misc JARs not included with Tomcat
noTldJars.add("ant.jar");
noTldJars.add("commons-dbcp.jar");
noTldJars.add("commons-beanutils.jar");
noTldJars.add("commons-fileupload-1.0.jar");
noTldJars.add("commons-pool.jar");
noTldJars.add("commons-digester.jar");
noTldJars.add("commons-logging.jar");
noTldJars.add("commons-collections.jar");
noTldJars.add("jmx.jar");
noTldJars.add("jmx-tools.jar");
noTldJars.add("xercesImpl.jar");
noTldJars.add("xmlParserAPIs.jar");
noTldJars.add("xml-apis.jar");
// JARs from J2SE runtime
noTldJars.add("sunjce_provider.jar");
noTldJars.add("ldapsec.jar");
noTldJars.add("localedata.jar");
noTldJars.add("dnsns.jar");
noTldJars.add("tools.jar");
noTldJars.add("sunpkcs11.jar");
}
// ----------------------------------------------------- Instance Variables
/**
* The Context we are associated with.
*/
private Context context = null;
/**
* The string resources for this package.
*/
private static final StringManager sm =
StringManager.getManager(Constants.Package);
/**
* The <code>Digester</code> we will use to process tag library
* descriptor files.
*/
private static Digester tldDigester = null;
/**
* Attribute value used to turn on/off TLD validation
*/
private static boolean tldValidation = false;
/**
* Attribute value used to turn on/off TLD namespace awarenes.
*/
private static boolean tldNamespaceAware = false;
private boolean rescan=true;
private ArrayList<String> listeners = new ArrayList<String>();
// --------------------------------------------------------- Public Methods
/**
* Sets the list of JARs that are known not to contain any TLDs.
*
* @param jarNames List of comma-separated names of JAR files that are
* known not to contain any TLDs
*/
public static void setNoTldJars(String jarNames) {
if (jarNames != null) {
noTldJars.clear();
StringTokenizer tokenizer = new StringTokenizer(jarNames, ",");
while (tokenizer.hasMoreElements()) {
noTldJars.add(tokenizer.nextToken());
}
}
}
/**
* Set the validation feature of the XML parser used when
* parsing xml instances.
* @param tldValidation true to enable xml instance validation
*/
public void setTldValidation(boolean tldValidation){
TldConfig.tldValidation = tldValidation;
}
/**
* Get the server.xml <host> attribute's xmlValidation.
* @return true if validation is enabled.
*
*/
public boolean getTldValidation(){
return tldValidation;
}
/**
* Get the server.xml <host> attribute's xmlNamespaceAware.
* @return true if namespace awarenes is enabled.
*
*/
public boolean getTldNamespaceAware(){
return tldNamespaceAware;
}
/**
* Set the namespace aware feature of the XML parser used when
* parsing xml instances.
* @param tldNamespaceAware true to enable namespace awareness
*/
public void setTldNamespaceAware(boolean tldNamespaceAware){
TldConfig.tldNamespaceAware = tldNamespaceAware;
}
public boolean isRescan() {
return rescan;
}
public void setRescan(boolean rescan) {
this.rescan = rescan;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public void addApplicationListener( String s ) {
//if(log.isDebugEnabled())
log.debug( "Add tld listener " + s);
listeners.add(s);
}
public String[] getTldListeners() {
String result[]=new String[listeners.size()];
listeners.toArray(result);
return result;
}
/**
* Scan for and configure all tag library descriptors found in this
* web application.
*
* @exception Exception if a fatal input/output or parsing error occurs
*/
public void execute() throws Exception {
long t1=System.currentTimeMillis();
File tldCache=null;
if (context instanceof StandardContext) {
File workDir= (File)
((StandardContext)context).getServletContext().getAttribute(Globals.WORK_DIR_ATTR);
//tldCache=new File( workDir, "tldCache.ser");
}
// Option to not rescan
if( ! rescan ) {
// find the cache
if( tldCache!= null && tldCache.exists()) {
// just read it...
processCache(tldCache);
return;
}
}
/*
* Acquire the list of TLD resource paths, possibly embedded in JAR
* files, to be processed
*/
Set resourcePaths = tldScanResourcePaths();
Map jarPaths = getJarPaths();
// Check to see if we can use cached listeners
if (tldCache != null && tldCache.exists()) {
long lastModified = getLastModified(resourcePaths, jarPaths);
if (lastModified < tldCache.lastModified()) {
processCache(tldCache);
return;
}
}
// Scan each accumulated resource path for TLDs to be processed
Iterator paths = resourcePaths.iterator();
while (paths.hasNext()) {
String path = (String) paths.next();
if (path.endsWith(".jar")) {
tldScanJar(path);
} else {
tldScanTld(path);
}
}
if (jarPaths != null) {
paths = jarPaths.values().iterator();
while (paths.hasNext()) {
tldScanJar((File) paths.next());
}
}
String list[] = getTldListeners();
if( tldCache!= null ) {
log.debug( "Saving tld cache: " + tldCache + " " + list.length);
try {
FileOutputStream out=new FileOutputStream(tldCache);
ObjectOutputStream oos=new ObjectOutputStream( out );
oos.writeObject( list );
oos.close();
} catch( IOException ex ) {
ex.printStackTrace();
}
}
if( log.isDebugEnabled() )
log.debug( "Adding tld listeners:" + list.length);
for( int i=0; list!=null && i<list.length; i++ ) {
context.addApplicationListener(list[i]);
}
long t2=System.currentTimeMillis();
if( context instanceof StandardContext ) {
((StandardContext)context).setTldScanTime(t2-t1);
}
}
// -------------------------------------------------------- Private Methods
/*
* Returns the last modification date of the given sets of resources.
*
* @param resourcePaths
* @param jarPaths
*
* @return Last modification date
*/
private long getLastModified(Set resourcePaths, Map jarPaths)
throws Exception {
long lastModified = 0;
Iterator paths = resourcePaths.iterator();
while (paths.hasNext()) {
String path = (String) paths.next();
URL url = context.getServletContext().getResource(path);
if (url == null) {
log.debug( "Null url "+ path );
break;
}
long lastM = url.openConnection().getLastModified();
if (lastM > lastModified) lastModified = lastM;
if (log.isDebugEnabled()) {
log.debug( "Last modified " + path + " " + lastM);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -