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