1   /**
2    * Copyright (c) 2000-2008 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.CommonsLogFactoryImpl;
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              PortalClassLoaderUtil.setClassLoader(
81                  Thread.currentThread().getContextClassLoader());
82          }
83          catch (Exception e) {
84              e.printStackTrace();
85          }
86  
87          // Log4J
88  
89          if (GetterUtil.getBoolean(SystemProperties.get(
90                  "log4j.configure.on.startup"), true)) {
91  
92              ClassLoader classLoader = InitUtil.class.getClassLoader();
93  
94              Log4JUtil.configureLog4J(
95                  classLoader.getResource("META-INF/portal-log4j.xml"));
96              Log4JUtil.configureLog4J(
97                  classLoader.getResource("META-INF/portal-log4j-ext.xml"));
98          }
99  
100         // Shared log
101 
102         try {
103             LogFactoryUtil.setLogFactory(new CommonsLogFactoryImpl());
104         }
105         catch (Exception e) {
106             e.printStackTrace();
107         }
108 
109         // Configuration factory
110 
111         ConfigurationFactoryUtil.setConfigurationFactory(
112             new ConfigurationFactoryImpl());
113 
114         // Java properties
115 
116         JavaProps.isJDK5();
117 
118         if (_PRINT_TIME) {
119             System.out.println(
120                 "InitAction takes " + stopWatch.getTime() + " ms");
121         }
122 
123         _initialized = true;
124     }
125 
126     public synchronized static void initWithSpring() {
127         if (_initialized) {
128             return;
129         }
130 
131         init();
132 
133         SpringUtil.loadContext();
134 
135         _initialized = true;
136     }
137 
138     private static final boolean _PRINT_TIME = false;
139 
140     private static boolean _initialized;
141 
142 }