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