1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.jdbc.store;
12
13 import java.io.Serializable;
14 import java.sql.SQLException;
15
16 import org.apache.commons.dbutils.ResultSetHandler;
17 import org.apache.commons.dbutils.handlers.ArrayHandler;
18 import org.mule.api.store.ObjectAlreadyExistsException;
19 import org.mule.api.store.ObjectDoesNotExistException;
20 import org.mule.api.store.ObjectStoreException;
21 import org.mule.api.transaction.TransactionCallback;
22 import org.mule.api.transaction.TransactionConfig;
23 import org.mule.config.i18n.CoreMessages;
24 import org.mule.transaction.TransactionTemplate;
25 import org.mule.transport.jdbc.JdbcConnector;
26 import org.mule.util.store.AbstractMonitoredObjectStore;
27
28 public class JdbcObjectStore<T extends Serializable> extends AbstractMonitoredObjectStore<T>
29 {
30
31 private JdbcConnector jdbcConnector;
32 private TransactionConfig transactionConfig;
33 private String insertQueryKey;
34 private String selectQueryKey;
35 private String deleteQueryKey;
36
37 private ArrayHandler arrayHandler;
38
39 public JdbcObjectStore()
40 {
41 this.arrayHandler = new ArrayHandler();
42 this.maxEntries = -1;
43 }
44
45
46
47
48 public boolean isPersistent()
49 {
50 return true;
51 }
52
53
54
55
56 protected void expire()
57 {
58
59 }
60
61
62
63
64 public boolean contains(Serializable key) throws ObjectStoreException
65 {
66 this.notNullKey(key);
67 Object[] result = (Object[]) this.query(this.getSelectQuery(), this.arrayHandler, key);
68 return result != null;
69 }
70
71
72
73
74 public T remove(Serializable key) throws ObjectStoreException
75 {
76 this.notNullKey(key);
77 T value = this.retrieve(key);
78 this.update(this.getDeleteQuery(), key);
79 return value;
80 }
81
82
83
84
85 public T retrieve(Serializable key) throws ObjectStoreException
86 {
87 Object[] row = (Object[]) this.query(this.getSelectQuery(), this.arrayHandler, key);
88 if (row == null)
89 {
90 throw new ObjectDoesNotExistException(CoreMessages.objectNotFound(key));
91 }
92 else
93 {
94 return (T) row[1];
95 }
96 }
97
98 public void store(Serializable key, T value, String[] parameters) throws ObjectStoreException
99 {
100 Object[] arguments = new Object[2 + parameters.length];
101 arguments[0] = key;
102 arguments[1] = value;
103
104 for (int i = 0; i < parameters.length; i++)
105 {
106 String parameter = parameters[i];
107 arguments[2 + i] = parameter;
108 }
109
110 this.update(this.getInsertQuery(), arguments);
111 }
112
113
114
115
116 public void store(Serializable key, T value) throws ObjectStoreException
117 {
118 this.notNullKey(key);
119 try
120 {
121 this.update(this.getInsertQuery(), key, value);
122 }
123 catch (ObjectStoreException e)
124 {
125 throw new ObjectAlreadyExistsException(e);
126 }
127 }
128
129
130
131
132
133
134
135 private void notNullKey(Serializable key) throws ObjectStoreException
136 {
137 if (key == null)
138 {
139 throw new ObjectStoreException(CoreMessages.objectIsNull("id"));
140 }
141 }
142
143
144
145
146
147
148
149
150
151
152 private Object query(final String sql, final ResultSetHandler handler, final Object... arguments)
153 throws ObjectStoreException
154 {
155 try
156 {
157 TransactionTemplate<Object> tt = new TransactionTemplate<Object>(this.transactionConfig,
158 this.jdbcConnector.getMuleContext());
159
160 Object result = tt.execute(new TransactionCallback<Object>()
161 {
162 public Object doInTransaction() throws Exception
163 {
164 return jdbcConnector.getQueryRunner().query(sql, handler, arguments);
165 }
166 });
167
168 return result;
169 }
170 catch (SQLException e)
171 {
172 throw new ObjectStoreException(e);
173 }
174 catch (Exception e)
175 {
176 throw new ObjectStoreException(e);
177 }
178 }
179
180
181
182
183
184
185
186
187
188 private Object update(final String sql, final Object... arguments) throws ObjectStoreException
189 {
190 try
191 {
192 TransactionTemplate<Object> tt = new TransactionTemplate<Object>(this.transactionConfig,
193 this.jdbcConnector.getMuleContext());
194
195 Object result = tt.execute(new TransactionCallback<Object>()
196 {
197 public Object doInTransaction() throws Exception
198 {
199 return jdbcConnector.getQueryRunner().update(sql, arguments);
200 }
201 });
202
203 return result;
204 }
205 catch (SQLException e)
206 {
207 throw new ObjectStoreException(e);
208 }
209 catch (Exception e)
210 {
211 throw new ObjectStoreException(e);
212 }
213 }
214
215 public JdbcConnector getJdbcConnector()
216 {
217 return jdbcConnector;
218 }
219
220 public void setJdbcConnector(JdbcConnector jdbcConnector)
221 {
222 this.jdbcConnector = jdbcConnector;
223 }
224
225 public TransactionConfig getTransactionConfig()
226 {
227 return transactionConfig;
228 }
229
230 public void setTransactionConfig(TransactionConfig transactionConfig)
231 {
232 this.transactionConfig = transactionConfig;
233 }
234
235 public String getInsertQuery()
236 {
237 return (String) this.jdbcConnector.getQueries().get(this.insertQueryKey);
238 }
239
240 public String getSelectQuery()
241 {
242 return (String) this.jdbcConnector.getQueries().get(this.selectQueryKey);
243 }
244
245 public String getDeleteQuery()
246 {
247 return (String) this.jdbcConnector.getQueries().get(this.deleteQueryKey);
248 }
249
250 public String getInsertQueryKey()
251 {
252 return insertQueryKey;
253 }
254
255 public void setInsertQueryKey(String insertQueryKey)
256 {
257 this.insertQueryKey = insertQueryKey;
258 }
259
260 public String getSelectQueryKey()
261 {
262 return selectQueryKey;
263 }
264
265 public void setSelectQueryKey(String selectQueryKey)
266 {
267 this.selectQueryKey = selectQueryKey;
268 }
269
270 public String getDeleteQueryKey()
271 {
272 return deleteQueryKey;
273 }
274
275 public void setDeleteQueryKey(String deleteQueryKey)
276 {
277 this.deleteQueryKey = deleteQueryKey;
278 }
279 }