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