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             !ServerDetector.isSun()) {
104 
105             ClassLoader classLoader = getClass().getClassLoader();
106 
107             Log4JUtil.configureLog4J(
108                 classLoader.getResource("META-INF/portal-log4j.xml"));
109             Log4JUtil.configureLog4J(
110                 classLoader.getResource("META-INF/portal-log4j-ext.xml"));
111         }
112 
113         // Shared log
114 
115         try {
116             LogFactoryUtil.setLogFactory(new CommonsLogFactoryImpl());
117         }
118         catch (Exception e) {
119             e.printStackTrace();
120         }
121 
122         // Set the portal property "resource.repositories.root" as a system
123         // property as well so it can be referenced by Ehcache.
124 
125         SystemProperties.set(
126             PropsUtil.RESOURCE_REPOSITORIES_ROOT,
127             PropsUtil.get(PropsUtil.RESOURCE_REPOSITORIES_ROOT));
128 
129         // Bean locator
130 
131         BeanLocatorUtil.setBeanLocator(new BeanLocatorImpl());
132 
133         // Java properties
134 
135         JavaProps.isJDK5();
136 
137         // JAAS
138 
139         if ((GetterUtil.getBoolean(PropsUtil.get(
140                 PropsUtil.PORTAL_CONFIGURATION))) &&
141             (ServerDetector.isJBoss() || ServerDetector.isPramati() ||
142              ServerDetector.isSun() || ServerDetector.isWebLogic())) {
143 
144             PortalConfiguration portalConfig = new PortalConfiguration(
145                 Configuration.getConfiguration());
146 
147             Configuration.setConfiguration(portalConfig);
148         }
149 
150         // JCR
151 
152         try {
153             File repositoryRoot = new File(JCRFactoryImpl.REPOSITORY_ROOT);
154 
155             if (!repositoryRoot.exists()) {
156                 repositoryRoot.mkdirs();
157 
158                 File tempFile = new File(
159                     SystemProperties.get(SystemProperties.TMP_DIR) +
160                         File.separator + Time.getTimestamp());
161 
162                 String repositoryXmlPath =
163                     "com/liferay/portal/jcr/jackrabbit/dependencies/" +
164                         "repository-ext.xml";
165 
166                 ClassLoader classLoader = getClass().getClassLoader();
167 
168                 if (classLoader.getResource(repositoryXmlPath) == null) {
169                     repositoryXmlPath =
170                         "com/liferay/portal/jcr/jackrabbit/dependencies/" +
171                             "repository.xml";
172                 }
173 
174                 String content = StringUtil.read(
175                     classLoader, repositoryXmlPath);
176 
177                 FileUtil.write(tempFile, content);
178 
179                 FileUtil.copyFile(
180                     tempFile, new File(JCRFactoryImpl.CONFIG_FILE_PATH));
181 
182                 tempFile.delete();
183             }
184         }
185         catch (Exception e) {
186             e.printStackTrace();
187         }
188 
189         // Velocity
190 
191         LiferayResourceLoader.setListeners(PropsUtil.getArray(
192             PropsUtil.VELOCITY_ENGINE_RESOURCE_LISTENERS));
193 
194         ExtendedProperties props = new ExtendedProperties();
195 
196         props.setProperty(RuntimeConstants.RESOURCE_LOADER, "servlet");
197 
198         props.setProperty(
199             "servlet." + RuntimeConstants.RESOURCE_LOADER + ".class",
200             LiferayResourceLoader.class.getName());
201 
202         props.setProperty(
203             RuntimeConstants.RESOURCE_MANAGER_CLASS,
204             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER));
205 
206         props.setProperty(
207             RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS,
208             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
209 
210         props.setProperty(
211             "velocimacro.library",
212             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
213 
214         props.setProperty(
215             RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
216             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_LOGGER));
217 
218         props.setProperty(
219             "runtime.log.logsystem.log4j.category",
220             PropsUtil.get(PropsUtil.VELOCITY_ENGINE_LOGGER_CATEGORY));
221 
222         Velocity.setExtendedProperties(props);
223 
224         try {
225             Velocity.init();
226         }
227         catch (Exception e) {
228             e.printStackTrace();
229         }
230 
231         if (_PRINT_TIME) {
232             System.out.println(
233                 "InitAction takes " + stopWatch.getTime() + " ms");
234         }
235     }
236 
237     private static final boolean _PRINT_TIME = false;
238 
239 }