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: InterceptorHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.helper;
025:
026: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType.UNDEFINED;
027: import static org.ow2.easybeans.tests.common.helper.EJBHelper.getBeanRemoteInstance;
028:
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: import javax.interceptor.InvocationContext;
033: import javax.jms.Message;
034:
035: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.ItfCallbackLoggerAccess;
036: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.SLSBCallbackLoggerAccess;
037: import org.ow2.util.log.Log;
038: import org.ow2.util.log.LogFactory;
039:
040: /**
041: * This helper is used to do operation with InvocationContext and interceptors.
042: * @author Eduardo Studzinski Estima de Castro
043: * @author Gisele Pinheiro Souza
044: */
045: public final class InterceptorHelper {
046:
047: /**
048: * Logger.
049: */
050: private static Log logger = LogFactory
051: .getLog(InterceptorHelper.class);
052:
053: /**
054: * Helper should have a private constructor.
055: */
056: private InterceptorHelper() {
057: }
058:
059: /**
060: * Intercepts the method and adds a value in the list that was get from
061: * InvocationContext.
062: * @param <E> Element Type
063: * @param invocationContext contains attributes of invocation, the first
064: * parameter of the intercepted method must be a list or a message.
065: * @param value value to add
066: * @param className class that is invoking this method.
067: * @return method's invocation result
068: * @throws Exception if invocation fails
069: */
070: @SuppressWarnings("unchecked")
071: public static <E> Object addValue(
072: final InvocationContext invocationContext, final E value,
073: final String className) throws Exception {
074: ItfCallbackLoggerAccess beanLogger = getBeanRemoteInstance(
075: SLSBCallbackLoggerAccess.class,
076: ItfCallbackLoggerAccess.class);
077: boolean added = false;
078:
079: Object[] arObj = invocationContext.getParameters();
080:
081: List<E> arOrder = null;
082:
083: if (arObj[0] instanceof List) {
084: //Standard interceptor
085: arOrder = (List<E>) arObj[0];
086: added = arOrder.add(value);
087:
088: } else if (arObj[0] instanceof Message) {
089: //Log event
090: beanLogger.insertCallbackLogger(invocationContext
091: .getTarget().getClass().toString(), UNDEFINED,
092: className);
093: added = true;
094: }
095:
096: // If can't add, throw an exception to avoid cascade errors.
097: if (!added) {
098: throw new IllegalStateException(className
099: + " can not add the value " + value + ".");
100: }
101: logger.debug(
102: "Added: target={0}, interceptorName={1}, order={2}",
103: invocationContext.getTarget().toString(), className,
104: value);
105: return invocationContext.proceed();
106: }
107:
108: /**
109: * Gets the string representation of a list.
110: * @param list object to get the string representation.
111: * @return string representation of the list.
112: */
113: public static String getString(final List list) {
114: StringBuffer result = new StringBuffer("");
115: if (list != null) {
116: for (Object o : list) {
117: result.append(",");
118: result.append(o.toString());
119: }
120: result.replace(0, 0, "");
121: }
122: logger.debug("Result string = {0}", result.toString());
123: return result.toString();
124: }
125:
126: /**
127: * Gets the array from string representation.
128: * @param str representation of the list.
129: * @return list obtained using the string representation.
130: */
131: @SuppressWarnings({"boxing","unchecked"})
132: public static List getArray(final String str) {
133: List lst = new ArrayList<Integer>();
134:
135: if (str != null) {
136: String[] values = str.split(",");
137: for (int i = 0; i < values.length; i++) {
138: lst.add(Integer.parseInt(values[i]));
139: }
140: }
141: return lst;
142: }
143:
144: /**
145: * Gets the error msg formatted.
146: * @param expected values to a correct answer
147: * @param result result values after an execution
148: * @param seedMessage standard message to show
149: * @return error message
150: */
151: public static String getPrintOrderErrorMsg(final List expected,
152: final List result, final String seedMessage) {
153: String strMessage = seedMessage;
154:
155: if (expected == null & result == null) {
156: strMessage += " Result=[null], Expected=[null].";
157: } else if (expected == null) {
158: strMessage += " Result=" + result.toString()
159: + ", Expected=[null].";
160: } else if (result == null) {
161: strMessage += " Result=[null], Expected="
162: + expected.toString() + ".";
163: } else {
164: strMessage += " Actual=" + result.toString()
165: + ", Expected=" + expected.toString() + ".";
166: }
167: return strMessage;
168: }
169: }
|