PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
16package com.google.appengine.datanucleus.query;
17
18import com.google.appengine.api.datastore.DatastoreFailureException;
19import com.google.appengine.api.datastore.DatastoreTimeoutException;
20
21import org.datanucleus.exceptions.NucleusException;
22import org.datanucleus.api.ApiAdapter;
23import org.datanucleus.store.query.QueryTimeoutException;
24
25import com.google.appengine.datanucleus.Utils;
26import com.google.appengine.datanucleus.Utils.Supplier;
27
28import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapDatastoreFailureException;
29import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapDatastoreTimeoutExceptionForQuery;
30import static com.google.appengine.datanucleus.DatastoreExceptionTranslator.wrapIllegalArgumentException;
31
32/**
33 * Helper methods that wrap the execution of the provided {@link Utils.Supplier}
34 * with exception handling logic that translates from one set of exceptions to
35 * another.
36 *
37 * @author Max Ross <max.ross@gmail.com>
38 */
39final class QueryExceptionWrappers {
40  private QueryExceptionWrappers() { }
41
42  /**
43   * Translates datastore runtime exceptions to DataNucleus runtime exceptions.
44   */
45  static <T> Supplier<T> datastoreToDataNucleus(final Supplier<T> supplier) {
46    return new Supplier<T>() {
47      public T get() {
48        try {
49          return supplier.get();
50        } catch (IllegalArgumentException e) {
51          throw wrapIllegalArgumentException(e);
52        } catch (DatastoreTimeoutException e) {
53          throw wrapDatastoreTimeoutExceptionForQuery(e);
54        } catch (DatastoreFailureException e) {
55          throw wrapDatastoreFailureException(e);
56        }
57      }
58    };
59  }
60
61  /**
62   * Translates DataNucleus runtime exceptions to Api runtime exceptions.
63   */
64  static <T> Supplier<T> dataNucleusToApi(final ApiAdapter api, final Supplier<T> supplier) {
65    return new Supplier<T>() {
66      public T get() {
67        try {
68          return supplier.get();
69        } catch (QueryTimeoutException te) {
70          throw api.getApiExceptionForNucleusException(te);
71        } catch (NucleusException ne) {
72          throw api.getApiExceptionForNucleusException(ne);
73        }
74      }
75    };
76  }
77}