1 9 package org.jboss.portal.core.impl.tree; 10 11 import org.jboss.portal.core.impl.tree.tm.TransactionManagerObserver; 12 import org.jboss.portal.core.impl.tree.tm.TransactionObservation; 13 import org.jboss.portal.core.impl.tree.tm.TransactionListener; 14 import org.jboss.cache.Fqn; 15 import org.apache.log4j.Logger; 16 17 import java.util.Map ; 18 import java.util.HashMap ; 19 20 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 21 22 28 class NodeStateEventAdapter 29 { 30 31 32 private final Logger log; 33 34 35 private final Map map; 36 37 38 private final TransactionManagerObserver observer; 39 40 41 private final NodeStateEventListener handler; 42 43 NodeStateEventAdapter(TransactionManagerObserver observer, NodeStateEventListener handler) 44 { 45 if (observer == null) 46 { 47 throw new IllegalArgumentException ("Provider cannot be null"); 48 } 49 if (handler == null) 50 { 51 throw new IllegalArgumentException ("Handler cannot be null"); 52 } 53 this.log = Logger.getLogger(NodeStateEventAdapter.class); 54 this.map = new ConcurrentHashMap(); 55 this.observer = observer; 56 this.handler = handler; 57 } 58 59 void created(Fqn fqn) throws IllegalStateException 60 { 61 Callback callback = getCallback(); 62 63 if (callback != null) 64 { 65 NodeStateEvent ns = callback.getNodeState(fqn); 66 if (ns == null) 67 { 68 ns = new NodeStateEvent(fqn, NodeStateEvent.STATUS_CREATED); 69 callback.putNodeState(fqn, ns); 70 } 71 else 72 { 73 switch (ns.getStatus()) 74 { 75 case NodeStateEvent.STATUS_CREATED: 76 throw new Error ("impossible"); 77 case NodeStateEvent.STATUS_MODIFIED: 78 throw new Error ("impossible"); 79 case NodeStateEvent.STATUS_REMOVED: 80 ns.setStatus(NodeStateEvent.STATUS_CREATED); 81 break; 82 } 83 } 84 } 85 else 86 { 87 NodeStateEvent ns = new NodeStateEvent(fqn, NodeStateEvent.STATUS_CREATED); 88 handler.handle(new NodeStateEvent[]{ns}); 89 } 90 91 } 92 93 void modified(Fqn fqn) throws IllegalStateException 94 { 95 Callback callback = getCallback(); 96 97 if (callback != null) 98 { 99 NodeStateEvent ns = callback.getNodeState(fqn); 100 if (ns == null) 101 { 102 ns = new NodeStateEvent(fqn, NodeStateEvent.STATUS_MODIFIED); 103 callback.putNodeState(fqn, ns); 104 } 105 else 106 { 107 switch (ns.getStatus()) 108 { 109 case NodeStateEvent.STATUS_CREATED: 110 break; 111 case NodeStateEvent.STATUS_MODIFIED: 112 break; 113 case NodeStateEvent.STATUS_REMOVED: 114 throw new Error ("impossible"); 115 } 116 } 117 } 118 else 119 { 120 NodeStateEvent ns = new NodeStateEvent(fqn, NodeStateEvent.STATUS_MODIFIED); 121 handler.handle(new NodeStateEvent[]{ns}); 122 } 123 } 124 125 void removed(Fqn fqn) throws IllegalStateException 126 { 127 Callback callback = getCallback(); 128 129 if (callback != null) 130 { 131 NodeStateEvent ns = callback.getNodeState(fqn); 132 if (ns == null) 133 { 134 ns = new NodeStateEvent(fqn, NodeStateEvent.STATUS_REMOVED); 135 callback.putNodeState(fqn, ns); 136 } 137 else 138 { 139 switch (ns.getStatus()) 140 { 141 case NodeStateEvent.STATUS_CREATED: 142 ns.setStatus(NodeStateEvent.STATUS_REMOVED); 143 break; 144 case NodeStateEvent.STATUS_MODIFIED: 145 ns.setStatus(NodeStateEvent.STATUS_REMOVED); 146 break; 147 case NodeStateEvent.STATUS_REMOVED: 148 throw new Error ("impossible"); 149 } 150 } 151 } 152 else 153 { 154 NodeStateEvent ns = new NodeStateEvent(fqn, NodeStateEvent.STATUS_REMOVED); 155 handler.handle(new NodeStateEvent[]{ns}); 156 } 157 158 } 159 160 private Callback getCallback() throws IllegalStateException 161 { 162 TransactionObservation observation = observer.getObservation(); 163 if (observation == null) 164 { 165 return null; 166 } 167 Callback cb = (Callback)map.get(observation); 168 if (cb == null) 169 { 170 cb = new Callback(observation); 171 observation.addListener(cb); 172 map.put(observation, cb); 173 } 174 return cb; 175 } 176 177 private class Callback implements TransactionListener 178 { 179 180 private final TransactionObservation observation; 181 private final Map nodeStates = new HashMap (); 182 183 private Callback(TransactionObservation observation) 184 { 185 this.observation = observation; 186 } 187 188 public NodeStateEvent getNodeState(Fqn fqn) 189 { 190 return (NodeStateEvent)nodeStates.get(fqn); 191 } 192 193 public void putNodeState(Fqn fqn, NodeStateEvent node) 194 { 195 nodeStates.put(fqn, node); 196 } 197 198 199 public void afterCommit() 200 { 201 map.remove(observation); 202 NodeStateEvent[] states = new NodeStateEvent[this.nodeStates.size()]; 203 this.nodeStates.values().toArray(states); 204 handler.handle(states); 205 } 206 207 public void afterRollack() 208 { 209 map.remove(observation); 210 } 211 } 212 } 213 | Popular Tags |