001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.interceptor;
018:
019: import org.apache.openejb.core.Operation;
020:
021: import javax.interceptor.InvocationContext;
022: import java.lang.reflect.Method;
023: import java.util.List;
024: import java.util.ArrayList;
025: import java.util.Map;
026:
027: /**
028: * @version $Rev: 530344 $ $Date: 2007-04-19 02:15:16 -0700 $
029: */
030: public class InterceptorStack {
031: private final Object beanInstance;
032: private final List<Interceptor> interceptors;
033: private final Method targetMethod;
034: private final Operation operation;
035:
036: public InterceptorStack(Object beanInstance, Method targetMethod,
037: Operation operation,
038: List<InterceptorData> interceptorDatas,
039: Map<String, Object> interceptorInstances) {
040: if (interceptorDatas == null)
041: throw new NullPointerException("interceptorDatas is null");
042: if (interceptorInstances == null)
043: throw new NullPointerException(
044: "interceptorInstances is null");
045: this .beanInstance = beanInstance;
046: this .targetMethod = targetMethod;
047: this .operation = operation;
048:
049: interceptors = new ArrayList<Interceptor>(interceptorDatas
050: .size());
051: for (InterceptorData interceptorData : interceptorDatas) {
052: Class interceptorClass = interceptorData
053: .getInterceptorClass();
054: Object interceptorInstance = interceptorInstances
055: .get(interceptorClass.getName());
056: if (interceptorInstance == null) {
057: throw new IllegalArgumentException(
058: "No interceptor of type "
059: + interceptorClass.getName());
060: }
061:
062: List<Method> methods = interceptorData
063: .getMethods(operation);
064: for (Method method : methods) {
065: Interceptor interceptor = new Interceptor(
066: interceptorInstance, method);
067: interceptors.add(interceptor);
068: }
069: }
070: }
071:
072: public InvocationContext createInvocationContext(
073: Object... parameters) {
074: InvocationContext invocationContext = new ReflectionInvocationContext(
075: operation, interceptors, beanInstance, targetMethod,
076: parameters);
077: return invocationContext;
078: }
079:
080: public Object invoke(Object... parameters) throws Exception {
081: InvocationContext invocationContext = createInvocationContext(parameters);
082: Object value = invocationContext.proceed();
083: return value;
084: }
085:
086: public Object invoke(
087: javax.xml.ws.handler.MessageContext messageContext,
088: Object... parameters) throws Exception {
089: InvocationContext invocationContext = new JaxWsInvocationContext(
090: operation, interceptors, beanInstance, targetMethod,
091: messageContext, parameters);
092: Object value = invocationContext.proceed();
093: return value;
094: }
095:
096: public Object invoke(
097: javax.xml.rpc.handler.MessageContext messageContext,
098: Object... parameters) throws Exception {
099: InvocationContext invocationContext = new JaxRpcInvocationContext(
100: operation, interceptors, beanInstance, targetMethod,
101: messageContext, parameters);
102: Object value = invocationContext.proceed();
103: return value;
104: }
105: }
|