config.java

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

JAVA
803
字号
/* * 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.config;import com.caucho.config.attribute.*;import com.caucho.config.type.*;import com.caucho.config.types.*;import com.caucho.el.EL;import com.caucho.el.EnvironmentContext;import com.caucho.relaxng.*;import com.caucho.util.*;import com.caucho.vfs.*;import com.caucho.webbeans.manager.WebBeansContainer;import com.caucho.xml.DOMBuilder;import com.caucho.xml.QDocument;import com.caucho.xml.QName;import com.caucho.xml.QAttr;import com.caucho.xml.Xml;import org.w3c.dom.Node;import org.xml.sax.InputSource;import javax.el.ELContext;import javax.el.ELException;import java.io.IOException;import java.io.InputStream;import java.lang.ref.SoftReference;import java.lang.reflect.*;import java.net.*;import java.util.HashMap;import java.util.logging.*;/** * Facade for Resin's configuration builder. */public class Config {  private static final L10N L = new L10N(Config.class);  private static final Logger log    = Logger.getLogger(Config.class.getName());    private HashMap<String,Object> _vars    = new HashMap<String,Object>();    // the context class loader of the config  private ClassLoader _classLoader;  private boolean _isEL = true;  private boolean _isIgnoreEnvironment;  private boolean _allowResinInclude;  public Config()  {    this(Thread.currentThread().getContextClassLoader());  }  /**   * @param loader the class loader environment to use.   */  public Config(ClassLoader loader)  {    _classLoader = loader; }  /**   * Set true if resin:include should be allowed.   */  public void setResinInclude(boolean useResinInclude)  {    _allowResinInclude = useResinInclude;  }  /**   * True if EL expressions are allowed   */  public boolean isEL()  {    return _isEL;  }  /**   * True if EL expressions are allowed   */  public void setEL(boolean isEL)  {    _isEL = isEL;  }  /**   * True if environment tags are ignored   */  public boolean isIgnoreEnvironment()  {    return _isIgnoreEnvironment;  }  /**   * True if environment tags are ignored   */  public void setIgnoreEnvironment(boolean isIgnore)  {    _isIgnoreEnvironment = isIgnore;  }  /**   * Configures a bean with a configuration file.   */  public Object configure(Object obj, Path path)    throws ConfigException, IOException  {    try {      QDocument doc = parseDocument(path, null);      return configure(obj, doc.getDocumentElement());    } catch (RuntimeException e) {      throw e;    } catch (IOException e) {      throw e;    } catch (Exception e) {      throw ConfigException.create(e);    }  }  /**   * Configures a bean with a configuration file.   */  public Object configure(Object obj, InputStream is)    throws Exception  {    QDocument doc = parseDocument(is, null);        return configure(obj, doc.getDocumentElement());  }  /**   * Configures a bean with a configuration file and schema.   */  public Object configure(Object obj, Path path, String schemaLocation)    throws ConfigException  {    try {      Schema schema = findCompactSchema(schemaLocation);      QDocument doc = parseDocument(path, schema);      return configure(obj, doc.getDocumentElement());    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw LineConfigException.create(e);    }  }  /**   * Configures a bean with a configuration file and schema.   */  public Object configure(Object obj, Path path, Schema schema)    throws ConfigException  {    try {      QDocument doc = parseDocument(path, schema);      return configure(obj, doc.getDocumentElement());    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw ConfigException.create(e);    }  }  /**   * Configures a bean with a configuration file.   */  public Object configure(Object obj,			  InputStream is,			  String schemaLocation)    throws Exception  {    Schema schema = findCompactSchema(schemaLocation);    QDocument doc = parseDocument(is, schema);    return configure(obj, doc.getDocumentElement());  }  /**   * Configures a bean with a configuration file.   */  public Object configure(Object obj,			  InputStream is,			  Schema schema)    throws Exception  {    QDocument doc = parseDocument(is, schema);    return configure(obj, doc.getDocumentElement());  }  /**   * Configures a bean with a DOM.   */  public Object configure(Object obj, Node topNode)    throws Exception  {    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      ConfigContext builder = createBuilder();      WebBeansContainer webBeans = WebBeansContainer.create();      if (webBeans != null && webBeans.findByName("__FILE__") == null)	webBeans.addSingleton(FileVar.__FILE__, "__FILE__");      if (webBeans != null && webBeans.findByName("__DIR__") == null)	webBeans.addSingleton(DirVar.__DIR__, "__DIR__");            return builder.configure(obj, topNode);    } finally {      thread.setContextClassLoader(oldLoader);    }  }  /**   * Configures a bean with a configuration file and schema.   */  public void configureBean(Object obj,			    Path path,			    String schemaLocation)    throws Exception  {    Schema schema = findCompactSchema(schemaLocation);    QDocument doc = parseDocument(path, schema);    configureBean(obj, doc.getDocumentElement());  }  /**   * Configures a bean with a configuration file and schema.   */  public void configureBean(Object obj, Path path)    throws Exception  {    QDocument doc = parseDocument(path, null);    configureBean(obj, doc.getDocumentElement());  }  /**   * Configures a bean with a DOM.  configureBean does not   * apply init() or replaceObject().   */  public void configureBean(Object obj, Node topNode)    throws Exception  {    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      ConfigContext builder = createBuilder();      WebBeansContainer webBeans = WebBeansContainer.create();            if (webBeans != null && webBeans.findByName("__FILE__") == null)	webBeans.addSingleton(FileVar.__FILE__, "__FILE__");      if (webBeans != null && webBeans.findByName("__DIR__") == null)	webBeans.addSingleton(DirVar.__DIR__, "__DIR__");      builder.configureBean(obj, topNode);    } finally {      thread.setContextClassLoader(oldLoader);    }  }  private ConfigContext createBuilder()  {    return new ConfigContext(this);  }  /**   * Configures a bean with a configuration file and schema.   */  public void configureBean(Object obj,			    Path path,			    Schema schema)    throws Exception  {    QDocument doc = parseDocument(path, schema);    configureBean(obj, doc.getDocumentElement());  }    /**   * Configures the bean from a path   */  private QDocument parseDocument(Path path, Schema schema)    throws LineConfigException, IOException, org.xml.sax.SAXException  {    // server/2d33    SoftReference<QDocument> docRef = null;//_parseCache.get(path);    QDocument doc;    if (docRef != null) {      doc = docRef.get();      if (doc != null && ! doc.isModified())	return doc;    }        ReadStream is = path.openRead();    try {      doc = parseDocument(is, schema);      // _parseCache.put(path, new SoftReference<QDocument>(doc));      return doc;    } finally {      is.close();    }  }    /**   * Configures the bean from an input stream.   */  private QDocument parseDocument(InputStream is, Schema schema)    throws LineConfigException,	   IOException,	   org.xml.sax.SAXException  {    QDocument doc = new QDocument();    DOMBuilder builder = new DOMBuilder();    builder.init(doc);    String systemId = null;    String filename = null;    if (is instanceof ReadStream) {      systemId = ((ReadStream) is).getPath().getURL();      filename = ((ReadStream) is).getPath().getUserPath();    }    doc.setSystemId(systemId);    builder.setSystemId(systemId);    doc.setRootFilename(filename);    builder.setFilename(filename);    builder.setSkipWhitespace(true);    InputSource in = new InputSource();    in.setByteStream(is);    in.setSystemId(systemId);    Xml xml = new Xml();    xml.setOwner(doc);    xml.setResinInclude(_allowResinInclude);    xml.setFilename(filename);    if (schema != null) {      Verifier verifier = schema.newVerifier();      VerifierFilter filter = verifier.getVerifierFilter();      filter.setParent(xml);      filter.setContentHandler(builder);      filter.setErrorHandler(builder);      filter.parse(in);    }    else {      xml.setContentHandler(builder);      xml.parse(in);    }

⌨️ 快捷键说明

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