001    /**
002     * Copyright (c) 2000-2012 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.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(classLoader);
095                    }
096    
097                    // Shared log
098    
099                    try {
100                            LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
101                    }
102                    catch (Exception e) {
103                            e.printStackTrace();
104                    }
105    
106                    // Cache registry
107    
108                    CacheRegistryUtil.setCacheRegistry(new CacheRegistryImpl());
109    
110                    // Configuration factory
111    
112                    ConfigurationFactoryUtil.setConfigurationFactory(
113                            new ConfigurationFactoryImpl());
114    
115                    // Data source factory
116    
117                    DataSourceFactoryUtil.setDataSourceFactory(new DataSourceFactoryImpl());
118    
119                    // DB factory
120    
121                    DBFactoryUtil.setDBFactory(new DBFactoryImpl());
122    
123                    // Java properties
124    
125                    JavaProps.isJDK5();
126    
127                    // ROME
128    
129                    XmlReader.setDefaultEncoding(StringPool.UTF8);
130    
131                    if (_PRINT_TIME) {
132                            System.out.println(
133                                    "InitAction takes " + stopWatch.getTime() + " ms");
134                    }
135    
136                    _initialized = true;
137            }
138    
139            public synchronized static void initWithSpring() {
140                    initWithSpring(false);
141            }
142    
143            public synchronized static void initWithSpring(boolean force) {
144                    if (force) {
145                            _initialized = false;
146                    }
147    
148                    if (_initialized) {
149                            return;
150                    }
151    
152                    if (!_neverInitialized) {
153                            PropsUtil.reload();
154                    }
155                    else {
156                            _neverInitialized = false;
157                    }
158    
159                    init();
160    
161                    SpringUtil.loadContext();
162    
163                    _initialized = true;
164            }
165    
166            private static final boolean _PRINT_TIME = false;
167    
168            private static boolean _initialized;
169            private static boolean _neverInitialized = true;
170    
171    }