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.cfg;
031:
032: import com.caucho.bytecode.*;
033: import com.caucho.config.*;
034: import com.caucho.util.*;
035: import com.caucho.vfs.*;
036: import com.caucho.webbeans.Singleton;
037: import com.caucho.webbeans.component.*;
038: import com.caucho.webbeans.context.*;
039: import com.caucho.webbeans.manager.WebBeansContainer;
040:
041: import java.io.IOException;
042: import java.lang.annotation.*;
043: import java.lang.reflect.*;
044: import java.util.*;
045: import java.util.logging.*;
046: import java.util.zip.*;
047:
048: import javax.annotation.PostConstruct;
049: import javax.webbeans.*;
050:
051: /**
052: * Configuration for the top-level web bean
053: */
054: public class WbWebBeans {
055: private static final L10N L = new L10N(WbWebBeans.class);
056: private static final Logger log = Logger.getLogger(WbWebBeans.class
057: .getName());
058:
059: private WebBeansContainer _webBeansContainer;
060: private Path _root;
061:
062: private Path _webBeansFile;
063:
064: private HashMap<String, WbComponentType> _componentTypeMap = new HashMap<String, WbComponentType>();
065:
066: private ArrayList<WbComponentType> _componentTypeList;
067:
068: private ArrayList<ComponentImpl> _pendingComponentList = new ArrayList<ComponentImpl>();
069:
070: private ArrayList<ComponentImpl> _pendingBindList = new ArrayList<ComponentImpl>();
071:
072: private ArrayList<WbInterceptor> _interceptorBindingList = new ArrayList<WbInterceptor>();
073:
074: private ArrayList<WbInterceptor> _enabledInterceptors;
075:
076: private ArrayList<Class> _pendingClasses = new ArrayList<Class>();
077:
078: private boolean _isConfigured;
079:
080: public WbWebBeans(WebBeansContainer webBeansContainer, Path root) {
081: _webBeansContainer = webBeansContainer;
082:
083: _root = root;
084: _webBeansFile = root.lookup("META-INF/web-beans.xml");
085: _webBeansFile.setUserPath(_webBeansFile.getURL());
086: }
087:
088: /**
089: * returns the owning container.
090: */
091: public WebBeansContainer getContainer() {
092: return _webBeansContainer;
093: }
094:
095: /**
096: * Returns the owning classloader.
097: */
098: public ClassLoader getClassLoader() {
099: return getContainer().getClassLoader();
100: }
101:
102: /**
103: * Gets the web beans root directory
104: */
105: public Path getRoot() {
106: return _root;
107: }
108:
109: /**
110: * Adds a scanned class
111: */
112: public void addScannedClass(Class cl) {
113: _pendingClasses.add(cl);
114: }
115:
116: /**
117: * True if the configuration file has been passed.
118: */
119: public boolean isConfigured() {
120: return _isConfigured;
121: }
122:
123: /**
124: * True if the configuration file has been passed.
125: */
126: public void setConfigured(boolean isConfigured) {
127: _isConfigured = isConfigured;
128: }
129:
130: //
131: // web-beans syntax
132: //
133:
134: /**
135: * Adds a component.
136: */
137: public WbComponentConfig createComponent() {
138: return new WbComponentConfig(this );
139: }
140:
141: public void addWbComponent(ComponentImpl component) {
142: _pendingComponentList.remove(component);
143: _pendingComponentList.add(component);
144: }
145:
146: /**
147: * Adds a component.
148: */
149: public WbComponentTypes createComponentTypes() {
150: return new WbComponentTypes();
151: }
152:
153: /**
154: * Adds the interceptors
155: */
156: public Interceptors createInterceptors() {
157: return new Interceptors();
158: }
159:
160: /**
161: * Returns the enabled interceptors
162: */
163: public ArrayList<WbInterceptor> getEnabledInterceptors() {
164: return _enabledInterceptors;
165: }
166:
167: /**
168: * Returns matching interceptors
169: */
170: public ArrayList<WbInterceptor> findInterceptors(
171: ArrayList<Annotation> bindingList) {
172: ArrayList<WbInterceptor> list = null;
173:
174: if (_enabledInterceptors != null) {
175: for (WbInterceptor interceptor : _enabledInterceptors) {
176: if (!interceptor.isMatch(bindingList))
177: continue;
178:
179: if (list == null)
180: list = new ArrayList<WbInterceptor>();
181:
182: list.add(interceptor);
183: }
184: }
185:
186: return list;
187: }
188:
189: /**
190: * Initialization and validation on parse completion.
191: */
192: @PostConstruct
193: public void init() {
194: if (_componentTypeList == null) {
195: _componentTypeList = new ArrayList<WbComponentType>();
196:
197: WbComponentType type = createComponentType(Standard.class);
198: type.setPriority(0);
199: _componentTypeList.add(type);
200: type = createComponentType(Component.class);
201: type.setPriority(1);
202: _componentTypeList.add(type);
203: }
204:
205: update();
206: }
207:
208: public void update() {
209: WebBeansContainer webBeans = _webBeansContainer;
210:
211: try {
212: if (_pendingClasses.size() > 0) {
213: ArrayList<Class> pendingClasses = new ArrayList<Class>(
214: _pendingClasses);
215: _pendingClasses.clear();
216:
217: for (Class cl : pendingClasses) {
218: /*
219: if (_componentTypeMap.get(cl.getName()) != null)
220: continue;
221: */
222:
223: ClassComponent component;
224:
225: if (cl.isAnnotationPresent(Singleton.class))
226: component = new SingletonClassComponent(this );
227: else
228: component = new ClassComponent(this );
229:
230: component.setInstanceClass(cl);
231: component.setTargetType(cl);
232: component.setFromClass(true);
233: component.introspect();
234: component.init();
235:
236: _pendingComponentList.add(component);
237: }
238: }
239:
240: if (_pendingComponentList.size() > 0) {
241: ArrayList<ComponentImpl> componentList = new ArrayList<ComponentImpl>(
242: _pendingComponentList);
243: _pendingComponentList.clear();
244:
245: for (ComponentImpl comp : componentList) {
246: if (comp.getType().isEnabled()) {
247: webBeans.addComponent(comp);
248:
249: _pendingBindList.add(comp);
250: }
251: }
252: }
253: } catch (Exception e) {
254: throw LineConfigException.create(_webBeansFile.getURL(), 1,
255: e);
256: }
257: }
258:
259: public void bind() {
260: ArrayList<ComponentImpl> componentList = new ArrayList<ComponentImpl>(
261: _pendingBindList);
262: _pendingBindList.clear();
263:
264: for (ComponentImpl comp : componentList) {
265: if (comp.getType().isEnabled()) {
266: comp.bind();
267: }
268: }
269: }
270:
271: public WbComponentType createComponentType(Class cl) {
272: WbComponentType type = _componentTypeMap.get(cl.getName());
273:
274: if (type == null) {
275: type = new WbComponentType(cl);
276: _componentTypeMap.put(cl.getName(), type);
277: }
278:
279: return type;
280: }
281:
282: public ScopeContext getScopeContext(Class cl) {
283: return _webBeansContainer.getScopeContext(cl);
284: }
285:
286: public ComponentImpl bindParameter(String loc, Type type,
287: Annotation[] annotations) {
288: return _webBeansContainer.bind(loc, type, annotations);
289: }
290:
291: public String toString() {
292: if (_root != null)
293: return "WbWebBeans[" + _root.getURL() + "]";
294: else
295: return "WbWebBeans[]";
296: }
297:
298: public class WbComponentTypes {
299: public void addComponentType(Class cl) {
300: if (!cl.isAnnotationPresent(ComponentType.class))
301: throw new ConfigException(
302: L
303: .l(
304: "'{0}' is missing a @ComponentType annotation. Component annotations must be annotated with @ComponentType.",
305: cl.getName()));
306:
307: if (_componentTypeList == null)
308: _componentTypeList = new ArrayList<WbComponentType>();
309:
310: int priority = _componentTypeList.size();
311:
312: WbComponentType type = createComponentType(cl);
313:
314: type.setPriority(priority);
315:
316: _componentTypeList.add(type);
317: }
318: }
319:
320: public void addEnabledInterceptor(Class cl) {
321: if (_enabledInterceptors == null)
322: _enabledInterceptors = new ArrayList<WbInterceptor>();
323:
324: _enabledInterceptors.add(new WbInterceptor(cl));
325: }
326:
327: public class Interceptors {
328: public void addInterceptor(Class cl) {
329: if (_enabledInterceptors == null)
330: _enabledInterceptors = new ArrayList<WbInterceptor>();
331:
332: _enabledInterceptors.add(new WbInterceptor(cl));
333: }
334: }
335: }
|