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.tools;
016    
017    import com.liferay.portal.dao.db.HypersonicDB;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.nio.charset.StandardCharsets;
026    import java.nio.file.Files;
027    import java.nio.file.Paths;
028    
029    import java.sql.Connection;
030    import java.sql.DriverManager;
031    import java.sql.Statement;
032    
033    import java.util.List;
034    import java.util.Map;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Shuyang Zhou
039     */
040    public class HypersonicLoader {
041    
042            public static void loadHypersonic(Connection con, String fileName)
043                    throws Exception {
044    
045                    List<String> lines = Files.readAllLines(
046                            Paths.get(fileName), StandardCharsets.UTF_8);
047    
048                    StringBundler sb = new StringBundler(lines.size() * 2);
049    
050                    for (String line : lines) {
051                            if (line.isEmpty() || line.startsWith(StringPool.DOUBLE_SLASH)) {
052                                    continue;
053                            }
054    
055                            sb.append(line);
056                            sb.append(StringPool.NEW_LINE);
057                    }
058    
059                    DB db = new HypersonicDB(0, 0);
060    
061                    db.runSQLTemplateString(con, sb.toString(), false, true);
062            }
063    
064            public static void main(String[] args) throws Exception {
065                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
066    
067                    String databaseName = arguments.get("db.database.name");
068                    String sqlDir = arguments.get("db.sql.dir");
069                    String fileNames = arguments.get("db.file.names");
070    
071                    try {
072                            new HypersonicLoader(databaseName, sqlDir, fileNames);
073                    }
074                    catch (Exception e) {
075                            ArgumentsUtil.processMainException(arguments, e);
076                    }
077            }
078    
079            public HypersonicLoader(
080                            String databaseName, String sqlDir, String fileNames)
081                    throws Exception {
082    
083                    ToolDependencies.wireBasic();
084    
085                    DBFactoryUtil.setDB(DB.TYPE_HYPERSONIC, null);
086    
087                    // See LEP-2927. Appending ;shutdown=true to the database connection URL
088                    // guarantees that ${databaseName}.log is purged.
089    
090                    try (Connection con = DriverManager.getConnection(
091                                    "jdbc:hsqldb:" + sqlDir + "/" + databaseName +
092                                            ";shutdown=true",
093                                    "sa", "")) {
094    
095                            if (Validator.isNull(fileNames)) {
096                                    loadHypersonic(con, sqlDir + "/portal/portal-hypersonic.sql");
097                                    loadHypersonic(con, sqlDir + "/indexes.sql");
098                            }
099                            else {
100                                    for (String fileName : StringUtil.split(fileNames)) {
101                                            loadHypersonic(con, sqlDir + "/" + fileName);
102                                    }
103                            }
104    
105                            // Shutdown Hypersonic
106    
107                            try (Statement statement = con.createStatement()) {
108                                    statement.execute("SHUTDOWN COMPACT");
109                            }
110                    }
111            }
112    
113    }