SchemaManager.java 19 KB
Newer Older
1 2 3 4
/**
 * $Revision$
 * $Date$
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
18 19 20 21
 */

package org.jivesoftware.database;

22 23 24 25 26 27 28 29 30 31 32 33 34
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

35
import org.jivesoftware.database.bugfix.OF33;
36 37 38
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
39 40 41 42
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.LocaleUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
43 44

/**
45
 * Manages database schemas for Openfire and Openfire plugins. The manager uses the
46
 * ofVersion database table to figure out which database schema is currently installed
47 48 49 50 51 52 53 54 55 56 57 58
 * and then attempts to automatically apply database schema changes as necessary.<p>
 *
 * Running database schemas automatically requires appropriate database permissions.
 * Without those permissions, the automatic installation/upgrade process will fail
 * and users will be prompted to apply database changes manually.
 *
 * @see DbConnectionManager#getSchemaManager()
 *
 * @author Matt Tucker
 */
public class SchemaManager {

59 60
	private static final Logger Log = LoggerFactory.getLogger(SchemaManager.class);

61
    private static final String CHECK_VERSION_OLD =
62
            "SELECT minorVersion FROM jiveVersion";
63
    private static final String CHECK_VERSION =
64
            "SELECT version FROM ofVersion WHERE name=?";
65 66
    private static final String CHECK_VERSION_JIVE =
            "SELECT version FROM jiveVersion WHERE name=?";
67 68

    /**
69
     * Current Openfire database schema version.
70
     */
71
    private static final int DATABASE_VERSION = 21;
72 73 74 75 76 77 78 79 80

    /**
     * Creates a new Schema manager.
     */
    SchemaManager() {

    }

