1 22 package org.objectweb.petals.engine.pojo; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.util.logging.Logger ; 27 28 import javax.jbi.component.ComponentContext; 29 import javax.jbi.messaging.DeliveryChannel; 30 import javax.jbi.messaging.MessageExchange; 31 32 import org.objectweb.petals.component.common.HandlingException; 33 import org.objectweb.petals.component.common.PEtALSComponentSDKException; 34 35 public class Pojo { 36 37 private Object object; 38 39 private PojoSE pojoSE; 40 41 private Method onExchangeMethod; 42 43 public Pojo(Object object, PojoSE pojoSE) { 44 this.object = object; 45 this.pojoSE = pojoSE; 46 } 47 48 public void init() throws PEtALSComponentSDKException { 49 if (object == null) { 50 throw new PEtALSComponentSDKException("POJO is null."); 51 } 52 setLoggerOnPojo(); 53 setContextOnPojo(); 54 setChannelOnPojo(); 55 56 call("init"); 57 58 setupOnExchangeMethod(); 59 } 60 61 public void start() throws PEtALSComponentSDKException { 62 if (object == null) { 63 throw new PEtALSComponentSDKException("POJO is null."); 64 } 65 call("start"); 66 } 67 68 public void stop() throws PEtALSComponentSDKException { 69 if (object == null) { 70 throw new PEtALSComponentSDKException("POJO is null."); 71 } 72 call("stop"); 73 } 74 75 81 public boolean callOnExchangeMethod(MessageExchange ex) 82 throws HandlingException { 83 84 boolean result = false; 85 try { 86 Object o = onExchangeMethod.invoke(object, ex); 87 88 result = (Boolean ) o; 89 90 } catch (IllegalArgumentException e) { 91 throw new HandlingException( 92 "java reflection exception during call on " 93 + onExchangeMethod.getName() + "() on the POJO.", e); 94 } catch (IllegalAccessException e) { 95 throw new HandlingException( 96 "java reflection exception during call on " 97 + onExchangeMethod.getName() + "() on the POJO.", e); 98 } catch (InvocationTargetException e) { 99 102 throw new HandlingException( 103 "processing exception during call on " 104 + onExchangeMethod.getName() + "() on the POJO.", e); 105 }catch (Throwable e) { 106 throw new HandlingException(e.getMessage(), e); 107 } 108 return result; 109 } 110 111 118 protected void setContextOnPojo() throws PEtALSComponentSDKException { 119 120 Method setContextMethod = findMethod(object.getClass(), "set", 121 ComponentContext.class); 122 123 if (setContextMethod != null) { 124 try { 125 setContextMethod.invoke(object, pojoSE.getContext()); 126 127 } catch (Throwable e) { 128 throw new PEtALSComponentSDKException( 129 "Can not set the ComponentContext on the POJO.", e); 130 } 131 } 132 } 133 134 141 protected void setChannelOnPojo() throws PEtALSComponentSDKException { 142 143 Method setChannelMethod = findMethod(object.getClass(), "set", 144 DeliveryChannel.class); 145 146 if (setChannelMethod != null) { 147 try { 148 setChannelMethod.invoke(object, pojoSE.getChannel()); 149 150 } catch (Throwable e) { 151 throw new PEtALSComponentSDKException( 152 "Can not set the DeliveryChannel on the POJO.", e); 153 } 154 } 155 } 156 157 164 protected void setupOnExchangeMethod() throws PEtALSComponentSDKException { 165 166 onExchangeMethod = findMethod(object.getClass(), "on", 167 MessageExchange.class); 168 169 if (onExchangeMethod == null) { 170 throw new PEtALSComponentSDKException( 171 "A handler method for MessageExchange is not found in " 172 + object.getClass()); 173 174 } 175 if (onExchangeMethod.getReturnType() != Boolean.TYPE) { 176 throw new PEtALSComponentSDKException( 177 "The handler method for MessageExchange must return a boolean."); 178 } 179 180 } 181 182 189 protected void call(String methodName) throws PEtALSComponentSDKException { 190 191 Method m = findMethod(object.getClass(), methodName, null); 192 193 if (m != null) { 194 try { 195 m.invoke(object); 196 } catch (IllegalArgumentException e) { 197 throw new PEtALSComponentSDKException( 198 "java reflection exception during call on " + methodName 199 + "() on the POJO.", e); 200 } catch (IllegalAccessException e) { 201 throw new PEtALSComponentSDKException( 202 "java reflection exception during call on " + methodName 203 + "() on the POJO.", e); 204 } catch (InvocationTargetException e) { 205 throw new PEtALSComponentSDKException( 206 "java reflection exception during call on " + methodName 207 + "() on the POJO.", e); 208 } catch (Throwable e) { 209 throw new PEtALSComponentSDKException(e.getMessage(), e); 210 } 211 212 } 213 } 214 215 222 protected void setLoggerOnPojo() throws PEtALSComponentSDKException { 223 224 Method setLoggerMethod = findMethod(object.getClass(), "set", 225 Logger .class); 226 227 if (setLoggerMethod != null) { 228 try { 229 setLoggerMethod.invoke(object, pojoSE.getLogger()); 230 231 } catch (Throwable e) { 232 throw new PEtALSComponentSDKException( 233 "Can not set the Logger on the POJO.", e); 234 } 235 } 236 } 237 238 251 protected static Method findMethod(Class clazz, String prefix, 252 Class parameterClass) { 253 Method m = null; 254 Method [] ms = clazz.getDeclaredMethods(); 255 256 for (int i = 0; i < ms.length && m == null; i++) { 257 258 Method tmpMethod = ms[i]; 259 260 if (tmpMethod.getName().startsWith(prefix)) { 261 262 if (parameterClass != null 263 && tmpMethod.getParameterTypes().length == 1 264 && tmpMethod.getParameterTypes()[0].equals(parameterClass)) { 265 266 m = tmpMethod; 267 } else if (parameterClass == null 268 && tmpMethod.getParameterTypes().length == 0) { 269 270 m = tmpMethod; 271 } 272 } 273 } 274 return m; 275 } 276 } 277 | Popular Tags |