KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > store > jdbc > JdbcStoreFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.store.jdbc;
18
19 import java.io.IOException JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.sql.DataSource JavaDoc;
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 JavaDoc dataSource;
39     private IdGenerator idGenerator = new IdGenerator();
40     private Map JavaDoc stores = new HashMap JavaDoc();
41     private String JavaDoc tableName = "SM_STORE";
42     private boolean createDataBase = true;
43     private JDBCAdapter adapter;
44     private Statements statements;
45     
46     /* (non-Javadoc)
47      * @see org.apache.servicemix.store.ExchangeStoreFactory#get(java.lang.String)
48      */

49     public synchronized Store open(String JavaDoc name) throws IOException JavaDoc {
50         if (adapter == null) {
51             Connection JavaDoc 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 JavaDoc e) {
65                 throw (IOException JavaDoc) new IOException JavaDoc("Exception while creating database").initCause(e);
66             } finally {
67                 if (connection != null) {
68                     try {
69                         connection.close();
70                     } catch (Exception JavaDoc 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     /* (non-Javadoc)
84      * @see org.apache.servicemix.store.ExchangeStoreFactory#release(org.apache.servicemix.store.ExchangeStore)
85      */

86     public synchronized void close(Store store) throws IOException JavaDoc {
87         stores.remove(store);
88     }
89     
90     /**
91      * @return Returns the adapter.
92      */

93     public JDBCAdapter getAdapter() {
94         return adapter;
95     }
96     
97     /**
98      * @return Returns the dataSource.
99      */

100     public DataSource JavaDoc getDataSource() {
101         return dataSource;
102     }
103
104     /**
105      * @param dataSource The dataSource to set.
106      */

107     public void setDataSource(DataSource JavaDoc dataSource) {
108         this.dataSource = dataSource;
109     }
110
111     /**
112      * @return Returns the clustered.
113      */

114     public boolean isClustered() {
115         return clustered;
116     }
117
118     /**
119      * @param clustered The clustered to set.
120      */

121     public void setClustered(boolean clustered) {
122         this.clustered = clustered;
123     }
124
125     /**
126      * @return Returns the transactional.
127      */

128     public boolean isTransactional() {
129         return transactional;
130     }
131
132     /**
133      * @param transactional The transactional to set.
134      */

135     public void setTransactional(boolean transactional) {
136         this.transactional = transactional;
137     }
138
139     /**
140      * @return Returns the idGenerator.
141      */

142     public IdGenerator getIdGenerator() {
143         return idGenerator;
144     }
145
146     /**
147      * @param idGenerator The idGenerator to set.
148      */

149     public void setIdGenerator(IdGenerator idGenerator) {
150         this.idGenerator = idGenerator;
151     }
152
153     /**
154      * @return Returns the tableName.
155      */

156     public String JavaDoc getTableName() {
157         return tableName;
158     }
159
160     /**
161      * @param tableName The tableName to set.
162      */

163     public void setTableName(String JavaDoc tableName) {
164         this.tableName = tableName;
165     }
166
167     /**
168      * @return Returns the createDataBase.
169      */

170     public boolean isCreateDataBase() {
171         return createDataBase;
172     }
173
174     /**
175      * @param createDataBase The createDataBase to set.
176      */

177     public void setCreateDataBase(boolean createDataBase) {
178         this.createDataBase = createDataBase;
179     }
180     
181 }
182
Popular Tags