1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.location.config; |
20 | |
21 | import static org.jclouds.location.reference.LocationConstants.ENDPOINT; |
22 | import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGION; |
23 | import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS; |
24 | |
25 | import java.net.URI; |
26 | import java.util.Map; |
27 | import java.util.Map.Entry; |
28 | |
29 | import javax.inject.Inject; |
30 | import javax.inject.Named; |
31 | import javax.inject.Singleton; |
32 | |
33 | import org.jclouds.location.Region; |
34 | |
35 | import com.google.common.base.Splitter; |
36 | import com.google.common.collect.ImmutableMap; |
37 | import com.google.common.collect.Multimap; |
38 | import com.google.common.collect.ImmutableMap.Builder; |
39 | import com.google.inject.ConfigurationException; |
40 | import com.google.inject.Injector; |
41 | import com.google.inject.Key; |
42 | import com.google.inject.name.Names; |
43 | |
44 | /** |
45 | * |
46 | * looks for properties bound to the naming convention jclouds.region.{@code regionId}.endpoint |
47 | * |
48 | * @author Adrian Cole |
49 | */ |
50 | @Singleton |
51 | public class ProvideRegionToURIViaProperties implements javax.inject.Provider<Map<String, URI>> { |
52 | |
53 | private final Injector injector; |
54 | private final Multimap<String, String> constants; |
55 | |
56 | @Inject |
57 | protected ProvideRegionToURIViaProperties(Injector injector, @Named("CONSTANTS") Multimap<String, String> constants) { |
58 | this.injector = injector; |
59 | this.constants = constants; |
60 | } |
61 | |
62 | @Singleton |
63 | @Region |
64 | @Override |
65 | public Map<String, URI> get() { |
66 | try { |
67 | String regionString = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_REGIONS))); |
68 | Builder<String, URI> regions = ImmutableMap.<String, URI> builder(); |
69 | for (String region : Splitter.on(',').split(regionString)) { |
70 | String regionUri = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_REGION + "." + region |
71 | + "." + ENDPOINT))); |
72 | for (Entry<String, String> entry : constants.entries()) { |
73 | regionUri = regionUri.replace(new StringBuilder().append('{').append(entry.getKey()).append('}').toString(), entry |
74 | .getValue()); |
75 | } |
76 | regions.put(region, URI.create(regionUri)); |
77 | } |
78 | return regions.build(); |
79 | } catch (ConfigurationException e) { |
80 | // this happens if regions property isn't set |
81 | // services not run by AWS may not have regions, so this is ok. |
82 | return ImmutableMap.of(); |
83 | } |
84 | } |
85 | |
86 | } |