PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 71 lines | 35 code | 13 blank | 23 comment | 2 complexity | c43dbc99017c0cb739008e9872965edc MD5 | raw file
Possible License(s): Apache-2.0
  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 Iterable} implementation that catches runtime exceptions thrown by
  20. * the datastore api and translates them to the appropriate JDO or JPA exception.
  21. * DataNucleus is supposed to do this for us, but they miss runtime exceptions
  22. * that are thrown while iterating.
  23. *
  24. * @author Max Ross <maxr@google.com>
  25. */
  26. class RuntimeExceptionWrappingIterable implements Iterable<Entity>, RuntimeExceptionObserver {
  27. private final ApiAdapter api;
  28. private final Supplier<Iterator<Entity>> iteratorSupplier;
  29. private boolean hasError = false;
  30. RuntimeExceptionWrappingIterable(ApiAdapter api, final Iterable<Entity> inner) {
  31. if (inner == null) {
  32. throw new NullPointerException("inner cannot be null");
  33. }
  34. this.api = api;
  35. Supplier<Iterator<Entity>> supplier = QueryExceptionWrappers.datastoreToDataNucleus(
  36. new Supplier<Iterator<Entity>>() {
  37. public Iterator<Entity> get() {
  38. return newIterator(inner.iterator());
  39. }
  40. });
  41. this.iteratorSupplier = QueryExceptionWrappers.dataNucleusToApi(api, supplier);
  42. }
  43. public Iterator<Entity> iterator() {
  44. return iteratorSupplier.get();
  45. }
  46. Iterator<Entity> newIterator(Iterator<Entity> innerIter) {
  47. return new RuntimeExceptionWrappingIterator(api, innerIter, this);
  48. }
  49. public void onException() {
  50. hasError = true;
  51. }
  52. boolean hasError() {
  53. return hasError;
  54. }
  55. }