beantype.java

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

JAVA
678
字号
/* * 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.type;import java.beans.*;import java.lang.reflect.*;import java.util.*;import java.util.logging.*;import com.caucho.config.program.ConfigProgram;import com.caucho.config.program.PropertyStringProgram;import com.caucho.config.*;import com.caucho.config.attribute.*;import com.caucho.config.j2ee.*;import com.caucho.config.program.ConfigProgram;import com.caucho.config.types.*;import com.caucho.util.*;import com.caucho.xml.*;import com.caucho.vfs.*;import com.caucho.webbeans.component.*;import com.caucho.webbeans.manager.*;import org.w3c.dom.*;/** * Represents an introspected bean type for configuration. */public class BeanType extends ConfigType{  private static final L10N L = new L10N(BeanType.class);  private static final Logger log    = Logger.getLogger(BeanType.class.getName());  private static final QName TEXT = new QName("#text");  private static final Object _introspectLock = new Object();  private final Class _beanClass;    private HashMap<QName,Attribute> _nsAttributeMap    = new HashMap<QName,Attribute>();    private HashMap<String,Attribute> _attributeMap    = new HashMap<String,Attribute>();  /*  private HashMap<String,Method> _createMap    = new HashMap<String,Method>();  */  private Constructor _stringConstructor;    private Method _valueOf;  private Method _setParent;  private Method _replaceObject;  private Method _setConfigLocation;    private Attribute _addText;  private Attribute _addProgram;  private Attribute _addContentProgram;  private Attribute _setProperty;    private ComponentImpl _component;  private ArrayList<ConfigProgram> _injectList    = new ArrayList<ConfigProgram>();  private ArrayList<ConfigProgram> _initList = new ArrayList<ConfigProgram>();  private boolean _isIntrospecting;  private boolean _isIntrospected;  private boolean _isIntrospectComplete;  private ArrayList<BeanType> _pendingChildList = new ArrayList<BeanType>();  public BeanType(Class beanClass)  {    _beanClass = beanClass;  }  /**   * Returns the given type.   */  public Class getType()  {    return _beanClass;  }  /**   * Creates a new instance   */  @Override  public Object create(Object parent)  {    try {      if (_component == null) {	if (_beanClass.isInterface())	  throw new ConfigException(L.l("{0} cannot be instantiated because it is an interface",					_beanClass.getName()));	WebBeansContainer webBeans	  = WebBeansContainer.create(_beanClass.getClassLoader());	_component = (ComponentImpl) webBeans.createTransient(_beanClass);      }      Object bean = _component.createNoInit();      if (_setParent != null	  && parent != null	  && _setParent.getParameterTypes()[0].isAssignableFrom(parent.getClass())) {	try {	  _setParent.invoke(bean, parent);	} catch (IllegalArgumentException e) {	  throw ConfigException.create(_setParent,				       L.l("{0}: setParent value of '{1}' is not valid",					   bean, parent),				       e);	} catch (Exception e) {	  throw ConfigException.create(_setParent, e);	}      }      return bean;    } catch (Exception e) {      throw ConfigException.create(e);    }  }  /**   * Called before the children are configured.   */  @Override  public void beforeConfigure(ConfigContext env, Object bean, Node node)  {    super.beforeConfigure(env, bean, node);    if (_setConfigLocation != null && node instanceof QNode) {      String filename = ((QNode) node).getFilename();      int line = ((QNode) node).getLine();      try {	_setConfigLocation.invoke(bean, filename, line);      } catch (Exception e) {	throw ConfigException.create(e);      }    }    if (bean instanceof DependencyBean) {      DependencyBean dependencyBean = (DependencyBean) bean;            ArrayList<Dependency> dependencyList = env.getDependencyList();      if (dependencyList != null) {	for (Dependency depend : dependencyList) {	  dependencyBean.addDependency((PersistentDependency) depend);	}      }    }  }  /**   * Returns the attribute based on the given name.   */  @Override  public Attribute getAttribute(QName name)  {    synchronized (_nsAttributeMap) {      Attribute attr = _nsAttributeMap.get(name);      if (attr == null) {	attr = _attributeMap.get(name.getLocalName());	if (attr != null)	  _nsAttributeMap.put(name, attr);      }      if (attr != null)	return attr;    }        return null;  }  /**   * Returns the program attribute.   */  @Override  public Attribute getProgramAttribute()  {    if (_setProperty != null)      return _setProperty;    else      return _addProgram;  }  /**   * Returns the content program attribute (program excluding if, choose).   */  @Override  public Attribute getContentProgramAttribute()  {    return _addContentProgram;  }  /**   * Initialize the type   */  @Override  public void inject(Object bean)  {    for (int i = 0; i < _injectList.size(); i++)      _injectList.get(i).inject(bean, null);  }  /**   * Initialize the type   */  @Override  public void init(Object bean)  {    for (int i = 0; i < _initList.size(); i++)      _initList.get(i).inject(bean, null);  }    /**   * Replace the type with the generated object   */  @Override  public Object replaceObject(Object bean)  {    if (_replaceObject != null) {      try {	return _replaceObject.invoke(bean);      } catch (Exception e) {	throw ConfigException.create(_replaceObject, e);      }    }    else      return bean;  }    /**   * Converts the string to the given value.   */  public Object valueOf(String text)  {    if (_valueOf != null) {      try {	return _valueOf.invoke(null, text);      } catch (Exception e) {	throw ConfigException.create(e);      }    }    else if (_stringConstructor != null) {      try {	return _stringConstructor.newInstance(text);      } catch (Exception e) {	throw ConfigException.create(e);      }    }    else if (_addText != null) {      Object bean = create(null);      _addText.setText(bean, TEXT, text);      inject(bean);      init(bean);            return bean;    }    else if (_addProgram != null || _addContentProgram != null) {      Object bean = create(null);      inject(bean);            try {	ConfigProgram program = new PropertyStringProgram("value", text);	if (_addProgram != null)	  _addProgram.setValue(bean, TEXT, program);	else	  _addContentProgram.setValue(bean, TEXT, program);      } catch (Exception e) {	throw ConfigException.create(e);      }      init(bean);      return bean;    }    throw new ConfigException(L.l("Can't convert to '{0}' from '{1}'.",				  _beanClass.getName(), text));  }    /**   * Converts the string to the given value.   */  @Override  public Object valueOf(Object value)  {    if (value == null)      return null;    else if (value instanceof String)      return valueOf((String) value);    else if (_beanClass.isAssignableFrom(value.getClass()))      return value;    else if (value.getClass().getName().startsWith("java.lang."))      return valueOf(String.valueOf(value));    else      return value;  }  //  // Introspection

⌨️ 快捷键说明

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