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