001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sql.cloudscape;
005    
006    import java.io.File;
007    import java.io.IOException;
008    import java.sql.SQLException;
009    import java.text.SimpleDateFormat;
010    import java.util.Date;
011    
012    
013    /**
014     * Hypersonic temporary data source. It is similar to standalone datasource, but
015     * data files are created in system temporary directory and are scheduled to deletion 
016     * on JVM exit. Use this datasource if you need to keep large amount of temporary data,
017     * for example data pumping applications can benefit from such data source.
018     * @author Pavel Vlasov
019     * @version $Revision: 1.2 $
020     */
021    public class CloudscapeTmpDataSource extends CloudscapeDataSource {
022            
023            /** 
024             * @param initScript Fully qualified name of database initialization script to be loaded by 
025             * classloader. Can be null.
026             * @throws ClassNotFoundException
027             * @throws IOException
028             * @throws SQLException
029             */
030            public CloudscapeTmpDataSource(String initScript) throws ClassNotFoundException, IOException, SQLException {            
031                    super("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:"+createTmpDir()+";create=true", "sa", "sa", null);
032                    initDB(initScript);
033            }
034            
035            private static String createTmpDir() throws IOException {
036                    String tmpDir=System.getProperty("java.io.tmpdir");
037                    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
038                    String prefix = "CloudscapeDB_"+sdf.format(new Date())+"_";
039                    String suffix = "_TMP";
040                    final File tmpDbDir = tmpDir==null ? File.createTempFile(prefix, suffix) : File.createTempFile(prefix, suffix, new File(tmpDir));
041                    if (tmpDbDir.delete()) {
042                            if (tmpDbDir.mkdirs()) {
043                                    Runtime.getRuntime().addShutdownHook(   new Thread() {
044                                            public void run() {                                             
045                                                    
046                                                    File[] dbFiles=tmpDbDir.listFiles();
047                                                    for (int i=0; i<dbFiles.length; i++) {
048                                                            dbFiles[i].deleteOnExit();
049                                                    }
050                                                    
051                                                    tmpDbDir.deleteOnExit();
052                                            }
053                                    });
054                                    return tmpDbDir.getAbsolutePath()+File.separator+"CloudscapeTmpDB";
055                            } else { 
056                                    throw new IOException("Cannot create directory "+tmpDbDir.getAbsolutePath());
057                            }
058                    } else {
059                            throw new IOException("Cannot delete file "+tmpDbDir.getAbsolutePath());
060                    }
061            }       
062    }