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 Scott Ferguson
028: */
029:
030: package com.caucho.ejb3.gen;
031:
032: import java.lang.reflect.*;
033: import java.util.*;
034: import javax.interceptor.*;
035:
036: public class InvocationContextImpl implements InvocationContext {
037: private final Object _target;
038: private final Method _apiMethod;
039: private final Method _implMethod;
040:
041: private final Method[] _chainMethods;
042: private final Object[] _chainObjects;
043:
044: private Object[] _param;
045:
046: private int _index;
047: private HashMap<String, Object> _map;
048:
049: public InvocationContextImpl(Object target, Method apiMethod,
050: Method implMethod, Method[] chainMethods,
051: Object[] chainObjects, Object[] param) {
052: _target = target;
053: _apiMethod = apiMethod;
054: _implMethod = implMethod;
055: _chainMethods = chainMethods;
056: _chainObjects = chainObjects;
057: _param = param;
058: }
059:
060: public Object getTarget() {
061: return _target;
062: }
063:
064: public Method getMethod() {
065: return _apiMethod;
066: }
067:
068: public Object[] getParameters() throws IllegalStateException {
069: return _param;
070: }
071:
072: public void setParameters(Object[] parameters)
073: throws IllegalStateException {
074: _param = parameters;
075: }
076:
077: public Map<String, Object> getContextData() {
078: if (_map == null)
079: _map = new HashMap<String, Object>();
080:
081: return _map;
082: }
083:
084: public Object proceed() throws Exception {
085: try {
086: if (_index < _chainMethods.length) {
087: int i = _index++;
088:
089: return _chainMethods[i].invoke(_chainObjects[i], this );
090: } else
091: return _implMethod.invoke(_target, _param);
092: } catch (InvocationTargetException e) {
093: Throwable cause = e.getCause();
094:
095: if (cause instanceof Exception)
096: throw (Exception) cause;
097: else
098: throw e;
099: }
100: }
101: }
|