1 17 package org.apache.servicemix.store.jdbc; 18 19 import java.io.IOException ; 20 import java.sql.Connection ; 21 import java.sql.SQLException ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import javax.sql.DataSource ; 26 27 import org.apache.servicemix.id.IdGenerator; 28 import org.apache.servicemix.jdbc.JDBCAdapter; 29 import org.apache.servicemix.jdbc.JDBCAdapterFactory; 30 import org.apache.servicemix.jdbc.Statements; 31 import org.apache.servicemix.store.Store; 32 import org.apache.servicemix.store.StoreFactory; 33 34 public class JdbcStoreFactory implements StoreFactory { 35 36 private boolean transactional; 37 private boolean clustered; 38 private DataSource dataSource; 39 private IdGenerator idGenerator = new IdGenerator(); 40 private Map stores = new HashMap (); 41 private String tableName = "SM_STORE"; 42 private boolean createDataBase = true; 43 private JDBCAdapter adapter; 44 private Statements statements; 45 46 49 public synchronized Store open(String name) throws IOException { 50 if (adapter == null) { 51 Connection connection = null; 52 try { 53 connection = getDataSource().getConnection(); 54 adapter = JDBCAdapterFactory.getAdapter(connection); 55 if (statements == null) { 56 statements = new Statements(); 57 statements.setStoreTableName(tableName); 58 } 59 adapter.setStatements(statements); 60 if (createDataBase) { 61 adapter.doCreateTables(connection); 62 } 63 connection.commit(); 64 } catch (SQLException e) { 65 throw (IOException ) new IOException ("Exception while creating database").initCause(e); 66 } finally { 67 if (connection != null) { 68 try { 69 connection.close(); 70 } catch (Exception e) { 71 } 72 } 73 } 74 } 75 JdbcStore store = (JdbcStore) stores.get(name); 76 if (store == null) { 77 store = new JdbcStore(this, name); 78 stores.put(name, store); 79 } 80 return store; 81 } 82 83 86 public synchronized void close(Store store) throws IOException { 87 stores.remove(store); 88 } 89 90 93 public JDBCAdapter getAdapter() { 94 return adapter; 95 } 96 97 100 public DataSource getDataSource() { 101 return dataSource; 102 } 103 104 107 public void setDataSource(DataSource dataSource) { 108 this.dataSource = dataSource; 109 } 110 111 114 public boolean isClustered() { 115 return clustered; 116 } 117 118 121 public void setClustered(boolean clustered) { 122 this.clustered = clustered; 123 } 124 125 128 public boolean isTransactional() { 129 return transactional; 130 } 131 132 135 public void setTransactional(boolean transactional) { 136 this.transactional = transactional; 137 } 138 139 142 public IdGenerator getIdGenerator() { 143 return idGenerator; 144 } 145 146 149 public void setIdGenerator(IdGenerator idGenerator) { 150 this.idGenerator = idGenerator; 151 } 152 153 156 public String getTableName() { 157 return tableName; 158 } 159 160 163 public void setTableName(String tableName) { 164 this.tableName = tableName; 165 } 166 167 170 public boolean isCreateDataBase() { 171 return createDataBase; 172 } 173 174 177 public void setCreateDataBase(boolean createDataBase) { 178 this.createDataBase = createDataBase; 179 } 180 181 } 182 | Popular Tags |