001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
022    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.JavaProps;
026    import com.liferay.portal.kernel.util.LocaleUtil;
027    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
028    import com.liferay.portal.kernel.util.TimeZoneUtil;
029    import com.liferay.portal.log.Log4jLogFactoryImpl;
030    import com.liferay.portal.spring.util.SpringUtil;
031    import com.liferay.util.SystemProperties;
032    import com.liferay.util.log4j.Log4JUtil;
033    
034    import org.apache.commons.lang.time.StopWatch;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class InitUtil {
040    
041            public static synchronized void init() {
042                    if (_initialized) {
043                            return;
044                    }
045    
046                    StopWatch stopWatch = null;
047    
048                    if (_PRINT_TIME) {
049                            stopWatch = new StopWatch();
050    
051                            stopWatch.start();
052                    }
053    
054                    // Set the default locale used by Liferay. This locale is no longer set
055                    // at the VM level. See LEP-2584.
056    
057                    String userLanguage = SystemProperties.get("user.language");
058                    String userCountry = SystemProperties.get("user.country");
059                    String userVariant = SystemProperties.get("user.variant");
060    
061                    LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
062    
063                    // Set the default time zone used by Liferay. This time zone is no
064                    // longer set at the VM level. See LEP-2584.
065    
066                    String userTimeZone = SystemProperties.get("user.timezone");
067    
068                    TimeZoneUtil.setDefault(userTimeZone);
069    
070                    // Shared class loader
071    
072                    try {
073                            Thread currentThread = Thread.currentThread();
074    
075                            PortalClassLoaderUtil.setClassLoader(
076                                    currentThread.getContextClassLoader());
077                    }
078                    catch (Exception e) {
079                            e.printStackTrace();
080                    }
081    
082                    // Log4J
083    
084                    if (GetterUtil.getBoolean(SystemProperties.get(
085                                    "log4j.configure.on.startup"), true)) {
086    
087                            ClassLoader classLoader = InitUtil.class.getClassLoader();
088    
089                            Log4JUtil.configureLog4J(
090                                    classLoader.getResource("META-INF/portal-log4j.xml"));
091                            Log4JUtil.configureLog4J(
092                                    classLoader.getResource("META-INF/portal-log4j-ext.xml"));
093                    }
094    
095                    // Shared log
096    
097                    try {
098                            LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
099                    }
100                    catch (Exception e) {
101                            e.printStackTrace();
102                    }
103    
104                    // Cache registry
105    
106                    CacheRegistryUtil.setCacheRegistry(new CacheRegistryImpl());
107    
108                    // Configuration factory
109    
110                    ConfigurationFactoryUtil.setConfigurationFactory(
111                            new ConfigurationFactoryImpl());
112    
113                    // DB factory
114    
115                    DBFactoryUtil.setDBFactory(new DBFactoryImpl());
116    
117                    // Java properties
118    
119                    JavaProps.isJDK5();
120    
121                    if (_PRINT_TIME) {
122                            System.out.println(
123                                    "InitAction takes " + stopWatch.getTime() + " ms");
124                    }
125    
126                    _initialized = true;
127            }
128    
129            public synchronized static void initWithSpring() {
130                    if (_initialized) {
131                            return;
132                    }
133    
134                    init();
135    
136                    SpringUtil.loadContext();
137    
138                    _initialized = true;
139            }
140    
141            private static final boolean _PRINT_TIME = false;
142    
143            private static boolean _initialized;
144    
145    }