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