1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.configuration.ConfigurationFactoryImpl;
18  import com.liferay.portal.dao.db.DBFactoryImpl;
19  import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
20  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.JavaProps;
24  import com.liferay.portal.kernel.util.LocaleUtil;
25  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
26  import com.liferay.portal.kernel.util.TimeZoneUtil;
27  import com.liferay.portal.log.Log4jLogFactoryImpl;
28  import com.liferay.portal.spring.util.SpringUtil;
29  import com.liferay.util.SystemProperties;
30  import com.liferay.util.log4j.Log4JUtil;
31  
32  import org.apache.commons.lang.time.StopWatch;
33  
34  /**
35   * <a href="InitUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class InitUtil {
40  
41      public static synchronized void init() {
42          if (_initialized) {
43              return;
44          }
45  
46          StopWatch stopWatch = null;
47  
48          if (_PRINT_TIME) {
49              stopWatch = new StopWatch();
50  
51              stopWatch.start();
52          }
53  
54          // Set the default locale used by Liferay. This locale is no longer set
55          // at the VM level. See LEP-2584.
56  
57          String userLanguage = SystemProperties.get("user.language");
58          String userCountry = SystemProperties.get("user.country");
59          String userVariant = SystemProperties.get("user.variant");
60  
61          LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
62  
63          // Set the default time zone used by Liferay. This time zone is no
64          // longer set at the VM level. See LEP-2584.
65  
66          String userTimeZone = SystemProperties.get("user.timezone");
67  
68          TimeZoneUtil.setDefault(userTimeZone);
69  
70          // Shared class loader
71  
72          try {
73              Thread currentThread = Thread.currentThread();
74  
75              PortalClassLoaderUtil.setClassLoader(
76                  currentThread.getContextClassLoader());
77          }
78          catch (Exception e) {
79              e.printStackTrace();
80          }
81  
82          // Log4J
83  
84          if (GetterUtil.getBoolean(SystemProperties.get(
85                  "log4j.configure.on.startup"), true)) {
86  
87              ClassLoader classLoader = InitUtil.class.getClassLoader();
88  
89              Log4JUtil.configureLog4J(
90                  classLoader.getResource("META-INF/portal-log4j.xml"));
91              Log4JUtil.configureLog4J(
92                  classLoader.getResource("META-INF/portal-log4j-ext.xml"));
93          }
94  
95          // Shared log
96  
97          try {
98              LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
99          }
100         catch (Exception e) {
101             e.printStackTrace();
102         }
103 
104         // Configuration factory
105 
106         ConfigurationFactoryUtil.setConfigurationFactory(
107             new ConfigurationFactoryImpl());
108 
109         // DB factory
110 
111         DBFactoryUtil.setDBFactory(new DBFactoryImpl());
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 }