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.cache.CacheRegistryImpl;
019    import com.liferay.portal.configuration.ConfigurationFactoryImpl;
020    import com.liferay.portal.dao.db.DBFactoryImpl;
021    import com.liferay.portal.dao.jdbc.DataSourceFactoryImpl;
022    import com.liferay.portal.kernel.bean.BeanLocator;
023    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
024    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
025    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
026    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
027    import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.log.SanitizerLogWrapper;
030    import com.liferay.portal.kernel.util.ClassLoaderUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.ListUtil;
033    import com.liferay.portal.kernel.util.LocaleUtil;
034    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.SystemProperties;
037    import com.liferay.portal.kernel.util.TimeZoneUtil;
038    import com.liferay.portal.log.Log4jLogFactoryImpl;
039    import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
040    import com.liferay.portal.security.lang.DoPrivilegedUtil;
041    import com.liferay.portal.security.lang.SecurityManagerUtil;
042    import com.liferay.portal.spring.context.ArrayApplicationContext;
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                    // Cache registry
135    
136                    CacheRegistryUtil.setCacheRegistry(
137                            DoPrivilegedUtil.wrap(new CacheRegistryImpl()));
138    
139                    // Configuration factory
140    
141                    ConfigurationFactoryUtil.setConfigurationFactory(
142                            DoPrivilegedUtil.wrap(new ConfigurationFactoryImpl()));
143    
144                    // Data source factory
145    
146                    DataSourceFactoryUtil.setDataSourceFactory(
147                            DoPrivilegedUtil.wrap(new DataSourceFactoryImpl()));
148    
149                    // DB factory
150    
151                    DBFactoryUtil.setDBFactory(DoPrivilegedUtil.wrap(new DBFactoryImpl()));
152    
153                    // ROME
154    
155                    XmlReader.setDefaultEncoding(StringPool.UTF8);
156    
157                    if (_PRINT_TIME) {
158                            System.out.println(
159                                    "InitAction takes " + stopWatch.getTime() + " ms");
160                    }
161    
162                    _initialized = true;
163            }
164    
165            public synchronized static void initWithSpring(
166                    boolean initModuleFramework) {
167    
168                    List<String> configLocations = ListUtil.fromArray(
169                            PropsUtil.getArray(
170                                    com.liferay.portal.kernel.util.PropsKeys.SPRING_CONFIGS));
171    
172                    initWithSpring(configLocations, initModuleFramework);
173            }
174    
175            public synchronized static void initWithSpring(
176                    List<String> configLocations, boolean initModuleFramework) {
177    
178                    if (_initialized) {
179                            return;
180                    }
181    
182                    if (!_neverInitialized) {
183                            PropsUtil.reload();
184                    }
185                    else {
186                            _neverInitialized = false;
187                    }
188    
189                    init();
190    
191                    try {
192                            if (initModuleFramework) {
193                                    PropsValues.LIFERAY_WEB_PORTAL_CONTEXT_TEMPDIR =
194                                            System.getProperty(SystemProperties.TMP_DIR);
195    
196                                    ModuleFrameworkUtilAdapter.initFramework();
197                            }
198    
199                            ApplicationContext applicationContext = new ArrayApplicationContext(
200                                    PropsValues.SPRING_INFRASTRUCTURE_CONFIGS);
201    
202                            if (initModuleFramework) {
203                                    ModuleFrameworkUtilAdapter.registerContext(applicationContext);
204    
205                                    ModuleFrameworkUtilAdapter.startFramework();
206                            }
207    
208                            applicationContext = new ClassPathXmlApplicationContext(
209                                    configLocations.toArray(new String[configLocations.size()]),
210                                    applicationContext);
211    
212                            BeanLocator beanLocator = new BeanLocatorImpl(
213                                    ClassLoaderUtil.getPortalClassLoader(), applicationContext);
214    
215                            PortalBeanLocatorUtil.setBeanLocator(beanLocator);
216    
217                            if (initModuleFramework) {
218                                    ModuleFrameworkUtilAdapter.registerContext(applicationContext);
219    
220                                    ModuleFrameworkUtilAdapter.startRuntime();
221                            }
222                    }
223                    catch (Exception e) {
224                            throw new RuntimeException(e);
225                    }
226    
227                    _initialized = true;
228            }
229    
230            public static boolean isInitialized() {
231                    return _initialized;
232            }
233    
234            public synchronized static void stopModuleFramework() {
235                    try {
236                            ModuleFrameworkUtilAdapter.stopFramework(0);
237                    }
238                    catch (Exception e) {
239                            throw new RuntimeException(e);
240                    }
241            }
242    
243            public synchronized static void stopRuntime() {
244                    try {
245                            ModuleFrameworkUtilAdapter.stopRuntime();
246                    }
247                    catch (Exception e) {
248                            throw new RuntimeException(e);
249                    }
250            }
251    
252            private static final boolean _PRINT_TIME = false;
253    
254            private static boolean _initialized;
255            private static boolean _neverInitialized = true;
256    
257    }