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.config.types;
031:
032: import com.caucho.config.*;
033: import com.caucho.config.type.*;
034: import com.caucho.config.j2ee.*;
035: import com.caucho.jca.program.*;
036: import com.caucho.naming.*;
037: import com.caucho.util.*;
038: import com.caucho.webbeans.*;
039: import com.caucho.webbeans.cfg.*;
040: import com.caucho.webbeans.component.*;
041: import com.caucho.webbeans.context.*;
042:
043: import java.util.*;
044: import java.lang.reflect.*;
045: import java.lang.annotation.*;
046:
047: import javax.annotation.*;
048:
049: import javax.resource.spi.*;
050:
051: import javax.webbeans.*;
052:
053: /**
054: * Configuration for the xml web bean component.
055: */
056: public class BeanConfig extends WbComponentConfig {
057: private static final L10N L = new L10N(BeanConfig.class);
058:
059: private String _filename;
060: private int _line;
061:
062: private String _uri;
063:
064: private String _jndiName;
065:
066: private String _mbeanName;
067: private Class _mbeanClass;
068:
069: private Class _beanConfigClass;
070:
071: public BeanConfig() {
072: if (getDefaultScope() != null)
073: setScope(getDefaultScope());
074: }
075:
076: protected String getDefaultScope() {
077: return "singleton";
078: }
079:
080: public void setConfigLocation(String filename, int line) {
081: _filename = filename;
082: _line = line;
083: }
084:
085: public String getFilename() {
086: return _filename;
087: }
088:
089: public int getLine() {
090: return _line;
091: }
092:
093: public void setJndiName(String jndiName) {
094: _jndiName = jndiName;
095:
096: if (getName() == null)
097: setName(jndiName);
098: }
099:
100: public void setMbeanName(String mbeanName) {
101: _mbeanName = mbeanName;
102: }
103:
104: @Override
105: public String getMBeanName() {
106: return _mbeanName;
107: }
108:
109: public void setMbeanClass(Class cl) {
110: setMbeanInterface(cl);
111: }
112:
113: public void setMbeanInterface(Class cl) {
114: _mbeanClass = cl;
115: }
116:
117: public Class getBeanConfigClass() {
118: return _beanConfigClass;
119: }
120:
121: public void setBeanConfigClass(Class cl) {
122: _beanConfigClass = cl;
123: }
124:
125: /**
126: * backwards compat
127: */
128: public void setType(Class cl) {
129: setClass(cl);
130: }
131:
132: /**
133: * Check the class
134: */
135: @Override
136: public void setClass(Class cl) {
137: super .setClass(cl);
138:
139: Class type = getBeanConfigClass();
140:
141: if (type != null && !type.isAssignableFrom(cl))
142: throw new ConfigException(L.l(
143: "'{0}' is not a valid instance of '{1}'", cl
144: .getName(), type.getName()));
145: }
146:
147: /**
148: * uri-style configuration like the jms-queue url="memory:"
149: */
150: public void setUri(String uri) {
151: Class beanConfigClass = getBeanConfigClass();
152:
153: if (beanConfigClass == null) {
154: throw new ConfigException(
155: L
156: .l(
157: "'{0}' does not support the 'uri' attribute because its bean-config-class is undefined",
158: getClass().getName()));
159: }
160:
161: _uri = uri;
162:
163: String scheme;
164: String properties = "";
165:
166: int p = uri.indexOf(':');
167: if (p >= 0) {
168: scheme = uri.substring(0, p);
169: properties = uri.substring(p + 1);
170: } else
171: scheme = uri;
172:
173: TypeFactory factory = TypeFactory.create();
174:
175: setClass(factory.getDriverClassByUrl(beanConfigClass, uri));
176:
177: String[] props = properties.split("[;]");
178:
179: for (String prop : props) {
180: if (prop.length() == 0)
181: continue;
182:
183: String[] values = prop.split("[=]");
184:
185: if (values.length != 2)
186: throw new ConfigException(
187: L
188: .l(
189: "'{0}' is an invalid URI. Bean URI syntax is 'scheme:prop1=value1;prop2=value2'",
190: uri));
191:
192: addStringProperty(values[0], values[1]);
193: }
194: }
195:
196: /**
197: * Returns the uri
198: */
199: public String getUri() {
200: return _uri;
201: }
202:
203: public void init() {
204: super .init();
205:
206: try {
207: if (_comp == null) {
208: } else if (_jndiName != null) {
209: Jndi.bindDeepShort(_jndiName, _comp);
210: }
211:
212: /*
213: if (_mbeanName != null) {
214: Object mbean = _object;
215:
216: if (_mbeanInterface != null)
217: mbean = new IntrospectionMBean(mbean, _mbeanInterface);
218:
219: Jmx.register(mbean, mbeanName);
220: _mbeanInfo = mbeanServer.getMBeanInfo(mbeanName);
221: }
222: */
223: } catch (RuntimeException e) {
224: throw e;
225: } catch (Exception e) {
226: throw ConfigException.create(e);
227: }
228: }
229:
230: /**
231: * Introspection after the init has been set and before the @PostConstruct
232: * for additional interception
233: */
234: @Override
235: protected void introspectPostInit() {
236: }
237: }
|