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