001    /**
002     * HSQLDBserver.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 04/07/2007
008     */
009    package jcolibri.test.database;
010    
011    import java.io.ByteArrayOutputStream;
012    import java.io.File;
013    import java.io.PrintStream;
014    import java.sql.Connection;
015    import java.sql.DriverManager;
016    import java.util.HashMap;
017    
018    import jcolibri.util.FileIO;
019    
020    import org.hsqldb.Server;
021    
022    /**
023     * Creates a data base server with the tables for the examples/tests using the HSQLDB library.
024     * @author Juan A. Recio-Garcia
025     * @version 1.0
026     */
027    public class HSQLDBserver
028    {
029        static boolean initialized = false;
030    
031        private static Server server;
032    
033        /**
034         * Initialize the server
035         */
036        public static void init()
037        {
038            if (initialized)
039                return;
040            org.apache.commons.logging.LogFactory.getLog(HSQLDBserver.class).info("Creating data base ...");
041    
042            server = new Server();
043            server.setDatabaseName(0, "travel");
044            server.setDatabasePath(0, "mem:travel;sql.enforce_strict_size=true");
045            
046            server.setDatabaseName(1, "travelext");
047            server.setDatabasePath(1, "mem:travelext;sql.enforce_strict_size=true");
048            
049            server.setLogWriter(null);
050            server.setErrWriter(null);
051            server.setSilent(true);
052            server.start();
053    
054            initialized = true;
055            try
056            {
057                Class.forName("org.hsqldb.jdbcDriver");
058    
059                PrintStream out = new PrintStream(new ByteArrayOutputStream());
060                Connection conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/travel", "sa", "");
061                SqlFile file = new SqlFile(new
062                File(FileIO.findFile("jcolibri/test/database/travel.sql").getFile()),false,new HashMap());
063                file.execute(conn,out,out, true);
064                
065                Connection connExt = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/travelext", "sa", "");
066                SqlFile fileExt = new SqlFile(new
067                File(FileIO.findFile("jcolibri/test/database/travelext.sql").getFile()),false,new HashMap());
068                fileExt.execute(connExt,out,out, true);
069    
070                org.apache.commons.logging.LogFactory.getLog(HSQLDBserver.class).info("Data base generation finished");
071                
072            } catch (Exception e)
073            {
074                org.apache.commons.logging.LogFactory.getLog(HSQLDBserver.class).error(e);
075            }
076    
077        }
078    
079        /**
080         * Shutdown the server
081         */
082        public static void shutDown()
083        {
084    
085            if (initialized)
086            {
087                server.stop();
088                initialized = false;
089            }
090        }
091    
092        /**
093         * Testing method
094         */
095        public static void main(String[] args)
096        {
097            HSQLDBserver.init();
098            HSQLDBserver.shutDown();
099            System.exit(0);
100            
101        }
102    
103    }