View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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 org.apache.commons.lang.StringUtils;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import javax.servlet.ServletContext;
46  
47  
48  /**
49   * This class is an entry point to all config.
50   *
51   * @author Sameer Charles
52   * @version 1.1
53   */
54  public class ConfigLoader {
55      private static final Logger log = LoggerFactory.getLogger(ConfigLoader.class);
56      private static final String JAAS_PROPERTYNAME = "java.security.auth.login.config";
57  
58      /**
59       * Initialize a ConfigLoader instance. All the supplied parameters will be set in
60       * <code>info.magnolia.cms.beans.runtime.SystemProperty</code>
61       *
62       * @param context ServletContext
63       * @see info.magnolia.cms.core.SystemProperty
64       */
65      public ConfigLoader(ServletContext context) {
66  
67          if (StringUtils.isEmpty(System.getProperty(JAAS_PROPERTYNAME))) {
68              try {
69                  System.setProperty(JAAS_PROPERTYNAME, Path.getAbsoluteFileSystemPath("WEB-INF/config/jaas.config"));
70              }
71              catch (SecurityException se) {
72                  log.error("Failed to set {}, check application server settings", JAAS_PROPERTYNAME);
73                  log.error(se.getMessage(), se);
74                  log.info("Aborting startup");
75                  return;
76              }
77          } else {
78              log.info("JAAS config file set by parent container or some other application"); //$NON-NLS-1$
79              log.info("Config in use {}", System.getProperty(JAAS_PROPERTYNAME)); //$NON-NLS-1$ //$NON-NLS-2$
80              log.info("Please make sure JAAS config has all necessary modules (refer config/jaas.config) configured"); //$NON-NLS-1$
81          }
82      }
83  
84      public void unload(ServletContext servletContext) {
85          ContentRepository.shutdown();
86      }
87  
88      /**
89       * Load magnolia configuration from repositories.
90       *
91       * @param servletContext ServletContext
92       */
93      public void load(ServletContext servletContext) {
94          // first check for the license information, will fail if this class does not exist
95          LicenseFileExtractor license = LicenseFileExtractor.getInstance();
96          license.init();
97          license.printVersionInfo();
98  
99          final long millis = System.currentTimeMillis();
100         log.info("Initializing content repositories"); //$NON-NLS-1$
101 
102         ContentRepository.init();
103 
104         try {
105             final ModuleManager moduleManager = ModuleManager.Factory.getInstance();
106             moduleManager.checkForInstallOrUpdates();
107             moduleManager.getUI().onStartup();
108 
109             // TODO make these regular ObservedManagers
110             MessagesManager.getInstance().init(); // TODO this was done before module init??
111             MIMEMapping.init();
112             VersionConfig.getInstance().init();
113 
114             // finished
115             log.info("Configuration loaded (took {} seconds)", Long.toString((System.currentTimeMillis() - millis) / 1000)); //$NON-NLS-1$
116 
117         } catch (ModuleManagementException e) {
118             log.error("An error occurred during initialization", e); //$NON-NLS-1$
119         } catch (ConfigurationException e) {
120             log.error("An error occurred during initialization", e); //$NON-NLS-1$
121         }
122 
123     }
124 }