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