View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 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.module.ModuleManagementException;
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 javax.inject.Inject;
49  import javax.inject.Singleton;
50  import javax.servlet.ServletContext;
51  
52  import org.apache.commons.lang3.StringUtils;
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  
57  /**
58   * This class is an entry point to all config.
59   */
60  @Singleton
61  public class ConfigLoader {
62      private static final Logger log = LoggerFactory.getLogger(ConfigLoader.class);
63      private static final String JAAS_PROPERTYNAME = "java.security.auth.login.config";
64  
65      private final ModuleManager moduleManager;
66      private final ModuleRegistry moduleRegistry;
67      private final LicenseFileExtractor license;
68      private final MessagesManager messagesManager;
69      private final VersionConfig versionConfig;
70      private GuiceComponentProvider main;
71  
72      /**
73       * Initialize a ConfigLoader instance. All the supplied parameters will be set in
74       * <code>info.magnolia.cms.beans.runtime.SystemProperty</code>
75       *
76       * @param context ServletContext
77       * @see info.magnolia.cms.core.SystemProperty
78       *
79       *      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)
80       */
81      @Inject
82      public ConfigLoader(ModuleManager moduleManager, ModuleRegistry moduleRegistry, LicenseFileExtractor licenseFileExtractor, MessagesManager messagesManager, VersionConfig versionConfig, ServletContext context) {
83          this.moduleManager = moduleManager;
84          this.moduleRegistry = moduleRegistry;
85          this.license = licenseFileExtractor;
86          this.messagesManager = messagesManager;
87          this.versionConfig = versionConfig;
88  
89          if (StringUtils.isEmpty(System.getProperty(JAAS_PROPERTYNAME))) {
90              try {
91                  System.setProperty(JAAS_PROPERTYNAME, Path.getAbsoluteFileSystemPath("WEB-INF/config/jaas.config"));
92              } catch (SecurityException se) {
93                  log.error("Failed to set {}, check application server settings", JAAS_PROPERTYNAME);
94                  log.error(se.getMessage(), se);
95                  log.info("Aborting startup");
96                  return;
97              }
98          } else {
99              log.info("JAAS config file set by parent container or some other application");
100             log.info("Config in use {}", System.getProperty(JAAS_PROPERTYNAME));
101             log.info("Please make sure JAAS config has all necessary modules (refer config/jaas.config) configured");
102         }
103     }
104 
105     /**
106      * @deprecated since 4.5, use {@link #unload()}, dependencies are injected.
107      */
108     public void unload(ServletContext servletContext) {
109         unload();
110     }
111 
112     public void unload() {
113         // See comment in GuiceServletContextListener
114         if (main != null) {
115             Components.setComponentProvider(main.getParent());
116             main.destroy();
117         }
118         ContentRepository.shutdown();
119     }
120 
121     /**
122      * @deprecated since 4.5, use {@link #load()}, dependencies are injected.
123      */
124     public void load(ServletContext servletContext) {
125         load();
126     }
127 
128     /**
129      * Load magnolia configuration from repositories.
130      */
131     public void load() {
132         license.init();
133         license.printVersionInfo();
134 
135         final long millis = System.currentTimeMillis();
136         log.info("Initializing content repositories");
137 
138         ContentRepository.init();
139 
140         GuiceComponentProviderBuilder builder = new GuiceComponentProviderBuilder();
141         builder.withConfiguration(getMainComponents());
142         builder.withParent((GuiceComponentProvider) Components.getComponentProvider());
143         builder.exposeGlobally();
144         main = builder.build();
145 
146         try {
147             moduleManager.checkForInstallOrUpdates();
148             moduleManager.getUI().onStartup();
149 
150             // TODO make these regular ObservedManagers
151             // TODO use container lifecycle instead of manually calling init() ??
152             messagesManager.init();
153             // TODO : de-staticize MimeMapping
154             MIMEMapping.init();
155             versionConfig.init();
156 
157             // finished
158             log.info("Configuration loaded (took {} seconds)", Long.toString((System.currentTimeMillis() - millis) / 1000));
159 
160         } catch (ModuleManagementException e) {
161             log.error("A module error occurred during initialization: {}", e.getMessage(), e);
162         } catch (ConfigurationException e) {
163             log.error("A configuration error occurred during initialization: {}", e.getMessage(), e);
164         } catch (Throwable e) {
165             log.error("An unspecified error occurred during initialization: {}", e.getMessage(), e);
166         }
167 
168     }
169 
170     protected ComponentProviderConfiguration getMainComponents() {
171         ComponentProviderConfigurationBuilder configurationBuilder = new ComponentProviderConfigurationBuilder();
172         return configurationBuilder.getComponentsFromModules("main", moduleRegistry.getModuleDefinitions());
173     }
174 }