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.events;
24  
25  import com.liferay.portal.bean.BeanLocatorImpl;
26  import com.liferay.portal.jcr.jackrabbit.JCRFactoryImpl;
27  import com.liferay.portal.kernel.bean.BeanLocatorUtil;
28  import com.liferay.portal.kernel.events.ActionException;
29  import com.liferay.portal.kernel.events.SimpleAction;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.JavaProps;
33  import com.liferay.portal.kernel.util.LocaleUtil;
34  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
35  import com.liferay.portal.kernel.util.ServerDetector;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.kernel.util.TimeZoneUtil;
38  import com.liferay.portal.log.CommonsLogFactoryImpl;
39  import com.liferay.portal.security.jaas.PortalConfiguration;
40  import com.liferay.portal.util.PropsUtil;
41  import com.liferay.portal.velocity.LiferayResourceLoader;
42  import com.liferay.util.FileUtil;
43  import com.liferay.util.SystemProperties;
44  import com.liferay.util.Time;
45  import com.liferay.util.log4j.Log4JUtil;
46  
47  import java.io.File;
48  
49  import javax.security.auth.login.Configuration;
50  
51  import org.apache.commons.collections.ExtendedProperties;
52  import org.apache.commons.lang.time.StopWatch;
53  import org.apache.velocity.app.Velocity;
54  import org.apache.velocity.runtime.RuntimeConstants;
55  
56  /**
57   * <a href="InitAction.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class InitAction extends SimpleAction {
63  
64      public void run(String[] ids) throws ActionException {
65          StopWatch stopWatch = null;
66  
67          if (_PRINT_TIME) {
68              stopWatch = new StopWatch();
69  
70              stopWatch.start();
71          }
72  
73          // Set the default locale used by Liferay. This locale is no longer set
74          // at the VM level. See LEP-2584.
75  
76          String userLanguage = SystemProperties.get("user.language");
77          String userCountry = SystemProperties.get("user.country");
78          String userVariant = SystemProperties.get("user.variant");
79  
80          LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
81  
82          // Set the default time zone used by Liferay. This time zone is no
83          // longer set at the VM level. See LEP-2584.
84  
85          String userTimeZone = SystemProperties.get("user.timezone");
86  
87          TimeZoneUtil.setDefault(userTimeZone);
88  
89          // Shared class loader
90  
91          try {
92              PortalClassLoaderUtil.setClassLoader(
93                  Thread.currentThread().getContextClassLoader());
94          }
95          catch (Exception e) {
96              e.printStackTrace();
97          }
98  
99          // Log4J
100 
101         if (GetterUtil.getBoolean(SystemProperties.get(
102                 "log4j.configure.on.startup"), true)) {
103 
104             ClassLoader classLoader = getClass().getClassLoader();
105 
106             Log4JUtil.configureLog4J(
107                 classLoader.getResource("META-INF/portal-log4j.xml"));
108             Log4JUtil.configureLog4J(
109                 classLoader.getResource("META-INF/portal-log4j-ext.xml"));
110         }
111 
112         // Shared log
113 
114         try {
115             LogFactoryUtil.setLogFactory(new CommonsLogFactoryImpl());
116         }
117         catch (Exception e) {
118             e.printStackTrace();
119         }
120 
121         // Set the portal property "resource.repositories.root" as a system
122         // property as well so it can be referenced by Ehcache.
123 
124         SystemProperties.set(
125             PropsUtil.RESOURCE_REPOSITORIES_ROOT,
126             PropsUtil.get(PropsUtil.RESOURCE_REPOSITORIES_ROOT));
127 
128         // Bean locator
129 
130         BeanLocatorUtil.setBeanLocator(new BeanLocatorImpl());
131 
132         // Java properties
133 
134         JavaProps.isJDK5();
135 
136         // JAAS
137 
138         if ((GetterUtil.getBoolean(PropsUtil.get(
139                 PropsUtil.PORTAL_CONFIGURATION))) &&
140             (ServerDetector.isJBoss() || ServerDetector.isPramati() ||
141              ServerDetector.isWebLogic())) {
142 
143             PortalConfiguration portalConfig = new PortalConfiguration(
144                 Configuration.getConfiguration());
145 
146             Configuration.setConfiguration(portalConfig);
147         }
148 
149         // JCR
150 
151         try {
152             File repositoryRoot = new File(JCRFactoryImpl.REPOSITORY_ROOT);
153 
154             if (!repositoryRoot.exists()) {
155                 repositoryRoot.mkdirs();
156 
157                 File tempFile = new File(
158                     SystemProperties.get(SystemProperties.TMP_DIR) +
159                         File.separator + Time.getTimestamp());
160 
161                 String repositoryXmlPath =
162                     "com/liferay/portal/jcr/jackrabbit/dependencies/" +
163                         "repository-ext.xml";
164 
165                 ClassLoader classLoader = getClass().getClassLoader();
166 
167                 if (classLoader.getResource(repositoryXmlPath) == null) {
168                     repositoryXmlPath =
169                         "com/liferay/portal/jcr/jackrabbit/dependencies/" +
170                             "repository.xml";
171                 }
172 
173                 String content = StringUtil.read(
174                     classLoader, repositoryXmlPath);
175 
176                 FileUtil.write(tempFile, content);
177 
178                 FileUtil.copyFile(
179                     tempFile, new File(JCRFactoryImpl.CONFIG_FILE_PATH));
180 
181                 tempFile.delete();
182             }
183         }
184         catch (Exception e) {
185             e.printStackTrace();
186         }
187 
188         // Velocity
189 
190         LiferayResourceLoader.setListeners(PropsUtil.getArray(
191             PropsUtil.VELOCITY_ENGINE_RESOURCE_LISTENERS));
192 
193         ExtendedProperties props = new ExtendedProperties();
194 
195         props.setProperty(RuntimeConstants.RESOURCE_LOADER, "servlet");
196 
197         props.setProperty(
198             "servlet." + RuntimeConstants.RESOURCE_LOADER + ".class",
199             LiferayResourceLoader.class.getName());
200 
201         props.setProperty(
202             RuntimeConstants.RESOURCE_MANAGER_CLASS,
203             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER));
204 
205         props.setProperty(
206             RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS,
207             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
208 
209         props.setProperty(
210             "velocimacro.library",
211             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
212 
213         props.setProperty(
214             RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
215             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_LOGGER));
216 
217         props.setProperty(
218             "runtime.log.logsystem.log4j.category",
219             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_LOGGER_CATEGORY));
220 
221         Velocity.setExtendedProperties(props);
222 
223         try {
224             Velocity.init();
225         }
226         catch (Exception e) {
227             e.printStackTrace();
228         }
229 
230         if (_PRINT_TIME) {
231             System.out.println(
232                 "InitAction takes " + stopWatch.getTime() + " ms");
233         }
234     }
235 
236     private static final boolean _PRINT_TIME = false;
237 
238 }