KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > ConfigLoader


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.beans.config;
14
15 import info.magnolia.cms.core.Path;
16 import info.magnolia.cms.core.SystemProperty;
17 import info.magnolia.cms.i18n.MessagesManager;
18 import info.magnolia.cms.license.LicenseFileExtractor;
19 import info.magnolia.cms.module.ModuleFactory;
20 import info.magnolia.cms.security.SecureURI;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.jcr.RepositoryException;
26 import javax.servlet.ServletContext JavaDoc;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.log4j.Logger;
30
31
32 /**
33  * This class is an entry point to all config.
34  * @author Sameer Charles
35  * @version 1.1
36  */

37 public class ConfigLoader {
38
39     /**
40      * Logger.
41      */

42     protected static Logger log = Logger.getLogger(ConfigLoader.class);
43
44     /**
45      * Is this magnolia istance configured?
46      */

47     private static boolean configured;
48
49     /**
50      * Set to true when bootstrapping is in progress or if it has failed.
51      */

52     private static boolean bootstrapping;
53
54     /**
55      * Initialize a ConfigLoader instance. All the supplied parameters will be set in
56      * <code>info.magnolia.cms.beans.runtime.SystemProperty</code>
57      * @param context ServletContext
58      * @param config contains initialization parameters
59      * @see SystemProperty
60      */

61     public ConfigLoader(ServletContext JavaDoc context, Map JavaDoc config) {
62
63         String JavaDoc rootDir = context.getRealPath(StringUtils.EMPTY);
64
65         if (log.isInfoEnabled()) {
66             log.info("Assuming paths relative to " + rootDir); //$NON-NLS-1$
67
}
68         SystemProperty.setProperty(SystemProperty.MAGNOLIA_APP_ROOTDIR, rootDir);
69
70         Iterator JavaDoc it = config.entrySet().iterator();
71         while (it.hasNext()) {
72             Map.Entry JavaDoc param = (Map.Entry JavaDoc) it.next();
73             SystemProperty.setProperty((String JavaDoc) param.getKey(), (String JavaDoc) param.getValue());
74         }
75
76         if (StringUtils.isEmpty(System.getProperty("java.security.auth.login.config"))) { //$NON-NLS-1$
77
System.setProperty("java.security.auth.login.config", Path //$NON-NLS-1$
78
.getAbsoluteFileSystemPath("WEB-INF/config/jaas.config")); //$NON-NLS-1$
79
}
80         else {
81             if (log.isInfoEnabled()) {
82                 log.info("JAAS config file set by parent container or some other application"); //$NON-NLS-1$
83
log.info("Config in use " + System.getProperty("java.security.auth.login.config")); //$NON-NLS-1$ //$NON-NLS-2$
84
log
85                     .info("Please make sure JAAS config has all necessary modules (refer config/jaas.config) configured"); //$NON-NLS-1$
86
}
87         }
88
89         this.load(context);
90     }
91
92     /**
93      * Load magnolia configuration from repositories.
94      * @param context ServletContext
95      */

96     private void load(ServletContext JavaDoc context) {
97         // first check for the license information, will fail if this class does not exist
98
LicenseFileExtractor license = LicenseFileExtractor.getInstance();
99         license.init();
100         printVersionInfo(license);
101
102         log.info("Init content repositories"); //$NON-NLS-1$
103
ContentRepository.init();
104
105         // check for initialized repositories
106
boolean initialized = false;
107
108         try {
109             initialized = ContentRepository.checkIfInitialized();
110         }
111         catch (RepositoryException re) {
112             log.fatal("Unable to initialize repositories. Magnolia can't start.", re); //$NON-NLS-1$
113
return;
114         }
115
116         if (initialized) {
117             log.info("Repositories are initialized (some content found). Loading configuration"); //$NON-NLS-1$
118
}
119         else {
120             log.warn("Repositories are not initialized (no content found)."); //$NON-NLS-1$
121

122             String JavaDoc bootdir = SystemProperty.getProperty(SystemProperty.MAGNOLIA_BOOTSTRAP_ROOTDIR);
123             if (StringUtils.isEmpty(bootdir)) {
124                 enterListeningMode();
125                 return;
126             }
127
128             bootstrapping = true;
129
130             // a bootstrap directory is configured, trying to initialize repositories
131
Bootstrapper.bootstrapRepositories(Path.getAbsoluteFileSystemPath(bootdir));
132         }
133
134         log.info("Init virtualMap"); //$NON-NLS-1$
135
VirtualMap.init();
136         log.info("Init i18n"); //$NON-NLS-1$
137
MessagesManager.init(context);
138         log.info("Init secureURI"); //$NON-NLS-1$
139
SecureURI.init();
140
141         try {
142             Server.init();
143             ModuleFactory.init();
144             ModuleLoader.init();
145             Listener.init();
146             Subscriber.init();
147             Cache.init();
148             MIMEMapping.init();
149             setConfigured(true);
150             log.info("Configuration loaded!"); //$NON-NLS-1$
151
}
152         catch (ConfigurationException e) {
153             log.info("An error occurred during initialization, incomplete configuration found?"); //$NON-NLS-1$
154
enterListeningMode();
155             return;
156         }
157
158     }
159
160     /**
161      * Print version info to console.
162      * @param license loaded License
163      */

164     private void printVersionInfo(LicenseFileExtractor license) {
165         System.out.println("---------------------------------------------"); //$NON-NLS-1$
166
System.out.println("MAGNOLIA LICENSE"); //$NON-NLS-1$
167
System.out.println("---------------------------------------------"); //$NON-NLS-1$
168
System.out.println("Version number : " + license.get(LicenseFileExtractor.VERSION_NUMBER)); //$NON-NLS-1$
169
System.out.println("Build : " + license.get(LicenseFileExtractor.BUILD_NUMBER)); //$NON-NLS-1$
170
System.out.println("Provider : " //$NON-NLS-1$
171
+ license.get(LicenseFileExtractor.PROVIDER) + " (" //$NON-NLS-1$
172
+ license.get(LicenseFileExtractor.PRIVIDER_EMAIL) + ")"); //$NON-NLS-1$
173
}
174
175     /**
176      * Returns true is magnolia is running with all basic configuration.
177      * @return <code>true</code> if Magnolia is configured
178      */

179     public static boolean isConfigured() {
180         return ConfigLoader.configured;
181     }
182
183     /**
184      * Returns true if repository bootstrapping has started but the configuration has not been loaded successfully.
185      * @return <code>true</code> if repository bootstrapping has started but the configuration has not been loaded
186      * successfully
187      */

188     public static boolean isBootstrapping() {
189         return bootstrapping;
190     }
191
192     /**
193      * Set the current state of Magnolia.
194      * @param configured <code>true</code> if Magnolia is configured
195      */

196     protected static void setConfigured(boolean configured) {
197         ConfigLoader.configured = configured;
198
199         // if we are here, bootstrapping has completed or never started
200
ConfigLoader.bootstrapping = false;
201     }
202
203     /**
204      * Set the configured propery to false and print out an informative message to System.out.
205      */

206     private void enterListeningMode() {
207         System.out.println("\n-----------------------------------------------------------------"); //$NON-NLS-1$
208
System.out.println("Server not configured, entering in listening mode for activation."); //$NON-NLS-1$
209
System.out.println("You can now activate content from an existing magnolia instance."); //$NON-NLS-1$
210
System.out.println("-----------------------------------------------------------------\n"); //$NON-NLS-1$
211
setConfigured(false);
212     }
213
214 }
215
Popular Tags