001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sql.hypersonic;
005    
006    import java.io.IOException;
007    import java.io.InputStream;
008    import java.io.InputStreamReader;
009    import java.io.Reader;
010    import java.sql.SQLException;
011    
012    
013    /**
014     * In memory Hypersonic datasource with unique name.
015     * @author Pavel Vlasov
016     * @version $Revision: 1.4 $
017     */
018    public class HypersonicInMemoryDataSource extends HypersonicDataSource {
019            private static int counter;
020            
021            private static String dbName() {
022                    return "db"+(counter++)+"_"+Long.toString(System.currentTimeMillis(), Character.MAX_RADIX);
023            }
024    
025            /**
026             * @param initScript Fully qualified name of database initialization script to be loaded by 
027             * classloader. Can be null.
028             * @throws ClassNotFoundException
029             * @throws IOException
030             * @throws SQLException
031             */
032            public HypersonicInMemoryDataSource(String initScript) throws ClassNotFoundException, IOException, SQLException {
033                    super("jdbc:hsqldb:mem:"+dbName(), "sa", "", null);             
034                    if (initScript!=null) {
035                            InputStream in = getClass().getClassLoader().getResourceAsStream(initScript);
036                            if (in==null) {
037                                    throw new IOException("Resource not found: "+initScript);
038                            }
039                            initDB(new InputStreamReader(in), null);
040                    }
041            }
042            
043            /**
044             * @param initScript Fully qualified name of database initialization script to be loaded by 
045             * classloader. Can be null.
046             * @throws ClassNotFoundException
047             * @throws IOException
048             * @throws SQLException
049             */
050            public HypersonicInMemoryDataSource(Reader scriptReader) throws ClassNotFoundException, IOException, SQLException {
051                    super("jdbc:hsqldb:mem:"+dbName(), "sa", "", null);
052                    initDB(scriptReader, null);
053            }
054    }