    /**
81
     * Checks the Openfire database schema to ensure that it's installed and up to date.
82 83 84 85 86 87
     * If the schema isn't present or up to date, an automatic update will be attempted.
     *
     * @param con a connection to the database.
     * @return true if database schema checked out fine, or was automatically installed
     *      or updated successfully.
     */
88
    public boolean checkOpenfireSchema(Connection con) {
89
        // Change 'wildfire' to 'openfire' in ofVersion table (update to new name)
90
        updateToOpenfire(con);
91
        try {
92
            return checkSchema(con, "openfire", DATABASE_VERSION,
93
                    new ResourceLoader() {
94 95
                        @Override
						public InputStream loadResource(String resourceName) {
96 97 98 99
                            File file = new File(JiveGlobals.getHomeDirectory() + File.separator +
                                    "resources" + File.separator + "database", resourceName);
                            try {
                                return new FileInputStream(file);
100 101
                            }
                            catch (FileNotFoundException e) {
102 103
                                return null;
                            }
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                        }
                    });
        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("upgrade.database.failure"), e);
            System.out.println(LocaleUtils.getLocalizedString("upgrade.database.failure"));
        }
        return false;
    }

    /**
     * Checks the plugin's database schema (if one is required) to ensure that it's
     * installed and up to date. If the schema isn't present or up to date, an automatic
     * update will be attempted.
     *
     * @param plugin the plugin.
     * @return true if database schema checked out fine, or was automatically installed
Matt Tucker's avatar
Matt Tucker committed
121 122
     *      or updated successfully, or if it isn't needed. False will only be returned
     *      if there is an error.
123 124 125 126 127 128 129 130
     */
    public boolean checkPluginSchema(final Plugin plugin) {
        final PluginManager pluginManager = XMPPServer.getInstance().getPluginManager();
        String schemaKey = pluginManager.getDatabaseKey(plugin);
        int schemaVersion = pluginManager.getDatabaseVersion(plugin);
        // If the schema key or database version aren't defined, then the plugin doesn't
        // need database tables.
        if (schemaKey == null || schemaVersion == -1) {
Matt Tucker's avatar
Matt Tucker committed
131
            return true;
132 133 134 135 136
        }
        Connection con = null;
        try {
            con = DbConnectionManager.getConnection();
            return checkSchema(con, schemaKey, schemaVersion, new ResourceLoader() {
137 138
                @Override
				public InputStream loadResource(String resourceName) {
139 140 141 142 143 144 145 146 147 148
                    File file = new File(pluginManager.getPluginDirectory(plugin) +
                            File.separator + "database", resourceName);
                    try {
                        return new FileInputStream(file);
                    }
                    catch (FileNotFoundException e) {
                        return null;
                    }
                }
            });
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("upgrade.database.failure"), e);
            System.out.println(LocaleUtils.getLocalizedString("upgrade.database.failure"));
        }
        finally {
            DbConnectionManager.closeConnection(con);
        }
        return false;
    }

    /**
     * Checks to see if the database needs to be upgraded. This method should be
     * called once every time the application starts up.
     *
     * @param con the database connection to use to check the schema with.
     * @param schemaKey the database schema key (name).
     * @param requiredVersion the version that the schema should be at.
     * @param resourceLoader a resource loader that knows how to load schema files.
     * @throws Exception if an error occured.
169
     * @return True if the schema update was successful.
170 171 172 173 174 175
     */
    private boolean checkSchema(Connection con, String schemaKey, int requiredVersion,
            ResourceLoader resourceLoader) throws Exception
    {
        int currentVersion = -1;
        PreparedStatement pstmt = null;
176
        ResultSet rs = null;
177 178 179
        try {
            pstmt = con.prepareStatement(CHECK_VERSION);
            pstmt.setString(1, schemaKey);
180
            rs = pstmt.executeQuery();
181 182 183
            if (rs.next()) {
                currentVersion = rs.getInt(1);
            }
184 185
        }
        catch (SQLException sqle) {
186 187
            // The database schema must not be installed.
            Log.debug("SchemaManager: Error verifying "+schemaKey+" version, probably ignorable.", sqle);
188
            DbConnectionManager.closeStatement(rs, pstmt);
189
            if (schemaKey.equals("openfire")) {
190
                try {
191 192 193
                    // Releases of Openfire before 3.6.0 stored the version in a jiveVersion table.
                    pstmt = con.prepareStatement(CHECK_VERSION_JIVE);
                    pstmt.setString(1, schemaKey);
194
                    rs = pstmt.executeQuery();
195 196 197 198
                    if (rs.next()) {
                        currentVersion = rs.getInt(1);
                    }
                }
199
                catch (SQLException sqlea) {
200 201
                    // The database schema must not be installed.
                    Log.debug("SchemaManager: Error verifying "+schemaKey+" version, probably ignorable.", sqlea);
202 203
                    DbConnectionManager.closeStatement(rs, pstmt);

204 205 206 207
                    // Releases of Openfire before 2.6.0 stored a major and minor version
                    // number so the normal check for version can fail. Check for the
                    // version using the old format in that case.
                    try {
208

209 210 211 212 213 214 215 216
                        pstmt = con.prepareStatement(CHECK_VERSION_OLD);
                        rs = pstmt.executeQuery();
                        if (rs.next()) {
                            currentVersion = rs.getInt(1);
                        }
                    }
                    catch (SQLException sqle2) {
                        // The database schema must not be installed.
217
                        Log.debug("SchemaManager: Error verifying "+schemaKey+" version, probably ignorable", sqle2);
218
                    }
219 220 221 222
                }
            }
        }
        finally {
223
            DbConnectionManager.closeStatement(rs, pstmt);
224 225
        }
        // If already up to date, return.
226
        if (currentVersion >= requiredVersion) {
Matt Tucker's avatar
Matt Tucker committed
227
            return true;
228 229 230
        }
        // If the database schema isn't installed at all, we need to install it.
        else if (currentVersion == -1) {
231
            Log.info(LocaleUtils.getLocalizedString("upgrade.database.missing_schema",
232 233 234
                    Arrays.asList(schemaKey)));
            System.out.println(LocaleUtils.getLocalizedString("upgrade.database.missing_schema",
                    Arrays.asList(schemaKey)));
235
            // Resource will be like "/database/openfire_hsqldb.sql"
236 237 238 239 240 241 242
            String resourceName = schemaKey + "_" +
                    DbConnectionManager.getDatabaseType() + ".sql";
            InputStream resource = resourceLoader.loadResource(resourceName);
            if (resource == null) {
                return false;
            }
            try {
243 244
                // For plugins, we will automatically convert jiveVersion to ofVersion
                executeSQLScript(con, resource, !schemaKey.equals("openfire") && !schemaKey.equals("wildfire"));
245 246
            }
            catch (Exception e) {
247
                Log.error(e.getMessage(), e);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
                return false;
            }
            finally {
                try {
                    resource.close();
                }
                catch (Exception e) {
                    // Ignore.
                }
            }
            Log.info(LocaleUtils.getLocalizedString("upgrade.database.success"));
            System.out.println(LocaleUtils.getLocalizedString("upgrade.database.success"));
            return true;
        }
        // Must have a version of the schema that needs to be upgraded.
        else {
            // The database is an old version that needs to be upgraded.
            Log.info(LocaleUtils.getLocalizedString("upgrade.database.old_schema",
                    Arrays.asList(currentVersion, schemaKey, requiredVersion)));
            System.out.println(LocaleUtils.getLocalizedString("upgrade.database.old_schema",
                    Arrays.asList(currentVersion, schemaKey, requiredVersion)));
            // If the database type is unknown, we don't know how to upgrade it.
            if (DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.unknown) {
                Log.info(LocaleUtils.getLocalizedString("upgrade.database.unknown_db"));
                System.out.println(LocaleUtils.getLocalizedString("upgrade.database.unknown_db"));
                return false;
            }
            // Upgrade scripts for interbase are not maintained.
            else if (DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.interbase) {
                Log.info(LocaleUtils.getLocalizedString("upgrade.database.interbase_db"));
                System.out.println(LocaleUtils.getLocalizedString("upgrade.database.interbase_db"));
                return false;
            }

            // Run all upgrade scripts until we're up to the latest schema.
283
            for (int i = currentVersion + 1; i <= requiredVersion; i++) {
284
                InputStream resource = getUpgradeResource(resourceLoader, i, schemaKey);
285 286 287 288 289 290 291
                
                // apply the 'database-patches-done-in-java'
				try {
					if (i == 21 && schemaKey.equals("openfire")) {
						OF33.executeFix(con);
					}
				} catch (Exception e) {
292
					Log.error(e.getMessage(), e);
293 294
					return false;
				}
295
                if (resource == null) {
296 297 298
                    continue;
                }
                try {
299
                    executeSQLScript(con, resource, !schemaKey.equals("openfire") && !schemaKey.equals("wildfire"));
300 301
                }
                catch (Exception e) {
302
                    Log.error(e.getMessage(), e);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
                    return false;
                }
                finally {
                    try {
                        resource.close();
                    }
                    catch (Exception e) {
                        // Ignore.
                    }
                }
            }
            Log.info(LocaleUtils.getLocalizedString("upgrade.database.success"));
            System.out.println(LocaleUtils.getLocalizedString("upgrade.database.success"));
            return true;
        }
    }

