1 25 package org.objectweb.easybeans.tests.common.helper; 26 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.Map ; 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 43 public final class InvocationContextHelper { 44 45 48 public static final Integer INT_VALUE_0 = new Integer (0); 49 50 53 public static final String STR_VALUE_0 = "intValue0"; 54 55 58 public static final Integer INT_VALUE_1 = new Integer (1); 59 60 63 public static final String STR_VALUE_1 = "intValue1"; 64 65 68 public static final Integer INT_VALUE_2 = new Integer (2); 69 70 73 public static final String STR_VALUE_2 = "intValue2"; 74 75 78 private InvocationContextHelper() { 79 80 } 81 82 88 public static Object getParametersArray(final InvocationContext ic) throws Exception { 89 JLog logger = JLogFactory.getLog(InvocationContextHelper.class); 90 91 logger.debug("Starting method getParametersArray..."); 92 93 Object [] arNew = null; 94 Object [] arParam = ic.getParameters(); 95 96 logger.debug("current parameters: {0}", arParam[0]); 97 98 if (arParam != null) { 99 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}", 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 122 public static Object setParametersNull(final InvocationContext ic) throws Exception { 123 JLog logger = JLogFactory.getLog(InvocationContextHelper.class); 124 125 logger.debug("Starting method setParametersNull..."); 126 127 Object [] 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 154 @SuppressWarnings ("boxing") 155 public static Object modifyParameters(final InvocationContext ic) throws Exception { 156 Object [] objParams = ic.getParameters(); 157 158 ComplexObject00 cmpObj = (ComplexObject00) objParams[0]; 160 cmpObj.setHashCode(INT_VALUE_0); 161 cmpObj.setInterceptedMethod(STR_VALUE_0); 162 163 List <BeanDescriptor> lstDesc = new ArrayList <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 182 public static Object checkBeanDescriptor(final InvocationContext ic) throws Exception { 183 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 ("The referenced bean is not equal as the invocation context reference."); 191 } 192 193 201 @SuppressWarnings ({"unchecked", "boxing"}) 202 public static Object addContextData(final InvocationContext ic, final Object [] objs) throws Exception { 203 Map 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 219 public static Object checkContextKeys(final InvocationContext ic, final Object [] keys) throws Exception { 220 Map mContext = ic.getContextData(); 221 if (keys != null) { 222 for (Object o : keys) { 223 if (!mContext.containsKey(o)) { 224 throw new Exception ("Object not found."); 225 } 226 } 227 } 228 return ic.proceed(); 229 } 230 231 238 public static Object checkContextData(final InvocationContext ic, final Object [] objs) throws Exception { 239 Map mContext = ic.getContextData(); 240 if (objs != null) { 241 for (Object o : objs) { 242 if (!mContext.containsValue(o)) { 243 throw new Exception ("Object not found."); 244 } 245 } 246 } 247 return ic.proceed(); 248 } 249 250 256 public static Object clearContextData(final InvocationContext ic) throws Exception { 257 ic.getContextData().clear(); 258 return ic.proceed(); 259 } 260 261 267 public static Object isEmptyContextData(final InvocationContext ic) throws Exception { 268 if (ic.getContextData().isEmpty()){ 269 return ic.proceed(); 270 } 271 throw new Exception ("Context data should be empty."); 272 } 273 274 } 275 | Popular Tags |