Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / DatastoreServiceFactoryImpl.java
blob536298b9acb7a2369e3ec2da18dbf4e8a4e8eb4d
1 // Copyright 2012 Google Inc. All rights reserved.
3 package com.google.appengine.api.datastore;
5 import com.google.appengine.api.datastore.DatastoreServiceConfig.ApiVersion;
6 import com.google.common.base.Preconditions;
8 /**
9 * Creates DatastoreService instances.
12 final class DatastoreServiceFactoryImpl implements IDatastoreServiceFactory {
14 private static final String MIXED_SERVICES_MESSAGE =
15 "Cannot create both Cloud and non-Cloud Datastore services.";
17 private static boolean constructedNonCloudService;
18 private static boolean constructedCloudService;
20 @Override
21 public DatastoreService getDatastoreService(DatastoreServiceConfig config) {
22 return new DatastoreServiceImpl(getAsyncDatastoreService(config));
25 @Override
26 public AsyncDatastoreServiceInternal getAsyncDatastoreService(DatastoreServiceConfig config) {
27 TransactionStack txnStack = new TransactionStackImpl();
28 DatastoreV4Proxy datastoreProxy = config.getDatastoreV4Proxy();
30 ApiVersion apiVersion = config.getApiVersion();
32 synchronized (DatastoreServiceFactoryImpl.class) {
33 if (apiVersion.isCloudService()) {
34 if (constructedNonCloudService) {
35 throw new IllegalArgumentException(MIXED_SERVICES_MESSAGE);
37 constructedCloudService = true;
38 } else {
39 if (constructedCloudService) {
40 throw new IllegalArgumentException(MIXED_SERVICES_MESSAGE);
42 constructedNonCloudService = true;
46 switch (apiVersion) {
47 case V3:
48 Preconditions.checkState(datastoreProxy == null);
49 return new AsyncDatastoreServiceImpl(config, config.constructApiConfig(), txnStack);
50 case V4:
51 Preconditions.checkState(datastoreProxy == null);
52 return new AsyncDatastoreV4ServiceImpl(config,
53 new AppEngineDatastoreV4Proxy(config.constructApiConfig()), txnStack);
54 case CLOUD_DATASTORE:
55 if (datastoreProxy == null) {
56 datastoreProxy = CloudDatastoreProxy.create(config);
58 return new AsyncDatastoreV4ServiceImpl(config, datastoreProxy, txnStack);
59 default:
60 throw new IllegalArgumentException(
61 "Can't instantiate service with API version: " + apiVersion);
65 static synchronized void resetConstructionStateForTest() {
66 constructedNonCloudService = false;
67 constructedCloudService = false;