Skip to content

Commit dcf765d

Browse files
authored
fix(mobile-client): initialize key value store inside mobile client constructor to prevent NPE (#2383)
1 parent 73bb3e4 commit dcf765d

File tree

4 files changed

+181
-61
lines changed

4 files changed

+181
-61
lines changed

aws-android-sdk-mobile-client/src/main/java/com/amazonaws/mobile/client/AWSMobileClient.java

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@
126126
import java.util.Set;
127127
import java.util.concurrent.CountDownLatch;
128128
import java.util.concurrent.locks.Lock;
129-
import java.util.concurrent.locks.ReadWriteLock;
130129
import java.util.concurrent.locks.ReentrantLock;
131-
import java.util.concurrent.locks.ReentrantReadWriteLock;
132130

133131
import static com.amazonaws.mobile.client.results.SignInState.CUSTOM_CHALLENGE;
134132

@@ -261,7 +259,7 @@ public final class AWSMobileClient implements AWSCredentialsProvider {
261259
private volatile CountDownLatch showSignInWaitLatch;
262260
private Object federateWithCognitoIdentityLockObject;
263261
private Object initLockObject;
264-
AWSMobileClientStore mStore;
262+
KeyValueStore mStore;
265263
AWSMobileClientCognitoIdentityProvider provider;
266264
DeviceOperations mDeviceOperations;
267265
AmazonCognitoIdentityProvider userpoolLL;
@@ -327,6 +325,7 @@ private AWSMobileClient() {
327325
federateWithCognitoIdentityLockObject = new Object();
328326
showSignInWaitLatch = new CountDownLatch(1);
329327
initLockObject = new Object();
328+
mStore = new DummyStore();
330329
}
331330

332331
/**
@@ -3802,64 +3801,6 @@ public String[] getProviderPermissions() {
38023801
}
38033802
}
38043803

3805-
class AWSMobileClientStore {
3806-
AWSKeyValueStore mAWSKeyValueStore;
3807-
3808-
private ReadWriteLock mReadWriteLock = new ReentrantReadWriteLock();
3809-
3810-
AWSMobileClientStore(AWSMobileClient client) {
3811-
mAWSKeyValueStore = new AWSKeyValueStore(client.mContext,
3812-
AWSMobileClient.SHARED_PREFERENCES_KEY,
3813-
client.mIsPersistenceEnabled);
3814-
}
3815-
3816-
Map<String, String> get(final String... keys) {
3817-
try {
3818-
mReadWriteLock.readLock().lock();
3819-
HashMap<String, String> attributes = new HashMap<String, String>();
3820-
for (String key : keys) {
3821-
attributes.put(key, mAWSKeyValueStore.get(key));
3822-
}
3823-
return attributes;
3824-
} finally {
3825-
mReadWriteLock.readLock().unlock();
3826-
}
3827-
}
3828-
3829-
String get(final String key) {
3830-
try {
3831-
mReadWriteLock.readLock().lock();
3832-
return mAWSKeyValueStore.get(key);
3833-
} finally {
3834-
mReadWriteLock.readLock().unlock();
3835-
}
3836-
}
3837-
3838-
void set(final Map<String, String> attributes) {
3839-
try {
3840-
mReadWriteLock.writeLock().lock();
3841-
for (String key : attributes.keySet()) {
3842-
mAWSKeyValueStore.put(key, attributes.get(key));
3843-
}
3844-
} finally {
3845-
mReadWriteLock.writeLock().unlock();
3846-
}
3847-
}
3848-
3849-
void set(final String key, final String value) {
3850-
try {
3851-
mReadWriteLock.writeLock().lock();
3852-
mAWSKeyValueStore.put(key, value);
3853-
} finally {
3854-
mReadWriteLock.writeLock().unlock();
3855-
}
3856-
}
3857-
3858-
void clear() {
3859-
mAWSKeyValueStore.clear();
3860-
}
3861-
}
3862-
38633804
/**
38643805
* A duplicate class of AWSEnhancedCognitoIdentityProvider that provides the ability to
38653806
* branch into developer authenticated identities.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2017-2021 Amazon.com, Inc. or its affiliates.
3+
* All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.amazonaws.mobile.client;
19+
20+
import com.amazonaws.internal.keyvaluestore.AWSKeyValueStore;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import java.util.concurrent.locks.ReadWriteLock;
25+
import java.util.concurrent.locks.ReentrantReadWriteLock;
26+
27+
/**
28+
* Uses {@link AWSKeyValueStore} to persistently store values for mobile client.
29+
*/
30+
final class AWSMobileClientStore implements KeyValueStore {
31+
private final AWSKeyValueStore mAWSKeyValueStore;
32+
private final ReadWriteLock mReadWriteLock;
33+
34+
AWSMobileClientStore(AWSMobileClient client) {
35+
mAWSKeyValueStore = new AWSKeyValueStore(client.mContext,
36+
AWSMobileClient.SHARED_PREFERENCES_KEY,
37+
client.mIsPersistenceEnabled);
38+
mReadWriteLock = new ReentrantReadWriteLock();
39+
}
40+
41+
@Override
42+
public Map<String, String> get(final String... keys) {
43+
try {
44+
mReadWriteLock.readLock().lock();
45+
HashMap<String, String> attributes = new HashMap<String, String>();
46+
for (String key : keys) {
47+
attributes.put(key, mAWSKeyValueStore.get(key));
48+
}
49+
return attributes;
50+
} finally {
51+
mReadWriteLock.readLock().unlock();
52+
}
53+
}
54+
55+
@Override
56+
public String get(final String key) {
57+
try {
58+
mReadWriteLock.readLock().lock();
59+
return mAWSKeyValueStore.get(key);
60+
} finally {
61+
mReadWriteLock.readLock().unlock();
62+
}
63+
}
64+
65+
@Override
66+
public void set(final Map<String, String> attributes) {
67+
try {
68+
mReadWriteLock.writeLock().lock();
69+
for (String key : attributes.keySet()) {
70+
mAWSKeyValueStore.put(key, attributes.get(key));
71+
}
72+
} finally {
73+
mReadWriteLock.writeLock().unlock();
74+
}
75+
}
76+
77+
@Override
78+
public void set(final String key, final String value) {
79+
try {
80+
mReadWriteLock.writeLock().lock();
81+
mAWSKeyValueStore.put(key, value);
82+
} finally {
83+
mReadWriteLock.writeLock().unlock();
84+
}
85+
}
86+
87+
@Override
88+
public void clear() {
89+
mAWSKeyValueStore.clear();
90+
}
91+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2017-2021 Amazon.com, Inc. or its affiliates.
3+
* All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.amazonaws.mobile.client;
19+
20+
import java.util.Map;
21+
22+
/**
23+
* Default key value store to be replaced during mobile client initialization.
24+
*/
25+
final class DummyStore implements KeyValueStore {
26+
@Override
27+
public Map<String, String> get(String... keys) {
28+
throwNotInitializedException();
29+
return null;
30+
}
31+
32+
@Override
33+
public String get(String key) {
34+
throwNotInitializedException();
35+
return null;
36+
}
37+
38+
@Override
39+
public void set(Map<String, String> attributes) {
40+
throwNotInitializedException();
41+
}
42+
43+
@Override
44+
public void set(String key, String value) {
45+
throwNotInitializedException();
46+
47+
}
48+
49+
@Override
50+
public void clear() {
51+
throwNotInitializedException();
52+
}
53+
54+
private void throwNotInitializedException() {
55+
throw new IllegalStateException("AWSMobileClient has not been initialized yet.");
56+
}
57+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2017-2021 Amazon.com, Inc. or its affiliates.
3+
* All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.amazonaws.mobile.client;
19+
20+
import java.util.Map;
21+
22+
/**
23+
* Allows key-value pairs to be stored and retrieved.
24+
*/
25+
interface KeyValueStore {
26+
Map<String, String> get(final String... keys);
27+
String get(final String key);
28+
void set(final Map<String, String> attributes);
29+
void set(final String key, final String value);
30+
void clear();
31+
}

0 commit comments

Comments
 (0)