| 
 1 | 
  
 |  | 
| 
 2 | 
  
 |  | 
| 
 3 | 
  
 |  | 
| 
 4 | 
  
 |  | 
| 
 5 | 
  
 |  | 
| 
 6 | 
  
 |  | 
| 
 7 | 
  
 |  | 
| 
 8 | 
  
 |  | 
| 
 9 | 
  
 |  | 
| 
 10 | 
  
 |  | 
| 
 11 | 
  
 |  | 
| 
 12 | 
  
 |  | 
| 
 13 | 
  
 |  | 
| 
 14 | 
  
 |  | 
| 
 15 | 
  
 | package org.apache.hivemind.lib.impl; | 
| 
 16 | 
  
 |  | 
| 
 17 | 
  
 | import java.util.ArrayList; | 
| 
 18 | 
  
 | import java.util.List; | 
| 
 19 | 
  
 |  | 
| 
 20 | 
  
 | import org.apache.hivemind.ApplicationRuntimeException; | 
| 
 21 | 
  
 | import org.apache.hivemind.lib.RemoteExceptionCoordinator; | 
| 
 22 | 
  
 | import org.apache.hivemind.lib.RemoteExceptionEvent; | 
| 
 23 | 
  
 | import org.apache.hivemind.lib.RemoteExceptionListener; | 
| 
 24 | 
  
 |  | 
| 
 25 | 
  
 |  | 
| 
 26 | 
  
 |  | 
| 
 27 | 
  
 |  | 
| 
 28 | 
  
 |  | 
| 
 29 | 
  
 |  | 
| 
 30 | 
  
 |  | 
| 
 31 | 
  
 | public class RemoteExceptionCoordinatorImpl implements RemoteExceptionCoordinator | 
| 
 32 | 
  
 | { | 
| 
 33 | 
  
 |     private boolean _locked; | 
| 
 34 | 
  
 |     private List _listeners; | 
| 
 35 | 
  
 |  | 
| 
 36 | 
 2
 |     private void checkLocked(String methodName)
 | 
| 
 37 | 
  
 |     { | 
| 
 38 | 
 2
 |         if (_locked)
 | 
| 
 39 | 
 0
 |             throw new ApplicationRuntimeException(ImplMessages.coordinatorLocked(methodName));
 | 
| 
 40 | 
  
 |     } | 
| 
 41 | 
  
 |  | 
| 
 42 | 
 0
 |     public synchronized void addRemoteExceptionListener(RemoteExceptionListener listener)
 | 
| 
 43 | 
  
 |     { | 
| 
 44 | 
 0
 |         checkLocked("addRemoteExceptionListener");
 | 
| 
 45 | 
  
 |  | 
| 
 46 | 
 0
 |         if (_listeners == null)
 | 
| 
 47 | 
 0
 |             _listeners = new ArrayList();
 | 
| 
 48 | 
  
 |  | 
| 
 49 | 
 0
 |         _listeners.add(listener);
 | 
| 
 50 | 
  
 |     } | 
| 
 51 | 
  
 |  | 
| 
 52 | 
 0
 |     public synchronized void removeRemoteExceptionListener(RemoteExceptionListener listener)
 | 
| 
 53 | 
  
 |     { | 
| 
 54 | 
 0
 |         checkLocked("removeRemoteExceptionListener");
 | 
| 
 55 | 
  
 |  | 
| 
 56 | 
 0
 |         if (_listeners == null)
 | 
| 
 57 | 
 0
 |             return;
 | 
| 
 58 | 
  
 |  | 
| 
 59 | 
 0
 |         _listeners.remove(listener);
 | 
| 
 60 | 
  
 |     } | 
| 
 61 | 
  
 |  | 
| 
 62 | 
 2
 |     public synchronized void fireRemoteExceptionDidOccur(Object source, Throwable exception)
 | 
| 
 63 | 
  
 |     { | 
| 
 64 | 
 2
 |         checkLocked("sendNotification");
 | 
| 
 65 | 
  
 |  | 
| 
 66 | 
 2
 |         if (_listeners == null || _listeners.size() == 0)
 | 
| 
 67 | 
 2
 |             return;
 | 
| 
 68 | 
  
 |  | 
| 
 69 | 
 0
 |         RemoteExceptionEvent event = new RemoteExceptionEvent(source, exception);
 | 
| 
 70 | 
  
 |  | 
| 
 71 | 
 0
 |         int count = _listeners.size();
 | 
| 
 72 | 
  
 |  | 
| 
 73 | 
 0
 |         _locked = true;
 | 
| 
 74 | 
  
 |  | 
| 
 75 | 
 0
 |         try
 | 
| 
 76 | 
  
 |         { | 
| 
 77 | 
  
 |  | 
| 
 78 | 
 0
 |             for (int i = 0; i < count; i++)
 | 
| 
 79 | 
  
 |             { | 
| 
 80 | 
 0
 |                 RemoteExceptionListener listener = (RemoteExceptionListener) _listeners.get(i);
 | 
| 
 81 | 
  
 |  | 
| 
 82 | 
 0
 |                 listener.remoteExceptionDidOccur(event);
 | 
| 
 83 | 
  
 |             } | 
| 
 84 | 
  
 |         } | 
| 
 85 | 
  
 |         finally | 
| 
 86 | 
  
 |         { | 
| 
 87 | 
 0
 |             _locked = false;
 | 
| 
 88 | 
  
 |         } | 
| 
 89 | 
  
 |  | 
| 
 90 | 
  
 |     } | 
| 
 91 | 
  
 |  | 
| 
 92 | 
  
 | } |