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
;
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
;
21 public DatastoreService
getDatastoreService(DatastoreServiceConfig config
) {
22 return new DatastoreServiceImpl(getAsyncDatastoreService(config
));
26 public AsyncDatastoreServiceInternal
getAsyncDatastoreService(DatastoreServiceConfig config
) {
27 TransactionStack txnStack
= new TransactionStackImpl();
28 CloudDatastoreV1Proxy cloudDatastoreProxy
= config
.getCloudDatastoreV1Proxy();
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;
39 if (constructedCloudService
) {
40 throw new IllegalArgumentException(MIXED_SERVICES_MESSAGE
);
42 constructedNonCloudService
= true;
48 Preconditions
.checkState(cloudDatastoreProxy
== null);
49 return new AsyncDatastoreServiceImpl(config
, config
.constructApiConfig(), txnStack
);
50 case CLOUD_DATASTORE_V1_VIA_API_PROXY
:
51 Preconditions
.checkState(cloudDatastoreProxy
== null);
52 return new AsyncCloudDatastoreV1ServiceImpl(config
,
53 new LocalCloudDatastoreV1Proxy(config
.constructApiConfig()), txnStack
);
54 case CLOUD_DATASTORE_V1_REMOTE
:
55 if (cloudDatastoreProxy
== null) {
56 cloudDatastoreProxy
= RemoteCloudDatastoreV1Proxy
.create(config
);
58 return new AsyncCloudDatastoreV1ServiceImpl(config
, cloudDatastoreProxy
, txnStack
);
60 throw new IllegalArgumentException(
61 "Can't instantiate service with API version: " + apiVersion
);
65 static synchronized void resetConstructionStateForTest() {
66 constructedNonCloudService
= false;
67 constructedCloudService
= false;