PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/ExceptionThrowingDatastoreDelegate.java

http://datanucleus-appengine.googlecode.com/
Java | 83 lines | 48 code | 16 blank | 19 comment | 1 complexity | f50df4acb9c3fbc48013771ebb3bdf90 MD5 | raw file
 1/*
 2 * /**********************************************************************
 3 * Copyright (c) 2009 Google Inc.
 4 *
 5 * Licensed under the Apache License, Version 2.0 (the "License");
 6 * you may not use this file except in compliance with the License.
 7 * You may obtain a copy of the License at
 8 *
 9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * **********************************************************************/
17
18package com.google.appengine.datanucleus;
19
20import com.google.apphosting.api.ApiProxy;
21
22import java.util.List;
23import java.util.Set;
24import java.util.concurrent.Future;
25
26/**
27 * @author Max Ross <maxr@google.com>
28 */
29public class ExceptionThrowingDatastoreDelegate implements ApiProxy.Delegate {
30  private final ApiProxy.Delegate inner;
31  private final ExceptionPolicy policy;
32
33  public ExceptionThrowingDatastoreDelegate(ApiProxy.Delegate inner, ExceptionPolicy policy) {
34    this.inner = inner;
35    this.policy = policy;
36  }
37
38  public byte[] makeSyncCall(ApiProxy.Environment environment, String packageName,
39      String methodName, byte[] request) throws ApiProxy.ApiProxyException {
40    policy.intercept(methodName);
41    return inner.makeSyncCall(environment, packageName, methodName, request);
42  }
43
44  public Future<byte[]> makeAsyncCall(ApiProxy.Environment environment, String packageName, String methodName,
45                              byte[] request, ApiProxy.ApiConfig apiConfig) {
46    policy.intercept(methodName);
47    return inner.makeAsyncCall(environment, packageName, methodName, request, apiConfig);
48  }
49
50  public void log(ApiProxy.Environment environment, ApiProxy.LogRecord logRecord) {
51    inner.log(environment, logRecord);
52  }
53
54  public void flushLogs(ApiProxy.Environment environment) {
55    inner.flushLogs(environment);
56  }
57
58  public List<Thread> getRequestThreads(ApiProxy.Environment environment) {
59    return inner.getRequestThreads(environment);
60  }
61
62  public interface ExceptionPolicy {
63    void intercept(String methodName);
64  }
65
66  public static abstract class BaseExceptionPolicy implements ExceptionPolicy {
67
68    private static final Set<String> RPCS_TO_INTERCEPT =
69        Utils.newHashSet("Put", "Delete", "Commit", "RunQuery", "Next");
70
71    public final void intercept(String methodName) {
72      if (RPCS_TO_INTERCEPT.contains(methodName)) {
73        doIntercept(methodName);
74      }
75    }
76
77    protected abstract void doIntercept(String methodName);
78  }
79
80  public ApiProxy.Delegate getInner() {
81    return inner;
82  }
83}