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