KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > event > AbstractApplicationEventMulticaster


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

16
17 package org.springframework.context.event;
18
19 import java.util.Collection JavaDoc;
20
21 import org.springframework.beans.BeanUtils;
22 import org.springframework.context.ApplicationListener;
23 import org.springframework.core.CollectionFactory;
24
25 /**
26  * Abstract implementation of the ApplicationEventMulticaster interface,
27  * providing the basic listener registration facility.
28  *
29  * <p>Doesn't permit multiple instances of the same listener by default,
30  * as it keeps listeners in a linked Set. The collection class used to hold
31  * ApplicationListener objects can be overridden through the "collectionClass"
32  * bean property.
33  *
34  * <p>Note that this class doesn't try to do anything clever to ensure thread
35  * safety if listeners are added or removed at runtime. A technique such as
36  * Copy-on-Write (Lea:137) could be used to ensure this, but the assumption in
37  * the basic version of the class is that listeners will be added at application
38  * configuration time and not added or removed as the application runs.
39  *
40  * <p>A custom collection class must be specified to allow for thread-safe
41  * runtime registration of listeners. A good candidate for this is Doug Lea's
42  * <code>java.util.concurrent.CopyOnWriteArraySet</code> or its non-JDK predecessor,
43  * <code>EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet</code> (or the
44  * respective CopyOnWriteArrayList version, allowing for registering the same
45  * listener multiple times). Those classes provide a thread-safe Iterator,
46  * optimized for read-mostly usage - matching this use case nicely.
47  *
48  * <p>Implementing ApplicationEventMulticaster's actual <code>multicastEvent</code>
49  * method is left to subclasses. SimpleApplicationEventMulticaster simply multicasts
50  * all events to all registered listeners, invoking them in the calling thread.
51  * Alternative implementations could be more sophisticated in those respects.
52  *
53  * @author Juergen Hoeller
54  * @since 1.2.3
55  * @see #setCollectionClass
56  * @see #getApplicationListeners()
57  * @see SimpleApplicationEventMulticaster
58  */

59 public abstract class AbstractApplicationEventMulticaster implements ApplicationEventMulticaster {
60
61     /** Collection of ApplicationListeners */
62     private Collection JavaDoc applicationListeners = CollectionFactory.createLinkedSetIfPossible(16);
63
64
65     /**
66      * Specify the collection class to use. Can be populated with a fully
67      * qualified class name when defined in a Spring application context.
68      * <p>Default is a linked HashSet, keeping the registration order.
69      * If no linked Set implementation is available, a plain HashSet will
70      * be used as fallback (not keeping the registration order).
71      * <p>Note that a Set class specified will not permit multiple instances
72      * of the same listener, while a List class will allow for registering
73      * the same listener multiple times.
74      * <p>Consider Doug Lea's <code>java.util.concurrent.CopyOnWriteArraySet</code> or its
75      * non-JDK predecessor, <code>EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet</code>
76      * (or the respective CopyOnWriteArrayList version). Those classes provide a thread-safe
77      * Iterator, optimized for read-mostly usage - matching this use case nicely.
78      * @see org.springframework.core.CollectionFactory#createLinkedSetIfPossible
79      * @see java.util.concurrent.CopyOnWriteArraySet
80      * @see EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet
81      */

82     public void setCollectionClass(Class JavaDoc collectionClass) {
83         if (collectionClass == null) {
84             throw new IllegalArgumentException JavaDoc("collectionClass must not be null");
85         }
86         if (!Collection JavaDoc.class.isAssignableFrom(collectionClass)) {
87             throw new IllegalArgumentException JavaDoc("collectionClass must implement [java.util.Collection]");
88         }
89         // Create desired collection instance.
90
// Add all previously registered listeners (usually none).
91
Collection JavaDoc newColl = (Collection JavaDoc) BeanUtils.instantiateClass(collectionClass);
92         newColl.addAll(this.applicationListeners);
93         this.applicationListeners = newColl;
94     }
95
96
97     public void addApplicationListener(ApplicationListener listener) {
98         this.applicationListeners.add(listener);
99     }
100
101     public void removeApplicationListener(ApplicationListener listener) {
102         this.applicationListeners.remove(listener);
103     }
104
105     public void removeAllListeners() {
106         this.applicationListeners.clear();
107     }
108
109     /**
110      * Return the current Collection of ApplicationListeners.
111      * <p>Note that this is the raw Collection of ApplicationListeners,
112      * potentially modified when new listeners get registered or
113      * existing ones get removed. This Collection is not a snapshot copy.
114      * @return a Collection of ApplicationListeners
115      * @see org.springframework.context.ApplicationListener
116      */

117     protected Collection JavaDoc getApplicationListeners() {
118         return applicationListeners;
119     }
120
121 }
122
Popular Tags