webbeanscontainer.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,283 行 · 第 1/3 页
JAVA
1,283 行
/* * 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.webbeans.manager;import com.caucho.config.program.MethodComponentProgram;import com.caucho.config.*;import com.caucho.config.j2ee.*;import com.caucho.config.program.ConfigProgram;import com.caucho.loader.*;import com.caucho.loader.enhancer.*;import com.caucho.util.*;import com.caucho.vfs.*;import com.caucho.server.util.*;import com.caucho.webbeans.*;import com.caucho.webbeans.cfg.*;import com.caucho.webbeans.component.*;import com.caucho.webbeans.context.*;import com.caucho.webbeans.event.*;import java.io.*;import java.util.*;import java.util.logging.*;import java.lang.annotation.*;import java.lang.reflect.*;import javax.el.*;import javax.webbeans.*;/** * The web beans container for a given environment. */public class WebBeansContainer implements ScanListener, EnvironmentListener, Container, java.io.Serializable{ private static final L10N L = new L10N(WebBeansContainer.class); private static final Logger log = Logger.getLogger(WebBeansContainer.class.getName()); private static final String SCHEMA = "com/caucho/webbeans/cfg/webbeans.rnc"; private static final EnvironmentLocal<WebBeansContainer> _localContainer = new EnvironmentLocal<WebBeansContainer>(); private static final Annotation []NULL_ANN = new Annotation[0]; private WebBeansContainer _parent; private EnvironmentClassLoader _classLoader; private ClassLoader _tempClassLoader; private WbWebBeans _wbWebBeans; private HashMap<Path,WbWebBeans> _webBeansMap = new HashMap<Path,WbWebBeans>(); private HashMap<Type,WebComponent> _componentMap = new HashMap<Type,WebComponent>(); private HashMap<String,ComponentImpl> _namedComponentMap = new HashMap<String,ComponentImpl>(); private HashMap<Path,WebBeansRootContext> _rootContextMap = new HashMap<Path,WebBeansRootContext>(); private HashMap<Class,Context> _contextMap = new HashMap<Class,Context>(); private HashMap<Class,ObserverMap> _observerMap = new HashMap<Class,ObserverMap>(); private HashMap<Class,ArrayList<ObserverMap>> _observerListCache = new HashMap<Class,ArrayList<ObserverMap>>(); private HashMap<Class,ClassComponent> _transientMap = new HashMap<Class,ClassComponent>(); private HashMap<FactoryBinding,ComponentFactory> _objectFactoryMap = new HashMap<FactoryBinding,ComponentFactory>(); private ArrayList<WebBeansRootContext> _pendingRootContextList = new ArrayList<WebBeansRootContext>(); private ArrayList<ComponentImpl> _pendingBindList = new ArrayList<ComponentImpl>(); private ArrayList<ComponentImpl> _pendingSingletonList = new ArrayList<ComponentImpl>(); private HashMap<Class,InjectProgram> _injectMap = new HashMap<Class,InjectProgram>(); private RuntimeException _configException; private WebBeansContainer(ClassLoader loader) { _classLoader = Environment.getEnvironmentClassLoader(loader); if (_classLoader != null) { _parent = WebBeansContainer.create(_classLoader.getParent()); } _localContainer.set(this, _classLoader); if (_classLoader != null) _tempClassLoader = _classLoader.getNewTempClassLoader(); else _tempClassLoader = new DynamicClassLoader(null); _wbWebBeans = new WbWebBeans(this, Vfs.lookup()); _contextMap.put(RequestScoped.class, new RequestScope()); _contextMap.put(SessionScoped.class, new SessionScope()); _contextMap.put(ConversationScoped.class, new ConversationScope()); _contextMap.put(ApplicationScoped.class, new ApplicationScope()); _contextMap.put(Singleton.class, new SingletonScope()); if (_classLoader != null) _classLoader.addScanListener(this); Environment.addEnvironmentListener(this, _classLoader); } /** * Returns the local container. */ public static WebBeansContainer getCurrent() { return getCurrent(Thread.currentThread().getContextClassLoader()); } /** * Returns the current environment container. */ public static WebBeansContainer getCurrent(ClassLoader loader) { synchronized (_localContainer) { return _localContainer.get(loader); } } /** * Returns the current active container. */ public static WebBeansContainer create() { return create(Thread.currentThread().getContextClassLoader()); } /** * Returns the current active container. */ public static WebBeansContainer create(ClassLoader loader) { WebBeansContainer webBeans = null; synchronized (_localContainer) { webBeans = _localContainer.getLevel(loader); if (webBeans != null) return webBeans; webBeans = new WebBeansContainer(loader); _localContainer.set(webBeans); } return webBeans; } public WbWebBeans getWbWebBeans() { return _wbWebBeans; } public ClassLoader getClassLoader() { return _classLoader; } private void init() { try { update(); } catch (RuntimeException e) { _configException = e; throw _configException; } catch (Exception e) { _configException = ConfigException.create(e); throw _configException; } Environment.addEnvironmentListener(this); } public WbComponentType createComponentType(Class cl) { return _wbWebBeans.createComponentType(cl); } public void addComponent(ComponentImpl comp) { addComponentByType(comp.getTargetType(), comp); String name = comp.getName(); /* if (name != null && comp.getScope() != null) _namedComponentMap.put(name, comp); */ // ioc/0030 if (name != null) _namedComponentMap.put(name, comp); _pendingBindList.add(comp); if (comp.isSingleton()) { _pendingSingletonList.add(comp); } } public void addComponentByName(String name, ComponentImpl comp) { _namedComponentMap.put(name, comp); } /** * Adds a component by the interface type * * @param type the interface type to expose the component * @param comp the component to register */ public void addComponentByType(Type type, ComponentImpl comp) { if (type == null) return; if (log.isLoggable(Level.FINE)) log.fine(comp.toDebugString() + " added to " + this); if (comp.isSingleton()) { _pendingSingletonList.add(comp); } addComponentRec(type, comp); } public ArrayList<ComponentFactory> getBeansOfType(Type type) { ArrayList<ComponentFactory> beans = new ArrayList<ComponentFactory>(); WebComponent webComponent = _componentMap.get(type); if (webComponent == null) return beans; beans.addAll(webComponent.getComponentList()); return beans; } private void addComponentRec(Type type, ComponentImpl comp) { if (type == null || Object.class.equals(type)) return; WebComponent webComponent = _componentMap.get(type); if (webComponent == null) { webComponent = new WebComponent(type); _componentMap.put(type, webComponent); } webComponent.addComponent(comp); Class cl; if (type instanceof Class) cl = (Class) type; else if (type instanceof ParameterizedType) { cl = (Class) ((ParameterizedType) type).getRawType(); addComponentRec(cl, comp); return; } else { return; } addComponentRec(cl.getSuperclass(), comp); for (Class subClass : cl.getInterfaces()) { addComponentRec(subClass, comp); } } public void addSingleton(Object object) { SingletonComponent comp = new SingletonComponent(_wbWebBeans, object); comp.init(); addComponent(comp); } public void addSingleton(Object object, String name) { SingletonComponent comp = new SingletonComponent(_wbWebBeans, object); comp.setName(name); WbBinding binding = new WbBinding(); binding.setClass(Named.class); binding.addValue("value", name); ArrayList<WbBinding> bindingList = new ArrayList<WbBinding>(); bindingList.add(binding); comp.setBindingList(bindingList); comp.init(); addComponent(comp); } public void addSingleton(Object object, String name, Class componentType) { SingletonComponent comp = new SingletonComponent(_wbWebBeans, object); comp.setName(name); comp.setType(_wbWebBeans.createComponentType(componentType)); WbBinding binding = new WbBinding(); binding.setClass(Named.class); binding.addValue("value", name); ArrayList<WbBinding> bindingList = new ArrayList<WbBinding>(); bindingList.add(binding); comp.setBindingList(bindingList); comp.init(); addComponent(comp); } /** * Adds a singleton only to the name map * * @param object the singleton value * @param name the singleton's name */ public void addSingletonByName(Object object, String name) { SingletonComponent comp = new SingletonComponent(_wbWebBeans, object); comp.setName(name); comp.init(); _namedComponentMap.put(name, comp); } public void addEnabledInterceptor(Class cl) { _wbWebBeans.addEnabledInterceptor(cl); } public ArrayList<Class> findInterceptors(ArrayList<Annotation> annList) { ArrayList<Class> list = new ArrayList<Class>(); ArrayList<WbInterceptor> interceptors = _wbWebBeans.findInterceptors(annList); // root interceptors take priority if (interceptors != null) { addInterceptorClasses(list, interceptors); return list; } for (WbWebBeans wbWebBeans : _webBeansMap.values()) { interceptors = wbWebBeans.findInterceptors(annList); if (interceptors != null) addInterceptorClasses(list, interceptors); } return list; } private void addInterceptorClasses(ArrayList<Class> classes, ArrayList<WbInterceptor> interceptors) { for (WbInterceptor interceptor : interceptors) { classes.add(interceptor.getInterceptorClass()); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?