forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegion.java
More file actions
173 lines (152 loc) · 5.52 KB
/
Region.java
File metadata and controls
173 lines (152 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright 2013-2013 Amazon Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://aws.amazon.com/apache2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and
* limitations under the License.
*/
package com.amazonaws.regions;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import com.amazonaws.AmazonWebServiceClient;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentialsProvider;
/**
* Metadata for an AWS region, including its name and what services
* are available in it.
*/
public final class Region {
private final String name;
private final Map<String, String> serviceEndpoints = new HashMap<String, String>();
private final Map<String, Boolean> httpSupport = new HashMap<String, Boolean>();
private final Map<String, Boolean> httpsSupport = new HashMap<String, Boolean>();
Region(String name) {
this.name = name;
}
/**
* Returns the region with the id given, or null if it cannot be found in
* the current regions.xml file.
*/
public static Region getRegion(Regions region) {
return RegionUtils.getRegion(region.getName());
}
/**
* The unique system ID for this region; ex: us-east-1.
*
* @return The unique system ID for this region.
*/
public String getName() {
return name;
}
/**
* Returns a map of the available services in this region and their
* endpoints. The keys of the map are service abbreviations, as defined in
* {@link ServiceAbbreviations}, and the values are the endpoint URLs.
*
* @return A map of the available services in this region.
*/
Map<String, String> getServiceEndpoints() {
return serviceEndpoints;
}
Map<String, Boolean> getHttpSupport() {
return httpSupport;
}
Map<String, Boolean> getHttpsSupport() {
return httpsSupport;
}
/**
* Returns the endpoint for the service given.
*
* @see ServiceAbbreviations
*/
public String getServiceEndpoint(String serviceName) {
return serviceEndpoints.get(serviceName);
}
/**
* Returns whether the given service is supported in this region.
*
* @see ServiceAbbreviations
*/
public boolean isServiceSupported(String serviceName) {
return serviceEndpoints.containsKey(serviceName);
}
/**
* Returns whether the given service support the https protocol in this region.
*
* @see ServiceAbbreviations
*/
public boolean hasHttpsEndpoint(String serviceName) {
return httpsSupport.containsKey(serviceName) && httpsSupport.get(serviceName);
}
/**
* Returns whether the given service support the http protocol in this region.
*
* @see ServiceAbbreviations
*/
public boolean hasHttpEndpoint(String serviceName) {
return httpSupport.containsKey(serviceName) && httpSupport.get(serviceName);
}
/**
* Creates a new service client of the class given and configures it. If
* credentials or config are null, defaults will be used.
*
* @param serviceClass
* The service client class to instantiate, e.g. AmazonS3Client.class
* @param credentials
* The credentials provider to use, or null for the default
* credentials provider
* @param config
* The configuration to use, or null for the default
* configuration
*
* @see ServiceAbbreviations
*/
public <T extends AmazonWebServiceClient> T createClient(Class<T> serviceClass,
AWSCredentialsProvider credentials,
ClientConfiguration config) {
Constructor<T> constructor;
T client;
try {
if ( credentials == null && config == null ) {
constructor = serviceClass.getConstructor();
client = constructor.newInstance();
} else if ( credentials == null ) {
constructor = serviceClass.getConstructor(ClientConfiguration.class);
client = constructor.newInstance(config);
} else if ( config == null ) {
constructor = serviceClass.getConstructor(AWSCredentialsProvider.class);
client = constructor.newInstance(credentials);
} else {
constructor = serviceClass.getConstructor(AWSCredentialsProvider.class, ClientConfiguration.class);
client = constructor.newInstance(credentials, config);
}
client.setRegion(this);
return client;
} catch ( Exception e ) {
throw new RuntimeException("Couldn't instantiate instance of " + serviceClass, e);
}
}
@Override
public boolean equals(Object obj) {
if ( obj instanceof Region == false )
return false;
Region region = (Region) obj;
return this.getName().equals(region.getName());
}
@Override
public int hashCode() {
return getName().hashCode();
}
@Override
public String toString() {
return getName();
}
}