KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > helper > InterceptorHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: InterceptorHelper.java 915 2006-07-25 07:51:27Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.helper;
26
27 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType.UNDEFINED;
28 import static org.objectweb.easybeans.tests.common.helper.EJBHelper.getBeanRemoteInstance;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.interceptor.InvocationContext;
34 import javax.jms.Message JavaDoc;
35
36 import org.objectweb.easybeans.log.JLog;
37 import org.objectweb.easybeans.log.JLogFactory;
38 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.ItfCallbackLoggerAccess;
39 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.SLSBCallbackLoggerAccess;
40
41 /**
42  * This helper is used to do operation with InvocationContext and interceptors.
43  * @author Eduardo Studzinski Estima de Castro
44  * @author Gisele Pinheiro Souza
45  */

46 public final class InterceptorHelper {
47
48     /**
49      * Logger.
50      */

51     private static JLog logger = JLogFactory.getLog(InterceptorHelper.class);
52
53     /**
54      * Helper should have a private constructor.
55      */

56     private InterceptorHelper() {
57     }
58
59     /**
60      * Intercepts the method and adds a value in the list that was get from
61      * InvocationContext.
62      * @param <E> Element Type
63      * @param invocationContext contains attributes of invocation, the first
64      * parameter of the intercepted method must be a list or a message.
65      * @param value value to add
66      * @param className class that is invoking this method.
67      * @return method's invocation result
68      * @throws Exception if invocation fails
69      */

70     @SuppressWarnings JavaDoc("unchecked")
71     public static <E> Object JavaDoc addValue(final InvocationContext invocationContext, final E value, final String JavaDoc className)
72             throws Exception JavaDoc {
73         ItfCallbackLoggerAccess beanLogger = getBeanRemoteInstance(SLSBCallbackLoggerAccess.class,
74                 ItfCallbackLoggerAccess.class);
75         boolean added = false;
76
77         Object JavaDoc[] arObj = invocationContext.getParameters();
78
79         List JavaDoc<E> arOrder = null;
80
81         if (arObj[0] instanceof List JavaDoc) {
82             //Standard interceptor
83
arOrder = (List JavaDoc<E>) arObj[0];
84             added = arOrder.add(value);
85
86         }else if (arObj[0] instanceof Message JavaDoc){
87             //Log event
88
beanLogger.insertCallbackLogger(invocationContext.getTarget().getClass().toString(), UNDEFINED, className);
89             added = true;
90         }
91
92         // If can't add, throw an exception to avoid cascade errors.
93
if (!added) {
94             throw new IllegalStateException JavaDoc(className + " can not add the value " + value + ".");
95         }
96         logger.debug("Added: target={0}, interceptorName={1}, order={2}",
97                 invocationContext.getTarget().toString(), className, value);
98         return invocationContext.proceed();
99     }
100
101     /**
102      * Gets the string representation of a list.
103      * @param list object to get the string representation.
104      * @return string representation of the list.
105      */

106     public static String JavaDoc getString(final List JavaDoc list){
107         StringBuffer JavaDoc result = new StringBuffer JavaDoc("");
108         if (list != null){
109             for(Object JavaDoc o : list){
110                 result.append(",");
111                 result.append(o.toString());
112             }
113             result.replace(0, 0, "");
114         }
115         logger.debug("Result string = {0}", result.toString());
116         return result.toString();
117     }
118
119     /**
120      * Gets the array from string representation.
121      * @param str representation of the list.
122      * @return list obtained using the string representation.
123      */

124     @SuppressWarnings JavaDoc({"boxing", "unchecked"})
125     public static List JavaDoc getArray(final String JavaDoc str){
126         List JavaDoc lst = new ArrayList JavaDoc<Integer JavaDoc>();
127
128         if (str != null){
129             String JavaDoc[] values = str.split(",");
130             for(int i = 0; i < values.length; i++){
131                 lst.add(Integer.parseInt(values[i]));
132             }
133         }
134         return lst;
135     }
136
137     /**
138      * Gets the error msg formatted.
139      * @param expected values to a correct answer
140      * @param result result values after an execution
141      * @param seedMessage standard message to show
142      * @return error message
143      */

144     public static String JavaDoc getPrintOrderErrorMsg(final List JavaDoc expected, final List JavaDoc result, final String JavaDoc seedMessage) {
145         String JavaDoc strMessage = seedMessage;
146
147         if (expected == null & result == null) {
148             strMessage += " Result=[null], Expected=[null].";
149         } else if (expected == null) {
150             strMessage += " Result=" + result.toString() + ", Expected=[null].";
151         } else if (result == null) {
152             strMessage += " Result=[null], Expected=" + expected.toString() + ".";
153         } else {
154             strMessage += " Actual=" + result.toString() + ", Expected=" + expected.toString() + ".";
155         }
156         return strMessage;
157     }
158 }
159
Popular Tags