configcontext.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,406 行 · 第 1/3 页

JAVA
1,406
字号
/* * 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 */package com.caucho.config;import com.caucho.config.program.NodeBuilderChildProgram;import com.caucho.config.program.ConfigProgram;import com.caucho.config.types.Validator;import com.caucho.config.type.*;import com.caucho.config.types.*;import com.caucho.config.attribute.*;import com.caucho.el.ELParser;import com.caucho.el.Expr;import com.caucho.loader.*;import com.caucho.util.*;import com.caucho.vfs.*;import com.caucho.webbeans.component.ComponentImpl;import com.caucho.webbeans.context.DependentScope;import com.caucho.webbeans.context.ScopeContext;import com.caucho.xml.*;import org.w3c.dom.*;import javax.el.*;import java.io.Closeable;import java.io.IOException;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;/** * The ConfigContext contains the state of the current configuration. */public class ConfigContext {  private final static L10N L = new L10N(ConfigContext.class);  private final static Logger log    = Logger.getLogger(ConfigContext.class.getName());  private final static QName RESIN_TYPE = new QName("resin:type", null);  private final static QName RESIN_TYPE_NS    = new QName("resin:type", "http://caucho.com/ns/resin/core");  private final static QName RESIN_CLASS = new QName("resin:class", null);  private final static QName RESIN_CLASS_NS    = new QName("resin:class", "http://caucho.com/ns/resin/core");  private final static QName RESIN_PARAM = new QName("resin:param", null);  private final static QName RESIN_PARAM_NS    = new QName("resin:param", "http://caucho.com/ns/resin/core");    private final static QName TEXT = new QName("#text");  private final static QName VALUE = new QName("value");  private final static Object NULL = new Object();  private final static HashSet<QName> _resinClassSet = new HashSet<QName>();  private static ThreadLocal<ConfigContext> _currentBuilder    = new ThreadLocal<ConfigContext>();  private Config _config;  private ArrayList<ValidatorEntry> _validators    = new ArrayList<ValidatorEntry>();  private ConfigELContext _elContext = new ConfigELContext();    private DependentScope _dependentScope;  private ArrayList<Dependency> _dependList;  private Document _dependDocument;  private String _baseUri;  public ConfigContext()  {  }  public ConfigContext(ComponentImpl component,		       Object value,		       ScopeContext scope)  {    this();    _dependentScope = new DependentScope(component, value, scope);  }  public ConfigContext(ScopeContext scope)  {    this();    _dependentScope = new DependentScope(scope);  }    ConfigContext(Config config)  {    _config = config;  }    public static ConfigContext create()  {    ConfigContext env = _currentBuilder.get();        if (env != null)      return env;    else      return new ConfigContext();  }  public static ConfigContext createForProgram()  {    return new ConfigContext();  }  public static ConfigContext getCurrentBuilder()  {    return _currentBuilder.get();  }  public static ConfigContext getCurrent()  {    return _currentBuilder.get();  }  // s/b private?  static void setCurrentBuilder(ConfigContext builder)  {    _currentBuilder.set(builder);  }  /**   * Returns the file var   */  public String getBaseUri()  {    return _baseUri;  }  /**   * WebBeans method   *    * @param aThis   * @param value   */  public void addDestructor(ComponentImpl comp, Object value)  {    if (_dependentScope != null)      _dependentScope.addDestructor(comp, value);    else if (comp instanceof Closeable)      Environment.addCloseListener((Closeable) comp);  }  public boolean canInject(ScopeContext scope)  {    return _dependentScope == null || _dependentScope.canInject(scope);  }  /**   * Returns the component value for the dependent scope   *    * @param aThis   * @return   */  public Object get(ComponentImpl comp)  {    if (_dependentScope != null)      return _dependentScope.get(comp);    else      return null;  }  public Config getConfig()  {    return _config;  }  /**   * WebBeans dependent scope setting   *    * @param aThis   * @param obj   */  public void put(ComponentImpl comp, Object obj)  {    if (_dependentScope == null)      _dependentScope = new DependentScope();    _dependentScope.put(comp, obj);  }  /**   * Returns true if EL expressions are used.   */  private boolean isEL()  {    // server/26b6    return _config == null || _config.isEL();  }  public boolean isIgnoreEnvironment()  {    return _config != null && _config.isIgnoreEnvironment();  }  /**   * External call to configure a bean based on a top-level node.   * The init() and replaceObject() are not called.   *   * @param bean the object to be configured.   */  public Object configure(Object bean, Node top)    throws LineConfigException  {    if (bean == null)      throw new NullPointerException(L.l("unexpected null bean at node '{0}'", top));        ConfigContext oldBuilder = _currentBuilder.get();    try {      _currentBuilder.set(this);      ConfigType type = TypeFactory.getType(bean.getClass());      configureBean(bean, top);      type.init(bean);      return type.replaceObject(bean);    } catch (LineConfigException e) {      throw e;    } catch (Exception e) {      throw error(e, top);    } finally {      _currentBuilder.set(oldBuilder);    }  }  /**   * External call to configure a bean based on a top-level node, calling   * init() and replaceObject() when done.   *   * @param bean the bean to be configured   * @param top the top-level XML configuration node   * @return the configured object, or the factory generated object   */  public void configureBean(Object bean, Node top)    throws LineConfigException  {    ConfigContext oldBuilder = _currentBuilder.get();    String oldFile = _baseUri;    ArrayList<Dependency> oldDependList = _dependList;    try {      _currentBuilder.set(this);      if (top instanceof QNode) {        QNode qNode = (QNode) top;        	_baseUri = qNode.getBaseURI();      }      _dependList = getDependencyList(top);      ConfigType type = TypeFactory.getType(bean.getClass());      configureNode(top, bean, type);    } finally {      _currentBuilder.set(oldBuilder);      _dependList = oldDependList;      _baseUri = oldFile;    }  }  /**   * External call to configure a bean's attribute.   *   * @param bean the bean to be configured   * @param attribute the node representing the configured attribute   * @throws LineConfigException   */  public void configureAttribute(Object bean, Node attribute)    throws LineConfigException  {    String attrName = attribute.getNodeName();    if (attrName.equals("resin:type"))      return;    else if (attrName.startsWith("xmlns"))      return;    String oldFile = _baseUri;    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    ConfigContext oldBuilder = getCurrentBuilder();    try {      setCurrentBuilder(this);      _baseUri = attribute.getBaseURI();            ConfigType type = TypeFactory.getType(bean.getClass());      QName qName = ((QAbstractNode) attribute).getQName();            type.beforeConfigure(this, bean, attribute);      configureChildNode(attribute, qName, bean, type, false);            type.afterConfigure(this, bean);    }    catch (LineConfigException e) {      throw e;    }    catch (Exception e) {      throw error(e, attribute);    } finally {      _baseUri = oldFile;      setCurrentBuilder(oldBuilder);      thread.setContextClassLoader(oldLoader);    }  }  /**   * Configures a bean, calling its init() and replaceObject() methods.   *   * @param typeStrategy the strategy for handling the bean's type   * @param bean the bean instance   * @param top the configuration top   * @return the configured bean, possibly the replaced object   * @throws LineConfigException   */  private Object configureNode(Node node,                               Object bean,                               ConfigType type)    throws LineConfigException  {    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();        try {      type.beforeConfigure(this, bean, node);      type.beforeConfigureBean(this, bean, node);            configureNodeAttributes(node, bean, type);      for (Node childNode = node.getFirstChild();           childNode != null;           childNode = childNode.getNextSibling()) {        QName qName = ((QAbstractNode) childNode).getQName();                configureChildNode(childNode, qName, bean, type, false);      }      type.afterConfigure(this, bean);    } catch (LineConfigException e) {      throw e;    } catch (Exception e) {      throw error(e, node);    } finally {      thread.setContextClassLoader(oldLoader);    }    return bean;  }  /**   * Configures a bean, calling its init() and replaceObject() methods.   *   * @param typeStrategy the strategy for handling the bean's type   * @param bean the bean instance   * @param top the configuration top   * @return the configured bean, possibly the replaced object   * @throws LineConfigException   */  private void configureNodeAttributes(Node node,                                       Object bean,                                       ConfigType type)    throws Exception  {    if (node instanceof QAttributedNode) {      Node child = ((QAttributedNode) node).getFirstAttribute();      for (; child != null; child = child.getNextSibling()) {        Attr attr = (Attr) child;        QName qName = ((QNode) attr).getQName();                configureChildNode(attr, qName, bean, type, false);      }    }    else {      NamedNodeMap attrList = node.getAttributes();      if (attrList != null) {        int length = attrList.getLength();        for (int i = 0; i < length; i++) {          Attr attr = (Attr) attrList.item(i);          QName qName = ((QNode) attr).getQName();          configureChildNode(attr, qName, bean, type, false);        }      }    }  }    private void configureChildNode(Node childNode,                                  QName qName,                                  Object bean,                                  ConfigType type,				  boolean allowParam)    throws Exception  {    if (qName.getName().startsWith("xmlns")	|| ! allowParam && _resinClassSet.contains(qName)) {      return;    }    Attribute attrStrategy;    try {      attrStrategy = type.getAttribute(qName);      if (attrStrategy == null)	attrStrategy = type.getProgramAttribute();      if (attrStrategy == null) {	// ioc/2252 - flow attributes are not captured by ContentProgram		attrStrategy = type.getContentProgramAttribute();	Attribute envStrategy	  = TypeFactory.getFactory().getEnvironmentAttribute(qName);	if (envStrategy instanceof FlowAttribute)	  attrStrategy = null;      }      if (attrStrategy == null)	attrStrategy = TypeFactory.getFactory().getEnvironmentAttribute(qName);      if (attrStrategy == null) {	if (childNode instanceof Element || childNode instanceof Attr) {	  throw error(L.l("'{0}' is an unknown property of '{1}'.",			  qName.getName(), type.getTypeName()),		      childNode);	}	return;

⌨️ 快捷键说明

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