001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.webbeans.manager;
031:
032: import com.caucho.config.*;
033: import com.caucho.config.j2ee.*;
034: import com.caucho.config.program.ConfigProgram;
035: import com.caucho.util.*;
036: import com.caucho.webbeans.cfg.*;
037: import com.caucho.webbeans.component.*;
038:
039: import java.lang.annotation.*;
040: import java.lang.reflect.*;
041: import java.util.ArrayList;
042:
043: /**
044: * Configuration for the xml web bean component.
045: */
046: public class WebComponent {
047: private static L10N L = new L10N(WebComponent.class);
048:
049: private Type _type;
050:
051: private ArrayList<ComponentImpl> _componentList = new ArrayList<ComponentImpl>();
052:
053: public WebComponent(Type type) {
054: _type = type;
055: }
056:
057: public void addComponent(ComponentImpl comp) {
058: for (int i = _componentList.size() - 1; i >= 0; i--) {
059: ComponentImpl oldComponent = _componentList.get(i);
060:
061: if (!comp.getClassName()
062: .equals(oldComponent.getClassName())) {
063: } else if (comp.isFromClass()
064: && !oldComponent.isFromClass())
065: return;
066: else if (!comp.isFromClass() && oldComponent.isFromClass())
067: _componentList.remove(i);
068: else if (comp.equals(oldComponent)) {
069: return;
070: }
071: }
072:
073: _componentList.add(comp);
074: }
075:
076: public void createProgram(ArrayList<ConfigProgram> initList,
077: Field field, ArrayList<Annotation> bindList)
078: throws ConfigException {
079: ComponentImpl comp = bind(WebBeansContainer.location(field),
080: bindList);
081:
082: comp.createProgram(initList, field);
083: }
084:
085: public ComponentImpl bind(String location,
086: ArrayList<Annotation> bindList) throws ConfigException {
087: ComponentImpl matchComp = null;
088: ComponentImpl secondComp = null;
089:
090: for (int i = 0; i < _componentList.size(); i++) {
091: ComponentImpl comp = _componentList.get(i);
092:
093: if (!comp.isMatch(bindList))
094: continue;
095:
096: if (matchComp == null)
097: matchComp = comp;
098: else if (comp.getBindingList().size() == bindList.size()
099: && matchComp.getBindingList().size() != bindList
100: .size()) {
101: matchComp = comp;
102: secondComp = null;
103: } else if (matchComp.getBindingList().size() == bindList
104: .size()
105: && comp.getBindingList().size() != bindList.size()) {
106: } else if (matchComp.getType().getPriority() < comp
107: .getType().getPriority()) {
108: matchComp = comp;
109: secondComp = null;
110: } else if (comp.getType().getPriority() < matchComp
111: .getType().getPriority()) {
112: } else {
113: secondComp = comp;
114: }
115: }
116:
117: if (matchComp == null) {
118: return null;
119: } else if (matchComp != null && secondComp != null) {
120: throw new ConfigException(
121: location
122: + L
123: .l(
124: "WebBeans conflict between '{0}' and '{1}'. WebBean injection must match uniquely.",
125: matchComp, secondComp));
126: }
127:
128: return matchComp;
129: }
130:
131: public ComponentImpl bindByBindings(String location, Type type,
132: ArrayList<Binding> bindList) throws ConfigException {
133: ComponentImpl matchComp = null;
134: ComponentImpl secondComp = null;
135:
136: for (int i = 0; i < _componentList.size(); i++) {
137: ComponentImpl comp = _componentList.get(i);
138:
139: if (!comp.isMatchByBinding(bindList))
140: continue;
141:
142: if (matchComp == null)
143: matchComp = comp;
144: else if (comp.getBindingList().size() == bindList.size()
145: && matchComp.getBindingList().size() != bindList
146: .size()) {
147: matchComp = comp;
148: secondComp = null;
149: } else if (matchComp.getBindingList().size() == bindList
150: .size()
151: && comp.getBindingList().size() != bindList.size()) {
152: } else if (matchComp.getType().getPriority() < comp
153: .getType().getPriority()) {
154: matchComp = comp;
155: secondComp = null;
156: } else if (comp.getType().getPriority() < matchComp
157: .getType().getPriority()) {
158: } else {
159: secondComp = comp;
160: }
161: }
162:
163: if (matchComp == null) {
164: return null;
165: } else if (matchComp != null && secondComp != null) {
166: throw new ConfigException(
167: location
168: + L
169: .l(
170: "Injection of '{0}' conflicts between '{1}' and '{2}'. WebBean injection must match uniquely.",
171: getName(type), matchComp,
172: secondComp));
173: }
174:
175: return matchComp;
176: }
177:
178: static String getName(Type type) {
179: if (type instanceof Class)
180: return ((Class) type).getName();
181: else
182: return String.valueOf(type);
183: }
184: }
|