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
|
13
|
public MulticasterImpl()
|
36
|
|
{
|
37
|
13
|
this.objects = new LinkedList();
|
38
|
|
}
|
39
|
|
|
40
|
16
|
public void add(Object o)
|
41
|
|
{
|
42
|
16
|
objects.add(o);
|
43
|
|
}
|
44
|
|
|
45
|
2
|
public void remove(Object o)
|
46
|
|
{
|
47
|
2
|
objects.remove(o);
|
48
|
|
}
|
49
|
|
|
50
|
36
|
public boolean isEmpty()
|
51
|
|
{
|
52
|
36
|
return objects.isEmpty();
|
53
|
|
}
|
54
|
|
|
55
|
77
|
public Object invoke(Object proxy, Method method, Object[] parameters) throws Exception
|
56
|
|
{
|
57
|
77
|
Iterator it = objects.iterator();
|
58
|
77
|
while (it.hasNext())
|
59
|
|
{
|
60
|
80
|
Object o = it.next();
|
61
|
80
|
try
|
62
|
|
{
|
63
|
80
|
method.invoke(o, parameters);
|
64
|
|
}
|
65
|
|
catch (IllegalAccessException e)
|
66
|
|
{
|
67
|
0
|
throw new IllegalAccessError(e.getMessage());
|
68
|
|
}
|
69
|
|
catch (InvocationTargetException e)
|
70
|
|
{
|
71
|
3
|
throwRealException(e);
|
72
|
|
}
|
73
|
|
}
|
74
|
|
|
75
|
74
|
return null;
|
76
|
|
}
|
77
|
|
}
|
78
|
|
|
79
|
3
|
public static void throwRealException(InvocationTargetException e) throws Exception
|
80
|
|
{
|
81
|
3
|
Throwable realException = e.getTargetException();
|
82
|
3
|
if (realException instanceof Error)
|
83
|
|
{
|
84
|
1
|
throw (Error) realException;
|
85
|
|
}
|
86
|
2
|
else if (realException instanceof RuntimeException)
|
87
|
|
{
|
88
|
1
|
throw (RuntimeException) realException;
|
89
|
|
}
|
90
|
|
else
|
91
|
|
{
|
92
|
1
|
throw (Exception) realException;
|
93
|
|
}
|
94
|
|
}
|
95
|
|
|
96
|
5
|
public static Object create(Class itf)
|
97
|
|
{
|
98
|
5
|
return create(itf, false);
|
99
|
|
}
|
100
|
|
|
101
|
13
|
public static Object create(Class itf, boolean threadSafe)
|
102
|
|
{
|
103
|
13
|
MulticasterImpl multicast = new MulticasterImpl();
|
104
|
13
|
return Proxy.newProxyInstance(itf.getClassLoader(),
|
105
|
|
new Class[]{ itf },
|
106
|
|
multicast);
|
107
|
|
}
|
108
|
|
|
109
|
16
|
public static void add(Object multicasterProxy, Object o)
|
110
|
|
{
|
111
|
16
|
MulticasterImpl impl = (MulticasterImpl) Proxy.getInvocationHandler(multicasterProxy);
|
112
|
16
|
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
|
3
|
public static Object remove(Object multicasterProxy, Object o)
|
120
|
|
{
|
121
|
3
|
if (multicasterProxy != null)
|
122
|
|
{
|
123
|
2
|
MulticasterImpl impl = (MulticasterImpl) Proxy.getInvocationHandler(multicasterProxy);
|
124
|
2
|
impl.remove(o);
|
125
|
|
}
|
126
|
3
|
return multicasterProxy;
|
127
|
|
}
|
128
|
|
|
129
|
56
|
public static boolean isEmpty(Object multicasterProxy)
|
130
|
|
{
|
131
|
56
|
if (multicasterProxy == null)
|
132
|
20
|
return true;
|
133
|
|
else
|
134
|
|
{
|
135
|
36
|
MulticasterImpl impl = (MulticasterImpl) Proxy.getInvocationHandler(multicasterProxy);
|
136
|
36
|
return impl.isEmpty();
|
137
|
|
}
|
138
|
|
}
|
139
|
|
}
|
140
|
|
|