320
    private InputStream getUpgradeResource(ResourceLoader resourceLoader, int upgradeVersion,
321
            String schemaKey)
322 323
    {
        InputStream resource = null;
324 325
        if ("openfire".equals(schemaKey)) {
            // Resource will be like "/database/upgrade/6/openfire_hsqldb.sql"
326 327 328 329 330 331 332 333 334 335 336 337 338
            String path = JiveGlobals.getHomeDirectory() + File.separator + "resources" +
                    File.separator + "database" + File.separator + "upgrade" + File.separator +
                    upgradeVersion;
            String filename = schemaKey + "_" + DbConnectionManager.getDatabaseType() + ".sql";
            File file = new File(path, filename);
            try {
                resource = new FileInputStream(file);
            }
            catch (FileNotFoundException e) {
                // If the resource is null, the specific upgrade number is not available.
            }
        }
        else {
339
            String resourceName = "upgrade/" + upgradeVersion + "/" + schemaKey + "_" +
340 341 342 343 344 345
                    DbConnectionManager.getDatabaseType() + ".sql";
            resource = resourceLoader.loadResource(resourceName);
        }
        return resource;
    }

346 347 348
    private void updateToOpenfire(Connection con){
        PreparedStatement pstmt = null;
        try {
349
            pstmt = con.prepareStatement("UPDATE jiveVersion SET name='openfire' WHERE name='wildfire'");
350 351 352
            pstmt.executeUpdate();
        }
        catch (Exception ex) {
353 354 355
//            Log.warn("Error when trying to update to new name", ex);
            // This is "scary" to see in the logs and causes more confusion than it's worth at this point.
            // So silently move on.
356 357 358 359 360 361
        }
        finally {
            DbConnectionManager.closeStatement(pstmt);
        }
    }

