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
143 String serverName = platform.getComponent(MagnoliaInitPaths.class).getServerName();
144
145 System.setProperty("server", serverName);
146
147
148 moduleManager = platform.getComponent(ModuleManager.class);
149 moduleManager.loadDefinitions();
150
151
152 MagnoliaConfigurationProperties configurationProperties = platform.getComponent(MagnoliaConfigurationProperties.class);
153 configurationProperties.init();
154 log.info("Property sources loaded: {}", configurationProperties.describe());
155
156
157 SystemProperty.setMagnoliaConfigurationProperties(configurationProperties);
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 loader = system.getComponent(ConfigLoader.class);
171 if (startServer) {
172 startServer();
173 }
174
175 } catch (Throwable t) {
176 log.error("Oops, Magnolia could not be started", t);
177 t.printStackTrace();
178 if (t instanceof Error) {
179 throw (Error) t;
180 }
181 if (t instanceof RuntimeException) {
182 throw (RuntimeException) t;
183 }
184 throw new RuntimeException(t);
185 }
186 }
187
188 @Override
189 public void contextDestroyed(final ServletContextEvent sce) {
190
191
192 if (moduleManager != null) {
193 moduleManager.stopModules();
194 }
195
196 stopServer();
197
198
199
200
201
202
203 if (system != null) {
204 Components.setComponentProvider(system.getParent());
205 system.destroy();
206 }
207
208 if (platform != null) {
209 Components.setComponentProvider(platform.getParent());
210 platform.destroy();
211 }
212
213 Log4jConfigurer.shutdownLogging();
214 }
215
216 protected ComponentProviderConfiguration getPlatformComponents() {
217 ComponentProviderConfigurationBuilder configurationBuilder = new ComponentProviderConfigurationBuilder();
218 List<String> resources = getPlatformComponentsResources();
219 ComponentProviderConfiguration platformComponents = configurationBuilder.readConfiguration(resources, "platform");
220 platformComponents.registerInstance(ServletContext.class, servletContext);
221
222 platformComponents.registerInstance(MagnoliaServletContextListener.class, this);
223 return platformComponents;
224 }
225
226
227
228
229
230
231
232 protected List<String> getPlatformComponentsResources() {
233 String configLocation = servletContext.getInitParameter(PLATFORM_COMPONENTS_CONFIG_LOCATION_NAME);
234 if (StringUtils.isNotBlank(configLocation)) {
235 return Arrays.asList(StringUtils.split(configLocation, ", \n"));
236 }
237 return Collections.singletonList(DEFAULT_PLATFORM_COMPONENTS_CONFIG_LOCATION);
238 }
239
240 protected ComponentProviderConfiguration getSystemComponents() {
241 ComponentProviderConfigurationBuilder configurationBuilder = new ComponentProviderConfigurationBuilder();
242 return configurationBuilder.getComponentsFromModules("system", platform.getComponent(ModuleRegistry.class).getModuleDefinitions());
243 }
244
245 protected void startServer() {
246 MgnlContext.doInSystemContext(new MgnlContext.VoidOp() {
247 @Override
248 public void doExec() {
249 loader.load();
250 }
251 }, true);
252 }
253
254 protected void stopServer() {
255 if (loader != null) {
256 MgnlContext.doInSystemContext(new MgnlContext.VoidOp() {
257 @Override
258 public void doExec() {
259 loader.unload();
260 }
261 }, true);
262 }
263 }
264
265
266
267
268 protected String initWebappName(String rootPath) {
269 return null;
270 }
271
272
273
274
275 protected String initRootPath(final ServletContext context) {
276 return null;
277 }
278
279
280
281
282 protected String initServername(boolean unqualified) {
283 return null;
284 }
285
286 }