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