/src/com/google/appengine/datanucleus/query/QueryExceptionWrappers.java

http://datanucleus-appengine.googlecode.com/ · Java · 77 lines · 42 code · 7 blank · 28 comment · 0 complexity · 4e1f8cdd4df2f9cee95a3275474c421f MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.appengine.datanucleus.query;
  17. import com.google.appengine.api.datastore.DatastoreFailureException;
  18. import com.google.appengine.api.datastore.DatastoreTimeoutException;
  19. import org.datanucleus.exceptions.NucleusException;
  20. import org.datanucleus.api.ApiAdapter;
  21. import org.datanucleus.store.query.QueryTimeoutException;
  22. import com.google.appengine.datanucleus.Utils;
  23. import com.google.appengine.datanucleus.Utils.Supplier;
  24. import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapDatastoreFailureException;
  25. import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapDatastoreTimeoutExceptionForQuery;
  26. import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapIllegalArgumentException;
  27. /**
  28. * Helper methods that wrap the execution of the provided {@link Utils.Supplier}
  29. * with exception handling logic that translates from one set of exceptions to
  30. * another.
  31. *
  32. * @author Max Ross <max.ross@gmail.com>
  33. */
  34. final class QueryExceptionWrappers {
  35. private QueryExceptionWrappers() { }
  36. /**
  37. * Translates datastore runtime exceptions to DataNucleus runtime exceptions.
  38. */
  39. static <T> Supplier<T> datastoreToDataNucleus(final Supplier<T> supplier) {
  40. return new Supplier<T>() {
  41. public T get() {
  42. try {
  43. return supplier.get();
  44. } catch (IllegalArgumentException e) {
  45. throw wrapIllegalArgumentException(e);
  46. } catch (DatastoreTimeoutException e) {
  47. throw wrapDatastoreTimeoutExceptionForQuery(e);
  48. } catch (DatastoreFailureException e) {
  49. throw wrapDatastoreFailureException(e);
  50. }
  51. }
  52. };
  53. }
  54. /**
  55. * Translates DataNucleus runtime exceptions to Api runtime exceptions.
  56. */
  57. static <T> Supplier<T> dataNucleusToApi(final ApiAdapter api, final Supplier<T> supplier) {
  58. return new Supplier<T>() {
  59. public T get() {
  60. try {
  61. return supplier.get();
  62. } catch (QueryTimeoutException te) {
  63. throw api.getApiExceptionForNucleusException(te);
  64. } catch (NucleusException ne) {
  65. throw api.getApiExceptionForNucleusException(ne);
  66. }
  67. }
  68. };
  69. }
  70. }