1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.openejb.core.interceptor;
19
20 import org.apache.openejb.core.Operation;
21
22 import javax.interceptor.InvocationContext;
23 import java.lang.reflect.Method;
24 import java.util.List;
25 import java.util.ArrayList;
26 import java.util.Map;
27
28 /**
29 * @version $Rev: 530344 $ $Date: 2007-04-19 02:15:16 -0700 (Thu, 19 Apr 2007) $
30 */
31 public class InterceptorStack {
32 private final Object beanInstance;
33 private final List<Interceptor> interceptors;
34 private final Method targetMethod;
35 private final Operation operation;
36
37 public InterceptorStack(Object beanInstance, Method targetMethod, Operation operation, List<InterceptorData> interceptorDatas, Map<String, Object> interceptorInstances) {
38 if (interceptorDatas == null) throw new NullPointerException("interceptorDatas is null");
39 if (interceptorInstances == null) throw new NullPointerException("interceptorInstances is null");
40 this.beanInstance = beanInstance;
41 this.targetMethod = targetMethod;
42 this.operation = operation;
43
44 interceptors = new ArrayList<Interceptor>(interceptorDatas.size());
45 for (InterceptorData interceptorData : interceptorDatas) {
46 Class interceptorClass = interceptorData.getInterceptorClass();
47 Object interceptorInstance = interceptorInstances.get(interceptorClass.getName());
48 if (interceptorInstance == null) {
49 throw new IllegalArgumentException("No interceptor of type " + interceptorClass.getName());
50 }
51
52 List<Method> methods = interceptorData.getMethods(operation);
53 for (Method method : methods) {
54 Interceptor interceptor = new Interceptor(interceptorInstance, method);
55 interceptors.add(interceptor);
56 }
57 }
58 }
59
60 public InvocationContext createInvocationContext(Object... parameters) {
61 InvocationContext invocationContext = new ReflectionInvocationContext(operation, interceptors, beanInstance, targetMethod, parameters);
62 return invocationContext;
63 }
64
65 public Object invoke(Object... parameters) throws Exception {
66 InvocationContext invocationContext = createInvocationContext(parameters);
67 Object value = invocationContext.proceed();
68 return value;
69 }
70
71 public Object invoke(javax.xml.ws.handler.MessageContext messageContext, Object... parameters) throws Exception {
72 InvocationContext invocationContext = new JaxWsInvocationContext(operation, interceptors, beanInstance, targetMethod, messageContext, parameters);
73 Object value = invocationContext.proceed();
74 return value;
75 }
76
77 public Object invoke(javax.xml.rpc.handler.MessageContext messageContext, Object... parameters) throws Exception {
78 InvocationContext invocationContext = new JaxRpcInvocationContext(operation, interceptors, beanInstance, targetMethod, messageContext, parameters);
79 Object value = invocationContext.proceed();
80 return value;
81 }
82 }