1 21 package com.db4o; 22 23 import com.db4o.reflect.*; 24 25 26 public final class EventDispatcher { 27 private static final String [] events = { 28 "objectCanDelete", 29 "objectOnDelete", 30 "objectOnActivate", 31 "objectOnDeactivate", 32 "objectOnNew", 33 "objectOnUpdate", 34 "objectCanActivate", 35 "objectCanDeactivate", 36 "objectCanNew", 37 "objectCanUpdate" 38 }; 39 40 static final int CAN_DELETE = 0; 41 static final int DELETE = 1; 42 static final int SERVER_COUNT = 2; 43 static final int ACTIVATE = 2; 44 static final int DEACTIVATE = 3; 45 static final int NEW = 4; 46 public static final int UPDATE = 5; 47 static final int CAN_ACTIVATE = 6; 48 static final int CAN_DEACTIVATE = 7; 49 static final int CAN_NEW = 8; 50 static final int CAN_UPDATE = 9; 51 static final int COUNT = 10; 52 53 private final ReflectMethod[] methods; 54 55 private EventDispatcher(ReflectMethod[] methods_){ 56 methods = methods_; 57 } 58 59 boolean dispatch(YapStream stream, Object obj, int eventID){ 60 if(methods[eventID] != null){ 61 Object [] parameters = new Object []{stream}; 62 int stackDepth = stream.stackDepth(); 63 int topLevelCallId = stream.topLevelCallId(); 64 stream.stackDepth(0); 65 try{ 66 Object res = methods[eventID].invoke(obj,parameters); 67 if(res != null && res instanceof Boolean ){ 68 return ((Boolean )res).booleanValue(); 69 } 70 }catch(Throwable t){ 71 73 } finally { 74 stream.stackDepth(stackDepth); 75 stream.topLevelCallId(topLevelCallId); 76 } 77 } 78 return true; 79 } 80 81 static EventDispatcher forClass(YapStream a_stream, ReflectClass classReflector){ 82 83 if(a_stream == null || classReflector == null){ 84 return null; 85 } 86 87 EventDispatcher dispatcher = null; 88 int count = 0; 89 if(a_stream.configImpl().callbacks()){ 90 count = COUNT; 91 }else if(a_stream.configImpl().isServer()){ 92 count = SERVER_COUNT; 93 } 94 if(count > 0){ 95 ReflectClass[] parameterClasses = {a_stream.i_handlers.ICLASS_OBJECTCONTAINER}; 96 ReflectMethod[] methods = new ReflectMethod[COUNT]; 97 for (int i = COUNT -1; i >=0; i--){ 98 try{ 99 ReflectMethod method = classReflector.getMethod(events[i], parameterClasses); 100 if (null == method) { 101 method = classReflector.getMethod(toPascalCase(events[i]), parameterClasses); 102 } 103 if( method != null){ 104 methods[i] = method; 105 if(dispatcher == null){ 106 dispatcher = new EventDispatcher(methods); 107 } 108 } 109 }catch(Throwable t){} 110 } 111 } 112 113 return dispatcher; 114 } 115 116 private static String toPascalCase(String name) { 117 return name.substring(0, 1).toUpperCase() + name.substring(1); 118 } 119 } 120 | Popular Tags |