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