Skip to content

Commit 956b0e6

Browse files
committed
remove org json dependency & fix instance profile credential fetcher concurrent bug
1 parent adeb213 commit 956b0e6

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>com.aliyun.oss</groupId>
1313
<artifactId>aliyun-sdk-oss</artifactId>
14-
<version>3.4.0</version>
14+
<version>3.4.1</version>
1515
<packaging>jar</packaging>
1616
<name>Aliyun OSS SDK for Java</name>
1717
<description>The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service</description>
@@ -37,6 +37,12 @@
3737
<groupId>com.aliyun</groupId>
3838
<artifactId>aliyun-java-sdk-core</artifactId>
3939
<version>3.4.0</version>
40+
<exclusions>
41+
<exclusion>
42+
<groupId>org.json</groupId>
43+
<artifactId>json</artifactId>
44+
</exclusion>
45+
</exclusions>
4046
</dependency>
4147
<dependency>
4248
<groupId>com.aliyun</groupId>

src/main/java/com/aliyun/oss/common/auth/InstanceProfileCredentialsProvider.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
package com.aliyun.oss.common.auth;
2121

22-
import com.aliyun.oss.common.auth.Credentials;
23-
import com.aliyun.oss.common.auth.CredentialsProvider;
2422
import com.aliyun.oss.common.utils.AuthUtils;
2523
import com.aliyun.oss.common.utils.LogUtils;
2624
import com.aliyuncs.exceptions.ClientException;
2725

26+
import java.util.concurrent.locks.ReentrantLock;
27+
2828
/**
2929
* Credentials provider implementation that loads credentials from the Ali Cloud
3030
* ECS Instance Metadata Service.
@@ -52,29 +52,40 @@ public void setCredentials(Credentials creds) {
5252

5353
@Override
5454
public InstanceProfileCredentials getCredentials() {
55-
if (credentials == null || credentials.isExpired()) {
56-
try {
57-
credentials = (InstanceProfileCredentials) fetcher.fetch(maxRetryTimes);
58-
} catch (ClientException e) {
59-
LogUtils.logException("EcsInstanceCredentialsFetcher.fetch Exception:", e);
60-
return null;
61-
}
62-
} else if (credentials.willSoonExpire() && credentials.shouldRefresh()) {
63-
try {
64-
credentials = (InstanceProfileCredentials) fetcher.fetch();
65-
} catch (ClientException e) {
66-
// Use the current expiring session token and wait for next round
67-
credentials.setLastFailedRefreshTime();
68-
LogUtils.logException("EcsInstanceCredentialsFetcher.fetch Exception:", e);
55+
try {
56+
if (credentials == null || credentials.isExpired()) {
57+
lock.lock();
58+
if (credentials == null || credentials.isExpired()) {
59+
try {
60+
credentials = (InstanceProfileCredentials) fetcher.fetch(maxRetryTimes);
61+
} catch (ClientException e) {
62+
LogUtils.logException("EcsInstanceCredentialsFetcher.fetch Exception:", e);
63+
return null;
64+
}
65+
}
66+
} else if (credentials.willSoonExpire() && credentials.shouldRefresh()) {
67+
lock.lock();
68+
if (credentials.willSoonExpire() && credentials.shouldRefresh()) {
69+
try {
70+
credentials = (InstanceProfileCredentials) fetcher.fetch();
71+
} catch (ClientException e) {
72+
// Use the current expiring session token and wait for next round
73+
credentials.setLastFailedRefreshTime();
74+
LogUtils.logException("EcsInstanceCredentialsFetcher.fetch Exception:", e);
75+
}
76+
}
6977
}
78+
return credentials;
79+
} finally {
80+
lock.unlock();
7081
}
71-
return credentials;
7282
}
7383

7484
private final String roleName;
7585
private InstanceProfileCredentials credentials;
7686
private InstanceProfileCredentialsFetcher fetcher;
7787

7888
private int maxRetryTimes = AuthUtils.MAX_ECS_METADATA_FETCH_RETRY_TIMES;
89+
private ReentrantLock lock = new ReentrantLock();
7990

8091
}

src/main/java/com/aliyun/oss/common/parser/JAXBResponseParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*/
4343
public class JAXBResponseParser implements ResponseParser<Object> {
4444

45-
private static final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
45+
private static final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl",null);
4646

4747
// It allows to specify the class type, if the class type is specified,
4848
// the contextPath will be ignored.

src/main/java/com/aliyun/oss/model/ObjectMetadata.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ public void setLastModified(Date lastModified) {
129129
* The value is not in the Rfc822 format.
130130
*/
131131
public Date getExpirationTime() throws ParseException {
132-
return DateUtil.parseRfc822Date((String) metadata.get(OSSHeaders.EXPIRES));
132+
String expires = (String) metadata.get(OSSHeaders.EXPIRES);
133+
134+
if (expires != null)
135+
return DateUtil.parseRfc822Date((String) metadata.get(OSSHeaders.EXPIRES));
136+
137+
return null;
133138
}
134139

135140
/**

src/test/java/com/aliyun/oss/common/utils/VersionUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class VersionUtilTest {
2828
@Test
2929
public void testGetDefaultUserAgent() {
3030
String userAgent = VersionInfoUtils.getDefaultUserAgent();
31-
assertTrue(userAgent.startsWith("aliyun-sdk-java/3.4.0("));
31+
assertTrue(userAgent.startsWith("aliyun-sdk-java/3.4.1("));
3232
assertEquals(userAgent.split("/").length, 4);
3333
assertEquals(userAgent.split(";").length, 2);
3434
assertEquals(userAgent.split("\\(").length, 2);

0 commit comments

Comments
 (0)