webbeanscontainer.java

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

JAVA
1,283
字号
  }  /**   * Returns the scope context corresponding to the scope annotation type.   *   * @param scope the scope annotation type identifying the scope   */  public ScopeContext getScopeContext(Class scope)  {    if (scope == null)      throw new NullPointerException();    else if (Dependent.class.equals(scope))      return null;    Context context = _contextMap.get(scope);    if (context != null)      return (ScopeContext) context;    else      throw new IllegalArgumentException(L.l("'{0}' is an unknown scope.",					     scope.getName()));  }  /**   * Creates an injection program for the given field   */  public void createProgram(ArrayList<ConfigProgram> injectList,			    Field field,			    boolean isOptional)    throws ConfigException  {    ComponentImpl component;          component = bind(location(field),		     field.getGenericType(),		     field.getAnnotations());    if (component != null)      component.createProgram(injectList, field);    else if (! isOptional)      throw injectError(field, L.l("Can't find a component for '{0}'",				   field.getType().getName()));  }  /**   * Creates an injection program for the given method   */  public void createProgram(ArrayList<ConfigProgram> injectList,			    Method method)    throws ConfigException  {    if (method.isAnnotationPresent(Produces.class))      throw error(method, "An injection method may not have the @Produces annotation.");    // XXX: lazy binding    try {      Type []paramTypes = method.getGenericParameterTypes();      Annotation[][]paramAnn = method.getParameterAnnotations();            ComponentImpl []args = new ComponentImpl[paramTypes.length];      for (int i = 0; i < args.length; i++) {	args[i] = bind(location(method), paramTypes[i], paramAnn[i]);	if (args[i] == null) {	  throw error(method,		      L.l("Injection for type '{0}' of method parameter #{1} has no matching component.",			  getSimpleName(paramTypes[i]), i));	}      }      injectList.add(new MethodComponentProgram(method, args));    } catch (ConfigException e) {      throw e;    } catch (Exception e) {      throw LineConfigException.create(method, e);    }  }  /**   * Returns the web beans component corresponding to a method   * and a @Named value   */  public ComponentImpl bind(String location, Type type, String name)  {    ArrayList<Binding> bindingList = new ArrayList<Binding>();    Binding binding = new Binding(Named.class);    binding.put("value", name);    bindingList.add(binding);    return bindByBindings(location, type, bindingList);  }  /**   * Returns the web beans component corresponding to the return type.   */  public ComponentImpl bind(String location, Type type)  {    ArrayList<Binding> bindingList = new ArrayList<Binding>();    return bindByBindings(location, type, bindingList);  }  /**   * Returns the web beans component corresponding to a method   * parameter.   */  public ComponentImpl bind(String location,			    Type type,			    Annotation []paramAnn)  {    ArrayList<Annotation> bindingList = new ArrayList<Annotation>();    boolean isNew = false;    for (Annotation ann : paramAnn) {      if (ann instanceof New)	isNew = true;      else if (ann.annotationType().isAnnotationPresent(BindingType.class))	bindingList.add(ann);    }    if (isNew)      return bindNew(location, (Class) type);    else      return bind(location, type, bindingList);  }  /**   * Binds for the @New expression   */  private ComponentImpl bindNew(String location, Class type)  {    ComponentImpl component = bind(location, type, new Annotation[0]);    if (component == null) {      ClassComponent newComp = new ClassComponent(_wbWebBeans);      newComp.setInstanceClass(type);      newComp.setTargetType(type);      newComp.init();      addComponent(newComp);      component = newComp;    }    return component;  }  /**   * Returns the web beans component with a given binding list.   */  public ComponentImpl bind(String location,			    Type type,			    ArrayList<Annotation> bindingList)  {    _wbWebBeans.init();        WebComponent component = _componentMap.get(type);    if (component != null) {      ComponentImpl comp = component.bind(location, bindingList);      if (log.isLoggable(Level.FINER))	log.finer(this + " bind(" + getSimpleName(type) + ") returns " + comp);            return comp;    }    else if (_parent != null) {      return _parent.bind(location, type, bindingList);    }    else {      return null;    }  }  /**   * Returns the web beans component with a given binding list.   */  public ComponentImpl bindByBindings(String location,				      Type type,				      ArrayList<Binding> bindingList)  {    _wbWebBeans.init();        WebComponent component = _componentMap.get(type);    if (component != null)      return component.bindByBindings(location, type, bindingList);    else if (_parent != null)      return _parent.bindByBindings(location, type, bindingList);    else      return null;  }  /**   * Finds a component by its component name.   */  public ComponentImpl findByName(String name)  {    ComponentImpl comp = _namedComponentMap.get(name);        if (comp != null)      return comp;    else if (_parent != null)      return _parent.findByName(name);    else      return null;  }  /**   * Finds a component by its component name.   */  public Object getObjectByName(String name)  {    ComponentImpl comp = _namedComponentMap.get(name);    if (comp != null)      return comp.get();    else if (_parent != null)      return _parent.getObjectByName(name);    else      return null;  }  /**   * Injects an object   */  public void injectObject(Object obj)  {    if (obj == null)      return;    Class cl = obj.getClass();    InjectProgram program;    synchronized (_injectMap) {      program = _injectMap.get(cl);      if (program == null) {	program = InjectIntrospector.introspectProgram(cl);	_injectMap.put(cl, program);      }    }    program.configure(obj);  }    //  // events  //    public void addObserver(ObserverImpl observer)  {    synchronized (_observerMap) {      ObserverMap map = _observerMap.get(observer.getType());            if (map == null) {	map = new ObserverMap(observer.getType());	_observerMap.put(observer.getType(), map);      }      map.addObserver(observer);    }  }  //  // javax.webbeans.Container  //  /**   * Returns the component which matches the apiType and binding types   */  public <T> ComponentFactory<T> resolveByType(Class<T> apiType,					       Annotation...bindingTypes)  {    return bind("", apiType, bindingTypes);  }  /**   * Returns the component which matches the apiType and binding types   */  public <T> T getByType(Class<T> apiType, Annotation...bindingTypes)  {    ComponentFactory<T> factory =  bind("", apiType, bindingTypes);    if (factory != null)      return factory.get();    else      return null;  }    public void addContext(Class<Annotation> scopeType, Context context)  {    _contextMap.put(scopeType, context);  }    public Context getContext(Class<Annotation> scopeType)  {    return _contextMap.get(scopeType);  }  /**   * Sends the specified event to any observer instances in the scope   */  public void raiseEvent(Object event, Annotation... bindings)  {    if (_parent != null)      _parent.raiseEvent(event, bindings);    ArrayList<ObserverMap> observerList;    synchronized (_observerListCache) {      observerList = _observerListCache.get(event.getClass());      if (observerList == null) {	observerList = new ArrayList<ObserverMap>();		fillObserverList(observerList, event.getClass());	_observerListCache.put(event.getClass(), observerList);      }    }    int size = observerList.size();    for (int i = 0; i < size; i++) {      observerList.get(i).raiseEvent(event, bindings);    }  }  public Conversation createConversation()  {    return (Conversation) _contextMap.get(ConversationScoped.class);  }  private void fillObserverList(ArrayList<ObserverMap> list, Class cl)  {    if (cl == null)      return;    fillObserverList(list, cl.getSuperclass());    for (Class iface : cl.getInterfaces())      fillObserverList(list, iface);    ObserverMap map = _observerMap.get(cl);    if (map != null)      list.add(map);  }  /**   * Creates an object, but does not register the   * component with webbeans.   */  public <T> T createTransientObject(Class<T> type)  {    ComponentFactory<T> factory = createTransient(type);    return factory.create();  }  /**   * Creates an object, but does not register the   * component with webbeans.   */  public <T> T createTransientObjectNoInit(Class<T> type)  {    ComponentImpl comp = (ComponentImpl) createTransient(type);    return (T) comp.createNoInit();  }  /**   * Returns a ComponentFactory for a class, but does not register the   * component with webbeans.   */  public <T> ComponentFactory<T> createTransient(Class<T> type)  {    synchronized (_transientMap) {      ClassComponent comp = _transientMap.get(type);      if (comp == null) {	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()));		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();	_transientMap.put(type, comp);	// XXX: QA	comp.bind();      }      return comp;    }  }  public ELContext getELContext()  {    return new ConfigELContext();  }  /**   * Returns a new instance for a class, but does not register the   * component with webbeans.   */  public <T> T getObject(Class<T> type, Annotation ... ann)  {    return (T) createFactory(type, ann).get();  }  /**

⌨️ 快捷键说明

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