KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > CmsEventManager


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsEventManager.java,v $
3  * Date : $Date: 2006/04/28 15:20:52 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2005 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.main;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import org.apache.commons.logging.Log;
42
43 /**
44  * Manager that controls the OpenCms event system.
45  *
46  * There is only one instance of this event manager class used by the OpenCms runtime.
47  * This instance can be obtained by calling {@link OpenCms#getEventManager()}.<p>
48  *
49  * Events can be used in OpenCms to notify custom event listeners that certain system events have happened.
50  * Event listeners have to implement the interface {@link org.opencms.main.I_CmsEventListener}.<p>
51  *
52  * @author Alexander Kandzior
53  *
54  * @version $Revision: 1.2 $
55  *
56  * @since 7.0.0
57  *
58  * @see org.opencms.main.CmsEvent
59  * @see org.opencms.main.I_CmsEventListener
60  */

61 public class CmsEventManager {
62
63     /** Required as template for event list generation. */
64     protected static final I_CmsEventListener[] EVENT_LIST = new I_CmsEventListener[0];
65
66     /** The static log object for this class. */
67     private static final Log LOG = CmsLog.getLog(CmsEventManager.class);
68
69     /** Stores the active event listeners. */
70     private Map JavaDoc m_eventListeners;
71
72     /**
73      * Create a new instance of an OpenCms event manager.<p>
74      */

75     public CmsEventManager() {
76
77         m_eventListeners = new HashMap JavaDoc();
78     }
79
80     /**
81      * Add a cms event listener that listens to all events.<p>
82      *
83      * @param listener the listener to add
84      */

85     public void addCmsEventListener(I_CmsEventListener listener) {
86
87         addCmsEventListener(listener, null);
88     }
89
90     /**
91      * Add a cms event listener.<p>
92      *
93      * @param listener the listener to add
94      * @param eventTypes the events to listen for
95      */

96     public void addCmsEventListener(I_CmsEventListener listener, int[] eventTypes) {
97
98         synchronized (m_eventListeners) {
99             if (eventTypes == null) {
100                 // no event types given - register the listener for all event types
101
eventTypes = new int[] {I_CmsEventListener.LISTENERS_FOR_ALL_EVENTS.intValue()};
102             }
103             for (int i = 0; i < eventTypes.length; i++) {
104                 // register the listener for all configured event types
105
Integer JavaDoc eventType = new Integer JavaDoc(eventTypes[i]);
106                 List JavaDoc listeners = (List JavaDoc)m_eventListeners.get(eventType);
107                 if (listeners == null) {
108                     listeners = new ArrayList JavaDoc();
109                     m_eventListeners.put(eventType, listeners);
110                 }
111                 if (!listeners.contains(listener)) {
112                     // add listerner only if it is not already registered
113
listeners.add(listener);
114                 }
115             }
116         }
117     }
118
119     /**
120      * Notify all event listeners that a particular event has occurred.<p>
121      *
122      * @param event the event that is forwarded to all listeners
123      */

124     public void fireEvent(CmsEvent event) {
125
126         fireEventHandler((List JavaDoc)m_eventListeners.get(event.getTypeInteger()), event);
127         fireEventHandler((List JavaDoc)m_eventListeners.get(I_CmsEventListener.LISTENERS_FOR_ALL_EVENTS), event);
128     }
129
130     /**
131      * Notify all event listeners that a particular event has occurred without any additional event data.<p>
132      *
133      * @param type event type
134      */

135     public void fireEvent(int type) {
136
137         fireEvent(type, Collections.EMPTY_MAP);
138     }
139
140     /**
141      * Notify all event listeners that a particular event has occurred.<p>
142      *
143      * @param type event type
144      * @param data event data
145      */

146     public void fireEvent(int type, Map JavaDoc data) {
147
148         fireEvent(new CmsEvent(type, data));
149     }
150
151     /**
152      * Removes a cms event listener.<p>
153      *
154      * @param listener the listener to remove
155      */

156     public void removeCmsEventListener(I_CmsEventListener listener) {
157
158         synchronized (m_eventListeners) {
159             Iterator JavaDoc it = m_eventListeners.keySet().iterator();
160             while (it.hasNext()) {
161                 List JavaDoc listeners = (List JavaDoc)m_eventListeners.get(it.next());
162                 listeners.remove(listener);
163             }
164         }
165     }
166
167     /**
168      * Returns the map of all configured event listeners.<p>
169      *
170      * @return the map of all configured event listeners
171      */

172     protected Map JavaDoc getEventListeners() {
173
174         return m_eventListeners;
175     }
176
177     /**
178      * Initialize this event manager with all events from the given base event manager.<p>
179      *
180      * @param base the base event manager to initialize this event manager with
181      */

182     protected void initialize(CmsEventManager base) {
183
184         m_eventListeners = new HashMap JavaDoc(base.getEventListeners());
185     }
186
187     /**
188      * Fires the specified event to a list of event listeners.<p>
189      *
190      * @param listeners the listeners to fire
191      * @param event the event to fire
192      */

193     protected void fireEventHandler(List JavaDoc listeners, CmsEvent event) {
194
195         if (!LOG.isDebugEnabled()) {
196             // no logging required
197
if ((listeners != null) && (listeners.size() > 0)) {
198                 // handle all event listeners that listen to this event type
199
I_CmsEventListener[] list = (I_CmsEventListener[])listeners.toArray(EVENT_LIST);
200                 // loop through all registered event listeners
201
for (int i = 0; i < list.length; i++) {
202                     // fire the event
203
list[i].cmsEvent(event);
204                 }
205             }
206         } else {
207             // add lots of event debug output (this should usually be disabled)
208
// repeat event handling code to avoid multiple "is log enabled" checks in normal operation
209
LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_EVENT_1, event.toString()));
210             if ((listeners != null) && (listeners.size() > 0)) {
211                 // handle all event listeners that listen to this event type
212
I_CmsEventListener[] list = (I_CmsEventListener[])listeners.toArray(EVENT_LIST);
213                 // log the event data
214
if (event.getData() != null) {
215                     Iterator JavaDoc i = event.getData().keySet().iterator();
216                     while (i.hasNext()) {
217                         String JavaDoc key = (String JavaDoc)i.next();
218                         Object JavaDoc value = event.getData().get(key);
219                         LOG.debug(Messages.get().getBundle().key(
220                             Messages.LOG_DEBUG_EVENT_VALUE_3,
221                             key,
222                             value,
223                             event.toString()));
224                     }
225                 } else {
226                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_NO_EVENT_VALUE_1, event.toString()));
227                 }
228                 // log all the registered event listeners
229
for (int j = 0; j < list.length; j++) {
230                     LOG.debug(Messages.get().getBundle().key(
231                         Messages.LOG_DEBUG_EVENT_LISTENERS_3,
232                         list[j],
233                         new Integer JavaDoc(j),
234                         event.toString()));
235                 }
236                 // loop through all registered event listeners
237
for (int i = 0; i < list.length; i++) {
238                     LOG.debug(Messages.get().getBundle().key(
239                         Messages.LOG_DEBUG_EVENT_START_LISTENER_3,
240                         list[i],
241                         new Integer JavaDoc(i),
242                         event.toString()));
243                     // fire the event
244
list[i].cmsEvent(event);
245                     LOG.debug(Messages.get().getBundle().key(
246                         Messages.LOG_DEBUG_EVENT_END_LISTENER_3,
247                         list[i],
248                         new Integer JavaDoc(i),
249                         event.toString()));
250                 }
251             } else {
252                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_EVENT_NO_LISTENER_1, event.toString()));
253             }
254             LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_EVENT_COMPLETE_1, event.toString()));
255         }
256     }
257 }
Popular Tags