/src/com/google/appengine/datanucleus/WrappedDatastoreService.java

http://datanucleus-appengine.googlecode.com/ · Java · 313 lines · 261 code · 31 blank · 21 comment · 0 complexity · c548d70860ecf75186f476362fc91893 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus;
  14. import com.google.appengine.api.datastore.DatastoreAttributes;
  15. import com.google.appengine.api.datastore.DatastoreFailureException;
  16. import com.google.appengine.api.datastore.DatastoreService;
  17. import com.google.appengine.api.datastore.Entity;
  18. import com.google.appengine.api.datastore.EntityNotFoundException;
  19. import com.google.appengine.api.datastore.Index;
  20. import com.google.appengine.api.datastore.Key;
  21. import com.google.appengine.api.datastore.KeyRange;
  22. import com.google.appengine.api.datastore.PreparedQuery;
  23. import com.google.appengine.api.datastore.Query;
  24. import com.google.appengine.api.datastore.Transaction;
  25. import com.google.appengine.api.datastore.TransactionOptions;
  26. import java.util.Collection;
  27. import java.util.ConcurrentModificationException;
  28. import java.util.List;
  29. import java.util.Map;
  30. import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapConcurrentModificationException;
  31. import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapDatastoreFailureException;
  32. import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapIllegalArgumentException;
  33. /**
  34. * {@link DatastoreService} implementation that catches runtime exceptions
  35. * thrown and wraps them in the appropriate DataNucleus exception.
  36. *
  37. * @author Max Ross <maxr@google.com>
  38. */
  39. public class WrappedDatastoreService implements DatastoreService {
  40. private final DatastoreService inner;
  41. public WrappedDatastoreService(DatastoreService inner) {
  42. this.inner = inner;
  43. }
  44. public DatastoreService getDelegate() {
  45. return inner;
  46. }
  47. public Entity get(Key key) throws EntityNotFoundException {
  48. try {
  49. return inner.get(key);
  50. } catch (IllegalArgumentException e) {
  51. throw wrapIllegalArgumentException(e);
  52. } catch (DatastoreFailureException e) {
  53. throw wrapDatastoreFailureException(e);
  54. }
  55. }
  56. public Entity get(Transaction transaction, Key key) throws EntityNotFoundException {
  57. try {
  58. return inner.get(transaction, key);
  59. } catch (IllegalArgumentException e) {
  60. throw wrapIllegalArgumentException(e);
  61. } catch (DatastoreFailureException e) {
  62. throw wrapDatastoreFailureException(e);
  63. }
  64. }
  65. public Map<Key, Entity> get(Iterable<Key> keyIterable) {
  66. try {
  67. return inner.get(keyIterable);
  68. } catch (IllegalArgumentException e) {
  69. throw wrapIllegalArgumentException(e);
  70. } catch (DatastoreFailureException e) {
  71. throw wrapDatastoreFailureException(e);
  72. }
  73. }
  74. public Map<Key, Entity> get(Transaction transaction, Iterable<Key> keyIterable) {
  75. try {
  76. return inner.get(transaction, keyIterable);
  77. } catch (IllegalArgumentException e) {
  78. throw wrapIllegalArgumentException(e);
  79. } catch (DatastoreFailureException e) {
  80. throw wrapDatastoreFailureException(e);
  81. }
  82. }
  83. public Key put(Entity entity) {
  84. try {
  85. return inner.put(entity);
  86. } catch (IllegalArgumentException e) {
  87. throw wrapIllegalArgumentException(e);
  88. } catch (ConcurrentModificationException e) {
  89. throw wrapConcurrentModificationException(e);
  90. } catch (DatastoreFailureException e) {
  91. throw wrapDatastoreFailureException(e);
  92. }
  93. }
  94. public Key put(Transaction transaction, Entity entity) {
  95. try {
  96. return inner.put(transaction, entity);
  97. } catch (IllegalArgumentException e) {
  98. throw wrapIllegalArgumentException(e);
  99. } catch (ConcurrentModificationException e) {
  100. throw wrapConcurrentModificationException(e);
  101. } catch (DatastoreFailureException e) {
  102. throw wrapDatastoreFailureException(e);
  103. }
  104. }
  105. public List<Key> put(Iterable<Entity> entityIterable) {
  106. try {
  107. return inner.put(entityIterable);
  108. } catch (IllegalArgumentException e) {
  109. throw wrapIllegalArgumentException(e);
  110. } catch (ConcurrentModificationException e) {
  111. throw wrapConcurrentModificationException(e);
  112. } catch (DatastoreFailureException e) {
  113. throw wrapDatastoreFailureException(e);
  114. }
  115. }
  116. public List<Key> put(Transaction transaction, Iterable<Entity> entityIterable) {
  117. try {
  118. return inner.put(transaction, entityIterable);
  119. } catch (IllegalArgumentException e) {
  120. throw wrapIllegalArgumentException(e);
  121. } catch (ConcurrentModificationException e) {
  122. throw wrapConcurrentModificationException(e);
  123. } catch (DatastoreFailureException e) {
  124. throw wrapDatastoreFailureException(e);
  125. }
  126. }
  127. public void delete(Key... keys) {
  128. try {
  129. inner.delete(keys);
  130. } catch (IllegalArgumentException e) {
  131. throw wrapIllegalArgumentException(e);
  132. } catch (ConcurrentModificationException e) {
  133. throw wrapConcurrentModificationException(e);
  134. } catch (DatastoreFailureException e) {
  135. throw wrapDatastoreFailureException(e);
  136. }
  137. }
  138. public void delete(Transaction transaction, Key... keys) {
  139. try {
  140. inner.delete(transaction, keys);
  141. } catch (IllegalArgumentException e) {
  142. throw wrapIllegalArgumentException(e);
  143. } catch (ConcurrentModificationException e) {
  144. throw wrapConcurrentModificationException(e);
  145. } catch (DatastoreFailureException e) {
  146. throw wrapDatastoreFailureException(e);
  147. }
  148. }
  149. public void delete(Iterable<Key> keyIterable) {
  150. try {
  151. inner.delete(keyIterable);
  152. } catch (IllegalArgumentException e) {
  153. throw wrapIllegalArgumentException(e);
  154. } catch (ConcurrentModificationException e) {
  155. throw wrapConcurrentModificationException(e);
  156. } catch (DatastoreFailureException e) {
  157. throw wrapDatastoreFailureException(e);
  158. }
  159. }
  160. public void delete(Transaction transaction, Iterable<Key> keyIterable) {
  161. try {
  162. inner.delete(transaction, keyIterable);
  163. } catch (IllegalArgumentException e) {
  164. throw wrapIllegalArgumentException(e);
  165. } catch (ConcurrentModificationException e) {
  166. throw wrapConcurrentModificationException(e);
  167. } catch (DatastoreFailureException e) {
  168. throw wrapDatastoreFailureException(e);
  169. }
  170. }
  171. public PreparedQuery prepare(Query query) {
  172. try {
  173. return inner.prepare(query);
  174. } catch (IllegalArgumentException e) {
  175. throw wrapIllegalArgumentException(e);
  176. } catch (DatastoreFailureException e) {
  177. throw wrapDatastoreFailureException(e);
  178. }
  179. }
  180. public PreparedQuery prepare(Transaction transaction, Query query) {
  181. try {
  182. return inner.prepare(transaction, query);
  183. } catch (IllegalArgumentException e) {
  184. throw wrapIllegalArgumentException(e);
  185. } catch (DatastoreFailureException e) {
  186. throw wrapDatastoreFailureException(e);
  187. }
  188. }
  189. public Transaction beginTransaction() {
  190. try {
  191. return inner.beginTransaction();
  192. } catch (IllegalArgumentException e) {
  193. throw wrapIllegalArgumentException(e);
  194. } catch (DatastoreFailureException e) {
  195. throw wrapDatastoreFailureException(e);
  196. }
  197. }
  198. public Transaction beginTransaction(TransactionOptions transactionOptions) {
  199. try {
  200. return inner.beginTransaction(transactionOptions);
  201. } catch (IllegalArgumentException e) {
  202. throw wrapIllegalArgumentException(e);
  203. } catch (DatastoreFailureException e) {
  204. throw wrapDatastoreFailureException(e);
  205. }
  206. }
  207. public Transaction getCurrentTransaction() {
  208. try {
  209. return inner.getCurrentTransaction();
  210. } catch (IllegalArgumentException e) {
  211. throw wrapIllegalArgumentException(e);
  212. } catch (DatastoreFailureException e) {
  213. throw wrapDatastoreFailureException(e);
  214. }
  215. }
  216. public Transaction getCurrentTransaction(Transaction transaction) {
  217. try {
  218. return inner.getCurrentTransaction(transaction);
  219. } catch (IllegalArgumentException e) {
  220. throw wrapIllegalArgumentException(e);
  221. } catch (DatastoreFailureException e) {
  222. throw wrapDatastoreFailureException(e);
  223. }
  224. }
  225. public Collection<Transaction> getActiveTransactions() {
  226. try {
  227. return inner.getActiveTransactions();
  228. } catch (IllegalArgumentException e) {
  229. throw wrapIllegalArgumentException(e);
  230. } catch (DatastoreFailureException e) {
  231. throw wrapDatastoreFailureException(e);
  232. }
  233. }
  234. public KeyRange allocateIds(String kind, long num) {
  235. try {
  236. return inner.allocateIds(kind, num);
  237. } catch (IllegalArgumentException e) {
  238. throw wrapIllegalArgumentException(e);
  239. } catch (DatastoreFailureException e) {
  240. throw wrapDatastoreFailureException(e);
  241. }
  242. }
  243. public KeyRange allocateIds(Key parent, String kind, long num) {
  244. try {
  245. return inner.allocateIds(parent, kind, num);
  246. } catch (IllegalArgumentException e) {
  247. throw wrapIllegalArgumentException(e);
  248. } catch (DatastoreFailureException e) {
  249. throw wrapDatastoreFailureException(e);
  250. }
  251. }
  252. public KeyRangeState allocateIdRange(KeyRange keyRange) {
  253. try {
  254. return inner.allocateIdRange(keyRange);
  255. } catch (IllegalArgumentException e) {
  256. throw wrapIllegalArgumentException(e);
  257. } catch (DatastoreFailureException e) {
  258. throw wrapDatastoreFailureException(e);
  259. }
  260. }
  261. public DatastoreAttributes getDatastoreAttributes() {
  262. try {
  263. return inner.getDatastoreAttributes();
  264. } catch (IllegalArgumentException e) {
  265. throw wrapIllegalArgumentException(e);
  266. } catch (DatastoreFailureException e) {
  267. throw wrapDatastoreFailureException(e);
  268. }
  269. }
  270. public Map<Index, Index.IndexState> getIndexes() {
  271. try {
  272. return inner.getIndexes();
  273. } catch (IllegalArgumentException e) {
  274. throw wrapIllegalArgumentException(e);
  275. } catch (DatastoreFailureException e) {
  276. throw wrapDatastoreFailureException(e);
  277. }
  278. }
  279. }