1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.configuration.ConfigurationFactoryImpl;
26  import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.JavaProps;
30  import com.liferay.portal.kernel.util.LocaleUtil;
31  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
32  import com.liferay.portal.kernel.util.TimeZoneUtil;
33  import com.liferay.portal.log.Log4jLogFactoryImpl;
34  import com.liferay.portal.spring.util.SpringUtil;
35  import com.liferay.util.SystemProperties;
36  import com.liferay.util.log4j.Log4JUtil;
37  
38  import org.apache.commons.lang.time.StopWatch;
39  
40  /**
41   * <a href="InitUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class InitUtil {
47  
48      public synchronized static void init() {
49          if (_initialized) {
50              return;
51          }
52  
53          StopWatch stopWatch = null;
54  
55          if (_PRINT_TIME) {
56              stopWatch = new StopWatch();
57  
58              stopWatch.start();
59          }
60  
61          // Set the default locale used by Liferay. This locale is no longer set
62          // at the VM level. See LEP-2584.
63  
64          String userLanguage = SystemProperties.get("user.language");
65          String userCountry = SystemProperties.get("user.country");
66          String userVariant = SystemProperties.get("user.variant");
67  
68          LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
69  
70          // Set the default time zone used by Liferay. This time zone is no
71          // longer set at the VM level. See LEP-2584.
72  
73          String userTimeZone = SystemProperties.get("user.timezone");
74  
75          TimeZoneUtil.setDefault(userTimeZone);
76  
77          // Shared class loader
78  
79          try {
80              Thread currentThread = Thread.currentThread();
81  
82              PortalClassLoaderUtil.setClassLoader(
83                  currentThread.getContextClassLoader());
84          }
85          catch (Exception e) {
86              e.printStackTrace();
87          }
88  
89          // Log4J
90  
91          if (GetterUtil.getBoolean(SystemProperties.get(
92                  "log4j.configure.on.startup"), true)) {
93  
94              ClassLoader classLoader = InitUtil.class.getClassLoader();
95  
96              Log4JUtil.configureLog4J(
97                  classLoader.getResource("META-INF/portal-log4j.xml"));
98              Log4JUtil.configureLog4J(
99                  classLoader.getResource("META-INF/portal-log4j-ext.xml"));
100         }
101 
102         // Shared log
103 
104         try {
105             LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
106         }
107         catch (Exception e) {
108             e.printStackTrace();
109         }
110 
111         // Configuration factory
112 
113         ConfigurationFactoryUtil.setConfigurationFactory(
114             new ConfigurationFactoryImpl());
115 
116         // Java properties
117 
118         JavaProps.isJDK5();
119 
120         if (_PRINT_TIME) {
121             System.out.println(
122                 "InitAction takes " + stopWatch.getTime() + " ms");
123         }
124 
125         _initialized = true;
126     }
127 
128     public synchronized static void initWithSpring() {
129         if (_initialized) {
130             return;
131         }
132 
133         init();
134 
135         SpringUtil.loadContext();
136 
137         _initialized = true;
138     }
139 
140     private static final boolean _PRINT_TIME = false;
141 
142     private static boolean _initialized;
143 
144 }