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.kernel.util.WebKeys;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PropsValues;
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                    _processOtherProperties(request, unicodeProperties);
140    
141                    updateLanguage(request, response);
142    
143                    unicodeProperties.put(
144                            PropsKeys.SETUP_WIZARD_ENABLED, String.valueOf(false));
145    
146                    HttpSession session = request.getSession();
147    
148                    session.setAttribute(
149                            WebKeys.SETUP_WIZARD_PROPERTIES, unicodeProperties);
150                    session.setAttribute(
151                            WebKeys.SETUP_WIZARD_PROPERTIES_FILE_CREATED,
152                            _writePropertiesFile(unicodeProperties));
153            }
154    
155            private static String _getParameter(
156                    HttpServletRequest request, String name, String defaultValue) {
157    
158                    name = _PROPERTIES_PREFIX.concat(name).concat(StringPool.DOUBLE_DASH);
159    
160                    return ParamUtil.getString(request, name, defaultValue);
161            }
162    
163            private static boolean _isDatabaseConfigured(
164                    UnicodeProperties unicodeProperties) {
165    
166                    String defaultDriverClassName = unicodeProperties.get(
167                            PropsKeys.JDBC_DEFAULT_DRIVER_CLASS_NAME);
168                    String defaultPassword = unicodeProperties.get(
169                            PropsKeys.JDBC_DEFAULT_PASSWORD);
170                    String defaultURL = unicodeProperties.get(PropsKeys.JDBC_DEFAULT_URL);
171                    String defaultUsername = unicodeProperties.get(
172                            PropsKeys.JDBC_DEFAULT_USERNAME);
173    
174                    if (PropsValues.JDBC_DEFAULT_DRIVER_CLASS_NAME.equals(
175                                    defaultDriverClassName) &&
176                            PropsValues.JDBC_DEFAULT_PASSWORD.equals(defaultPassword) &&
177                            PropsValues.JDBC_DEFAULT_URL.equals(defaultURL) &&
178                            PropsValues.JDBC_DEFAULT_USERNAME.equals(defaultUsername) ) {
179    
180                            return true;
181                    }
182    
183                    return false;
184            }
185    
186            private static void _processDatabaseProperties(
187                            HttpServletRequest request, UnicodeProperties unicodeProperties,
188                            boolean databaseConfigured)
189                    throws Exception {
190    
191                    boolean defaultDatabase = ParamUtil.getBoolean(
192                            request, "defaultDatabase", true);
193    
194                    if (defaultDatabase || databaseConfigured) {
195                            unicodeProperties.remove(PropsKeys.JDBC_DEFAULT_URL);
196                            unicodeProperties.remove(PropsKeys.JDBC_DEFAULT_DRIVER_CLASS_NAME);
197                            unicodeProperties.remove(PropsKeys.JDBC_DEFAULT_USERNAME);
198                            unicodeProperties.remove(PropsKeys.JDBC_DEFAULT_PASSWORD);
199                    }
200            }
201    
202            private static void _processOtherProperties(
203                            HttpServletRequest request, UnicodeProperties unicodeProperties)
204                    throws Exception {
205    
206                    _processProperty(
207                            request, unicodeProperties, "adminFirstName",
208                            PropsKeys.DEFAULT_ADMIN_FIRST_NAME,
209                            PropsValues.DEFAULT_ADMIN_FIRST_NAME);
210                    _processProperty(
211                            request, unicodeProperties, "adminLastName",
212                            PropsKeys.DEFAULT_ADMIN_LAST_NAME,
213                            PropsValues.DEFAULT_ADMIN_LAST_NAME);
214                    _processProperty(
215                            request, unicodeProperties, "companyName",
216                            PropsKeys.COMPANY_DEFAULT_NAME, PropsValues.COMPANY_DEFAULT_NAME);
217            }
218    
219            private static void _processProperty(
220                            HttpServletRequest request, UnicodeProperties unicodeProperties,
221                            String parameterName, String propertyKey, String defaultValue)
222                    throws Exception {
223    
224                    String value = ParamUtil.getString(
225                            request, parameterName, defaultValue);
226    
227                    if (!value.equals(defaultValue)) {
228                            unicodeProperties.put(propertyKey, value);
229                    }
230            }
231    
232            private static void _testConnection(
233                            String driverClassName, String url, String userName,
234                            String password, String jndiName)
235                    throws Exception {
236    
237                    if (Validator.isNull(jndiName)) {
238                            Class.forName(driverClassName);
239                    }
240    
241                    DataSource dataSource = null;
242                    Connection connection = null;
243    
244                    try {
245                            dataSource = DataSourceFactoryUtil.initDataSource(
246                                    driverClassName, url, userName, password, jndiName);
247    
248                            connection = dataSource.getConnection();
249                    }
250                    finally {
251                            DataAccess.cleanUp(connection);
252                            DataSourceFactoryUtil.destroyDataSource(dataSource);
253                    }
254            }
255    
256            private static boolean _writePropertiesFile(
257                    UnicodeProperties unicodeProperties) {
258    
259                    try {
260                            FileUtil.write(
261                                    PropsValues.LIFERAY_HOME, PROPERTIES_FILE_NAME,
262                                    unicodeProperties.toString());
263    
264                            if (FileUtil.exists(
265                                            PropsValues.LIFERAY_HOME + StringPool.SLASH +
266                                                    PROPERTIES_FILE_NAME)) {
267    
268                                    return true;
269                            }
270                    }
271                    catch (IOException ioe) {
272                            _log.error(ioe, ioe);
273                    }
274    
275                    return false;
276            }
277    
278            private static final String _PROPERTIES_PREFIX = "properties--";
279    
280            private static final Log _log = LogFactoryUtil.getLog(
281                    SetupWizardUtil.class);
282    
283    }