001/* Manage HSQL server for the provenance module.
002 * 
003 * Copyright (c) 2011 The Regents of the University of California.
004 * All rights reserved.
005 *
006 * '$Author: crawl $'
007 * '$Date: 2012-11-26 22:23:07 +0000 (Mon, 26 Nov 2012) $' 
008 * '$Revision: 31125 $'
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.provenance;
031
032import java.sql.SQLException;
033import java.util.Map;
034
035import org.kepler.module.ModuleHSQLManager;
036import org.kepler.provenance.ProvenanceRecorder;
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 HSQL database managed by the provenance
043 *  module:
044 *  
045 *  KeplerData/modules/provenance/provenanceDB
046 * 
047 *  The HSQL server is run in a separate process.
048 *  
049 *  @author Daniel Crawl
050 *  @version $Id: HSQLManager.java 31125 2012-11-26 22:23:07Z crawl $
051 *
052 */
053public class HSQLManager implements ModuleHSQLManager {
054
055    /** Start HSQL servers in a separate process. */
056    public void start() {      
057        try {
058            _getProvenanceDBConnection();
059        } catch (Exception e) {
060            MessageHandler.error("Error starting provenanceDB.", e);
061        }        
062    }
063
064    /** Stop HSQL servers in a separate process. */
065    public void stop() {
066        
067        DatabaseType dbType;
068        try {
069            dbType = _getProvenanceDBConnection();
070        } catch (Exception e) {
071            MessageHandler.error("Error getting connection to provenanceDB.", e);
072            return;
073        }
074        
075        if(dbType != null) {
076            try {
077                dbType.disconnect();
078            } catch (SQLException e) {
079                MessageHandler.error("Error shutting down provenanceDB.", e);
080            }
081        }
082        
083    }
084
085    /** Get a connection to the provenance database. This returns null if the
086     *  database type is not HSQL or is accessing the file directly. 
087     */
088    private DatabaseType _getProvenanceDBConnection() throws Exception
089    {
090        Map<String,String> config = ProvenanceRecorder.getDefaultsMap();
091        
092        // make sure type is HSQL
093        String typeStr = config.get(DatabaseFactory.Parameter.TYPE.getName());
094        if(typeStr != null && typeStr.equals("HSQL"))
095        {
096            // make sure it is a server and not direct file access
097            String hostStr = config.get(DatabaseFactory.Parameter.HOST.getName());
098            if(hostStr != null && !hostStr.isEmpty())
099            {
100                return DatabaseFactory.getConnectedDatabaseType(config, "provenance");
101            }
102        }
103        
104        return null;
105    }
106
107}