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.init;
35
36 import info.magnolia.cms.beans.config.ConfigLoader;
37 import info.magnolia.cms.core.SystemProperty;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.logging.Log4jConfigurer;
40 import info.magnolia.module.ModuleManager;
41 import info.magnolia.module.ModuleRegistry;
42 import info.magnolia.objectfactory.Components;
43 import info.magnolia.objectfactory.configuration.ComponentProviderConfiguration;
44 import info.magnolia.objectfactory.configuration.ComponentProviderConfigurationBuilder;
45 import info.magnolia.objectfactory.guice.GuiceComponentProvider;
46 import info.magnolia.objectfactory.guice.GuiceComponentProviderBuilder;
47
48 import java.util.Arrays;
49 import java.util.Collections;
50 import java.util.List;
51
52 import javax.inject.Singleton;
53 import javax.servlet.ServletContext;
54 import javax.servlet.ServletContextEvent;
55 import javax.servlet.ServletContextListener;
56
57 import org.apache.commons.lang3.StringUtils;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 import com.google.inject.Stage;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 @Singleton
112 public class MagnoliaServletContextListener implements ServletContextListener {
113
114 public static final String PLATFORM_COMPONENTS_CONFIG_LOCATION_NAME = "magnolia.platform.components.config.location";
115 public static final String DEFAULT_PLATFORM_COMPONENTS_CONFIG_LOCATION = "/info/magnolia/init/platform-components.xml";
116
117 private static final Logger log = LoggerFactory.getLogger(MagnoliaServletContextListener.class);
118
119 private ServletContext servletContext;
120 private GuiceComponentProvider platform;
121 private GuiceComponentProvider system;
122 private ModuleManager moduleManager;
123 private ConfigLoader loader;
124
125 @Override
126 public void contextInitialized(final ServletContextEvent sce) {
127 contextInitialized(sce, true);
128 }
129
130 public void contextInitialized(final ServletContextEvent sce, boolean startServer) {
131 try {
132 servletContext = sce.getServletContext();
133
134
135 GuiceComponentProviderBuilder builder = new GuiceComponentProviderBuilder();
136 builder.withConfiguration(getPlatformComponents());
137 builder.inStage(Stage.PRODUCTION);
138 builder.exposeGlobally();
139 platform = builder.build();
140
141
142
143
144 String serverName = platform.getComponent(MagnoliaInitPaths.class).getServerName();
145
146 System.setProperty("server", serverName);
147
148
149 MagnoliaConfigurationProperties configurationProperties = platform.getComponent(MagnoliaConfigurationProperties.class);
150 configurationProperties.init();
151
152
153 SystemProperty.setMagnoliaConfigurationProperties(configurationProperties);
154
155
156 moduleManager = platform.getComponent(ModuleManager.class);
157 moduleManager.loadDefinitions();
158
159
160 Log4jConfigurer.initLogging();
161
162
163 builder = new GuiceComponentProviderBuilder();
164 builder.withConfiguration(getSystemComponents());
165 builder.withParent(platform);
166 builder.exposeGlobally();
167 system = builder.build();
168
169
170 configurationProperties = system.getComponent(MagnoliaConfigurationProperties.class);
171 configurationProperties.init();
172 log.info("Property sources loaded: {}", configurationProperties.describe());
173
174
175 SystemProperty.setMagnoliaConfigurationProperties(configurationProperties);
176
177
178 loader = system.getComponent(ConfigLoader.class);
179 if (startServer) {
180 startServer();
181 }
182
183 } catch (Throwable t) {
184 log.error("Oops, Magnolia could not be started", t);
185 t.printStackTrace();
186 if (t instanceof Error) {
187 throw (Error) t;
188 }
189 if (t instanceof RuntimeException) {
190 throw (RuntimeException) t;
191 }
192 throw new RuntimeException(t);
193 }
194 }
195
196 @Override
197 public void contextDestroyed(final ServletContextEvent sce) {
198
199
200 if (moduleManager != null) {
201 moduleManager.stopModules();
202 }
203
204 stopServer();
205
206
207
208
209
210
211 if (system != null) {
212 Components.setComponentProvider(system.getParent());
213 system.destroy();
214 }
215
216 if (platform != null) {
217 Components.setComponentProvider(platform.getParent());
218 platform.destroy();
219 }
220
221 Log4jConfigurer.shutdownLogging();
222 }
223
224 protected ComponentProviderConfiguration getPlatformComponents() {
225 ComponentProviderConfigurationBuilder configurationBuilder = new ComponentProviderConfigurationBuilder();
226 List<String> resources = getPlatformComponentsResources();
227 ComponentProviderConfiguration platformComponents = configurationBuilder.readConfiguration(resources, "platform");
228 platformComponents.registerInstance(ServletContext.class, servletContext);
229
230 platformComponents.registerInstance(MagnoliaServletContextListener.class, this);
231 return platformComponents;
232 }
233
234
235
236
237
238
239
240 protected List<String> getPlatformComponentsResources() {
241 String configLocation = servletContext.getInitParameter(PLATFORM_COMPONENTS_CONFIG_LOCATION_NAME);
242 if (StringUtils.isNotBlank(configLocation)) {
243 return Arrays.asList(StringUtils.split(configLocation, ", \n"));
244 }
245 return Collections.singletonList(DEFAULT_PLATFORM_COMPONENTS_CONFIG_LOCATION);
246 }
247
248 protected ComponentProviderConfiguration getSystemComponents() {
249 ComponentProviderConfigurationBuilder configurationBuilder = new ComponentProviderConfigurationBuilder();
250 return configurationBuilder.getComponentsFromModules("system", platform.getComponent(ModuleRegistry.class).getModuleDefinitions());
251 }
252
253 protected void startServer() {
254 MgnlContext.doInSystemContext(new MgnlContext.VoidOp() {
255 @Override
256 public void doExec() {
257 loader.load();
258 }
259 }, true);
260 }
261
262 protected void stopServer() {
263 if (loader != null) {
264 MgnlContext.doInSystemContext(new MgnlContext.VoidOp() {
265 @Override
266 public void doExec() {
267 loader.unload();
268 }
269 }, true);
270 }
271 }
272
273
274
275
276 protected String initWebappName(String rootPath) {
277 return null;
278 }
279
280
281
282
283 protected String initRootPath(final ServletContext context) {
284 return null;
285 }
286
287
288
289
290 protected String initServername(boolean unqualified) {
291 return null;
292 }
293
294 }