001    /**
002     * Copyright (c) 2000-2011 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.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.JavaProps;
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.spring.util.SpringUtil;
035    import com.liferay.util.log4j.Log4JUtil;
036    
037    import com.sun.syndication.io.XmlReader;
038    
039    import org.apache.commons.lang.time.StopWatch;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class InitUtil {
045    
046            public static synchronized void init() {
047                    if (_initialized) {
048                            return;
049                    }
050    
051                    StopWatch stopWatch = null;
052    
053                    if (_PRINT_TIME) {
054                            stopWatch = new StopWatch();
055    
056                            stopWatch.start();
057                    }
058    
059                    // Set the default locale used by Liferay. This locale is no longer set
060                    // at the VM level. See LEP-2584.
061    
062                    String userLanguage = SystemProperties.get("user.language");
063                    String userCountry = SystemProperties.get("user.country");
064                    String userVariant = SystemProperties.get("user.variant");
065    
066                    LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
067    
068                    // Set the default time zone used by Liferay. This time zone is no
069                    // longer set at the VM level. See LEP-2584.
070    
071                    String userTimeZone = SystemProperties.get("user.timezone");
072    
073                    TimeZoneUtil.setDefault(userTimeZone);
074    
075                    // Shared class loader
076    
077                    try {
078                            Thread currentThread = Thread.currentThread();
079    
080                            PortalClassLoaderUtil.setClassLoader(
081                                    currentThread.getContextClassLoader());
082                    }
083                    catch (Exception e) {
084                            e.printStackTrace();
085                    }
086    
087                    // Log4J
088    
089                    if (GetterUtil.getBoolean(SystemProperties.get(
090                                    "log4j.configure.on.startup"), true)) {
091    
092                            ClassLoader classLoader = InitUtil.class.getClassLoader();
093    
094                            Log4JUtil.configureLog4J(
095                                    classLoader.getResource("META-INF/portal-log4j.xml"));
096                            Log4JUtil.configureLog4J(
097                                    classLoader.getResource("META-INF/portal-log4j-ext.xml"));
098                    }
099    
100                    // Shared log
101    
102                    try {
103                            LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
104                    }
105                    catch (Exception e) {
106                            e.printStackTrace();
107                    }
108    
109                    // Cache registry
110    
111                    CacheRegistryUtil.setCacheRegistry(new CacheRegistryImpl());
112    
113                    // Configuration factory
114    
115                    ConfigurationFactoryUtil.setConfigurationFactory(
116                            new ConfigurationFactoryImpl());
117    
118                    // Data source factory
119    
120                    DataSourceFactoryUtil.setDataSourceFactory(new DataSourceFactoryImpl());
121    
122                    // DB factory
123    
124                    DBFactoryUtil.setDBFactory(new DBFactoryImpl());
125    
126                    // Java properties
127    
128                    JavaProps.isJDK5();
129    
130                    // ROME
131    
132                    XmlReader.setDefaultEncoding(StringPool.UTF8);
133    
134                    if (_PRINT_TIME) {
135                            System.out.println(
136                                    "InitAction takes " + stopWatch.getTime() + " ms");
137                    }
138    
139                    _initialized = true;
140            }
141    
142            public synchronized static void initWithSpring() {
143                    initWithSpring(false);
144            }
145    
146            public synchronized static void initWithSpring(boolean force) {
147                    if (force) {
148                            _initialized = false;
149                    }
150    
151                    if (_initialized) {
152                            return;
153                    }
154    
155                    if (!_neverInitialized) {
156                            PropsUtil.reload();
157                    }
158                    else {
159                            _neverInitialized = false;
160                    }
161    
162                    init();
163    
164                    SpringUtil.loadContext();
165    
166                    _initialized = true;
167            }
168    
169            private static final boolean _PRINT_TIME = false;
170    
171            private static boolean _initialized;
172            private static boolean _neverInitialized = true;
173    
174    }