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.config.*;
033: import com.caucho.config.j2ee.*;
034: import com.caucho.config.program.ContainerProgram;
035: import com.caucho.config.types.*;
036: import com.caucho.naming.*;
037: import com.caucho.util.*;
038: import com.caucho.webbeans.*;
039: import com.caucho.webbeans.component.*;
040: import com.caucho.webbeans.context.*;
041: import com.caucho.webbeans.manager.WebBeansContainer;
042:
043: import java.lang.reflect.*;
044: import java.lang.annotation.*;
045: import java.util.ArrayList;
046:
047: import javax.annotation.*;
048: import javax.naming.*;
049: import javax.webbeans.*;
050:
051: /**
052: * Convenience classes for the bean config
053: */
054: abstract public class AbstractBeanConfig {
055: private static final L10N L = new L10N(AbstractBeanConfig.class);
056:
057: private String _filename;
058: private int _line;
059:
060: private String _name;
061: private String _jndiName;
062:
063: private Class _cl;
064:
065: private WbComponentType _type;
066:
067: private ArrayList<WbBinding> _bindingList = new ArrayList<WbBinding>();
068:
069: private Class _scope;
070:
071: private ContainerProgram _init;
072:
073: protected AbstractBeanConfig() {
074: }
075:
076: /**
077: * Sets the configuration location
078: */
079: public void setConfigLocation(String filename, int line) {
080: _filename = filename;
081: _line = line;
082: }
083:
084: public String getFilename() {
085: return _filename;
086: }
087:
088: public int getLine() {
089: return _line;
090: }
091:
092: /**
093: * Returns the component's EL binding name.
094: */
095: public void setName(String name) {
096: _name = name;
097:
098: WbBinding binding = new WbBinding();
099: binding.setClass(Named.class);
100: binding.addValue("value", name);
101:
102: _bindingList.add(binding);
103: }
104:
105: /**
106: * Gets the component's EL binding name.
107: */
108: public String getName() {
109: return _name;
110: }
111:
112: /**
113: * Returns the component's EL binding name.
114: */
115: public void setJndiName(String name) {
116: _jndiName = name;
117: }
118:
119: /**
120: * Gets the component's EL binding name.
121: */
122: public String getJndiName() {
123: return _jndiName;
124: }
125:
126: /**
127: * Assigns the class
128: */
129: public void setClass(Class cl) {
130: _cl = cl;
131: }
132:
133: /**
134: * Returns the instance class
135: */
136: public Class getInstanceClass() {
137: return _cl;
138: }
139:
140: /**
141: * Sets the component type.
142: */
143: public void setComponentType(Class type) {
144: if (!type.isAnnotationPresent(ComponentType.class))
145: throw new ConfigException(
146: L
147: .l(
148: "'{0}' is an invalid component annotation. Component types must be annotated by @ComponentType.",
149: type.getName()));
150:
151: WebBeansContainer webBeans = WebBeansContainer.create();
152:
153: _type = webBeans.createComponentType(type);
154: }
155:
156: /**
157: * Gets the component type.
158: */
159: public WbComponentType getComponentType() {
160: return _type;
161: }
162:
163: /**
164: * Adds a component binding.
165: */
166: public void addBinding(WbBinding binding) {
167: _bindingList.add(binding);
168: }
169:
170: /**
171: * Sets the scope attribute.
172: */
173: public void setScope(String scope) {
174: if ("singleton".equals(scope))
175: _scope = Singleton.class;
176: else if ("dependent".equals(scope))
177: _scope = Dependent.class;
178: else if ("request".equals(scope))
179: _scope = RequestScoped.class;
180: else if ("session".equals(scope))
181: _scope = SessionScoped.class;
182: else if ("application".equals(scope))
183: _scope = ApplicationScoped.class;
184: else if ("conversation".equals(scope))
185: _scope = ConversationScoped.class;
186: else {
187: Class cl = null;
188:
189: try {
190: ClassLoader loader = Thread.currentThread()
191: .getContextClassLoader();
192:
193: cl = Class.forName(scope, false, loader);
194: } catch (ClassNotFoundException e) {
195: }
196:
197: if (cl == null)
198: throw new ConfigException(
199: L
200: .l("'{0}' is an invalid scope. The scope must be a valid @ScopeType annotation."));
201:
202: if (!Annotation.class.isAssignableFrom(cl))
203: throw new ConfigException(
204: L
205: .l("'{0}' is an invalid scope. The scope must be a valid @ScopeType annotation."));
206:
207: if (!cl.isAnnotationPresent(ScopeType.class))
208: throw new ConfigException(
209: L
210: .l("'{0}' is an invalid scope. The scope must be a valid @ScopeType annotation."));
211:
212: _scope = cl;
213: }
214: }
215:
216: /**
217: * Sets the init program.
218: */
219: public void setInit(ContainerProgram init) {
220: _init = init;
221: }
222:
223: /**
224: * Sets the init program.
225: */
226: public ContainerProgram getInit() {
227: return _init;
228: }
229:
230: protected void register() {
231: register(null, null);
232: }
233:
234: protected void register(Object value) {
235: register(value, value != null ? value.getClass() : null);
236: }
237:
238: protected void register(Object value, Class api) {
239: WebBeansContainer webBeans = WebBeansContainer.create();
240: WbWebBeans wbWebBeans = webBeans.getWbWebBeans();
241:
242: ComponentImpl comp;
243:
244: if (value != null) {
245: comp = new SingletonComponent(wbWebBeans, value);
246: } else {
247: ClassComponent classComp = new SingletonClassComponent(
248: wbWebBeans);
249:
250: classComp.setInstanceClass(_cl);
251:
252: comp = classComp;
253: }
254:
255: if (api != null)
256: comp.setTargetType(api);
257:
258: if (_name != null)
259: comp.setName(_name);
260:
261: comp.setBindingList(_bindingList);
262:
263: if (_type != null)
264: comp.setType(_type);
265: else
266: comp.setType(wbWebBeans
267: .createComponentType(ComponentType.class));
268:
269: if (_scope != null)
270: comp.setScope(webBeans.getScopeContext(_scope));
271:
272: comp.init();
273:
274: webBeans.addComponent(comp);
275:
276: if (_jndiName != null) {
277: try {
278: Jndi.bindDeepShort(_jndiName, comp);
279: } catch (NamingException e) {
280: throw ConfigException.create(e);
281: }
282: }
283: }
284: }
|