24
24
import java .io .PrintStream ;
25
25
import java .nio .charset .StandardCharsets ;
26
26
import java .util .ArrayList ;
27
+ import java .util .HashSet ;
27
28
import java .util .List ;
28
29
import java .util .Map ;
29
30
import java .util .Properties ;
31
+ import java .util .Set ;
30
32
import java .util .concurrent .ExecutorService ;
31
33
import java .util .concurrent .Executors ;
32
34
import java .util .concurrent .ThreadFactory ;
@@ -101,8 +103,10 @@ public static void main(String[] args) throws Exception {
101
103
.build ();
102
104
ExecutorService executorService = DynamicExecutors .newScalingThreadPool (
103
105
1 , 20 , 60 * 1000 , factory );
104
- ImmutableMap .Builder <String , Map .Entry <String , BlobStore >> locators =
106
+ ImmutableMap .Builder <Map .Entry <String , String >,
107
+ Map .Entry <String , BlobStore >> locators =
105
108
ImmutableMap .builder ();
109
+ Set <Map .Entry <String , String >> parsedLocations = new HashSet <>();
106
110
for (File propertiesFile : options .propertiesFiles ) {
107
111
Properties properties = new Properties ();
108
112
try (InputStream is = new FileInputStream (propertiesFile )) {
@@ -117,14 +121,42 @@ public static void main(String[] args) throws Exception {
117
121
118
122
String s3ProxyAuthorizationString = properties .getProperty (
119
123
S3ProxyConstants .PROPERTY_AUTHORIZATION );
124
+ ImmutableList .Builder <String > locatorBuckets =
125
+ new ImmutableList .Builder <>();
126
+ for (String key : properties .stringPropertyNames ()) {
127
+ if (key .startsWith (S3ProxyConstants .PROPERTY_BUCKET_LOCATOR )) {
128
+ locatorBuckets .add (properties .getProperty (key ));
129
+ }
130
+ }
131
+ ImmutableList <String > buckets = locatorBuckets .build ();
132
+ String localIdentity = null ;
133
+ String localCredential = null ;
120
134
if (AuthenticationType .fromString (s3ProxyAuthorizationString ) !=
121
135
AuthenticationType .NONE ) {
122
- String localIdentity = properties .getProperty (
136
+ localIdentity = properties .getProperty (
123
137
S3ProxyConstants .PROPERTY_IDENTITY );
124
- String localCredential = properties .getProperty (
138
+ localCredential = properties .getProperty (
125
139
S3ProxyConstants .PROPERTY_CREDENTIAL );
126
- locators .put (localIdentity , Maps .immutableEntry (
127
- localCredential , blobStore ));
140
+ Map .Entry <String , String > key = Maps .immutableEntry (
141
+ localIdentity , null );
142
+ if (parsedLocations .add (key )) {
143
+ locators .put (key , Maps .immutableEntry (
144
+ localCredential , blobStore ));
145
+ }
146
+ }
147
+ if (!buckets .isEmpty ()) {
148
+ for (String bucket : buckets ) {
149
+ Map .Entry <String , String > key = Maps .immutableEntry (
150
+ localIdentity , bucket );
151
+ if (!parsedLocations .add (key )) {
152
+ System .err .printf ("The same bucket locator cannot be" +
153
+ " used in two properties files: %s\n " ,
154
+ bucket );
155
+ System .exit (1 );
156
+ }
157
+ locators .put (key ,
158
+ Maps .immutableEntry (localCredential , blobStore ));
159
+ }
128
160
}
129
161
130
162
S3Proxy .Builder s3ProxyBuilder2 = S3Proxy .Builder
@@ -149,23 +181,22 @@ public static void main(String[] args) throws Exception {
149
181
throw e ;
150
182
}
151
183
152
- final Map <String , Map .Entry <String , BlobStore >> locator =
153
- locators .build ();
184
+ final Map <Map . Entry < String , String >, Map .Entry <String , BlobStore >>
185
+ locator = locators .build ();
154
186
if (!locator .isEmpty ()) {
155
187
s3Proxy .setBlobStoreLocator (new BlobStoreLocator () {
156
188
@ Override
157
189
public Map .Entry <String , BlobStore > locateBlobStore (
158
190
String identity , String container , String blob ) {
191
+ Map .Entry <String , BlobStore > entry = locator .get (
192
+ Maps .immutableEntry (identity , container ));
193
+ if (entry != null ) {
194
+ return entry ;
195
+ }
159
196
if (identity == null ) {
160
- if (locator .size () == 1 ) {
161
- return locator .entrySet ().iterator ().next ()
162
- .getValue ();
163
- }
164
- throw new IllegalArgumentException (
165
- "cannot use anonymous access with multiple" +
166
- " backends" );
197
+ return locator .entrySet ().iterator ().next ().getValue ();
167
198
}
168
- return locator .get (identity );
199
+ return locator .get (Maps . immutableEntry ( identity , null ) );
169
200
}
170
201
});
171
202
}
0 commit comments