1 18 package org.apache.activemq.store.jdbc; 19 20 import org.apache.derby.jdbc.EmbeddedDataSource; 21 import org.apache.activemq.util.IOHelper; 22 23 import javax.sql.DataSource ; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 28 34 public class DataSourceSupport { 35 36 private String dataDirectory = IOHelper.getDefaultDataDirectory(); 37 private File dataDirectoryFile; 38 private DataSource dataSource; 39 40 public DataSourceSupport() { 41 } 42 43 public DataSourceSupport(DataSource dataSource) { 44 this.dataSource = dataSource; 45 } 46 47 public File getDataDirectoryFile() { 48 if (dataDirectoryFile == null) { 49 dataDirectoryFile = new File (getDataDirectory()); 50 } 51 return dataDirectoryFile; 52 } 53 54 public void setDataDirectoryFile(File dataDirectory) { 55 this.dataDirectoryFile = dataDirectory; 56 } 57 58 public String getDataDirectory() { 59 return dataDirectory; 60 } 61 62 public void setDataDirectory(String dataDirectory) { 63 this.dataDirectory = dataDirectory; 64 } 65 66 public DataSource getDataSource() throws IOException { 67 if (dataSource == null) { 68 dataSource = createDataSource(); 69 if (dataSource == null) { 70 throw new IllegalArgumentException ("No dataSource property has been configured"); 71 } 72 } 73 return dataSource; 74 } 75 76 public void setDataSource(DataSource dataSource) { 77 this.dataSource = dataSource; 78 } 79 80 protected DataSource createDataSource() throws IOException { 81 82 System.setProperty("derby.system.home", getDataDirectoryFile().getCanonicalPath()); 84 System.setProperty("derby.storage.fileSyncTransactionLog", "true"); 85 System.setProperty("derby.storage.pageCacheSize", "100"); 86 87 final EmbeddedDataSource ds = new EmbeddedDataSource(); 88 ds.setDatabaseName("derbydb"); 89 ds.setCreateDatabase("create"); 90 return ds; 91 } 92 93 public String toString(){ 94 return ""+dataSource; 95 } 96 97 } 98 | Popular Tags |