KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > Multicaster


1 /*
2  * $Id: Multicaster.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.lang.reflect.InvocationHandler JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.lang.reflect.Proxy JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * <code>Multicaster</code> is a utility that can call a given method on a
23  * collection of objects that implement one or more common interfaces. The create
24  * method returns a proxy that can be cast to any of the the interfaces passed and be
25  * used like a single object.
26  */

27 // @ThreadSafe
28
public class Multicaster
29 {
30
31     public static Object JavaDoc create(Class JavaDoc theInterface, Collection JavaDoc objects)
32     {
33         return create(new Class JavaDoc[]{theInterface}, objects);
34     }
35
36     public static Object JavaDoc create(Class JavaDoc theInterface, Collection JavaDoc objects, InvokeListener listener)
37     {
38         return create(new Class JavaDoc[]{theInterface}, objects, listener);
39     }
40
41     public static Object JavaDoc create(Class JavaDoc[] interfaces, Collection JavaDoc objects)
42     {
43         return create(interfaces, objects, null);
44     }
45
46     public static Object JavaDoc create(Class JavaDoc[] interfaces, Collection JavaDoc objects, InvokeListener listener)
47     {
48         return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces,
49             new CastingHandler(objects, listener));
50     }
51
52     private static class CastingHandler implements InvocationHandler JavaDoc
53     {
54         private final Collection JavaDoc objects;
55         private final InvokeListener listener;
56
57         public CastingHandler(Collection JavaDoc objects)
58         {
59             this(objects, null);
60         }
61
62         public CastingHandler(Collection JavaDoc objects, InvokeListener listener)
63         {
64             this.objects = objects;
65             this.listener = listener;
66         }
67
68         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
69         {
70             List JavaDoc results = new ArrayList JavaDoc();
71             Object JavaDoc item = null;
72             Object JavaDoc result;
73
74             for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
75             {
76                 try
77                 {
78                     item = iterator.next();
79                     result = method.invoke(item, args);
80                     if (listener != null)
81                     {
82                         listener.afterExecute(item, method, args);
83                     }
84                     if (result != null)
85                     {
86                         results.add(result);
87                     }
88                 }
89                 catch (Throwable JavaDoc t)
90                 {
91                     if (listener != null)
92                     {
93                         t = listener.onException(item, method, args, t);
94                         if (t != null)
95                         {
96                             throw t;
97                         }
98                     }
99                 }
100             }
101             return results;
102         }
103     }
104
105     public static interface InvokeListener
106     {
107         void afterExecute(Object JavaDoc object, Method JavaDoc method, Object JavaDoc[] args);
108
109         Throwable JavaDoc onException(Object JavaDoc object, Method JavaDoc method, Object JavaDoc[] args, Throwable JavaDoc t);
110     }
111
112 }
113
Popular Tags