PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms 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
 1/**********************************************************************
 2Copyright (c) 2009 Google Inc.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15**********************************************************************/
16package com.google.appengine.datanucleus.query;
17
18import com.google.appengine.api.datastore.Entity;
19
20import com.google.appengine.datanucleus.Utils.Supplier;
21
22import java.util.Iterator;
23
24import org.datanucleus.api.ApiAdapter;
25
26/**
27 * {@link Iterable} implementation that catches runtime exceptions thrown by
28 * the datastore api and translates them to the appropriate JDO or JPA exception.
29 * DataNucleus is supposed to do this for us, but they miss runtime exceptions
30 * that are thrown while iterating.
31 *
32 * @author Max Ross <maxr@google.com>
33 */
34class RuntimeExceptionWrappingIterable implements Iterable<Entity>, RuntimeExceptionObserver {
35
36  private final ApiAdapter api;
37
38  private final Supplier<Iterator<Entity>> iteratorSupplier;
39  private boolean hasError = false;
40
41  RuntimeExceptionWrappingIterable(ApiAdapter api, final Iterable<Entity> inner) {
42    if (inner == null) {
43      throw new NullPointerException("inner cannot be null");
44    }
45    this.api = api;
46    Supplier<Iterator<Entity>> supplier = QueryExceptionWrappers.datastoreToDataNucleus(
47        new Supplier<Iterator<Entity>>() {
48          public Iterator<Entity> get() {
49            return newIterator(inner.iterator());
50          }
51        });
52
53    this.iteratorSupplier = QueryExceptionWrappers.dataNucleusToApi(api, supplier);
54  }
55
56  public Iterator<Entity> iterator() {
57    return iteratorSupplier.get();
58  }
59
60  Iterator<Entity> newIterator(Iterator<Entity> innerIter) {
61    return new RuntimeExceptionWrappingIterator(api, innerIter, this);
62  }
63
64  public void onException() {
65    hasError = true;
66  }
67
68  boolean hasError() {
69    return hasError;
70  }
71}