1
2
3
4
5
6
7
8
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
40
41 private final Map events = new WeakHashMap();
42
43
44
45
46
47
48 public synchronized boolean hasEvents()
49 {
50 return !events.isEmpty();
51 }
52
53
54
55
56
57 public synchronized void addEvent(Event event)
58 {
59 events.put(event, event);
60 }
61
62
63
64
65
66 public synchronized void removeEvent(Event event)
67 {
68 events.remove(event);
69 }
70
71
72
73
74
75 public synchronized Collection getEvents()
76 {
77
78 return Collections.unmodifiableCollection(new ArrayList(events.values()));
79 }
80 }