001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.setup;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.PropertiesParamUtil;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.SystemProperties;
029    import com.liferay.portal.kernel.util.UnicodeProperties;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portal.util.WebKeys;
034    
035    import java.io.IOException;
036    
037    import java.sql.Connection;
038    
039    import java.util.Locale;
040    
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    import javax.servlet.http.HttpSession;
044    
045    import javax.sql.DataSource;
046    
047    import org.apache.struts.Globals;
048    
049    /**
050     * @author Manuel de la Pe??a
051     * @author Julio Camarero
052     * @author Brian Wing Shun Chan
053     * @author Miguel Pastor
054     */
055    public class SetupWizardUtil {
056    
057            public static final String PROPERTIES_FILE_NAME =
058                    "portal-setup-wizard.properties";
059    
060            public static String getDefaultLanguageId() {
061                    Locale defaultLocale = LocaleUtil.getDefault();
062    
063                    return LocaleUtil.toLanguageId(defaultLocale);
064            }
065    
066            public static boolean isDefaultDatabase(HttpServletRequest request) {
067                    boolean hsqldb = ParamUtil.getBoolean(
068                            request, "defaultDatabase",
069                            PropsValues.JDBC_DEFAULT_URL.contains("hsqldb"));
070    
071                    boolean jndi = Validator.isNotNull(PropsValues.JDBC_DEFAULT_JNDI_NAME);
072    
073                    return hsqldb && !jndi;
074            }
075    
076            public static void testDatabase(HttpServletRequest request)
077                    throws Exception {
078    
079                    String driverClassName = _getParameter(
080                            request, PropsKeys.JDBC_DEFAULT_DRIVER_CLASS_NAME,
081                            PropsValues.JDBC_DEFAULT_DRIVER_CLASS_NAME);
082                    String url = _getParameter(request, PropsKeys.JDBC_DEFAULT_URL, null);
083                    String userName = _getParameter(
084                            request, PropsKeys.JDBC_DEFAULT_USERNAME, null);
085                    String password = _getParameter(
086                            request, PropsKeys.JDBC_DEFAULT_PASSWORD, null);
087    
088                    String jndiName = StringPool.BLANK;
089    
090                    if (Validator.isNotNull(PropsValues.JDBC_DEFAULT_JNDI_NAME)) {
091                            jndiName = PropsValues.JDBC_DEFAULT_JNDI_NAME;
092                    }
093    
094                    _testConnection(driverClassName, url, userName, password, jndiName);
095            }
096    
097            public static void updateLanguage(
098                    HttpServletRequest request, HttpServletResponse response) {
099    
100                    String languageId = ParamUtil.getString(
101                            request, "companyLocale", getDefaultLanguageId());
102    
103                    Locale locale = LocaleUtil.fromLanguageId(languageId);
104    
105                    if (!LanguageUtil.isAvailableLocale(locale)) {
106                            return;
107                    }
108    
109                    HttpSession session = request.getSession();
110    
111                    session.setAttribute(Globals.LOCALE_KEY, locale);
112                    session.setAttribute(WebKeys.SETUP_WIZARD_DEFAULT_LOCALE, languageId);
113    
114                    LanguageUtil.updateCookie(request, response, locale);
115    
116                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
117                            WebKeys.THEME_DISPLAY);
118    
119                    themeDisplay.setLanguageId(languageId);
120                    themeDisplay.setLocale(locale);
121            }
122    
123            public static void updateSetup(
124                            HttpServletRequest request, HttpServletResponse response)
125                    throws Exception {
126    
127                    UnicodeProperties unicodeProperties = PropertiesParamUtil.getProperties(
128                            request, _PROPERTIES_PREFIX);
129    
130                    unicodeProperties.setProperty(
131                            PropsKeys.LIFERAY_HOME,
132                            SystemProperties.get(PropsKeys.LIFERAY_HOME));
133    
134                    boolean databaseConfigured = _isDatabaseConfigured(unicodeProperties);
135    
136                    _processDatabaseProperties(
137                            request, unicodeProperties, databaseConfigured);
138    
139                    updateLanguage(request, response);
140    
141                    unicodeProperties.put(
142                            PropsKeys.SETUP_WIZARD_ENABLED, String.valueOf(false));
143    
144                    HttpSession session = request.getSession();
145    
146                    session.setAttribute(
147                            WebKeys.SETUP_WIZARD_PROPERTIES, unicodeProperties);
148                    session.setAttribute(
149                            WebKeys.SETUP_WIZARD_PROPERTIES_FILE_CREATED,
150                            _writePropertiesFile(unicodeProperties));
151            }
152    
153            private static String _getParameter(
154                    HttpServletRequest request, String name, String defaultValue) {
155    
156                    name = _PROPERTIES_PREFIX.concat(name).concat(StringPool.DOUBLE_DASH);
157    
158                    return ParamUtil.getString(request, name, defaultValue);
159            }
160    
161            private static boolean _isDatabaseConfigured(
162                    UnicodeProperties unicodeProperties) {
163    
164                    String defaultDriverClassName = unicodeProperties.get(
165                            PropsKeys.JDBC_DEFAULT_DRIVER_CLASS_NAME);
166                    String defaultPassword = unicodeProperties.get(
167                            PropsKeys.JDBC_DEFAULT_PASSWORD);
168                    String defaultURL = unicodeProperties.get(PropsKeys.JDBC_DEFAULT_URL);
169                    String defaultUsername = unicodeProperties.get(
170                            PropsKeys.JDBC_DEFAULT_USERNAME);
171    
172                    if (PropsValues.JDBC_DEFAULT_DRIVER_CLASS_NAME.equals(
173                                    defaultDriverClassName) &&
174                            PropsValues.JDBC_DEFAULT_PASSWORD.equals(defaultPassword) &&
175                            PropsValues.JDBC_DEFAULT_URL.equals(defaultURL) &&
176                            PropsValues.JDBC_DEFAULT_USERNAME.equals(defaultUsername) ) {
177    
178                            return true;
179                    }
180    
181                    return false;
182            }
183    
184            private static void _processDatabaseProperties(
185                            HttpServletRequest request, UnicodeProperties unicodeProperties,
186                            boolean databaseConfigured)
187                    throws Exception {
188    
189                    boolean defaultDatabase = ParamUtil.getBoolean(
190                            request, "defaultDatabase", true);
191    
192                    if (defaultDatabase || databaseConfigured) {
193                            unicodeProperties.remove(PropsKeys.JDBC_DEFAULT_URL);
194                            unicodeProperties.remove(PropsKeys.JDBC_DEFAULT_DRIVER_CLASS_NAME);
195                            unicodeProperties.remove(PropsKeys.JDBC_DEFAULT_USERNAME);
196                            unicodeProperties.remove(PropsKeys.JDBC_DEFAULT_PASSWORD);
197                    }
198            }
199    
200            private static void _testConnection(
201                            String driverClassName, String url, String userName,
202                            String password, String jndiName)
203                    throws Exception {
204    
205                    if (Validator.isNull(jndiName)) {
206                            Class.forName(driverClassName);
207                    }
208    
209                    DataSource dataSource = null;
210                    Connection connection = null;
211    
212                    try {
213                            dataSource = DataSourceFactoryUtil.initDataSource(
214                                    driverClassName, url, userName, password, jndiName);
215    
216                            connection = dataSource.getConnection();
217                    }
218                    finally {
219                            DataAccess.cleanUp(connection);
220                            DataSourceFactoryUtil.destroyDataSource(dataSource);
221                    }
222            }
223    
224            private static boolean _writePropertiesFile(
225                    UnicodeProperties unicodeProperties) {
226    
227                    try {
228                            FileUtil.write(
229                                    PropsValues.LIFERAY_HOME, PROPERTIES_FILE_NAME,
230                                    unicodeProperties.toString());
231    
232                            if (FileUtil.exists(
233                                            PropsValues.LIFERAY_HOME + StringPool.SLASH +
234                                                    PROPERTIES_FILE_NAME)) {
235    
236                                    return true;
237                            }
238                    }
239                    catch (IOException ioe) {
240                            _log.error(ioe, ioe);
241                    }
242    
243                    return false;
244            }
245    
246            private static final String _PROPERTIES_PREFIX = "properties--";
247    
248            private static final Log _log = LogFactoryUtil.getLog(
249                    SetupWizardUtil.class);
250    
251    }