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.j2ee;
031:
032: import com.caucho.config.program.ValueGenerator;
033: import com.caucho.config.program.ConfigProgram;
034: import com.caucho.config.ConfigException;
035: import com.caucho.config.ConfigContext;
036: import com.caucho.naming.*;
037: import com.caucho.util.L10N;
038: import com.caucho.webbeans.component.ComponentImpl;
039: import com.caucho.webbeans.manager.WebBeansContainer;
040:
041: import javax.naming.*;
042: import javax.persistence.*;
043: import java.lang.reflect.AccessibleObject;
044: import java.lang.reflect.Field;
045: import java.lang.reflect.Method;
046: import javax.rmi.*;
047: import java.util.*;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: /**
052: * Generator for the @Resource tag.
053: */
054: public class ResourceGenerator extends ValueGenerator {
055: private static final Logger log = Logger
056: .getLogger(ResourceGenerator.class.getName());
057: private static final L10N L = new L10N(ResourceGenerator.class);
058:
059: private final Class _type;
060: private final String _mappedName;
061:
062: private final String _jndiName;
063:
064: private final String _location;
065:
066: private ComponentImpl _component;
067: private boolean _isBound;
068:
069: ResourceGenerator(Class type, String mappedName, String jndiName,
070: String location) {
071: _type = type;
072: _mappedName = mappedName;
073:
074: _jndiName = jndiName;
075:
076: _location = location;
077: }
078:
079: /**
080: * Returns the expected type
081: */
082: @Override
083: public Class getType() {
084: return _type;
085: }
086:
087: /**
088: * Creates the value.
089: */
090: public Object create() {
091: if (_component == null && !_isBound) {
092: _isBound = false;
093:
094: WebBeansContainer webBeans = WebBeansContainer.create();
095:
096: if (_mappedName != null && !"".equals(_mappedName)) {
097: _component = webBeans.bind(_location, _type,
098: _mappedName);
099:
100: if (_component == null) {
101: Object value = getJndiValue(_type);
102:
103: if (value != null)
104: return value;
105:
106: throw new ConfigException(
107: _location
108: + L
109: .l(
110: "'{0}' with mappedName='{1}' is an unknown @EJB",
111: _type.getName(),
112: _mappedName));
113: }
114: } else if (_jndiName != null && !"".equals(_jndiName)) {
115: _component = webBeans.bind(_location, _type, _jndiName);
116:
117: if (_component == null) {
118: Object value = getJndiValue(_type);
119:
120: if (value != null)
121: return value;
122: }
123: }
124:
125: if (_component == null) {
126: _component = webBeans.bind(_location, _type);
127:
128: if (_component == null) {
129: Object value = getJndiValue(_type);
130:
131: if (value != null)
132: return value;
133:
134: throw new ConfigException(_location
135: + L.l("'{0}' is an unknown @Resource",
136: _type.getName()));
137: }
138: }
139:
140: if (_component != null && _jndiName != null
141: && !"".equals(_jndiName)) {
142: try {
143: Jndi.bindDeepShort(_jndiName, _component);
144: } catch (NamingException e) {
145: throw ConfigException.create(e);
146: }
147: }
148: }
149:
150: if (_component != null)
151: return _component.get();
152: else
153: return getJndiValue(_type);
154: }
155:
156: private Object getJndiValue(Class type) {
157: if (_jndiName == null || "".equals(_jndiName))
158: return null;
159:
160: try {
161: Object value = Jndi.lookup(_jndiName);
162:
163: if (value != null)
164: return PortableRemoteObject.narrow(value, type);
165: else
166: return null;
167: } catch (Exception e) {
168: log.log(Level.FINE, e.toString(), e);
169:
170: return null;
171: }
172: }
173:
174: @Override
175: public String toString() {
176: StringBuilder sb = new StringBuilder();
177:
178: sb.append(getClass().getSimpleName());
179: sb.append("[");
180: sb.append(_type.getName());
181:
182: if (_mappedName != null) {
183: sb.append(", mappedName=");
184: sb.append(_mappedName);
185: }
186:
187: if (_jndiName != null) {
188: sb.append(", jndiName=");
189: sb.append(_jndiName);
190: }
191:
192: sb.append("]");
193:
194: return sb.toString();
195: }
196: }
|