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.convert.database;
016    
017    import com.liferay.portal.convert.BaseConvertProcess;
018    import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.util.MaintenanceUtil;
021    import com.liferay.portal.util.ShutdownUtil;
022    import com.liferay.registry.collections.ServiceTrackerCollections;
023    
024    import java.util.List;
025    
026    import javax.sql.DataSource;
027    
028    /**
029     * @author Alexander Chow
030     */
031    public class DatabaseConvertProcess extends BaseConvertProcess {
032    
033            public void destroy() {
034                    _databaseConverters.clear();
035    
036                    _databaseConverters = null;
037            }
038    
039            @Override
040            public String getDescription() {
041                    return "migrate-data-from-one-database-to-another";
042            }
043    
044            @Override
045            public String getParameterDescription() {
046                    return "please-enter-jdbc-information-for-new-database";
047            }
048    
049            @Override
050            public String[] getParameterNames() {
051                    return new String[] {
052                            "jdbc-driver-class-name", "jdbc-url", "jdbc-user-name",
053                            "jdbc-password"
054                    };
055            }
056    
057            @Override
058            public boolean isEnabled() {
059                    return true;
060            }
061    
062            protected DataSource buildDatasource() throws Exception {
063                    String[] values = getParameterValues();
064    
065                    String driverClassName = values[0];
066                    String url = values[1];
067                    String userName = values[2];
068                    String password = values[3];
069                    String jndiName = StringPool.BLANK;
070    
071                    return DataSourceFactoryUtil.initDataSource(
072                            driverClassName, url, userName, password, jndiName);
073            }
074    
075            @Override
076            protected void doConvert() throws Exception {
077                    MaintenanceUtil.appendStatus("Starting database migration.");
078    
079                    DataSource dataSource = buildDatasource();
080    
081                    for (DatabaseConverter databaseConverter : _databaseConverters) {
082                            databaseConverter.convert(dataSource);
083                    }
084    
085                    MaintenanceUtil.appendStatus(
086                            "Please change your JDBC settings before restarting server");
087    
088                    ShutdownUtil.shutdown(0);
089            }
090    
091            private List<DatabaseConverter> _databaseConverters =
092                    ServiceTrackerCollections.openList(DatabaseConverter.class);
093    
094    }