View Javadoc

1   package com.atlassian.johnson;
2   
3   import com.atlassian.johnson.config.JohnsonConfig;
4   import com.atlassian.johnson.event.Event;
5   import com.atlassian.johnson.setup.ContainerFactory;
6   
7   import java.util.*;
8   import javax.servlet.ServletContext;
9   
10  public class JohnsonEventContainer
11  {
12      public static String SERVLET_CONTEXT_KEY = JohnsonEventContainer.class.getName();
13  
14      public synchronized static JohnsonEventContainer get(ServletContext context)
15      {
16          if (context.getAttribute(SERVLET_CONTEXT_KEY) == null)
17          {
18              final JohnsonConfig johnsonConfig = JohnsonConfig.getInstance();
19              final ContainerFactory containerFactory = johnsonConfig.getContainerFactory();
20              context.setAttribute(SERVLET_CONTEXT_KEY, containerFactory.create());
21          }
22  
23          return (JohnsonEventContainer) context.getAttribute(SERVLET_CONTEXT_KEY);
24      }
25  
26      /* a WeakHashMap so that the garbage collector will destroy it if there are no links left to it.
27      This may be needed if the user presses the Stop button while an action is performing*/
28      private final Map<Event, Event> events = new WeakHashMap<Event, Event>();
29  
30  
31      /**
32       * Determine if there are Application Error Events
33       * @return true if there are application errors otherwise false
34       */
35      public synchronized boolean hasEvents()
36      {
37          return !events.isEmpty();
38      }
39  
40      /**
41       * Adds an application event to the events Map
42       * @param event New Event
43       */
44      public synchronized void addEvent(Event event)
45      {
46          events.put(event, event);
47      }
48  
49      /**
50       * Removes an application event from the events Map
51       * @param event Event to be removed
52       */
53      public synchronized void removeEvent(Event event)
54      {
55          events.remove(event);
56      }
57  
58      /**
59       * Gets a collection of the Event objects
60       * @return an unmodifiable connection 
61       */
62      public synchronized Collection getEvents()
63      {
64          //prevent concurrent modification - clone the values before returning them.
65          return Collections.unmodifiableCollection(new ArrayList<Event>(events.values()));
66      }
67  }