1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class InitUtil {
46  
47      public synchronized static void init() {
48          if (_initialized) {
49              return;
50          }
51  
52          StopWatch stopWatch = null;
53  
54          if (_PRINT_TIME) {
55              stopWatch = new StopWatch();
56  
57              stopWatch.start();
58          }
59  
60          // Set the default locale used by Liferay. This locale is no longer set
61          // at the VM level. See LEP-2584.
62  
63          String userLanguage = SystemProperties.get("user.language");
64          String userCountry = SystemProperties.get("user.country");
65          String userVariant = SystemProperties.get("user.variant");
66  
67          LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
68  
69          // Set the default time zone used by Liferay. This time zone is no
70          // longer set at the VM level. See LEP-2584.
71  
72          String userTimeZone = SystemProperties.get("user.timezone");
73  
74          TimeZoneUtil.setDefault(userTimeZone);
75  
76          // Shared class loader
77  
78          try {
79              Thread currentThread = Thread.currentThread();
80  
81              PortalClassLoaderUtil.setClassLoader(
82                  currentThread.getContextClassLoader());
83          }
84          catch (Exception e) {
85              e.printStackTrace();
86          }
87  
88          // Log4J
89  
90          if (GetterUtil.getBoolean(SystemProperties.get(
91                  "log4j.configure.on.startup"), true)) {
92  
93              ClassLoader classLoader = InitUtil.class.getClassLoader();
94  
95              Log4JUtil.configureLog4J(
96                  classLoader.getResource("META-INF/portal-log4j.xml"));
97              Log4JUtil.configureLog4J(
98                  classLoader.getResource("META-INF/portal-log4j-ext.xml"));
99          }
100 
101         // Shared log
102 
103         try {
104             LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
105         }
106         catch (Exception e) {
107             e.printStackTrace();
108         }
109 
110         // Configuration factory
111 
112         ConfigurationFactoryUtil.setConfigurationFactory(
113             new ConfigurationFactoryImpl());
114 
115         // Java properties
116 
117         JavaProps.isJDK5();
118 
119         if (_PRINT_TIME) {
120             System.out.println(
121                 "InitAction takes " + stopWatch.getTime() + " ms");
122         }
123 
124         _initialized = true;
125     }
126 
127     public synchronized static void initWithSpring() {
128         if (_initialized) {
129             return;
130         }
131 
132         init();
133 
134         SpringUtil.loadContext();
135 
136         _initialized = true;
137     }
138 
139     private static final boolean _PRINT_TIME = false;
140 
141     private static boolean _initialized;
142 
143 }