Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 85908b1

Browse files
committed
SUBMARINE-1349. Change the Client to be singleton with enum class.
1 parent 000caca commit 85908b1

File tree

1 file changed

+23
-27
lines changed
  • submarine-server/server-core/src/main/java/org/apache/submarine/server/s3

1 file changed

+23
-27
lines changed

submarine-server/server-core/src/main/java/org/apache/submarine/server/s3/Client.java

+23-27
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,40 @@
4444
import org.apache.submarine.commons.utils.SubmarineConfiguration;
4545
import org.apache.submarine.commons.utils.exception.SubmarineRuntimeException;
4646

47-
/**
48-
* S3(Minio) default client
49-
*/
50-
public class Client {
5147

52-
/* minio client */
53-
public MinioClient minioClient;
48+
public enum Client {
49+
DEFAULT(S3Constants.ENDPOINT), CUSTOMER("http://localhost:9000");
50+
5451
public static Map<String, Client> clientFactory = new HashMap<String, Client>();
52+
private final String endpoint;
53+
private final MinioClient minioClient;
5554

56-
public static Client getClient(String endpoint) {
57-
Client client = clientFactory.get(endpoint);
58-
Map<String, Client> clientLocalFactory = clientFactory;
5955

60-
if (client == null) {
61-
synchronized (Client.class) {
62-
if (client == null) {
63-
client = new Client(endpoint);
64-
clientLocalFactory.put(endpoint, client);
65-
clientFactory = clientLocalFactory;
66-
}
67-
}
56+
static {
57+
for (Client clientSingleton : Client.values()) {
58+
clientFactory.put(clientSingleton.endpoint, clientSingleton);
6859
}
69-
return client;
7060
}
7161

72-
public static Client getInstance() {
73-
return getClient(S3Constants.ENDPOINT);
62+
Client(String endpoint) {
63+
this.endpoint = endpoint;
64+
this.minioClient = MinioClient.builder()
65+
.endpoint(endpoint)
66+
.credentials(S3Constants.ACCESSKEY, S3Constants.SECRETKEY)
67+
.build();
7468
}
7569

76-
public static Client getInstance(String endpoint) {
77-
return getClient(endpoint);
70+
public static Client getInstance() {
71+
return clientFactory.get(S3Constants.ENDPOINT);
7872
}
7973

80-
private Client(String endpoint) {
81-
minioClient = MinioClient.builder()
82-
.endpoint(endpoint)
83-
.credentials(S3Constants.ACCESSKEY, S3Constants.SECRETKEY)
84-
.build();
74+
public static Client getInstance(String endpoint) {
75+
try {
76+
return clientFactory.get(endpoint);
77+
} catch (Exception e) {
78+
throw new SubmarineRuntimeException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
79+
e.getMessage());
80+
}
8581
}
8682

8783
/**

0 commit comments

Comments
 (0)