tldmanager.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 868 行 · 第 1/2 页

JAVA
868
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.jsp;import com.caucho.config.Config;import com.caucho.config.ConfigException;import com.caucho.config.types.FileSetType;import com.caucho.jsp.cfg.*;import com.caucho.loader.DynamicClassLoader;import com.caucho.loader.EnvironmentLocal;import com.caucho.log.Log;import com.caucho.server.util.CauchoSystem;import com.caucho.server.webapp.WebApp;import com.caucho.util.Alarm;import com.caucho.util.L10N;import com.caucho.vfs.*;import com.caucho.jsf.cfg.JsfPropertyGroup;import java.io.IOException;import java.io.InputStream;import java.lang.ref.SoftReference;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.jar.JarFile;import java.util.logging.Level;import java.util.logging.Logger;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;/** * Stores the parsed tlds. */public class TldManager {  static final L10N L = new L10N(TldManager.class);  private static final Logger log = Log.open(TldManager.class);  private static ArrayList<TldPreload> _cauchoTaglibs;  private static ArrayList<TldPreload> _globalTaglibs;  private static ArrayList<Path> _globalPaths;    private static EnvironmentLocal<TldManager> _localManager    = new EnvironmentLocal<TldManager>();    private JspResourceManager _resourceManager;  private WebApp _webApp;    private HashMap<Path,SoftReference<TldTaglib>> _tldMap    = new HashMap<Path,SoftReference<TldTaglib>>();    private JspParseException _loadAllTldException;  private String _tldDir;  private FileSetType _tldFileSet;  private boolean _isFastJsf = true;  private volatile boolean _isInit;  private Config _config = new Config();  private ArrayList<TldPreload> _preloadTaglibs;  private TldManager(JspResourceManager resourceManager,		     WebApp app)    throws JspParseException, IOException  {    _resourceManager = resourceManager;    _webApp = app;    if (app != null) {      JspPropertyGroup jsp = app.getJsp();      if (jsp != null)	_tldFileSet = jsp.getTldFileSet();      JsfPropertyGroup jsf = app.getJsf();      if (jsf != null)        _isFastJsf = jsf.isFastJsf();    }    // JSF has a global listener hidden in one of the *.tld which    // requires Resin to search all the JSPs.    initGlobal();  }  static TldManager create(JspResourceManager resourceManager,			   WebApp app)    throws JspParseException, IOException  {        TldManager manager = null;        synchronized (_localManager) {      manager = _localManager.getLevel();      if (manager != null)	return manager;            manager = new TldManager(resourceManager, app);      _localManager.set(manager);    }    return manager;  }  /**   * Sets the webApp.   */  void setWebApp(WebApp webApp)  {    _webApp = webApp;  }  public String getSchema()  {    return "com/caucho/jsp/cfg/jsp-tld.rnc";  }      public void setTldDir(String tldDir)  {    _tldDir = tldDir;  }      public void setTldFileSet(FileSetType tldFileSet)  {    _tldFileSet = tldFileSet;  }  /**   * Loads all the .tld files in the WEB-INF and the META-INF for   * the entire classpath.   */  public synchronized void init()    throws JspParseException, IOException  {    if (_isInit)      return;    _isInit = true;    log.fine("Loading .tld files");        String dir;    if (_tldDir == null)      dir = "WEB-INF";    else if (_tldDir.startsWith("/"))      dir = _tldDir.substring(1);    else if (_tldDir.startsWith("WEB-INF"))      dir = _tldDir;    else      dir = "WEB-INF/" + _tldDir;          FileSetType fileSet = _tldFileSet;    if (fileSet == null) {      fileSet = new FileSetType();      fileSet.setDir(_resourceManager.resolvePath(dir));      try {	fileSet.init();      } catch (Exception e) {	log.config(e.toString());      }    }          ArrayList<TldPreload> taglibs = new ArrayList<TldPreload>();        taglibs.addAll(_globalTaglibs);    ArrayList<Path> paths = getClassPath();    for (int i = 0; i < paths.size(); i++) {      Path subPath = paths.get(i);      if (_globalPaths.contains(subPath))	continue;      if (subPath instanceof JarPath)	loadJarTlds(taglibs, ((JarPath) subPath).getContainer(), "META-INF");      else if (subPath.getPath().endsWith(".jar")) {	loadJarTlds(taglibs, subPath, "META-INF");      }      else	loadAllTlds(taglibs, subPath.lookup("META-INF"), 64, "META-INF");    }        if (fileSet != null)      loadAllTlds(taglibs, fileSet);    /*    for (int i = 0; i < taglibs.size(); i++) {      TldTaglib taglib = taglibs.get(i);      if (taglib.getConfigException() != null &&	  taglib.getURI() == null) {	_loadAllTldException = JspParseException.create(taglib.getConfigException());      }    }    */    taglibs.addAll(_cauchoTaglibs);    _preloadTaglibs = taglibs;    for (int i = 0; i < taglibs.size(); i++) {      try {	taglibs.get(i).initListeners(_webApp);      } catch (Exception e) {	throw new JspParseException(e);      }    }  }  public synchronized void initGlobal()  {    // loads tag libraries from the global context (so there's no    // need to reparse the jars for each web-app    if (_globalTaglibs == null) {      if (! Alarm.isTest()) {	log.info("Loading .tld files from global classpath");      }            ArrayList<TldPreload> globalTaglibs = new ArrayList<TldPreload>();      ArrayList<TldPreload> cauchoTaglibs = new ArrayList<TldPreload>();      Thread thread = Thread.currentThread();      ClassLoader oldLoader = thread.getContextClassLoader();      ClassLoader globalLoader = TldManager.class.getClassLoader();      thread.setContextClassLoader(globalLoader);      try {	ArrayList<Path> paths = getClassPath(globalLoader);	_globalPaths = paths;	loadClassPathTlds(globalTaglibs, paths, "");	for (int i = globalTaglibs.size() - 1; i >= 0; i--) {	  TldPreload tld = globalTaglibs.get(i);	  if (tld.getPath() == null || tld.getPath().getPath() == null)	    continue;	  	  String tldPathName = tld.getPath().getPath();	  if (tldPathName.startsWith("/com/caucho")) {	    cauchoTaglibs.add(globalTaglibs.remove(i));	  }	}      } catch (Exception e) {	log.log(Level.WARNING, e.toString(), e);      } finally {	thread.setContextClassLoader(oldLoader);      }      _globalTaglibs = globalTaglibs;      _cauchoTaglibs = cauchoTaglibs;    }  }  private void loadClassPathTlds(ArrayList<TldPreload> taglibs,				 ArrayList<Path> paths,				 String prefix)    throws JspParseException, IOException  {    for (int i = 0; i < paths.size(); i++) {      Path subPath = paths.get(i);      if (subPath.getPath().endsWith(".jar")) {	loadJarTlds(taglibs, subPath, prefix);      }      else if (prefix != null && ! prefix.equals(""))	loadAllTlds(taglibs, subPath.lookup(prefix), 64, prefix);      else	loadAllTlds(taglibs, subPath.lookup("META-INF"), 64, "META-INF");    }  }  /*  ArrayList<TldTaglib> getTaglibs()  {    return new ArrayList<TldTaglib>(_preloadTaglibs);  }  */  private void loadAllTlds(ArrayList<TldPreload> taglibs,			   FileSetType fileSet)    throws JspParseException, IOException  {    for (Path path : fileSet.getPaths()) {      if (path.getPath().startsWith(".")) {      }      else if ((path.getPath().endsWith(".tld")		|| path.getPath().endsWith(".ftld"))	       && path.isFile() && path.canRead()) {	try {	  TldPreload taglib = parseTldPreload(path);	  taglibs.add(taglib);	  if (taglib.getURI() == null &&	      taglib.getConfigException() != null &&	      _loadAllTldException == null)	    _loadAllTldException = new JspLineParseException(taglib.getConfigException());	} catch (Exception e) {	  log.warning(e.getMessage());	}      }    }  }    private void loadAllTlds(ArrayList<TldPreload> taglibs,			   Path path, int depth, String userPath)    throws JspParseException, IOException  {    if (depth < 0)      throw new JspParseException(L.l("max depth exceeded while reading .tld files.  Probable loop in filesystem detected at `{0}'.", path));    path.setUserPath(userPath);        if (path.getPath().startsWith(".")) {    }    else if ((path.getPath().endsWith(".tld")	      || path.getPath().endsWith(".ftld"))	     && path.isFile() && path.canRead()) {      try {	TldPreload taglib = parseTldPreload(path);	taglibs.add(taglib);	if (taglib.getURI() == null &&	    taglib.getConfigException() != null &&	    _loadAllTldException == null)	  _loadAllTldException = new JspLineParseException(taglib.getConfigException());      } catch (Exception e) {	/*	if (_loadAllTldException == null) {	}	else if (e instanceof JspParseException)	  _loadAllTldException = (JspParseException) e;	else	  _loadAllTldException = new JspParseException(e);	*/		log.warning(e.getMessage());      }    }    else if (path.isDirectory()) {      String []fileNames = path.list();            for (int i = 0; fileNames != null && i < fileNames.length; i++) {        String name = fileNames[i];        ArrayList<Path> resources = path.getResources(name);        for (int j = 0; resources != null && j < resources.size(); j++) {          Path subpath = resources.get(j);          loadAllTlds(taglibs, subpath, depth - 1, userPath + "/" + name);        }      }    }  }  private void loadJarTlds(ArrayList<TldPreload> taglibs,			   Path jarBacking,			   String prefix)    throws JspParseException, IOException  {    if (! jarBacking.canRead())      return;        String nativePath = jarBacking.getNativePath();    ZipFile zipFile;    JarPath jar = JarPath.create(jarBacking);    if (nativePath.endsWith(".jar"))      zipFile = new JarFile(nativePath);    else      zipFile = new ZipFile(nativePath);    ArrayList<Path> tldPaths = new ArrayList<Path>();    boolean isValidScan = false;    ZipScanner scan = null;    try {      if (true)	scan = new ZipScanner(jarBacking);	      if (scan != null && scan.open()) {	while (scan.next()) {	  String name = scan.getName();	  if (name.startsWith(prefix)	      && name.endsWith(".tld") || name.endsWith(".ftld")) {	    tldPaths.add(jar.lookup(name));	  }	}	isValidScan = true;      }    } catch (Exception e) {      log.log(Level.INFO, e.toString(), e);    }    if (! isValidScan) {      try {	Enumeration<? extends ZipEntry> en = zipFile.entries();	while (en.hasMoreElements()) {	  ZipEntry entry = en.nextElement();	  String name = entry.getName();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?