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

http://datanucleus-appengine.googlecode.com/ · Java · 113 lines · 77 code · 13 blank · 23 comment · 5 complexity · 12808f7beb8988c13c137583a77ae0da 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.query;
  14. import com.google.appengine.api.datastore.Entity;
  15. import com.google.appengine.datanucleus.Utils.Supplier;
  16. import java.util.Iterator;
  17. import org.datanucleus.api.ApiAdapter;
  18. /**
  19. * {@link Iterator} implementation that catches runtime exceptions thrown by
  20. * the datastore api and translates them to the appropriate JDO or JPA
  21. * exception. DataNucleus is supposed to do this for us, but they miss runtime
  22. * exceptions that are thrown while iterating.
  23. *
  24. * @author Max Ross <maxr@google.com>
  25. */
  26. class RuntimeExceptionWrappingIterator implements Iterator<Entity> {
  27. final ApiAdapter api;
  28. final Iterator<Entity> inner;
  29. final Supplier<Boolean> hasNextSupplier;
  30. final Supplier<Entity> nextSupplier;
  31. final Supplier<Void> removeSupplier;
  32. RuntimeExceptionWrappingIterator(final ApiAdapter api, final Iterator<Entity> inner,
  33. final RuntimeExceptionObserver exceptionObserver) {
  34. if (inner == null) {
  35. throw new NullPointerException("inner cannot be null");
  36. }
  37. this.api = api;
  38. this.inner = inner;
  39. Supplier<Boolean> datastoreHasNextSupplier = QueryExceptionWrappers.datastoreToDataNucleus(
  40. new Supplier<Boolean>() {
  41. public Boolean get() {
  42. boolean success = false;
  43. try {
  44. boolean result = inner.hasNext();
  45. success = true;
  46. return result;
  47. } finally {
  48. if (!success) {
  49. exceptionObserver.onException();
  50. }
  51. }
  52. }
  53. });
  54. Supplier<Entity> datastoreNextSupplier = QueryExceptionWrappers.datastoreToDataNucleus(
  55. new Supplier<Entity>() {
  56. public Entity get() {
  57. boolean success = false;
  58. try {
  59. Entity result = inner.next();
  60. success = true;
  61. return result;
  62. } finally {
  63. if (!success) {
  64. exceptionObserver.onException();
  65. }
  66. }
  67. }
  68. });
  69. Supplier<Void> datastoreRemoveSupplier = QueryExceptionWrappers.datastoreToDataNucleus(
  70. new Supplier<Void>() {
  71. public Void get() {
  72. boolean success = false;
  73. try {
  74. inner.remove();
  75. success = true;
  76. return null;
  77. } finally {
  78. if (!success) {
  79. exceptionObserver.onException();
  80. }
  81. }
  82. }
  83. });
  84. hasNextSupplier = QueryExceptionWrappers.dataNucleusToApi(api, datastoreHasNextSupplier);
  85. nextSupplier = QueryExceptionWrappers.dataNucleusToApi(api, datastoreNextSupplier);
  86. removeSupplier = QueryExceptionWrappers.dataNucleusToApi(api, datastoreRemoveSupplier);
  87. }
  88. public boolean hasNext() {
  89. return hasNextSupplier.get();
  90. }
  91. public Entity next() {
  92. return nextSupplier.get();
  93. }
  94. public void remove() {
  95. removeSupplier.get();
  96. }
  97. }