webbeanscontainer.java

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

JAVA
1,283
字号
   * Returns a new instance for a class, but does not register the   * component with webbeans.   */  public <T> T getObject(Class<T> type, String name)  {    Annotation []ann = new Annotation[] { Names.create(name) };        return (T) createFactory(type, ann).get();  }  /**   * Returns a new instance for a class, but does not register the   * component with webbeans.   */  public <T> T createFactory(Class<T> type, String name)  {    Annotation []ann = new Annotation[] { Names.create(name) };        return (T) createFactory(type, ann);  }  /**   * Returns a new instance for a class, but does not register the   * component with webbeans.   */  public <T> T getEnvironmentObject(Class<T> type, Annotation ... ann)  {    ComponentImpl comp = (ComponentImpl) createFactory(type, ann);    Object value = comp.get();    if (comp.isDestroyPresent())      Environment.addClassLoaderListener(new ComponentClose(value, comp));        return (T) value;  }  /**   * Returns a new instance for a class, but does not register the   * component with webbeans.   */  public ComponentFactory createFactory(Class type, Annotation ... ann)  {    FactoryBinding binding = new FactoryBinding(type, ann);        synchronized (_objectFactoryMap) {      ComponentFactory factory = _objectFactoryMap.get(binding);      if (factory != null)	return factory;      if (ann == null)	ann = NULL_ANN;            factory = resolveByType(type, ann);      if (factory != null) {	_objectFactoryMap.put(binding, factory);	return factory;      }            if (type.isInterface())	throw new ConfigException(L.l("'{0}' cannot be an interface.  createTransient requires a concrete type.", type.getName()));      else if (Modifier.isAbstract(type.getModifiers()))	throw new ConfigException(L.l("'{0}' cannot be an abstract.  createTransient requires a concrete type.", type.getName()));	      ClassComponent comp = new ClassComponent(_wbWebBeans);      comp.setInstanceClass(type);      try {	Constructor nullCtor = type.getConstructor(new Class[0]);	if (nullCtor != null)	  comp.setConstructor(nullCtor);      } catch (NoSuchMethodException e) {	// if the type doesn't have a null-arg constructor      }      comp.init();      _objectFactoryMap.put(binding, comp);      return comp;    }  }  //  // class loader updates  //  public void update()  {    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      ArrayList<WebBeansRootContext> rootContextList	= new ArrayList<WebBeansRootContext>(_pendingRootContextList);            _pendingRootContextList.clear();      for (WebBeansRootContext context : rootContextList) {	Path root = context.getRoot();      	WbWebBeans webBeans = _webBeansMap.get(root);	if (webBeans == null) {	  webBeans = new WbWebBeans(this, root);	  _webBeansMap.put(root, webBeans);	  Path path = root.lookup("META-INF/web-beans.xml");	  	  if (path.canRead()) {	    path.setUserPath(path.getURL());	    	    new Config().configure(webBeans, path, SCHEMA);	  }	}	for (String className : context.getClassNameList()) {	  try {	    Class cl = Class.forName(className, false, _classLoader);	    webBeans.addScannedClass(cl);	  } catch (ClassNotFoundException e) {	    log.log(Level.FINER, e.toString(), e);	  }	}		webBeans.update();	webBeans.init();      }          _wbWebBeans.init();    } catch (ConfigException e) {      if (_configException == null)	_configException = e;            throw e;    } finally {      thread.setContextClassLoader(oldLoader);    }  }  /**   * Starts the bind phase   */  public void bind()  {    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      ArrayList<ComponentImpl> bindList	= new ArrayList<ComponentImpl>(_pendingBindList);            _pendingBindList.clear();            for (ComponentImpl comp : bindList) {	if (comp.getType().isEnabled())	  comp.bind();      }    } catch (ConfigException e) {      if (_configException == null)	_configException = e;            throw e;    } finally {      thread.setContextClassLoader(oldLoader);    }  }  public Class loadClass(String className)  {    try {      return Class.forName(className, false, _classLoader);    } catch (ClassNotFoundException e) {      throw new RuntimeException(e);    }  }  /**   * Handles the case the environment config phase   */  public void environmentConfigure(EnvironmentClassLoader loader)  {    update();  }  /**   * Handles the case the environment config phase   */  public void environmentBind(EnvironmentClassLoader loader)  {    update();    bind();  }  /**   * Handles the case where the environment is starting (after init).   */  public void environmentStart(EnvironmentClassLoader loader)  {    update();    bind();        _wbWebBeans.init();    startSingletons();  }  /**   * Initialize all the singletons   */  private void startSingletons()  {    ArrayList<ComponentImpl> singletons;    synchronized (_pendingSingletonList) {      if (_pendingSingletonList.size() == 0)	return;      singletons = new ArrayList<ComponentImpl>();      singletons.addAll(_pendingSingletonList);      _pendingSingletonList.clear();    }    for (ComponentImpl singleton : singletons)      singleton.get();  }  /**   * Handles the case where the environment is stopping   */  public void environmentStop(EnvironmentClassLoader loader)  {    _componentMap = null;    _namedComponentMap = null;  }  public static ConfigException injectError(AccessibleObject prop, String msg)  {    String location = "";        if (prop instanceof Field) {      Field field = (Field) prop;      String className = field.getDeclaringClass().getName();      location = className + "." + field.getName() + ": ";    }    else if (prop instanceof Method) {      Method method = (Method) prop;      String className = method.getDeclaringClass().getName();      location = className + "." + method.getName() + ": ";    }    return new ConfigException(location + msg);  }  public static String location(Field field)  {    return field.getDeclaringClass().getName() + "." + field.getName() + ": ";  }  public static String location(Method method)  {    return LineConfigException.loc(method);  }  public static ConfigException error(Method method, String msg)  {    return new ConfigException(location(method) + msg);  }  //  // ScanListener  //  /**   * Since webbeans doesn't enhance, it's priority 1   */  public int getPriority()  {    return 1;  }  /**   * Returns true if the root is a valid scannable root.   */  public boolean isRootScannable(Path root)  {    if (! root.lookup("META-INF/web-beans.xml").canRead())      return false;    WebBeansRootContext context = _rootContextMap.get(root);    if (context == null) {      context = new WebBeansRootContext(root);      _rootContextMap.put(root, context);      _pendingRootContextList.add(context);    }    if (context.isScanComplete())      return false;    else {      if (log.isLoggable(Level.FINER))	log.finer("WebBeans scanning " + root.getURL());      context.setScanComplete(true);      return true;    }  }  public boolean isScanMatch(CharBuffer annotationName)  {    try {      String className = annotationName.toString();            Class cl = Class.forName(className, false, _tempClassLoader);            return cl.isAnnotationPresent(ComponentType.class);    } catch (ClassNotFoundException e) {    }    return false;  }    /**   * Callback to note the class matches   */  public void classMatchEvent(EnvironmentClassLoader loader,			      Path root,			      String className)  {    WebBeansRootContext context = _rootContextMap.get(root);    if (context == null) {      context = new WebBeansRootContext(root);      _rootContextMap.put(root, context);      _pendingRootContextList.add(context);    }          context.addClassName(className);  }  /**   * Serialization rewriting   */  public Object writeReplace()  {    return new WebBeansHandle(Container.class);  }  public String toString()  {    if (_classLoader != null && _classLoader.getId() != null)      return "WebBeansContainer[" + _classLoader.getId() + "]";    else      return "WebBeansContainer[]";  }  static String getSimpleName(Type type)  {    if (type instanceof Class)      return ((Class) type).getSimpleName();    else      return String.valueOf(type);  }  static class FactoryBinding {    private static final Annotation []NULL = new Annotation[0];        private final Type _type;    private final Annotation []_ann;    FactoryBinding(Type type, Annotation []ann)    {      _type = type;      if (ann != null)	_ann = ann;      else	_ann = NULL;    }    @Override    public int hashCode()    {      int hash = _type.hashCode();      for (Annotation ann : _ann)	hash = 65521 * hash + ann.hashCode();      return hash;    }    @Override    public boolean equals(Object obj)    {      if (! (obj instanceof FactoryBinding))	return false;      FactoryBinding binding = (FactoryBinding) obj;      if (_type != binding._type)	return false;      if (_ann.length != binding._ann.length)	return false;      for (int i = 0; i < _ann.length; i++) {	if (! _ann[i].equals(binding._ann[i]))	  return false;      }      return true;    }  }}

⌨️ 快捷键说明

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