1 8 9 15 package jfun.yan.lifecycle; 16 17 import java.util.Collections ; 18 import java.util.LinkedHashSet ; 19 import java.util.Set ; 20 21 import jfun.yan.Component; 22 23 24 33 public class LifecycleManager { 34 private final Set history = Collections.synchronizedSet(new LinkedHashSet ()); 36 40 public void addLiveObject(LiveObject lo){ 41 history.add(lo); 42 } 43 49 public Component withLifecycle(Component c, Life life){ 50 return c.mutate(new InstanceTracker(life, history)); 51 } 52 55 public Set getManagedInstances(){ 56 return history; 57 } 58 66 public void fifo(Object key, Object [] args, ExceptionHandler handler) 67 throws Throwable { 68 final LiveObject[] los = new LiveObject[history.size()]; 69 history.toArray(los); 70 for(int i=0; i<los.length; i++){ 71 final LiveObject lo = los[i]; 72 invokeOne(lo, key, args, handler); 73 } 74 } 75 83 public void filo(Object key, Object [] args, ExceptionHandler handler) 84 throws Throwable { 85 final LiveObject[] los = new LiveObject[history.size()]; 86 history.toArray(los); 87 for(int i=0; i<los.length; i++){ 88 final LiveObject lo = los[los.length-i-1]; 89 invokeOne(lo, key, args, handler); 90 } 91 } 92 private static void invokeOne(LiveObject lo, Object key, Object [] args, 93 ExceptionHandler handler) 94 throws Throwable { 95 if(!lo.containsPhase(key)) return; 96 try{ 97 lo.invoke(key, args); 98 } 99 catch(java.lang.reflect.InvocationTargetException e){ 100 handleException(e.getTargetException(), handler); 101 } 102 catch(Error e){ 103 throw e; 104 } 105 catch(Throwable e){ 106 handleException(e, handler); 107 } 108 } 109 private static void handleException(Throwable e, ExceptionHandler handler) 110 throws Throwable { 111 handler.handle(e); 112 } 113 } 114 | Popular Tags |