PageRenderTime 30ms CodeModel.GetById 26ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/google/appengine/datanucleus/DatastoreServiceFactoryInternal.java

http://datanucleus-appengine.googlecode.com/
Java | 72 lines | 19 code | 7 blank | 46 comment | 2 complexity | e43a731dd9ddba138de69e60f4bc9973 MD5 | raw file
 1/**********************************************************************
 2Copyright (c) 2009 Google Inc.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15**********************************************************************/
16package com.google.appengine.datanucleus;
17
18import com.google.appengine.api.datastore.DatastoreService;
19import com.google.appengine.api.datastore.DatastoreServiceConfig;
20import com.google.appengine.api.datastore.DatastoreServiceFactory;
21
22/**
23 * {@link DatastoreServiceFactory} doesn't provide an easy way to configure
24 * a specific instance to be returned, and that makes unit testing of classes
25 * that need a {@link DatastoreService} difficult.  Typically we'd use Guice
26 * to inject instances of {@link DatastoreService} into the various plugin
27 * classes that need them, but we don't want to force our users to have a
28 * runtime dependency on Guice.  Instead, we're kicking it old-school -
29 * our own factory with a static setter that lets you "install" the instance
30 * you want returned.  If you never call the setter,
31 * {@link #getDatastoreService} will return the result of
32 * {@link DatastoreServiceFactory#getDatastoreService()}.
33 *
34 * You should NEVER call {@link #setDatastoreService} from production code. It's for testing only!
35 *
36 * @author Max Ross <maxr@google.com>
37 */
38public final class DatastoreServiceFactoryInternal {
39
40  private DatastoreServiceFactoryInternal() {}
41
42  private static ThreadLocal<DatastoreService> datastoreServiceToReturn =
43      new ThreadLocal<DatastoreService>();
44
45  /**
46   * @param config The config to use.
47   * @return If a {@link DatastoreService} to return has been explicitly provided by a
48   *   call to {@link #setDatastoreService(DatastoreService)}, the explicitly provided instance. 
49   *   Otherwise a {@link DatastoreService} constructed by calling {@link DatastoreServiceFactory#getDatastoreService()}
50   */
51  public static DatastoreService getDatastoreService(DatastoreServiceConfig config) {
52    if (datastoreServiceToReturn.get() != null) {
53      return datastoreServiceToReturn.get();
54    }
55
56    // Wrap the service in an impl that properly translates the runtime exceptions thrown by the datastore api
57    return new WrappedDatastoreService(
58        DatastoreServiceFactory.getDatastoreService(config));
59  }
60
61  /**
62   * Provides a specific {@link DatastoreService} instance that will be the return value of all calls 
63   * to {@link #getDatastoreService(DatastoreServiceConfig)}. If {@code null} is provided, subsequent calls to
64   * {@link #getDatastoreService(DatastoreServiceConfig)} will return to its default behavior.
65   *
66   * @param ds The {@link DatastoreService} to be returned by all calls to
67   *   {@link #getDatastoreService(DatastoreServiceConfig)}. Can be null.
68   */
69  public static void setDatastoreService(DatastoreService ds) {
70    datastoreServiceToReturn.set(ds);
71  }
72}