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.dao.db.DBFactoryImpl;
27  import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
28  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.JavaProps;
32  import com.liferay.portal.kernel.util.LocaleUtil;
33  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
34  import com.liferay.portal.kernel.util.TimeZoneUtil;
35  import com.liferay.portal.log.Log4jLogFactoryImpl;
36  import com.liferay.portal.spring.util.SpringUtil;
37  import com.liferay.util.SystemProperties;
38  import com.liferay.util.log4j.Log4JUtil;
39  
40  import org.apache.commons.lang.time.StopWatch;
41  
42  /**
43   * <a href="InitUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class InitUtil {
48  
49      public synchronized static void init() {
50          if (_initialized) {
51              return;
52          }
53  
54          StopWatch stopWatch = null;
55  
56          if (_PRINT_TIME) {
57              stopWatch = new StopWatch();
58  
59              stopWatch.start();
60          }
61  
62          // Set the default locale used by Liferay. This locale is no longer set
63          // at the VM level. See LEP-2584.
64  
65          String userLanguage = SystemProperties.get("user.language");
66          String userCountry = SystemProperties.get("user.country");
67          String userVariant = SystemProperties.get("user.variant");
68  
69          LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
70  
71          // Set the default time zone used by Liferay. This time zone is no
72          // longer set at the VM level. See LEP-2584.
73  
74          String userTimeZone = SystemProperties.get("user.timezone");
75  
76          TimeZoneUtil.setDefault(userTimeZone);
77  
78          // Shared class loader
79  
80          try {
81              Thread currentThread = Thread.currentThread();
82  
83              PortalClassLoaderUtil.setClassLoader(
84                  currentThread.getContextClassLoader());
85          }
86          catch (Exception e) {
87              e.printStackTrace();
88          }
89  
90          // Log4J
91  
92          if (GetterUtil.getBoolean(SystemProperties.get(
93                  "log4j.configure.on.startup"), true)) {
94  
95              ClassLoader classLoader = InitUtil.class.getClassLoader();
96  
97              Log4JUtil.configureLog4J(
98                  classLoader.getResource("META-INF/portal-log4j.xml"));
99              Log4JUtil.configureLog4J(
100                 classLoader.getResource("META-INF/portal-log4j-ext.xml"));
101         }
102 
103         // Shared log
104 
105         try {
106             LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
107         }
108         catch (Exception e) {
109             e.printStackTrace();
110         }
111 
112         // Configuration factory
113 
114         ConfigurationFactoryUtil.setConfigurationFactory(
115             new ConfigurationFactoryImpl());
116 
117         // DB factory
118 
119         DBFactoryUtil.setDBFactory(new DBFactoryImpl());
120 
121         // Java properties
122 
123         JavaProps.isJDK5();
124 
125         if (_PRINT_TIME) {
126             System.out.println(
127                 "InitAction takes " + stopWatch.getTime() + " ms");
128         }
129 
130         _initialized = true;
131     }
132 
133     public synchronized static void initWithSpring() {
134         if (_initialized) {
135             return;
136         }
137 
138         init();
139 
140         SpringUtil.loadContext();
141 
142         _initialized = true;
143     }
144 
145     private static final boolean _PRINT_TIME = false;
146 
147     private static boolean _initialized;
148 
149 }