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