KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > impl > RemoteExceptionCoordinatorImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.lib.impl;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
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  * Core implementation of {@link org.apache.hivemind.lib.RemoteExceptionCoordinator}.
27  *
28  * @author Howard Lewis Ship
29  */

30
31 public class RemoteExceptionCoordinatorImpl implements RemoteExceptionCoordinator
32 {
33     private boolean _locked;
34     private List JavaDoc _listeners;
35
36     private void checkLocked(String JavaDoc methodName)
37     {
38         if (_locked)
39             throw new ApplicationRuntimeException(ImplMessages.coordinatorLocked(methodName));
40     }
41
42     public synchronized void addRemoteExceptionListener(RemoteExceptionListener listener)
43     {
44         checkLocked("addRemoteExceptionListener");
45
46         if (_listeners == null)
47             _listeners = new ArrayList JavaDoc();
48
49         _listeners.add(listener);
50     }
51
52     public synchronized void removeRemoteExceptionListener(RemoteExceptionListener listener)
53     {
54         checkLocked("removeRemoteExceptionListener");
55
56         if (_listeners == null)
57             return;
58
59         _listeners.remove(listener);
60     }
61
62     public synchronized void fireRemoteExceptionDidOccur(Object JavaDoc source, Throwable JavaDoc exception)
63     {
64         checkLocked("sendNotification");
65
66         if (_listeners == null || _listeners.size() == 0)
67             return;
68
69         RemoteExceptionEvent event = new RemoteExceptionEvent(source, exception);
70
71         int count = _listeners.size();
72
73         _locked = true;
74
75         try
76         {
77
78             for (int i = 0; i < count; i++)
79             {
80                 RemoteExceptionListener listener = (RemoteExceptionListener) _listeners.get(i);
81
82                 listener.remoteExceptionDidOccur(event);
83             }
84         }
85         finally
86         {
87             _locked = false;
88         }
89
90     }
91
92 }
93
Popular Tags