001    /**
002     * Copyright (c) 2000-2013 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.cache.CacheRegistryImpl;
018    import com.liferay.portal.configuration.ConfigurationFactoryImpl;
019    import com.liferay.portal.dao.db.DBFactoryImpl;
020    import com.liferay.portal.dao.jdbc.DataSourceFactoryImpl;
021    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
022    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
023    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
024    import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.JavaDetector;
028    import com.liferay.portal.kernel.util.LocaleUtil;
029    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.SystemProperties;
032    import com.liferay.portal.kernel.util.TimeZoneUtil;
033    import com.liferay.portal.log.Log4jLogFactoryImpl;
034    import com.liferay.portal.security.lang.DoPrivilegedUtil;
035    import com.liferay.portal.security.lang.SecurityManagerUtil;
036    import com.liferay.portal.spring.util.SpringUtil;
037    import com.liferay.util.log4j.Log4JUtil;
038    
039    import com.sun.syndication.io.XmlReader;
040    
041    import java.util.List;
042    
043    import org.apache.commons.lang.time.StopWatch;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class InitUtil {
049    
050            public static synchronized void init() {
051                    if (_initialized) {
052                            return;
053                    }
054    
055                    StopWatch stopWatch = null;
056    
057                    if (_PRINT_TIME) {
058                            stopWatch = new StopWatch();
059    
060                            stopWatch.start();
061                    }
062    
063                    // Set the default locale used by Liferay. This locale is no longer set
064                    // at the VM level. See LEP-2584.
065    
066                    String userLanguage = SystemProperties.get("user.language");
067                    String userCountry = SystemProperties.get("user.country");
068                    String userVariant = SystemProperties.get("user.variant");
069    
070                    LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
071    
072                    // Set the default time zone used by Liferay. This time zone is no
073                    // longer set at the VM level. See LEP-2584.
074    
075                    String userTimeZone = SystemProperties.get("user.timezone");
076    
077                    TimeZoneUtil.setDefault(userTimeZone);
078    
079                    // Shared class loader
080    
081                    try {
082                            PortalClassLoaderUtil.setClassLoader(
083                                    ClassLoaderUtil.getContextClassLoader());
084                    }
085                    catch (Exception e) {
086                            e.printStackTrace();
087                    }
088    
089                    // Properties
090    
091                    com.liferay.portal.kernel.util.PropsUtil.setProps(new PropsImpl());
092    
093                    // Log4J
094    
095                    if (GetterUtil.getBoolean(
096                                    SystemProperties.get("log4j.configure.on.startup"), true)) {
097    
098                            ClassLoader classLoader = InitUtil.class.getClassLoader();
099    
100                            Log4JUtil.configureLog4J(classLoader);
101                    }
102    
103                    // Shared log
104    
105                    try {
106                            LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
107                    }
108                    catch (Exception e) {
109                            e.printStackTrace();
110                    }
111    
112                    // Security manager
113    
114                    SecurityManagerUtil.init();
115    
116                    if (!SecurityManagerUtil.isPACLDisabled()) {
117                            com.liferay.portal.kernel.util.PropsUtil.setProps(
118                                    DoPrivilegedUtil.wrap(
119                                            com.liferay.portal.kernel.util.PropsUtil.getProps()));
120    
121                            LogFactoryUtil.setLogFactory(
122                                    DoPrivilegedUtil.wrap(LogFactoryUtil.getLogFactory()));
123                    }
124    
125                    // Cache registry
126    
127                    CacheRegistryUtil.setCacheRegistry(
128                            DoPrivilegedUtil.wrap(new CacheRegistryImpl()));
129    
130                    // Configuration factory
131    
132                    ConfigurationFactoryUtil.setConfigurationFactory(
133                            DoPrivilegedUtil.wrap(new ConfigurationFactoryImpl()));
134    
135                    // Data source factory
136    
137                    DataSourceFactoryUtil.setDataSourceFactory(
138                            DoPrivilegedUtil.wrap(new DataSourceFactoryImpl()));
139    
140                    // DB factory
141    
142                    DBFactoryUtil.setDBFactory(DoPrivilegedUtil.wrap(new DBFactoryImpl()));
143    
144                    // Java properties
145    
146                    JavaDetector.isJDK5();
147    
148                    // ROME
149    
150                    XmlReader.setDefaultEncoding(StringPool.UTF8);
151    
152                    if (_PRINT_TIME) {
153                            System.out.println(
154                                    "InitAction takes " + stopWatch.getTime() + " ms");
155                    }
156    
157                    _initialized = true;
158            }
159    
160            public synchronized static void initWithSpring() {
161                    initWithSpring(false, null);
162            }
163    
164            public synchronized static void initWithSpring(boolean force) {
165                    initWithSpring(force, null);
166            }
167    
168            public synchronized static void initWithSpring(
169                    boolean force, List<String> extraConfigLocations) {
170    
171                    if (force) {
172                            _initialized = false;
173                    }
174    
175                    if (_initialized) {
176                            return;
177                    }
178    
179                    if (!_neverInitialized) {
180                            PropsUtil.reload();
181                    }
182                    else {
183                            _neverInitialized = false;
184                    }
185    
186                    init();
187    
188                    SpringUtil.loadContext(extraConfigLocations);
189    
190                    _initialized = true;
191            }
192    
193            public synchronized static void initWithSpring(
194                    List<String> extraConfigLocations) {
195    
196                    initWithSpring(false, extraConfigLocations);
197            }
198    
199            private static final boolean _PRINT_TIME = false;
200    
201            private static boolean _initialized;
202            private static boolean _neverInitialized = true;
203    
204    }