001/* Manage HSQL servers for the core module.
002 * 
003 * Copyright (c) 2011 The Regents of the University of California.
004 * All rights reserved.
005 *
006 * '$Author: crawl $'
007 * '$Date: 2015-08-24 22:45:41 +0000 (Mon, 24 Aug 2015) $' 
008 * '$Revision: 33631 $'
009 * 
010 * Permission is hereby granted, without written agreement and without
011 * license or royalty fees, to use, copy, modify, and distribute this
012 * software and its documentation for any purpose, provided that the above
013 * copyright notice and the following two paragraphs appear in all copies
014 * of this software.
015 *
016 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
017 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
018 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
019 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
020 * SUCH DAMAGE.
021 *
022 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
023 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
025 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
026 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
027 * ENHANCEMENTS, OR MODIFICATIONS.
028 *
029 */
030package org.kepler.module.core;
031
032import java.sql.SQLException;
033
034import org.kepler.configuration.ConfigurationManager;
035import org.kepler.configuration.ConfigurationProperty;
036import org.kepler.module.ModuleHSQLManager;
037import org.kepler.util.sql.DatabaseFactory;
038import org.kepler.util.sql.DatabaseType;
039
040import ptolemy.util.MessageHandler;
041
042/** A class to start and stop the two HSQL databases managed by the core
043 *  module:
044 *  
045 *  KeplerData/modules/core/coreDB
046 *  .kepler/cache-2.1/cachedata/hsqldb
047 * 
048 *  The HSQL servers are run in a separate process.
049 *  
050 *  @author Daniel Crawl
051 *  @version $Id: HSQLManager.java 33631 2015-08-24 22:45:41Z crawl $
052 *
053 */
054public class HSQLManager implements ModuleHSQLManager {
055
056    /** Start HSQL servers in a separate process. */
057    public void start() {
058        
059        // start server for coreDB
060        
061        try {
062            _getCoreDBConnection();
063        } catch (Exception e) {
064            MessageHandler.error("Error starting coreDB.", e);
065        }
066        
067        // start server for .kepler cache db
068        
069        try {
070            DatabaseFactory.getDBConnection();
071        } catch (Exception e) {
072            MessageHandler.error("Error starting .kepler cache database.", e);
073        }
074    }
075
076    /** Stop HSQL servers in a separate process. */
077    public void stop() {
078
079        // stop server for coreDB
080        
081        DatabaseType dbType;
082        try {
083            dbType = _getCoreDBConnection();
084        } catch (Exception e) {
085            MessageHandler.error("Error getting connection to coreDB.", e);
086            return;
087        }
088        
089        try {
090            dbType.disconnect();
091        } catch (SQLException e) {
092            MessageHandler.error("Error shutting down coreDB.", e);
093        }
094        
095        // stop server for .kepler cache db
096        DatabaseFactory.shutdownCacheServer();        
097    }
098
099    /** Get a connection to the coreDB database. */
100    private DatabaseType _getCoreDBConnection() throws Exception
101    {
102        ConfigurationManager configManager = ConfigurationManager.getInstance();
103        ConfigurationProperty coreProperty = configManager
104                .getProperty(ConfigurationManager.getModule("core"));
105        ConfigurationProperty coreDBProperty = coreProperty
106                .getProperty("coreDB");
107
108        if (coreDBProperty == null) {
109            throw new Exception("Could not find " + "coreDB"
110                    + " in core module's configuration.xml.");
111        }
112
113        return DatabaseFactory.getConnectedDatabaseType(coreDBProperty);
114    }
115
116}