1 package com.atlassian.johnson.spring.web;
2
3 import org.springframework.util.StringUtils;
4
5 import javax.servlet.ServletConfig;
6 import javax.servlet.ServletContext;
7
8 /**
9 * Constants related to determining which {@link com.atlassian.johnson.event.EventType EventType} to use for Spring-
10 * related {@link com.atlassian.johnson.event.Event Event}s.
11 *
12 * @since 2.0
13 */
14 public class SpringEventType
15 {
16 /**
17 * Defines the {@code init-param} which may be used for controlling whether an event is added when a portion of
18 * Spring initialisation is bypassed due to previous errors.
19 * <p/>
20 * Note: This flag does not control whether an event is added when Spring initialisation is not bypassed and fails.
21 *
22 * @see #addEventOnBypass(javax.servlet.ServletContext)
23 */
24 public static final String ADD_EVENT_ON_BYPASS_PARAM = "johnson.spring.addEventOnBypass";
25 /**
26 * Defines the {@code init-param} which may be used for controlling the event type added when Spring events occur.
27 * Where the value must be set depends on the type being initialised.
28 *
29 * @see #getContextEventType(javax.servlet.ServletContext)
30 * @see #getServletEventType(javax.servlet.ServletConfig)
31 */
32 public static final String EVENT_TYPE_PARAM = "johnson.spring.eventType";
33
34 /**
35 * Defines the default context event type which will be used if one is not explicitly set.
36 */
37 public static final String SPRING_CONTEXT_EVENT_TYPE = "spring";
38 /**
39 * Defines the default servlet event type which will be used if one is not explicitly set.
40 */
41 public static final String SPRING_SERVLET_EVENT_TYPE = "spring-mvc";
42
43 private SpringEventType()
44 {
45
46 }
47
48 /**
49 * Retrieves a flag indicating whether a Johnson event should be added when Spring initialisation is bypassed
50 * due to previous fatal errors.
51 * <p/>
52 * By default, an event is <i>not</i> added. If a {@code context-param} named {@link #ADD_EVENT_ON_BYPASS_PARAM}
53 * exists with the value {@code true}, then an {@link #getContextEventType(javax.servlet.ServletContext) event}
54 * will be added when initialisation is bypassed.
55 * <p/>
56 * To set this value, add the following to {@code web.xml}:
57 * <pre><code>
58 * <context-param>
59 * <param-name>johnson.spring.addEventOnBypass</param-name>
60 * <param-value>true</param-value>
61 * </context-param>
62 * </code></pre>
63 * Note: If initialisation is not bypassed and fails, this flag <i>does not</i> control whether an event will be
64 * added at that time.
65 *
66 * @param context the servlet context
67 * @return {@code true} if an event has been explicitly requested; otherwise, {@code false}
68 */
69 public static boolean addEventOnBypass(ServletContext context)
70 {
71 return "true".equals(context.getInitParameter(ADD_EVENT_ON_BYPASS_PARAM));
72 }
73
74 /**
75 * Retrieves a flag indicating whether a Johnson event should be added when SpringMVC initialisation is bypassed
76 * due to previous fatal Spring errors.
77 * <p/>
78 * By default, an event is <i>not</i> added. If an {@code init-param} named {@link #ADD_EVENT_ON_BYPASS_PARAM}
79 * exists, its value ({@code true} or {@code false}) controls whether an event is added. Otherwise, a fallback check
80 * is made {@link #addEventOnBypass(javax.servlet.ServletContext) to the context} for a {@code context-param}.
81 * This means if an event is explicitly requested at the context level, by default it will also be requested at
82 * the servlet level. However, individual servlets can explicitly disable that by setting their {@code init-param}
83 * to {@code false}.
84 * <p/>
85 * To set this value, add the following to the declaration for the servlet in {@code web.xml}:
86 * <pre><code>
87 * <init-param>
88 * <param-name>johnson.spring.addEventOnBypass</param-name>
89 * <param-value>true</param-value>
90 * </init-param>
91 * </code></pre>
92 * Note: If initialisation is not bypassed and fails, this flag <i>does not</i> control whether an event will be
93 * added at that time.
94 *
95 * @param config the servlet configuration
96 * @return {@code true} if an event has been specifically requested, either at the servlet level or at the context
97 * level; otherwise, {@code false}
98 */
99 public static boolean addEventOnBypass(ServletConfig config)
100 {
101 String value = config.getInitParameter(ADD_EVENT_ON_BYPASS_PARAM);
102 if (value == null)
103 {
104 //If parameter was found at the servlet level, look at the context level.
105 return addEventOnBypass(config.getServletContext());
106 }
107 //If any value is found at the servlet level, be it true or false, that value always overrides any value set
108 //at the context level.
109 return "true".equals(value);
110 }
111
112 /**
113 * Examines the provided {@code ServletContext} for a {@code context-param} named {@link #EVENT_TYPE_PARAM} and, if
114 * one is found, returns its value; otherwise the default {@link #SPRING_CONTEXT_EVENT_TYPE} is returned.
115 * <p/>
116 * To set this value, add the following to {@code web.xml}:
117 * <pre><code>
118 * <context-param>
119 * <param-name>johnson.spring.eventType</param-name>
120 * <param-value>my-spring-context-event-type</param-value>
121 * </context-param>
122 * </code></pre>
123 *
124 * @param context the servlet context
125 * @return the context event type
126 */
127 public static String getContextEventType(ServletContext context)
128 {
129 String value = context.getInitParameter(EVENT_TYPE_PARAM);
130 if (!StringUtils.hasText(value))
131 {
132 value = SPRING_CONTEXT_EVENT_TYPE;
133 }
134 return value;
135 }
136
137 /**
138 * Examines the provided {@code ServletConfig} for an {@code init-param} named {@link #EVENT_TYPE_PARAM} and, if
139 * one is found, returns its value; otherwise, the default {@link #SPRING_SERVLET_EVENT_TYPE} is returned.
140 * <p/>
141 * To set this value, add the following to the declaration for the servlet in {@code web.xml}:
142 * <pre><code>
143 * <init-param>
144 * <param-name>johnson.spring.eventType</param-name>
145 * <param-value>my-spring-servlet-event-type</param-value>
146 * </init-param>
147 * </code></pre>
148 *
149 * @param config the servlet configuration
150 * @return the servlet event type
151 */
152 public static String getServletEventType(ServletConfig config)
153 {
154 String value = config.getInitParameter(EVENT_TYPE_PARAM);
155 if (!StringUtils.hasText(value))
156 {
157 value = SPRING_SERVLET_EVENT_TYPE;
158 }
159 return value;
160 }
161 }