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 @EJB tag.
053: */
054: public class EjbGenerator extends ValueGenerator {
055: private static final Logger log = Logger
056: .getLogger(EjbGenerator.class.getName());
057: private static final L10N L = new L10N(EjbGenerator.class);
058:
059: private final Class _type;
060: private final String _mappedName;
061: private final String _beanName;
062:
063: private final String _jndiName;
064:
065: private final String _location;
066:
067: private ComponentImpl _component;
068: private boolean _isBound;
069:
070: EjbGenerator(Class type, String mappedName, String beanName,
071: String jndiName, String location) {
072: _type = type;
073: _mappedName = mappedName;
074: _beanName = beanName;
075:
076: _jndiName = jndiName;
077:
078: _location = location;
079: }
080:
081: /**
082: * Returns the expected type
083: */
084: @Override
085: public Class getType() {
086: return _type;
087: }
088:
089: /**
090: * Creates the value.
091: */
092: public Object create() {
093: if (_component == null && !_isBound) {
094: _isBound = false;
095:
096: WebBeansContainer webBeans = WebBeansContainer.create();
097:
098: if (_mappedName != null && !"".equals(_mappedName)) {
099: _component = webBeans.bind("error", _type, _mappedName);
100:
101: if (_component == null) {
102: Object value = getJndiValue(_type);
103:
104: if (value != null)
105: return value;
106:
107: throw new ConfigException(
108: _location
109: + L
110: .l(
111: "'{0}' with mappedName='{1}' is an unknown @EJB",
112: _type.getName(),
113: _mappedName));
114: }
115: } else if (_beanName != null && !"".equals(_beanName)) {
116: _component = webBeans.bind("error", _type, _beanName);
117:
118: if (_component == null) {
119: Object value = getJndiValue(_type);
120:
121: if (value != null)
122: return value;
123:
124: throw new ConfigException(
125: _location
126: + L
127: .l(
128: "'{0}' with beanName='{1}' is an unknown @EJB",
129: _type.getName(),
130: _beanName));
131: }
132: } else {
133: _component = webBeans.bind("error", _type);
134:
135: if (_component == null) {
136: Object value = getJndiValue(_type);
137:
138: if (value != null)
139: return value;
140:
141: throw new ConfigException(_location
142: + L.l("'{0}' is an unknown @EJB", _type
143: .getName()));
144: }
145: }
146:
147: if (_component != null && _jndiName != null
148: && !"".equals(_jndiName)) {
149: try {
150: Jndi.bindDeepShort(_jndiName, _component);
151: } catch (NamingException e) {
152: throw ConfigException.create(e);
153: }
154: }
155: }
156:
157: if (_component != null)
158: return _component.get();
159: else
160: return getJndiValue(_type);
161: }
162:
163: private Object getJndiValue(Class type) {
164: if (_jndiName == null || "".equals(_jndiName))
165: return null;
166:
167: try {
168: Object value = Jndi.lookup(_jndiName);
169:
170: if (value != null)
171: return PortableRemoteObject.narrow(value, type);
172: else
173: return null;
174: } catch (Exception e) {
175: log.log(Level.FINE, e.toString(), e);
176:
177: return null;
178: }
179: }
180:
181: @Override
182: public String toString() {
183: StringBuilder sb = new StringBuilder();
184:
185: sb.append(getClass().getSimpleName());
186: sb.append("[");
187: sb.append(_type.getName());
188:
189: if (_mappedName != null) {
190: sb.append(", mappedName=");
191: sb.append(_mappedName);
192: }
193:
194: if (_beanName != null) {
195: sb.append(", beanName=");
196: sb.append(_beanName);
197: }
198:
199: if (_jndiName != null) {
200: sb.append(", jndiName=");
201: sb.append(_jndiName);
202: }
203:
204: sb.append("]");
205:
206: return sb.toString();
207: }
208: }
|