1 /*
2 * Angkor Web Framework
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7 package com.tirsen.angkor;
8
9 import java.lang.reflect.InvocationHandler;
10 import java.lang.reflect.InvocationTargetException;
11 import java.lang.reflect.Method;
12 import java.lang.reflect.Proxy;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Iterator;
16 import java.util.LinkedList;
17
18 /***
19 * Implements a multicaster which enables one to invoke the methods of an interface and the invocation
20 * is multicasted to several instances implementing the interface, also implements threadsafety making it
21 * possible to modify the Multicaster instance while multicasting.
22 *
23 * <!-- $Id: Multicaster.java,v 1.5 2002/10/13 13:37:26 tirsen Exp $ -->
24 *
25 * @author $Author: tirsen $
26 * @version $Revision: 1.5 $
27 */
28 public class Multicaster
29 {
30 private static class MulticasterImpl implements InvocationHandler
31 {
32 private Collection objects;
33 private boolean threadSafe;
34
35 public MulticasterImpl()
36 {
37 this.objects = new LinkedList();
38 }
39
40 public void add(Object o)
41 {
42 objects.add(o);
43 }
44
45 public void remove(Object o)
46 {
47 objects.remove(o);
48 }
49
50 public boolean isEmpty()
51 {
52 return objects.isEmpty();
53 }
54
55 public Object invoke(Object proxy, Method method, Object[] parameters) throws Exception
56 {
57 Iterator it = objects.iterator();
58 while (it.hasNext())
59 {
60 Object o = it.next();
61 try
62 {
63 method.invoke(o, parameters);
64 }
65 catch (IllegalAccessException e)
66 {
67 throw new IllegalAccessError(e.getMessage());
68 }
69 catch (InvocationTargetException e)
70 {
71 throwRealException(e);
72 }
73 }
74
75 return null;
76 }
77 }
78
79 public static void throwRealException(InvocationTargetException e) throws Exception
80 {
81 Throwable realException = e.getTargetException();
82 if (realException instanceof Error)
83 {
84 throw (Error) realException;
85 }
86 else if (realException instanceof RuntimeException)
87 {
88 throw (RuntimeException) realException;
89 }
90 else
91 {
92 throw (Exception) realException;
93 }
94 }
95
96 public static Object create(Class itf)
97 {
98 return create(itf, false);
99 }
100
101 public static Object create(Class itf, boolean threadSafe)
102 {
103 MulticasterImpl multicast = new MulticasterImpl();
104 return Proxy.newProxyInstance(itf.getClassLoader(),
105 new Class[]{ itf },
106 multicast);
107 }
108
109 public static void add(Object multicasterProxy, Object o)
110 {
111 MulticasterImpl impl = (MulticasterImpl) Proxy.getInvocationHandler(multicasterProxy);
112 impl.add(o);
113 }
114
115 /***
116 * Adds an object to the multicaster returns the same instance or null if it was empty.
117 * @return same instance or <code>null</code>.
118 */
119 public static Object remove(Object multicasterProxy, Object o)
120 {
121 if (multicasterProxy != null)
122 {
123 MulticasterImpl impl = (MulticasterImpl) Proxy.getInvocationHandler(multicasterProxy);
124 impl.remove(o);
125 }
126 return multicasterProxy;
127 }
128
129 public static boolean isEmpty(Object multicasterProxy)
130 {
131 if (multicasterProxy == null)
132 return true;
133 else
134 {
135 MulticasterImpl impl = (MulticasterImpl) Proxy.getInvocationHandler(multicasterProxy);
136 return impl.isEmpty();
137 }
138 }
139 }
This page was automatically generated by Maven