/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. package com.google.appengine.datanucleus;
  18. import com.google.apphosting.api.ApiProxy;
  19. import java.util.List;
  20. import java.util.Set;
  21. import java.util.concurrent.Future;
  22. /**
  23. * @author Max Ross <maxr@google.com>
  24. */
  25. public class ExceptionThrowingDatastoreDelegate implements ApiProxy.Delegate {
  26. private final ApiProxy.Delegate inner;
  27. private final ExceptionPolicy policy;
  28. public ExceptionThrowingDatastoreDelegate(ApiProxy.Delegate inner, ExceptionPolicy policy) {
  29. this.inner = inner;
  30. this.policy = policy;
  31. }
  32. public byte[] makeSyncCall(ApiProxy.Environment environment, String packageName,
  33. String methodName, byte[] request) throws ApiProxy.ApiProxyException {
  34. policy.intercept(methodName);
  35. return inner.makeSyncCall(environment, packageName, methodName, request);
  36. }
  37. public Future<byte[]> makeAsyncCall(ApiProxy.Environment environment, String packageName, String methodName,
  38. byte[] request, ApiProxy.ApiConfig apiConfig) {
  39. policy.intercept(methodName);
  40. return inner.makeAsyncCall(environment, packageName, methodName, request, apiConfig);
  41. }
  42. public void log(ApiProxy.Environment environment, ApiProxy.LogRecord logRecord) {
  43. inner.log(environment, logRecord);
  44. }
  45. public void flushLogs(ApiProxy.Environment environment) {
  46. inner.flushLogs(environment);
  47. }
  48. public List<Thread> getRequestThreads(ApiProxy.Environment environment) {
  49. return inner.getRequestThreads(environment);
  50. }
  51. public interface ExceptionPolicy {
  52. void intercept(String methodName);
  53. }
  54. public static abstract class BaseExceptionPolicy implements ExceptionPolicy {
  55. private static final Set<String> RPCS_TO_INTERCEPT =
  56. Utils.newHashSet("Put", "Delete", "Commit", "RunQuery", "Next");
  57. public final void intercept(String methodName) {
  58. if (RPCS_TO_INTERCEPT.contains(methodName)) {
  59. doIntercept(methodName);
  60. }
  61. }
  62. protected abstract void doIntercept(String methodName);
  63. }
  64. public ApiProxy.Delegate getInner() {
  65. return inner;
  66. }
  67. }