View Javadoc

1   /**
2    * Atlassian Source Code Template.
3    * User: Bobby
4    * Date: Apr 8, 2003
5    * Time: 9:22:01 AM
6    * CVS Revision: $Revision: 1.2 $
7    * Last CVS Commit: $Date: 2005/05/11 07:35:56 $
8    * Author of last CVS Commit: $Author: mchai $
9    */
10  
11  package com.atlassian.johnson;
12  
13  import com.atlassian.johnson.event.Event;
14  
15  import java.util.*;
16  import javax.servlet.ServletContext;
17  
18  public class JohnsonEventContainer
19  {
20      public static String SERVLET_CONTEXT_KEY = JohnsonEventContainer.class.getName();
21  
22      private final static JohnsonEventContainer instance = new JohnsonEventContainer();
23  
24      public static JohnsonEventContainer getInstance()
25      {
26          return instance;
27      }
28  
29      public synchronized static JohnsonEventContainer get(ServletContext context)
30      {
31          if (context.getAttribute(SERVLET_CONTEXT_KEY) == null)
32          {
33              context.setAttribute(SERVLET_CONTEXT_KEY, getInstance());
34          }
35  
36          return (JohnsonEventContainer) context.getAttribute(SERVLET_CONTEXT_KEY);
37      }
38  
39      /* a WeakHashMap so that the garbage collector will destroy it if there are no links left to it.
40      This may be needed if the user presses the Stop button while an action is performing*/
41      private final Map events = new WeakHashMap();
42  
43  
44      /**
45       * Determine if there are Application Error Events
46       * @return true if there are application errors otherwise false
47       */
48      public synchronized boolean hasEvents()
49      {
50          return !events.isEmpty();
51      }
52  
53      /**
54       * Adds an application event to the events Map
55       * @param event New Event
56       */
57      public synchronized void addEvent(Event event)
58      {
59          events.put(event, event);
60      }
61  
62      /**
63       * Removes an application event from the events Map
64       * @param event Event to be removed
65       */
66      public synchronized void removeEvent(Event event)
67      {
68          events.remove(event);
69      }
70  
71      /**
72       * Gets a collection of the Event objects
73       * @return an unmodifiable connection 
74       */
75      public synchronized Collection getEvents()
76      {
77          //prevent concurrent modification - clone the values before returning them.
78          return Collections.unmodifiableCollection(new ArrayList(events.values()));
79      }
80  }