001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: InvocationContextHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.helper;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.Map;
029:
030: import javax.interceptor.InvocationContext;
031:
032: import org.ow2.easybeans.tests.common.interceptors.invocationcontext.BeanDescriptor;
033: import org.ow2.easybeans.tests.common.interceptors.invocationcontext.ComplexObject00;
034: import org.ow2.util.log.Log;
035: import org.ow2.util.log.LogFactory;
036:
037: /**
038: * This helper is used to manage the invocation context.
039: * @author Eduardo Studzinski Estima de Castro
040: * @author Gisele Pinheiro Souza
041: */
042: public final class InvocationContextHelper {
043:
044: /**
045: * Int value.
046: */
047: public static final Integer INT_VALUE_0 = new Integer(0);
048:
049: /**
050: * String value.
051: */
052: public static final String STR_VALUE_0 = "intValue0";
053:
054: /**
055: * Int value.
056: */
057: public static final Integer INT_VALUE_1 = new Integer(1);
058:
059: /**
060: * String value.
061: */
062: public static final String STR_VALUE_1 = "intValue1";
063:
064: /**
065: * Int value.
066: */
067: public static final Integer INT_VALUE_2 = new Integer(2);
068:
069: /**
070: * String value.
071: */
072: public static final String STR_VALUE_2 = "intValue2";
073:
074: /**
075: * Helper should have a private constructor.
076: */
077: private InvocationContextHelper() {
078:
079: }
080:
081: /**
082: * Returns an array of objects with the intercepted method parameters.
083: * @param ic contains attributes of invocation
084: * @return method's invocation result
085: * @throws Exception if invocation fails
086: */
087: public static Object getParametersArray(final InvocationContext ic)
088: throws Exception {
089: Log logger = LogFactory.getLog(InvocationContextHelper.class);
090:
091: logger.debug("Starting method getParametersArray...");
092:
093: Object[] arNew = null;
094: Object[] arParam = ic.getParameters();
095:
096: logger.debug("current parameters: {0}", arParam[0]);
097:
098: if (arParam != null) {
099: arNew = new Object[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}",
107: ic.getParameters()[0]);
108:
109: ic.setParameters(arNew);
110:
111: logger.debug("after setParameters(), {0}",
112: ic.getParameters()[0]);
113: logger.debug("Finishing method getParametersArray...");
114:
115: return ic.proceed();
116: }
117:
118: /**
119: * Sets null all intercepted method parameters.
120: * @param ic contains attributes of invocation
121: * @return method's invocation result
122: * @throws Exception if invocation fails
123: */
124: public static Object setParametersNull(final InvocationContext ic)
125: throws Exception {
126: Log logger = LogFactory.getLog(InvocationContextHelper.class);
127:
128: logger.debug("Starting method setParametersNull...");
129:
130: Object[] arParam = ic.getParameters();
131:
132: logger.debug("current parameters: {0}", arParam[0]);
133:
134: if (arParam != null) {
135: for (int i = 0; i < arParam.length; i++) {
136: arParam[i] = null;
137: }
138: }
139:
140: logger.debug("new parameters: {0}", arParam[0]);
141: logger.debug("before setParameters(), {0}",
142: ic.getParameters()[0]);
143:
144: ic.setParameters(arParam);
145:
146: logger.debug("after setParameters(), {0}",
147: ic.getParameters()[0]);
148: logger.debug("Finishing method setParametersNull...");
149:
150: return ic.proceed();
151: }
152:
153: /**
154: * Modifies the objects passed as parameters.
155: * @param ic contains attributes of invocation
156: * @return method's invocation result
157: * @throws Exception if invocation fails
158: */
159: @SuppressWarnings("boxing")
160: public static Object modifyParameters(final InvocationContext ic)
161: throws Exception {
162: Object[] objParams = ic.getParameters();
163:
164: // Modifies the first attributes
165: ComplexObject00 cmpObj = (ComplexObject00) objParams[0];
166: cmpObj.setHashCode(INT_VALUE_0);
167: cmpObj.setInterceptedMethod(STR_VALUE_0);
168:
169: // Creates a new list of bean descriptors
170: List<BeanDescriptor> lstDesc = new ArrayList<BeanDescriptor>();
171: lstDesc.add(new BeanDescriptor(INT_VALUE_1, STR_VALUE_1));
172: lstDesc.add(new BeanDescriptor(INT_VALUE_2, STR_VALUE_2));
173:
174: cmpObj.addDescriptors(lstDesc);
175:
176: ic.setParameters(objParams);
177:
178: return ic.proceed();
179: }
180:
181: /**
182: * Checks if the bean descriptor is the same. <li>The first parameter of
183: * the intercepted method must be a BeanDescriptor.</li>
184: * @param ic contains attributes of invocation
185: * @return method's invocation result
186: * @throws Exception if invocation fails
187: */
188: public static Object checkBeanDescriptor(final InvocationContext ic)
189: throws Exception {
190: // Log logger = LogFactory.getLog(InvocationContextHelper.class);
191: BeanDescriptor icBean = new BeanDescriptor(ic.getTarget()
192: .hashCode(), ic.getMethod().toString());
193: BeanDescriptor bDesc = (BeanDescriptor) ic.getParameters()[0];
194:
195: if (icBean.equalsWithException(bDesc)) {
196: return ic.proceed();
197: }
198: throw new Exception(
199: "The referenced bean is not equal as the invocation context reference.");
200: }
201:
202: /**
203: * Adds objects in the contextual data map. The key of the object is its
204: * array position.
205: * @param ic contains attributes of invocation
206: * @param objs objects to add.
207: * @return method's invocation result
208: * @throws Exception if invocation fails
209: */
210: @SuppressWarnings({"unchecked","boxing"})
211: public static Object addContextData(final InvocationContext ic,
212: final Object[] objs) throws Exception {
213: Map mContext = ic.getContextData();
214: if (objs != null) {
215: for (int i = 0; i < objs.length; i++) {
216: mContext.put(i, objs[i]);
217: }
218: }
219: return ic.proceed();
220: }
221:
222: /**
223: * Checks if the all keys exists in the contextual data map.
224: * @param ic contains attributes of invocation
225: * @param keys keys to check
226: * @return method's invocation result
227: * @throws Exception if invocation fails
228: */
229: public static Object checkContextKeys(final InvocationContext ic,
230: final Object[] keys) throws Exception {
231: Map mContext = ic.getContextData();
232: if (keys != null) {
233: for (Object o : keys) {
234: if (!mContext.containsKey(o)) {
235: throw new Exception("Object not found.");
236: }
237: }
238: }
239: return ic.proceed();
240: }
241:
242: /**
243: * Checks if the all objects exists in the contextual map.
244: * @param ic contains attributes of invocation
245: * @param objs objects to check
246: * @return method's invocation result
247: * @throws Exception if invocation fails
248: */
249: public static Object checkContextData(final InvocationContext ic,
250: final Object[] objs) throws Exception {
251: Map mContext = ic.getContextData();
252: if (objs != null) {
253: for (Object o : objs) {
254: if (!mContext.containsValue(o)) {
255: throw new Exception("Object not found.");
256: }
257: }
258: }
259: return ic.proceed();
260: }
261:
262: /**
263: * Clears the contextual map.
264: * @param ic contains attributes of invocation
265: * @return method's invocation result
266: * @throws Exception if invocation fails
267: */
268: public static Object clearContextData(final InvocationContext ic)
269: throws Exception {
270: ic.getContextData().clear();
271: return ic.proceed();
272: }
273:
274: /**
275: * Checks if context data is empty.
276: * @param ic contains attributes of invocation
277: * @return method's invocation result
278: * @throws Exception if invocation fails
279: */
280: public static Object isEmptyContextData(final InvocationContext ic)
281: throws Exception {
282: if (ic.getContextData().isEmpty()) {
283: return ic.proceed();
284: }
285: throw new Exception("Context data should be empty.");
286: }
287:
288: }
|