View Javadoc
1   /**
2    * This file Copyright (c) 2003-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.beans.config;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.cms.i18n.MessagesManager;
38  import info.magnolia.cms.license.LicenseFileExtractor;
39  import info.magnolia.cms.pddescriptor.ProductDescriptorExtractor;
40  import info.magnolia.module.ModuleManagementException;
41  import info.magnolia.module.ModuleManager;
42  import info.magnolia.module.ModuleRegistry;
43  import info.magnolia.objectfactory.Components;
44  import info.magnolia.objectfactory.configuration.ComponentProviderConfiguration;
45  import info.magnolia.objectfactory.configuration.ComponentProviderConfigurationBuilder;
46  import info.magnolia.objectfactory.guice.GuiceComponentProvider;
47  import info.magnolia.objectfactory.guice.GuiceComponentProviderBuilder;
48  
49  import javax.inject.Inject;
50  import javax.inject.Singleton;
51  import javax.servlet.ServletContext;
52  
53  import org.apache.commons.lang3.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  
58  /**
59   * This class is an entry point to all config.
60   */
61  @Singleton
62  public class ConfigLoader {
63      private static final Logger log = LoggerFactory.getLogger(ConfigLoader.class);
64      private static final String JAAS_PROPERTYNAME = "java.security.auth.login.config";
65  
66      private final ModuleManager moduleManager;
67      private final ModuleRegistry moduleRegistry;
68      private final ProductDescriptorExtractor productDescriptorExtractor;
69      private final MessagesManager messagesManager;
70      private final VersionConfig versionConfig;
71      private GuiceComponentProvider main;
72  
73      /**
74       * Initialize a ConfigLoader instance. All the supplied parameters will be set in
75       * <code>info.magnolia.cms.beans.runtime.SystemProperty</code>
76       *
77       * @param context ServletContext
78       * @see info.magnolia.cms.core.SystemProperty
79       *
80       *      TODO - some of the dependencies here don't belong, we're only calling init() on those, which should be moved to a lifecycle management api (IoC has one)
81       */
82      @Inject
83      public ConfigLoader(ModuleManager moduleManager, ModuleRegistry moduleRegistry, ProductDescriptorExtractor productDescriptorExtractor, MessagesManager messagesManager, VersionConfig versionConfig, ServletContext context) {
84          this.moduleManager = moduleManager;
85          this.moduleRegistry = moduleRegistry;
86          this.productDescriptorExtractor = productDescriptorExtractor;
87          this.messagesManager = messagesManager;
88          this.versionConfig = versionConfig;
89  
90          if (StringUtils.isEmpty(System.getProperty(JAAS_PROPERTYNAME))) {
91              try {
92                  System.setProperty(JAAS_PROPERTYNAME, Path.getAbsoluteFileSystemPath("WEB-INF/config/jaas.config"));
93              } catch (SecurityException se) {
94                  log.error("Failed to set {}, check application server settings", JAAS_PROPERTYNAME);
95                  log.error(se.getMessage(), se);
96                  log.info("Aborting startup");
97                  return;
98              }
99          } else {
100             log.info("JAAS config file set by parent container or some other application");
101             log.info("Config in use {}", System.getProperty(JAAS_PROPERTYNAME));
102             log.info("Please make sure JAAS config has all necessary modules (refer config/jaas.config) configured");
103         }
104     }
105 
106     /**
107      * @deprecated since 5.4.3, use {@link ConfigLoader#ConfigLoader(info.magnolia.module.ModuleManager, info.magnolia.module.ModuleRegistry, info.magnolia.cms.pddescriptor.ProductDescriptorExtractor, info.magnolia.cms.i18n.MessagesManager, info.magnolia.cms.beans.config.VersionConfig, javax.servlet.ServletContext)} instead.
108      */
109     @Deprecated
110     public ConfigLoader(ModuleManager moduleManager, ModuleRegistry moduleRegistry, LicenseFileExtractor licenseFileExtractor, MessagesManager messagesManager, VersionConfig versionConfig, ServletContext context) {
111         this(moduleManager, moduleRegistry, (ProductDescriptorExtractor) licenseFileExtractor, messagesManager, versionConfig, context);
112     }
113 
114     /**
115      * @deprecated since 4.5, use {@link #unload()}, dependencies are injected.
116      */
117     public void unload(ServletContext servletContext) {
118         unload();
119     }
120 
121     public void unload() {
122         // See comment in GuiceServletContextListener
123         if (main != null) {
124             Components.setComponentProvider(main.getParent());
125             main.destroy();
126         }
127         ContentRepository.shutdown();
128     }
129 
130     /**
131      * @deprecated since 4.5, use {@link #load()}, dependencies are injected.
132      */
133     public void load(ServletContext servletContext) {
134         load();
135     }
136 
137     /**
138      * Load magnolia configuration from repositories.
139      */
140     public void load() {
141         productDescriptorExtractor.init();
142         productDescriptorExtractor.printVersionInfo();
143 
144         final long millis = System.currentTimeMillis();
145         log.info("Initializing content repositories");
146 
147         ContentRepository.init();
148 
149         GuiceComponentProviderBuilder builder = new GuiceComponentProviderBuilder();
150         builder.withConfiguration(getMainComponents());
151         builder.withParent((GuiceComponentProvider) Components.getComponentProvider());
152         builder.exposeGlobally();
153         main = builder.build();
154 
155         try {
156             moduleManager.checkForInstallOrUpdates();
157             moduleManager.getUI().onStartup();
158 
159             // TODO use container lifecycle instead of manually calling init() ??
160             messagesManager.init();
161             // TODO : de-staticize MimeMapping
162             MIMEMapping.init();
163             versionConfig.init();
164 
165             // finished
166             log.info("Configuration loaded (took {} seconds)", Long.toString((System.currentTimeMillis() - millis) / 1000));
167 
168         } catch (ModuleManagementException e) {
169             log.error("A module error occurred during initialization: {}", e.getMessage(), e);
170         } catch (ConfigurationException e) {
171             log.error("A configuration error occurred during initialization: {}", e.getMessage(), e);
172         } catch (Throwable e) {
173             log.error("An unspecified error occurred during initialization: {}", e.getMessage(), e);
174         }
175 
176     }
177 
178     protected ComponentProviderConfiguration getMainComponents() {
179         ComponentProviderConfigurationBuilder configurationBuilder = new ComponentProviderConfigurationBuilder();
180         return configurationBuilder.getComponentsFromModules("main", moduleRegistry.getModuleDefinitions());
181     }
182 }