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

http://datanucleus-appengine.googlecode.com/ · Java · 74 lines · 42 code · 9 blank · 23 comment · 0 complexity · f38fdeb18c1896d1fb9d6f431ad8aa82 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. */
  16. package com.google.appengine.datanucleus;
  17. import com.google.appengine.api.datastore.DatastoreService;
  18. import com.google.appengine.api.datastore.DatastoreServiceConfig;
  19. import java.lang.reflect.InvocationHandler;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.lang.reflect.Method;
  22. import java.lang.reflect.Proxy;
  23. /**
  24. * If you call
  25. * {@link #install} make sure you call
  26. * {@link #uninstall} in a finally block.
  27. *
  28. * @author Max Ross <max.ross@gmail.com>
  29. */
  30. public final class DatastoreServiceInterceptor {
  31. private static DatastoreService ORIGINAL_DATASTORE_SERVICE;
  32. private static final class Handler implements InvocationHandler {
  33. private final DatastoreService delegate;
  34. private final Policy policy;
  35. private Handler(DatastoreService delegate, Policy policy) {
  36. this.delegate = delegate;
  37. this.policy = policy;
  38. }
  39. public Object invoke(Object o, Method method, Object[] params) throws Throwable {
  40. policy.intercept(o, method, params);
  41. try {
  42. method.setAccessible(true);
  43. return method.invoke(delegate, params);
  44. } catch (InvocationTargetException ite) {
  45. // Always throw the real cause.
  46. throw ite.getTargetException();
  47. }
  48. }
  49. }
  50. public static void install(DatastoreManager storeManager, Policy policy) {
  51. DatastoreServiceConfig config = storeManager.getDefaultDatastoreServiceConfigForReads();
  52. ORIGINAL_DATASTORE_SERVICE = DatastoreServiceFactoryInternal.getDatastoreService(config);
  53. Handler handler = new Handler(ORIGINAL_DATASTORE_SERVICE, policy);
  54. DatastoreService ds = (DatastoreService) Proxy.newProxyInstance(
  55. DatastoreServiceInterceptor.class.getClassLoader(),
  56. new Class[] {DatastoreService.class}, handler);
  57. DatastoreServiceFactoryInternal.setDatastoreService(ds);
  58. }
  59. public static void uninstall() {
  60. DatastoreServiceFactoryInternal.setDatastoreService(ORIGINAL_DATASTORE_SERVICE);
  61. }
  62. public interface Policy {
  63. void intercept(Object o, Method method, Object[] params);
  64. }
  65. }