001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.store.jdbc;
018    
019    import java.io.IOException;
020    import java.sql.Connection;
021    import java.sql.SQLException;
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import javax.sql.DataSource;
026    
027    import org.apache.servicemix.id.IdGenerator;
028    import org.apache.servicemix.jdbc.JDBCAdapter;
029    import org.apache.servicemix.jdbc.JDBCAdapterFactory;
030    import org.apache.servicemix.jdbc.Statements;
031    import org.apache.servicemix.store.Store;
032    import org.apache.servicemix.store.StoreFactory;
033    
034    public class JdbcStoreFactory implements StoreFactory {
035    
036        private boolean transactional;
037        private boolean clustered;
038        private DataSource dataSource;
039        private IdGenerator idGenerator = new IdGenerator();
040        private Map<String, JdbcStore> stores = new HashMap<String, JdbcStore>();
041        private String tableName = "SM_STORE";
042        private boolean createDataBase = true;
043        private JDBCAdapter adapter;
044        private Statements statements;
045        
046        /* (non-Javadoc)
047         * @see org.apache.servicemix.store.ExchangeStoreFactory#get(java.lang.String)
048         */
049        public synchronized Store open(String name) throws IOException {
050            if (adapter == null) {
051                Connection connection = null;
052                try {
053                    connection = getDataSource().getConnection();
054                    adapter = JDBCAdapterFactory.getAdapter(connection);
055                    if (statements == null) {
056                        statements = new Statements();
057                        statements.setStoreTableName(tableName);
058                    }
059                    adapter.setStatements(statements);
060                    if (createDataBase) {
061                        adapter.doCreateTables(connection);
062                    }
063                    connection.commit();
064                } catch (SQLException e) {
065                    throw (IOException) new IOException("Exception while creating database").initCause(e); 
066                } finally {
067                    if (connection != null) {
068                        try {
069                            connection.close();
070                        } catch (Exception e) {
071                            // Do nothing
072                        }
073                    }
074                }
075            }
076            JdbcStore store = stores.get(name);
077            if (store == null) {
078                store = new JdbcStore(this, name);
079                stores.put(name, store);
080            }
081            return store;
082        }
083    
084        /* (non-Javadoc)
085         * @see org.apache.servicemix.store.ExchangeStoreFactory#release(org.apache.servicemix.store.ExchangeStore)
086         */
087        public synchronized void close(Store store) throws IOException {
088            stores.remove(store);
089        }
090        
091        /**
092         * @return Returns the adapter.
093         */
094        public JDBCAdapter getAdapter() {
095            return adapter;
096        }
097        
098        /**
099         * @return Returns the dataSource.
100         */
101        public DataSource getDataSource() {
102            return dataSource;
103        }
104    
105        /**
106         * @param dataSource The dataSource to set.
107         */
108        public void setDataSource(DataSource dataSource) {
109            this.dataSource = dataSource;
110        }
111    
112        /**
113         * @return Returns the clustered.
114         */
115        public boolean isClustered() {
116            return clustered;
117        }
118    
119        /**
120         * @param clustered The clustered to set.
121         */
122        public void setClustered(boolean clustered) {
123            this.clustered = clustered;
124        }
125    
126        /**
127         * @return Returns the transactional.
128         */
129        public boolean isTransactional() {
130            return transactional;
131        }
132    
133        /**
134         * @param transactional The transactional to set.
135         */
136        public void setTransactional(boolean transactional) {
137            this.transactional = transactional;
138        }
139    
140        /**
141         * @return Returns the idGenerator.
142         */
143        public IdGenerator getIdGenerator() {
144            return idGenerator;
145        }
146    
147        /**
148         * @param idGenerator The idGenerator to set.
149         */
150        public void setIdGenerator(IdGenerator idGenerator) {
151            this.idGenerator = idGenerator;
152        }
153    
154        /**
155         * @return Returns the tableName.
156         */
157        public String getTableName() {
158            return tableName;
159        }
160    
161        /**
162         * @param tableName The tableName to set.
163         */
164        public void setTableName(String tableName) {
165            this.tableName = tableName;
166        }
167    
168        /**
169         * @return Returns the createDataBase.
170         */
171        public boolean isCreateDataBase() {
172            return createDataBase;
173        }
174    
175        /**
176         * @param createDataBase The createDataBase to set.
177         */
178        public void setCreateDataBase(boolean createDataBase) {
179            this.createDataBase = createDataBase;
180        }
181        
182    }