View Javadoc

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