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.util.ClassLoaderUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.ListUtil;
031    import com.liferay.portal.kernel.util.LocaleUtil;
032    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.SystemProperties;
035    import com.liferay.portal.kernel.util.TimeZoneUtil;
036    import com.liferay.portal.log.Log4jLogFactoryImpl;
037    import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
038    import com.liferay.portal.security.lang.DoPrivilegedUtil;
039    import com.liferay.portal.security.lang.SecurityManagerUtil;
040    import com.liferay.portal.spring.context.ArrayApplicationContext;
041    import com.liferay.util.log4j.Log4JUtil;
042    
043    import com.sun.syndication.io.XmlReader;
044    
045    import java.util.List;
046    
047    import org.apache.commons.lang.time.StopWatch;
048    
049    import org.springframework.context.ApplicationContext;
050    import org.springframework.context.support.ClassPathXmlApplicationContext;
051    
052    /**
053     * @author Brian Wing Shun Chan
054     */
055    public class InitUtil {
056    
057            public static synchronized void init() {
058                    if (_initialized) {
059                            return;
060                    }
061    
062                    StopWatch stopWatch = new StopWatch();
063    
064                    stopWatch.start();
065    
066                    // Set the default locale used by Liferay. This locale is no longer set
067                    // at the VM level. See LEP-2584.
068    
069                    String userLanguage = SystemProperties.get("user.language");
070                    String userCountry = SystemProperties.get("user.country");
071                    String userVariant = SystemProperties.get("user.variant");
072    
073                    LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
074    
075                    // Set the default time zone used by Liferay. This time zone is no
076                    // longer set at the VM level. See LEP-2584.
077    
078                    String userTimeZone = SystemProperties.get("user.timezone");
079    
080                    TimeZoneUtil.setDefault(userTimeZone);
081    
082                    // Shared class loader
083    
084                    try {
085                            PortalClassLoaderUtil.setClassLoader(
086                                    ClassLoaderUtil.getContextClassLoader());
087                    }
088                    catch (Exception e) {
089                            e.printStackTrace();
090                    }
091    
092                    // Properties
093    
094                    com.liferay.portal.kernel.util.PropsUtil.setProps(new PropsImpl());
095    
096                    // Log4J
097    
098                    if (GetterUtil.getBoolean(
099                                    SystemProperties.get("log4j.configure.on.startup"), true)) {
100    
101                            ClassLoader classLoader = InitUtil.class.getClassLoader();
102    
103                            Log4JUtil.configureLog4J(classLoader);
104                    }
105    
106                    // Shared log
107    
108                    try {
109                            LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
110                    }
111                    catch (Exception e) {
112                            e.printStackTrace();
113                    }
114    
115                    // Log sanitizer
116    
117                    SanitizerLogWrapper.init();
118    
119                    // Security manager
120    
121                    SecurityManagerUtil.init();
122    
123                    if (SecurityManagerUtil.ENABLED) {
124                            com.liferay.portal.kernel.util.PropsUtil.setProps(
125                                    DoPrivilegedUtil.wrap(
126                                            com.liferay.portal.kernel.util.PropsUtil.getProps()));
127    
128                            LogFactoryUtil.setLogFactory(
129                                    DoPrivilegedUtil.wrap(LogFactoryUtil.getLogFactory()));
130                    }
131    
132                    // Configuration factory
133    
134                    ConfigurationFactoryUtil.setConfigurationFactory(
135                            DoPrivilegedUtil.wrap(new ConfigurationFactoryImpl()));
136    
137                    // Data source factory
138    
139                    DataSourceFactoryUtil.setDataSourceFactory(
140                            DoPrivilegedUtil.wrap(new DataSourceFactoryImpl()));
141    
142                    // DB manager
143    
144                    DBManagerUtil.setDBManager(DoPrivilegedUtil.wrap(new DBManagerImpl()));
145    
146                    // ROME
147    
148                    XmlReader.setDefaultEncoding(StringPool.UTF8);
149    
150                    if (_PRINT_TIME) {
151                            System.out.println(
152                                    "InitAction takes " + stopWatch.getTime() + " ms");
153                    }
154    
155                    _initialized = true;
156            }
157    
158            public synchronized static void initWithSpring(
159                    boolean initModuleFramework) {
160    
161                    List<String> configLocations = ListUtil.fromArray(
162                            PropsUtil.getArray(
163                                    com.liferay.portal.kernel.util.PropsKeys.SPRING_CONFIGS));
164    
165                    initWithSpring(configLocations, initModuleFramework);
166            }
167    
168            public synchronized static void initWithSpring(
169                    List<String> configLocations, boolean initModuleFramework) {
170    
171                    if (_initialized) {
172                            return;
173                    }
174    
175                    if (!_neverInitialized) {
176                            PropsUtil.reload();
177                    }
178                    else {
179                            _neverInitialized = false;
180                    }
181    
182                    init();
183    
184                    try {
185                            if (initModuleFramework) {
186                                    PropsValues.LIFERAY_WEB_PORTAL_CONTEXT_TEMPDIR =
187                                            System.getProperty(SystemProperties.TMP_DIR);
188    
189                                    ModuleFrameworkUtilAdapter.initFramework();
190                            }
191    
192                            ApplicationContext applicationContext = new ArrayApplicationContext(
193                                    PropsValues.SPRING_INFRASTRUCTURE_CONFIGS);
194    
195                            if (initModuleFramework) {
196                                    ModuleFrameworkUtilAdapter.registerContext(applicationContext);
197    
198                                    ModuleFrameworkUtilAdapter.startFramework();
199                            }
200    
201                            applicationContext = new ClassPathXmlApplicationContext(
202                                    configLocations.toArray(new String[configLocations.size()]),
203                                    applicationContext);
204    
205                            BeanLocator beanLocator = new BeanLocatorImpl(
206                                    ClassLoaderUtil.getPortalClassLoader(), applicationContext);
207    
208                            PortalBeanLocatorUtil.setBeanLocator(beanLocator);
209    
210                            if (initModuleFramework) {
211                                    ModuleFrameworkUtilAdapter.registerContext(applicationContext);
212    
213                                    ModuleFrameworkUtilAdapter.startRuntime();
214                            }
215                    }
216                    catch (Exception e) {
217                            throw new RuntimeException(e);
218                    }
219    
220                    _initialized = true;
221            }
222    
223            public static boolean isInitialized() {
224                    return _initialized;
225            }
226    
227            public synchronized static void stopModuleFramework() {
228                    try {
229                            ModuleFrameworkUtilAdapter.stopFramework(0);
230                    }
231                    catch (Exception e) {
232                            throw new RuntimeException(e);
233                    }
234            }
235    
236            public synchronized static void stopRuntime() {
237                    try {
238                            ModuleFrameworkUtilAdapter.stopRuntime();
239                    }
240                    catch (Exception e) {
241                            throw new RuntimeException(e);
242                    }
243            }
244    
245            private static final boolean _PRINT_TIME = false;
246    
247            private static boolean _initialized;
248            private static boolean _neverInitialized = true;
249    
250    }