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