KickJava   Java API By Example, From Geeks To Geeks.

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


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: InvocationContextHelper.java 880 2006-07-17 11:51:17Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.helper;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.interceptor.InvocationContext;
32
33 import org.objectweb.easybeans.log.JLog;
34 import org.objectweb.easybeans.log.JLogFactory;
35 import org.objectweb.easybeans.tests.common.interceptors.invocationcontext.BeanDescriptor;
36 import org.objectweb.easybeans.tests.common.interceptors.invocationcontext.ComplexObject00;
37
38 /**
39  * This helper is used to manage the invocation context.
40  * @author Eduardo Studzinski Estima de Castro
41  * @author Gisele Pinheiro Souza
42  */

43 public final class InvocationContextHelper {
44
45     /**
46      * Int value.
47      */

48     public static final Integer JavaDoc INT_VALUE_0 = new Integer JavaDoc(0);
49
50     /**
51      * String value.
52      */

53     public static final String JavaDoc STR_VALUE_0 = "intValue0";
54
55     /**
56      * Int value.
57      */

58     public static final Integer JavaDoc INT_VALUE_1 = new Integer JavaDoc(1);
59
60     /**
61      * String value.
62      */

63     public static final String JavaDoc STR_VALUE_1 = "intValue1";
64
65     /**
66      * Int value.
67      */

68     public static final Integer JavaDoc INT_VALUE_2 = new Integer JavaDoc(2);
69
70     /**
71      * String value.
72      */

73     public static final String JavaDoc STR_VALUE_2 = "intValue2";
74
75     /**
76      * Helper should have a private constructor.
77      */

78     private InvocationContextHelper() {
79
80     }
81
82     /**
83      * Returns an array of objects with the intercepted method parameters.
84      * @param ic contains attributes of invocation
85      * @return method's invocation result
86      * @throws Exception if invocation fails
87      */

88     public static Object JavaDoc getParametersArray(final InvocationContext ic) throws Exception JavaDoc {
89         JLog logger = JLogFactory.getLog(InvocationContextHelper.class);
90
91         logger.debug("Starting method getParametersArray...");
92
93         Object JavaDoc[] arNew = null;
94         Object JavaDoc[] arParam = ic.getParameters();
95
96         logger.debug("current parameters: {0}", arParam[0]);
97
98         if (arParam != null) {
99             arNew = new Object JavaDoc[arParam.length];
100             for (int i = 0; i < arParam.length; i++) {
101                 arNew[i] = arParam[i];
102             }
103         }
104
105         logger.debug("new parameters: {0}", arNew[0]);
106         logger.debug("before setParameters(), {0}", ic.getParameters()[0]);
107
108         ic.setParameters(arNew);
109
110         logger.debug("after setParameters(), {0}", ic.getParameters()[0]);
111         logger.debug("Finishing method getParametersArray...");
112
113         return ic.proceed();
114     }
115
116     /**
117      * Sets null all intercepted method parameters.
118      * @param ic contains attributes of invocation
119      * @return method's invocation result
120      * @throws Exception if invocation fails
121      */

122     public static Object JavaDoc setParametersNull(final InvocationContext ic) throws Exception JavaDoc {
123         JLog logger = JLogFactory.getLog(InvocationContextHelper.class);
124
125         logger.debug("Starting method setParametersNull...");
126
127         Object JavaDoc[] arParam = ic.getParameters();
128
129         logger.debug("current parameters: {0}", arParam[0]);
130
131         if (arParam != null) {
132             for (int i = 0; i < arParam.length; i++) {
133                 arParam[i] = null;
134             }
135         }
136
137         logger.debug("new parameters: {0}", arParam[0]);
138         logger.debug("before setParameters(), {0}", ic.getParameters()[0]);
139
140         ic.setParameters(arParam);
141
142         logger.debug("after setParameters(), {0}", ic.getParameters()[0]);
143         logger.debug("Finishing method setParametersNull...");
144
145         return ic.proceed();
146     }
147
148     /**
149      * Modifies the objects passed as parameters.
150      * @param ic contains attributes of invocation
151      * @return method's invocation result
152      * @throws Exception if invocation fails
153      */

154     @SuppressWarnings JavaDoc("boxing")
155     public static Object JavaDoc modifyParameters(final InvocationContext ic) throws Exception JavaDoc {
156         Object JavaDoc[] objParams = ic.getParameters();
157
158         // Modifies the first attributes
159
ComplexObject00 cmpObj = (ComplexObject00) objParams[0];
160         cmpObj.setHashCode(INT_VALUE_0);
161         cmpObj.setInterceptedMethod(STR_VALUE_0);
162
163         // Creates a new list of bean descriptors
164
List JavaDoc<BeanDescriptor> lstDesc = new ArrayList JavaDoc<BeanDescriptor>();
165         lstDesc.add(new BeanDescriptor(INT_VALUE_1, STR_VALUE_1));
166         lstDesc.add(new BeanDescriptor(INT_VALUE_2, STR_VALUE_2));
167
168         cmpObj.addDescriptors(lstDesc);
169
170         ic.setParameters(objParams);
171
172         return ic.proceed();
173     }
174
175     /**
176      * Checks if the bean descriptor is the same. <li>The first parameter of
177      * the intercepted method must be a BeanDescriptor.</li>
178      * @param ic contains attributes of invocation
179      * @return method's invocation result
180      * @throws Exception if invocation fails
181      */

182     public static Object JavaDoc checkBeanDescriptor(final InvocationContext ic) throws Exception JavaDoc {
183         // JLog logger = JLogFactory.getLog(InvocationContextHelper.class);
184
BeanDescriptor icBean = new BeanDescriptor(ic.getTarget().hashCode(), ic.getMethod().toString());
185         BeanDescriptor bDesc = (BeanDescriptor) ic.getParameters()[0];
186
187         if (icBean.equalsWithException(bDesc)) {
188             return ic.proceed();
189         }
190         throw new Exception JavaDoc("The referenced bean is not equal as the invocation context reference.");
191     }
192
193     /**
194      * Adds objects in the contextual data map. The key of the object is its
195      * array position.
196      * @param ic contains attributes of invocation
197      * @param objs objects to add.
198      * @return method's invocation result
199      * @throws Exception if invocation fails
200      */

201     @SuppressWarnings JavaDoc({"unchecked", "boxing"})
202     public static Object JavaDoc addContextData(final InvocationContext ic, final Object JavaDoc[] objs) throws Exception JavaDoc {
203         Map JavaDoc mContext = ic.getContextData();
204         if (objs != null) {
205             for (int i = 0; i < objs.length; i++) {
206                 mContext.put(i, objs[i]);
207             }
208         }
209         return ic.proceed();
210     }
211
212     /**
213      * Checks if the all keys exists in the contextual data map.
214      * @param ic contains attributes of invocation
215      * @param keys keys to check
216      * @return method's invocation result
217      * @throws Exception if invocation fails
218      */

219     public static Object JavaDoc checkContextKeys(final InvocationContext ic, final Object JavaDoc[] keys) throws Exception JavaDoc {
220         Map JavaDoc mContext = ic.getContextData();
221         if (keys != null) {
222             for (Object JavaDoc o : keys) {
223                 if (!mContext.containsKey(o)) {
224                     throw new Exception JavaDoc("Object not found.");
225                 }
226             }
227         }
228         return ic.proceed();
229     }
230
231     /**
232      * Checks if the all objects exists in the contextual map.
233      * @param ic contains attributes of invocation
234      * @param objs objects to check
235      * @return method's invocation result
236      * @throws Exception if invocation fails
237      */

238     public static Object JavaDoc checkContextData(final InvocationContext ic, final Object JavaDoc[] objs) throws Exception JavaDoc {
239         Map JavaDoc mContext = ic.getContextData();
240         if (objs != null) {
241             for (Object JavaDoc o : objs) {
242                 if (!mContext.containsValue(o)) {
243                     throw new Exception JavaDoc("Object not found.");
244                 }
245             }
246         }
247         return ic.proceed();
248     }
249
250     /**
251      * Clears the contextual map.
252      * @param ic contains attributes of invocation
253      * @return method's invocation result
254      * @throws Exception if invocation fails
255      */

256     public static Object JavaDoc clearContextData(final InvocationContext ic) throws Exception JavaDoc {
257         ic.getContextData().clear();
258         return ic.proceed();
259     }
260
261     /**
262      * Checks if context data is empty.
263      * @param ic contains attributes of invocation
264      * @return method's invocation result
265      * @throws Exception if invocation fails
266      */

267     public static Object JavaDoc isEmptyContextData(final InvocationContext ic) throws Exception JavaDoc {
268         if (ic.getContextData().isEmpty()){
269             return ic.proceed();
270         }
271         throw new Exception JavaDoc("Context data should be empty.");
272     }
273
274 }
275
Popular Tags