362 363 364 365 366
    /**
     * Executes a SQL script.
     *
     * @param con database connection.
     * @param resource an input stream for the script to execute.
367
     * @param autoreplace automatically replace jiveVersion with ofVersion
368 369 370
     * @throws IOException if an IOException occurs.
     * @throws SQLException if an SQLException occurs.
     */
371
    private static void executeSQLScript(Connection con, InputStream resource, Boolean autoreplace) throws IOException,
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
            SQLException
    {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(resource));
            boolean done = false;
            while (!done) {
                StringBuilder command = new StringBuilder();
                while (true) {
                    String line = in.readLine();
                    if (line == null) {
                        done = true;
                        break;
                    }
                    // Ignore comments and blank lines.
                    if (isSQLCommandPart(line)) {
388
                        command.append(" ").append(line);
389
                    }
390
                    if (line.trim().endsWith(";")) {
391 392 393 394 395
                        break;
                    }
                }
                // Send command to database.
                if (!done && !command.toString().equals("")) {
396 397 398
                    // Remove last semicolon when using Oracle or DB2 to prevent "invalid character error"
                    if (DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.oracle ||
                            DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.db2) {
399 400
                        command.deleteCharAt(command.length() - 1);
                    }
401
                    PreparedStatement pstmt = null;
402
                    try {
403 404 405 406
                        String cmdString = command.toString();
                        if (autoreplace)  {
                            cmdString = cmdString.replaceAll("jiveVersion", "ofVersion");
                        }
407 408
                        pstmt = con.prepareStatement(cmdString);
                        pstmt.execute();
409 410 411 412 413 414
                    }
                    catch (SQLException e) {
                        // Lets show what failed
                        Log.error("SchemaManager: Failed to execute SQL:\n"+command.toString());
                        throw e;
                    }
415 416 417
                    finally {
                        DbConnectionManager.closeStatement(pstmt);
                    }
418 419 420 421 422 423 424 425 426
                }
            }
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (Exception e) {
427
                    Log.error(e.getMessage(), e);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
                }
            }
        }
    }

    private static abstract class ResourceLoader {

        public abstract InputStream loadResource(String resourceName);

    }

    /**
     * Returns true if a line from a SQL schema is a valid command part.
     *
     * @param line the line of the schema.
     * @return true if a valid command part.
     */
    private static boolean isSQLCommandPart(String line) {
        line = line.trim();
        if (line.equals("")) {
            return false;
        }
        // Check to see if the line is a comment. Valid comment types:
        //   "//" is HSQLDB
        //   "--" is DB2 and Postgres
        //   "#" is MySQL
        //   "REM" is Oracle
        //   "/*" is SQLServer
        return !(line.startsWith("//") || line.startsWith("--") || line.startsWith("#") ||
                line.startsWith("REM") || line.startsWith("/*") || line.startsWith("*"));
    }
}