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