001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.interceptor;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import javax.interceptor.InvocationContext;
023:
024: import org.apache.openejb.test.SuperInterceptedBean;
025:
026: /**
027: * @author <a href="mailto:goyathlay.geronimo@gmail.com">Prasad Kashyap</a>
028: *
029: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
030: */
031: public class Interceptor {
032:
033: /*@Resource
034: static SessionContext sessionContext;*/
035:
036: /**
037: * This interceptor creates/updates an inner map for every method that it intercepts.
038: * The inner map contains the array of method parameters in the key PARAMETERS.
039: * The inner map contains the list of interceptor methods in the key INTERCEPTORS.
040: * The inner map is put back into the contextData against the method name as the key.
041: *
042: * @param ctx - InvocationContext
043: * @param interceptorName name of the interceptor
044: * @return contextData - the contextData which now has been filled with a hashmap of hashmap.
045: */
046: @SuppressWarnings("unchecked")
047: public static Map<String, Object> profile(InvocationContext ctx,
048: String interceptorName) {
049: /*if (sessionContext != null) {
050: System.out.println(sessionContext.lookup("java:comp/env"));
051: }
052: else {
053: System.out.println("SessionContext is null");
054: }*/
055:
056: Map<String, Object> ctxData = ctx.getContextData();
057:
058: String KEY;
059: if (ctx.getMethod() != null) {
060: KEY = ctx.getMethod().getName();
061: } else {
062: KEY = (ctx.getTarget()).getClass().getSimpleName();
063: }
064:
065: Map<String, Object> innerMap = (HashMap<String, Object>) ctxData
066: .get(KEY);
067: innerMap = updateInterceptorsList(innerMap, interceptorName);
068:
069: // don't try to get parameters for call back methods (you'll get an IllegalStateException)
070: if (ctx.getMethod() != null) {
071: Object[] params = ctx.getParameters();
072: innerMap.put("PARAMETERS", params);
073: }
074:
075: ctxData.put(KEY, innerMap);
076:
077: return ctxData;
078: }
079:
080: /**
081: * This is invoked by the lifecycle interceptor callback methods that are defined inside a bean.
082: */
083: @SuppressWarnings("unchecked")
084: public static Map<String, Object> profile(
085: SuperInterceptedBean bean, String interceptorName) {
086: Map<String, Object> ctxData = new HashMap<String, Object>();
087:
088: String KEY = bean.getClass().getSimpleName();
089:
090: Map<String, Object> innerMap = (HashMap<String, Object>) ctxData
091: .get(KEY);
092: innerMap = updateInterceptorsList(innerMap, interceptorName);
093:
094: ctxData.put(KEY, innerMap);
095: return ctxData;
096: }
097:
098: /**
099: * @param innerMap
100: * @param interceptorName
101: * @return innerMap
102: */
103: @SuppressWarnings("unchecked")
104: private static Map<String, Object> updateInterceptorsList(
105: Map<String, Object> innerMap, String interceptorName) {
106: if (innerMap == null) {
107: innerMap = new HashMap<String, Object>();
108: }
109:
110: ArrayList<String> interceptorsList = (ArrayList<String>) innerMap
111: .get("INTERCEPTORS");
112: if (interceptorsList == null) {
113: interceptorsList = new ArrayList<String>();
114: }
115: interceptorsList.add(interceptorName);
116: innerMap.put("INTERCEPTORS", interceptorsList);
117:
118: return innerMap;
119: }
120:
121: }
|