001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.events;
022:
023: import com.liferay.portal.bean.BeanLocatorImpl;
024: import com.liferay.portal.jcr.jackrabbit.JCRFactoryImpl;
025: import com.liferay.portal.kernel.bean.BeanLocatorUtil;
026: import com.liferay.portal.kernel.events.ActionException;
027: import com.liferay.portal.kernel.events.SimpleAction;
028: import com.liferay.portal.kernel.log.LogFactoryUtil;
029: import com.liferay.portal.kernel.util.GetterUtil;
030: import com.liferay.portal.kernel.util.JavaProps;
031: import com.liferay.portal.kernel.util.LocaleUtil;
032: import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
033: import com.liferay.portal.kernel.util.ServerDetector;
034: import com.liferay.portal.kernel.util.StringUtil;
035: import com.liferay.portal.kernel.util.TimeZoneUtil;
036: import com.liferay.portal.log.CommonsLogFactoryImpl;
037: import com.liferay.portal.security.jaas.PortalConfiguration;
038: import com.liferay.portal.util.PropsUtil;
039: import com.liferay.portal.velocity.LiferayResourceLoader;
040: import com.liferay.util.FileUtil;
041: import com.liferay.util.SystemProperties;
042: import com.liferay.util.Time;
043: import com.liferay.util.log4j.Log4JUtil;
044:
045: import java.io.File;
046:
047: import javax.security.auth.login.Configuration;
048:
049: import org.apache.commons.collections.ExtendedProperties;
050: import org.apache.velocity.app.Velocity;
051: import org.apache.velocity.runtime.RuntimeConstants;
052:
053: /**
054: * <a href="InitAction.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Brian Wing Shun Chan
057: *
058: */
059: public class InitAction extends SimpleAction {
060:
061: public void run(String[] ids) throws ActionException {
062:
063: // Set the default locale used by Liferay. This locale is no longer set
064: // at the VM level. See LEP-2584.
065:
066: String userLanguage = SystemProperties.get("user.language");
067: String userCountry = SystemProperties.get("user.country");
068: String userVariant = SystemProperties.get("user.variant");
069:
070: LocaleUtil.setDefault(userLanguage, userCountry, userVariant);
071:
072: // Set the default time zone used by Liferay. This time zone is no
073: // longer set at the VM level. See LEP-2584.
074:
075: String userTimeZone = SystemProperties.get("user.timezone");
076:
077: TimeZoneUtil.setDefault(userTimeZone);
078:
079: // Shared class loader
080:
081: try {
082: PortalClassLoaderUtil.setClassLoader(Thread.currentThread()
083: .getContextClassLoader());
084: } catch (Exception e) {
085: e.printStackTrace();
086: }
087:
088: // Log4J
089:
090: if (GetterUtil.getBoolean(SystemProperties
091: .get("log4j.configure.on.startup"), true)
092: && !ServerDetector.isSun()) {
093:
094: ClassLoader classLoader = getClass().getClassLoader();
095:
096: Log4JUtil.configureLog4J(classLoader
097: .getResource("META-INF/portal-log4j.xml"));
098: Log4JUtil.configureLog4J(classLoader
099: .getResource("META-INF/portal-log4j-ext.xml"));
100: }
101:
102: // Shared log
103:
104: try {
105: LogFactoryUtil.setLogFactory(new CommonsLogFactoryImpl());
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109:
110: // Set the portal property "resource.repositories.root" as a system
111: // property as well so it can be referenced by Ehcache.
112:
113: SystemProperties.set(PropsUtil.RESOURCE_REPOSITORIES_ROOT,
114: PropsUtil.get(PropsUtil.RESOURCE_REPOSITORIES_ROOT));
115:
116: // Bean locator
117:
118: BeanLocatorUtil.setBeanLocator(new BeanLocatorImpl());
119:
120: // Java properties
121:
122: JavaProps.isJDK5();
123:
124: // JAAS
125:
126: if ((GetterUtil.getBoolean(PropsUtil
127: .get(PropsUtil.PORTAL_CONFIGURATION)))
128: && (ServerDetector.isJBoss()
129: || ServerDetector.isPramati()
130: || ServerDetector.isSun() || ServerDetector
131: .isWebLogic())) {
132:
133: PortalConfiguration portalConfig = new PortalConfiguration(
134: Configuration.getConfiguration());
135:
136: Configuration.setConfiguration(portalConfig);
137: }
138:
139: // JCR
140:
141: try {
142: File repositoryRoot = new File(
143: JCRFactoryImpl.REPOSITORY_ROOT);
144:
145: if (!repositoryRoot.exists()) {
146: repositoryRoot.mkdirs();
147:
148: File tempFile = new File(SystemProperties
149: .get(SystemProperties.TMP_DIR)
150: + File.separator + Time.getTimestamp());
151:
152: String repositoryXmlPath = "com/liferay/portal/jcr/jackrabbit/dependencies/"
153: + "repository-ext.xml";
154:
155: ClassLoader classLoader = getClass().getClassLoader();
156:
157: if (classLoader.getResource(repositoryXmlPath) == null) {
158: repositoryXmlPath = "com/liferay/portal/jcr/jackrabbit/dependencies/"
159: + "repository.xml";
160: }
161:
162: String content = StringUtil.read(classLoader,
163: repositoryXmlPath);
164:
165: FileUtil.write(tempFile, content);
166:
167: FileUtil.copyFile(tempFile, new File(
168: JCRFactoryImpl.CONFIG_FILE_PATH));
169:
170: tempFile.delete();
171: }
172: } catch (Exception e) {
173: e.printStackTrace();
174: }
175:
176: // Velocity
177:
178: LiferayResourceLoader
179: .setListeners(PropsUtil
180: .getArray(PropsUtil.VELOCITY_ENGINE_RESOURCE_LISTENERS));
181:
182: ExtendedProperties props = new ExtendedProperties();
183:
184: props.setProperty(RuntimeConstants.RESOURCE_LOADER, "servlet");
185:
186: props.setProperty("servlet." + RuntimeConstants.RESOURCE_LOADER
187: + ".class", LiferayResourceLoader.class.getName());
188:
189: props
190: .setProperty(
191: RuntimeConstants.RESOURCE_MANAGER_CLASS,
192: PropsUtil
193: .get(PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER));
194:
195: props
196: .setProperty(
197: RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS,
198: PropsUtil
199: .get(PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
200:
201: props.setProperty("velocimacro.library", PropsUtil
202: .get(PropsUtil.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
203:
204: props.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
205: PropsUtil.get(PropsUtil.VELOCITY_ENGINE_LOGGER));
206:
207: props
208: .setProperty(
209: "runtime.log.logsystem.log4j.category",
210: PropsUtil
211: .get(PropsUtil.VELOCITY_ENGINE_LOGGER_CATEGORY));
212:
213: Velocity.setExtendedProperties(props);
214:
215: try {
216: Velocity.init();
217: } catch (Exception e) {
218: e.printStackTrace();
219: }
220: }
221:
222: }
|