001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.bean.BeanLocatorImpl;
018    import com.liferay.portal.configuration.ConfigurationFactoryImpl;
019    import com.liferay.portal.dao.db.DBManagerImpl;
020    import com.liferay.portal.dao.jdbc.DataSourceFactoryImpl;
021    import com.liferay.portal.kernel.bean.BeanLocator;
022    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
023    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
024    import com.liferay.portal.kernel.dao.db.DBManagerUtil;
025    import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.log.SanitizerLogWrapper;
028    import com.liferay.portal.kernel.upgrade.dao.orm.UpgradeOptimizedConnectionProviderRegistryUtil;
029    import com.liferay.portal.kernel.util.ClassLoaderUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.ListUtil;
032    import com.liferay.portal.kernel.util.LocaleUtil;
033    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.SystemProperties;
036    import com.liferay.portal.kernel.util.TimeZoneUtil;
037    import com.liferay.portal.log.Log4jLogFactoryImpl;
038    import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
039    import com.liferay.portal.security.lang.DoPrivilegedUtil;
040    import com.liferay.portal.security.lang.SecurityManagerUtil;
041    import com.liferay.portal.spring.context.ArrayApplicationContext;
042    import com.liferay.portal.upgrade.dao.orm.UpgradeOptimizedConnectionProviderRegistryImpl;
043    import com.liferay.util.log4j.Log4JUtil;
044    
045    import com.sun.syndication.io.XmlReader;
046    
047    import java.util.List;
048    
049    import org.apache.commons.lang.time.StopWatch;
050    
051    import org.springframework.context.ApplicationContext;
052    import org.springframework.context.support.ClassPathXmlApplicationContext;
053    
054    /**
055     * @author Brian Wing Shun Chan
056     */
057    public class InitUtil {
058    
059            public static synchronized void init() {
060                    if (_initialized) {
061                            return;
062                    }
063    
064                    StopWatch stopWatch = new StopWatch();
065    
066                    stopWatch.start();
067    
068                    // Set the default locale used by Liferay. This locale is no longer set
069                    // at the VM level. See LEP-2584.
070    
071                    String userLanguage = SystemProperties.get("user.language");
072                    String userCountry = SystemProperties.get("user.country");
073                    String userVariant = SystemProperties.get("user.variant");
074    
075                    LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
076    
077                    // Set the default time zone used by Liferay. This time zone is no
078                    // longer set at the VM level. See LEP-2584.
079    
080                    String userTimeZone = SystemProperties.get("user.timezone");
081    
082                    TimeZoneUtil.setDefault(userTimeZone);
083    
084                    // Shared class loader
085    
086                    try {
087                            PortalClassLoaderUtil.setClassLoader(
088                                    ClassLoaderUtil.getContextClassLoader());
089                    }
090                    catch (Exception e) {
091                            e.printStackTrace();
092                    }
093    
094                    // Properties
095    
096                    com.liferay.portal.kernel.util.PropsUtil.setProps(new PropsImpl());
097    
098                    // Log4J
099    
100                    if (GetterUtil.getBoolean(
101                                    SystemProperties.get("log4j.configure.on.startup"), true)) {
102    
103                            ClassLoader classLoader = InitUtil.class.getClassLoader();
104    
105                            Log4JUtil.configureLog4J(classLoader);
106                    }
107    
108                    // Shared log
109    
110                    try {
111                            LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
112                    }
113                    catch (Exception e) {
114                            e.printStackTrace();
115                    }
116    
117                    // Log sanitizer
118    
119                    SanitizerLogWrapper.init();
120    
121                    // Security manager
122    
123                    SecurityManagerUtil.init();
124    
125                    if (SecurityManagerUtil.ENABLED) {
126                            com.liferay.portal.kernel.util.PropsUtil.setProps(
127                                    DoPrivilegedUtil.wrap(
128                                            com.liferay.portal.kernel.util.PropsUtil.getProps()));
129    
130                            LogFactoryUtil.setLogFactory(
131                                    DoPrivilegedUtil.wrap(LogFactoryUtil.getLogFactory()));
132                    }
133    
134                    // Configuration factory
135    
136                    ConfigurationFactoryUtil.setConfigurationFactory(
137                            DoPrivilegedUtil.wrap(new ConfigurationFactoryImpl()));
138    
139                    // Data source factory
140    
141                    DataSourceFactoryUtil.setDataSourceFactory(
142                            DoPrivilegedUtil.wrap(new DataSourceFactoryImpl()));
143    
144                    // DB manager
145    
146                    DBManagerUtil.setDBManager(DoPrivilegedUtil.wrap(new DBManagerImpl()));
147    
148                    // Upgrade optimized connection provider registry
149    
150                    UpgradeOptimizedConnectionProviderRegistryUtil.
151                            setUpgradeOptimizedConnectionProviderRegistry(
152                                    new UpgradeOptimizedConnectionProviderRegistryImpl());
153    
154                    // ROME
155    
156                    XmlReader.setDefaultEncoding(StringPool.UTF8);
157    
158                    if (_PRINT_TIME) {
159                            System.out.println(
160                                    "InitAction takes " + stopWatch.getTime() + " ms");
161                    }
162    
163                    _initialized = true;
164            }
165    
166            public synchronized static void initWithSpring(
167                    boolean initModuleFramework) {
168    
169                    List<String> configLocations = ListUtil.fromArray(
170                            PropsUtil.getArray(
171                                    com.liferay.portal.kernel.util.PropsKeys.SPRING_CONFIGS));
172    
173                    initWithSpring(configLocations, initModuleFramework);
174            }
175    
176            public synchronized static void initWithSpring(
177                    List<String> configLocations, boolean initModuleFramework) {
178    
179                    if (_initialized) {
180                            return;
181                    }
182    
183                    if (!_neverInitialized) {
184                            PropsUtil.reload();
185                    }
186                    else {
187                            _neverInitialized = false;
188                    }
189    
190                    init();
191    
192                    try {
193                            if (initModuleFramework) {
194                                    PropsValues.LIFERAY_WEB_PORTAL_CONTEXT_TEMPDIR =
195                                            System.getProperty(SystemProperties.TMP_DIR);
196    
197                                    ModuleFrameworkUtilAdapter.initFramework();
198                            }
199    
200                            ApplicationContext applicationContext = new ArrayApplicationContext(
201                                    PropsValues.SPRING_INFRASTRUCTURE_CONFIGS);
202    
203                            if (initModuleFramework) {
204                                    ModuleFrameworkUtilAdapter.registerContext(applicationContext);
205    
206                                    ModuleFrameworkUtilAdapter.startFramework();
207                            }
208    
209                            applicationContext = new ClassPathXmlApplicationContext(
210                                    configLocations.toArray(new String[configLocations.size()]),
211                                    applicationContext);
212    
213                            BeanLocator beanLocator = new BeanLocatorImpl(
214                                    ClassLoaderUtil.getPortalClassLoader(), applicationContext);
215    
216                            PortalBeanLocatorUtil.setBeanLocator(beanLocator);
217    
218                            if (initModuleFramework) {
219                                    ModuleFrameworkUtilAdapter.registerContext(applicationContext);
220    
221                                    ModuleFrameworkUtilAdapter.startRuntime();
222                            }
223                    }
224                    catch (Exception e) {
225                            throw new RuntimeException(e);
226                    }
227    
228                    _initialized = true;
229            }
230    
231            public static boolean isInitialized() {
232                    return _initialized;
233            }
234    
235            public synchronized static void stopModuleFramework() {
236                    try {
237                            ModuleFrameworkUtilAdapter.stopFramework(0);
238                    }
239                    catch (Exception e) {
240                            throw new RuntimeException(e);
241                    }
242            }
243    
244            public synchronized static void stopRuntime() {
245                    try {
246                            ModuleFrameworkUtilAdapter.stopRuntime();
247                    }
248                    catch (Exception e) {
249                            throw new RuntimeException(e);
250                    }
251            }
252    
253            private static final boolean _PRINT_TIME = false;
254    
255            private static boolean _initialized;
256            private static boolean _neverInitialized = true;
257    
258    }