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 SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Rodrigo Westrupp
028: */
029:
030: package com.caucho.ejb.interceptor;
031:
032: import com.caucho.bytecode.JClass;
033: import com.caucho.ejb.cfg.Interceptor;
034: import com.caucho.util.L10N;
035:
036: import javax.interceptor.InvocationContext;
037: import java.lang.reflect.InvocationTargetException;
038: import java.lang.reflect.Method;
039: import java.util.HashMap;
040: import java.util.Map;
041:
042: /**
043: * InvocationContext implementation.
044: */
045: public class InvocationContextImpl implements InvocationContext {
046: private static final L10N L = new L10N(InvocationContextImpl.class);
047:
048: private Object _target;
049: private Object _home;
050: private Object _parameters[];
051:
052: private Method _method;
053: private String _methodName;
054: private Class _parameterTypes[];
055:
056: private HashMap<String, Object> _contextData;
057:
058: private Object _context[];
059:
060: // Current interceptor in chain.
061: private int _curr;
062:
063: private Object _interceptors[];
064: private Method _methods[];
065:
066: private boolean _hasCalledTargetMethod;
067:
068: public InvocationContextImpl(Object target, Object home,
069: String methodName, Class parameterTypes[],
070: Object interceptors[], Method methods[]) {
071: _target = target;
072: _home = home;
073: _methodName = methodName;
074: _parameterTypes = parameterTypes;
075:
076: _interceptors = interceptors;
077: _methods = methods;
078:
079: _context = new Object[] { this };
080: }
081:
082: public Object getHome() {
083: return _home;
084: }
085:
086: public Object getTarget() {
087: return _target;
088: }
089:
090: public Method getMethod() {
091: if (_method == null) {
092: if (_methodName == null)
093: return null;
094:
095: Class cl = _target.getClass();
096:
097: try {
098: _method = cl.getMethod(_methodName, _parameterTypes);
099: } catch (NoSuchMethodException e) {
100: throw new RuntimeException(e);
101: }
102: }
103:
104: return _method;
105: }
106:
107: public Object[] getParameters() throws IllegalStateException {
108: return _parameters;
109: }
110:
111: public void setParameters(Object[] parameters)
112: throws IllegalStateException {
113: _parameters = parameters;
114: }
115:
116: public Map<String, Object> getContextData() {
117: if (_contextData == null) {
118: _contextData = new HashMap<String, Object>();
119: }
120:
121: return _contextData;
122: }
123:
124: public Object proceed() throws Exception {
125: try {
126: if (hasNextInterceptor()) {
127: Object interceptor = nextInterceptor();
128:
129: return invokeMethod(getCurrentMethod(), interceptor,
130: _context);
131: } else if (!hasCalledTargetMethod()) {
132: return invokeMethod(getMethod(), getTarget(),
133: getParameters());
134: }
135: } catch (InvocationTargetException e) {
136: throw (Exception) e.getCause();
137: } catch (Exception e) {
138: throw e;
139: }
140:
141: return null;
142: }
143:
144: public static Object invokeMethod(Method method, Object obj,
145: Object params[]) throws Exception {
146: if (method == null)
147: return null;
148:
149: Interceptor.makeAccessible(method);
150:
151: return method.invoke(obj, params);
152: }
153:
154: private boolean hasCalledTargetMethod() {
155: return _hasCalledTargetMethod;
156: }
157:
158: private void setHasCalledTargetMethod(boolean b) {
159: _hasCalledTargetMethod = b;
160: }
161:
162: private boolean hasNextInterceptor() {
163: if (_curr < _interceptors.length)
164: return true;
165:
166: return false;
167: }
168:
169: private Object nextInterceptor() {
170: return _interceptors[_curr++];
171: }
172:
173: private Method getCurrentMethod() {
174: return _methods[_curr - 1];
175: }
176: }
|