1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.blossom.module;
35
36 import java.util.ArrayList;
37 import java.util.List;
38 import javax.servlet.ServletContext;
39 import javax.servlet.ServletException;
40
41 import org.springframework.util.StringUtils;
42 import org.springframework.web.context.ConfigurableWebApplicationContext;
43 import org.springframework.web.context.ContextLoader;
44 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
45 import org.springframework.web.servlet.DispatcherServlet;
46
47 import info.magnolia.module.blossom.render.BlossomDispatcherServlet;
48 import info.magnolia.module.blossom.support.CustomServletConfig;
49 import info.magnolia.objectfactory.Components;
50
51
52
53
54
55
56
57
58
59 public class BlossomModuleSupport {
60
61 private ContextLoader contextLoader;
62 private List<DispatcherServlet> dispatcherServlets = new ArrayList<DispatcherServlet>();
63
64
65
66
67
68 public void initRootWebApplicationContext(final String configLocation) {
69
70 initRootWebApplicationContext(new ContextLoader() {
71
72 @Override
73 protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
74
75 applicationContext.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation, ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
76 }
77 });
78 }
79
80
81
82
83
84 public void initRootWebApplicationContext(final Class<?>... configClasses) {
85
86 initRootWebApplicationContext(new ContextLoader() {
87
88 @Override
89 protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
90
91 applicationContext.setConfigLocations(classNamesToArray(configClasses));
92 }
93
94 @Override
95 protected Class<?> determineContextClass(ServletContext servletContext) {
96 return AnnotationConfigWebApplicationContext.class;
97 }
98 });
99 }
100
101
102
103
104 public void initRootWebApplicationContext(ContextLoader contextLoader) {
105 if (this.contextLoader != null) {
106 throw new IllegalStateException("ContextLoader already set, root WebApplicationContext has already been initialized");
107 }
108 this.contextLoader = contextLoader;
109 this.contextLoader.initWebApplicationContext(getServletContext());
110 }
111
112
113
114
115 public void closeRootWebApplicationContext() {
116 if (contextLoader != null) {
117 contextLoader.closeWebApplicationContext(getServletContext());
118 contextLoader = null;
119 }
120 }
121
122
123
124
125
126 public void initBlossomDispatcherServlet(String servletName, String configLocation) {
127 initDispatcherServlet(new BlossomDispatcherServlet(), servletName, configLocation);
128 }
129
130
131
132
133
134 public void initBlossomDispatcherServlet(String servletName, Class<?>... configurationClasses) {
135 BlossomDispatcherServlet dispatcherServlet = new BlossomDispatcherServlet();
136 dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
137 initDispatcherServlet(dispatcherServlet, servletName, classNamesToString(configurationClasses));
138 }
139
140
141
142
143
144 public void initDispatcherServlet(String servletName, String configLocation) {
145 initDispatcherServlet(new DispatcherServlet(), servletName, configLocation);
146 }
147
148
149
150
151
152 public void initDispatcherServlet(String servletName, Class<?>... configClasses) {
153 DispatcherServlet dispatcherServlet = new DispatcherServlet();
154 dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
155 initDispatcherServlet(dispatcherServlet, servletName, classNamesToString(configClasses));
156 }
157
158
159
160
161
162 public void initDispatcherServlet(DispatcherServlet dispatcherServlet, String servletName, String configLocation) {
163 try {
164
165 CustomServletConfig servletConfig = new CustomServletConfig(servletName, getServletContext());
166 servletConfig.addInitParameter("contextConfigLocation", configLocation);
167
168 dispatcherServlet.init(servletConfig);
169
170 dispatcherServlets.add(dispatcherServlet);
171
172 } catch (ServletException e) {
173 throw new RuntimeException("Servlet initialization failed for servlet [" + servletName + "]", e);
174 }
175 }
176
177
178
179
180 public void destroyDispatcherServlets() {
181 for (int i = dispatcherServlets.size() - 1; i >= 0; i--) {
182 dispatcherServlets.get(i).destroy();
183 }
184 dispatcherServlets.clear();
185 }
186
187 public List<DispatcherServlet> getDispatcherServlets() {
188 return dispatcherServlets;
189 }
190
191 public ServletContext getServletContext() {
192 return Components.getComponent(ServletContext.class);
193 }
194
195 protected String classNamesToString(Class<?>... classes) {
196 StringBuilder sb = new StringBuilder();
197 for (Class<?> clazz : classes) {
198 if (sb.length() > 0)
199 sb.append(",");
200 sb.append(clazz.getName());
201 }
202 return sb.toString();
203 }
204
205 protected String[] classNamesToArray(Class<?>... classes) {
206 String[] names = new String[classes.length];
207 for (int i = 0; i < classes.length; i++) {
208 names[i] = classes[i].getName();
209 }
210 return names;
211 }
212 }