1 24 25 package com.mckoi.database; 26 27 import java.util.*; 28 import com.mckoi.util.HashMapList; 29 30 52 53 final class TriggerManager { 54 55 58 private TransactionSystem system; 59 60 64 private HashMapList listener_map; 65 66 70 private HashMapList table_map; 71 72 75 TriggerManager(TransactionSystem system) { 76 this.system = system; 77 listener_map = new HashMapList(); 78 table_map = new HashMapList(); 79 } 80 81 86 void flushTriggerEvents(final ArrayList event_list) { 87 for (int i = 0; i < event_list.size(); ++i) { 88 TriggerEvent evt = (TriggerEvent) event_list.get(i); 89 fireTrigger(evt); 90 } 91 } 92 93 102 synchronized void addTriggerListener(DatabaseConnection database, 103 String trigger_name, int event_id, String trigger_source, 104 TriggerListener listener) { 105 106 List list = listener_map.get(database); 108 for (int i = 0; i < list.size(); ++i) { 109 TriggerAction action = (TriggerAction) list.get(i); 110 if (action.getName().equals(trigger_name)) { 111 throw new Error ("Duplicate trigger name '" + trigger_name + "'"); 112 } 113 } 114 115 TriggerAction action = new TriggerAction(database, trigger_name, event_id, 116 trigger_source, listener); 117 118 listener_map.put(database, action); 119 table_map.put(trigger_source, action); 120 } 121 122 125 synchronized void removeTriggerListener(DatabaseConnection database, 126 String trigger_name) { 127 List list = listener_map.get(database); 128 for (int i = 0; i < list.size(); ++i) { 129 TriggerAction action = (TriggerAction) list.get(i); 130 if (action.getName().equals(trigger_name)) { 131 listener_map.remove(database, action); 132 table_map.remove(action.trigger_source, action); 133 return; 134 } 135 } 136 throw new Error ("Trigger name '" + trigger_name + "' not found."); 137 } 138 139 142 synchronized void clearAllDatabaseConnectionTriggers( 143 DatabaseConnection database) { 144 List list = listener_map.clear(database); 145 for (int i = 0; i < list.size(); ++i) { 146 TriggerAction action = (TriggerAction) list.get(i); 147 table_map.remove(action.trigger_source, action); 148 } 149 } 150 151 160 private void fireTrigger(final TriggerEvent evt) { 161 162 final ArrayList trig_list; 163 synchronized (this) { 167 List list = table_map.get(evt.getSource()); 168 if (list.size() == 0) { 169 return; 170 } 171 trig_list = new ArrayList(list); 172 } 173 174 Runnable runner = new Runnable () { 176 public void run() { 177 for (int i = 0; i < trig_list.size(); ++i) { 178 TriggerAction action = (TriggerAction) trig_list.get(i); 179 if (evt.getType() == action.trigger_event) { 180 action.listener.fireTrigger(action.database, action.trigger_name, 181 evt); 182 } 183 } 184 } 185 }; 186 187 system.postEvent(3, system.createEvent(runner)); 189 190 } 191 192 194 198 private static class TriggerAction { 199 200 private DatabaseConnection database; 201 private String trigger_name; private TriggerListener listener; private String trigger_source; private int trigger_event; 206 209 TriggerAction(DatabaseConnection database, String name, int type, 210 String trigger_source, TriggerListener listener) { 211 this.database = database; 212 this.trigger_name = name; 213 this.trigger_event = type; 214 this.listener = listener; 215 this.trigger_source = trigger_source; 216 } 217 218 221 public String getName() { 222 return trigger_name; 223 } 224 225 } 226 227 } 228 | Popular Tags |