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