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 org.apache.commons.lang.StringUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import javax.inject.Inject;
53  import javax.inject.Singleton;
54  import javax.servlet.ServletContext;
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              }
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 4.5, use {@link #unload()}, dependencies are injected.
108      */
109     public void unload(ServletContext servletContext) {
110         unload();
111     }
112 
113     public void unload() {
114         // See comment in GuiceServletContextListener
115         if (main != null) {
116             Components.setComponentProvider(main.getParent());
117             main.destroy();
118         }
119         ContentRepository.shutdown();
120     }
121 
122     /**
123      * @deprecated since 4.5, use {@link #load()}, dependencies are injected.
124      */
125     public void load(ServletContext servletContext) {
126         load();
127     }
128 
129     /**
130      * Load magnolia configuration from repositories.
131      */
132     public void load() {
133         license.init();
134         license.printVersionInfo();
135 
136         final long millis = System.currentTimeMillis();
137         log.info("Initializing content repositories");
138 
139         ContentRepository.init();
140 
141         GuiceComponentProviderBuilder builder = new GuiceComponentProviderBuilder();
142         builder.withConfiguration(getMainComponents());
143         builder.withParent((GuiceComponentProvider) Components.getComponentProvider());
144         builder.exposeGlobally();
145         main = builder.build();
146 
147         try {
148             moduleManager.checkForInstallOrUpdates();
149             moduleManager.getUI().onStartup();
150 
151             // TODO make these regular ObservedManagers
152             // TODO use container lifecycle instead of manually calling init() ??
153             messagesManager.init();
154             // TODO : de-staticize MimeMapping
155             MIMEMapping.init();
156             versionConfig.init();
157 
158             // finished
159             log.info("Configuration loaded (took {} seconds)", Long.toString((System.currentTimeMillis() - millis) / 1000));
160 
161         } catch (ModuleManagementException e) {
162             log.error("A module error occurred during initialization: " + e.getMessage(), e);
163         } catch (ConfigurationException e) {
164             log.error("A configuration error occurred during initialization: " + e.getMessage(), e);
165         } catch (Throwable e) {
166             log.error("An unspecified error occurred during initialization: " + e.getMessage(), e);
167         }
168 
169     }
170 
171     protected ComponentProviderConfiguration getMainComponents() {
172         ComponentProviderConfigurationBuilder configurationBuilder = new ComponentProviderConfigurationBuilder();
173         return configurationBuilder.getComponentsFromModules("main", moduleRegistry.getModuleDefinitions());
174     }
175 }