PageRenderTime 47ms CodeModel.GetById 37ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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