KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > interceptor > MultiDispatcher


1 package org.sapia.ubik.rmi.interceptor;
2
3 import org.sapia.ubik.rmi.server.Log;
4
5 import java.lang.reflect.InvocationTargetException JavaDoc;
6 import java.lang.reflect.Method JavaDoc;
7
8 import java.util.*;
9
10
11 /**
12  * This dispatcher allows to register multiple interceptors
13  * for a given event.
14  *
15  * @author Yanick Duchesne
16  * <dl>
17  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
18  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
19  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
20  * </dl>
21  */

22 public class MultiDispatcher {
23   Map _interceptors = new HashMap();
24
25   /**
26    * Adds an interceptor for the given event type.
27    *
28    * @param event an event class.
29    * @param it an <code>Interceptor</code> instance.
30    *
31    * @throws InvalidInterceptorException if the interceptor could not be added.
32    */

33   public void addInterceptor(Class JavaDoc event, Interceptor it)
34     throws InvalidInterceptorException {
35     Class JavaDoc itClass = it.getClass();
36     int idx = event.getName().lastIndexOf('.');
37     String JavaDoc shortName;
38
39     if (idx < 0) {
40       shortName = event.getName();
41     } else {
42       shortName = event.getName().substring(idx + 1);
43     }
44
45     char[] content = shortName.toCharArray();
46     content[0] = Character.toUpperCase(content[0]);
47     shortName = "on" + new String JavaDoc(content);
48
49     Method JavaDoc m;
50
51     try {
52       m = itClass.getMethod(shortName, new Class JavaDoc[] { event });
53     } catch (Exception JavaDoc e) {
54       throw new InvalidInterceptorException(e);
55     }
56
57     List interceptors = (List) _interceptors.get(event);
58
59     if (interceptors == null) {
60       interceptors = new ArrayList();
61       _interceptors.put(event, interceptors);
62     }
63
64     interceptors.add(new InterceptorInfo(it, m));
65   }
66
67   /**
68    * Dispatches the given event to all interceptors that have
69    * registered for the event's class.
70    */

71   public void dispatch(Event event) {
72     List interceptors = (List) _interceptors.get(event.getClass());
73
74     if (interceptors == null) {
75       return;
76     }
77
78     InterceptorInfo info;
79
80     for (int i = 0; i < interceptors.size(); i++) {
81       info = (InterceptorInfo) interceptors.get(i);
82
83       try {
84         info.method.invoke(info.interceptor, new Object JavaDoc[] { event });
85       } catch (Throwable JavaDoc t) {
86         if (t instanceof RuntimeException JavaDoc) {
87           throw (RuntimeException JavaDoc) t;
88         } else if (t instanceof InvocationTargetException JavaDoc) {
89           Throwable JavaDoc t2 = ((InvocationTargetException JavaDoc) t).getTargetException();
90
91           if (t2 instanceof RuntimeException JavaDoc) {
92             throw (RuntimeException JavaDoc) t2;
93           }
94         }
95
96         handleError(t);
97       }
98     }
99   }
100
101   /**
102    * Template method that is called internally when an error is
103    * trapped when invoking the call-back method on a given
104    * interceptor instance.
105    */

106   protected void handleError(Throwable JavaDoc t) {
107     Log.error(getClass(), t);
108   }
109 }
110
Popular Tags