1 package org.oddjob.arooa; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.lang.reflect.Method ; 5 6 import org.apache.log4j.Logger; 7 8 13 public class Lifecycle { 14 private static final Logger logger = Logger.getLogger(Lifecycle.class); 15 16 public static boolean setContext(Object object, ArooaContext context) { 17 Class bean = object.getClass(); 18 Method method = null; 19 try { 20 method = bean.getMethod("setContext", 21 new Class [] { ArooaContext.class }); 22 } catch (NoSuchMethodException e) { 23 return false; 24 } 25 try { 26 Object result = 27 method.invoke(object, new Object [] { context }); 28 if (result == null) { 29 return true; 30 } 31 if (result instanceof Boolean ) { 32 return ((Boolean ) result).booleanValue(); 33 } 34 logger.debug("Unexpected return type [" + result.getClass().getName() + "]"); 35 return true; 36 } catch (IllegalAccessException e) { 37 logger.debug(e); 38 } catch (InvocationTargetException e) { 39 logger.debug(e); 40 } 41 return false; 42 } 43 44 49 public static void init(Object object) { 50 Class bean = object.getClass(); 51 Method method = null; 52 try { 53 method = bean.getMethod("init", new Class [0]); 54 } catch (NoSuchMethodException e) { 55 return; 56 } 57 try { 58 method.invoke(object, new Object [0]); 59 } catch (IllegalAccessException e) { 60 } catch (InvocationTargetException e) { 62 } 64 } 65 66 71 public static void destroy(Object [] objects) { 72 73 for (int i = 0; i < objects.length; ++i) { 74 destroy(objects[i]); 75 } 76 } 77 78 83 public static void destroy(Object object) { 84 85 Class bean = object.getClass(); 86 Method destroy = null; 87 try { 88 destroy = bean.getMethod("destroy", new Class [0]); 89 } catch (NoSuchMethodException e) { 90 return; 91 } 92 try { 93 destroy.invoke(object, new Object [0]); 94 } catch (IllegalAccessException e) { 95 logger.error("Exception destroying [" + object + "]", e); 96 } catch (InvocationTargetException e) { 97 logger.error("Exception destroying [" + object + "]", e); 98 } 99 } 100 101 } 102 | Popular Tags |