| 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 com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.inject.name.Names.named; |
| 23 | import static org.jclouds.location.reference.LocationConstants.ISO3166_CODES; |
| 24 | import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGION; |
| 25 | import static org.jclouds.location.reference.LocationConstants.PROPERTY_ZONE; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import javax.inject.Inject; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.location.Iso3166; |
| 34 | |
| 35 | import com.google.common.base.Splitter; |
| 36 | import com.google.common.collect.ImmutableMap; |
| 37 | import com.google.common.collect.ImmutableSet; |
| 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 | |
| 43 | /** |
| 44 | * |
| 45 | * looks for properties bound to the naming conventions jclouds.region.{@code regionId} |
| 46 | * .iso3166-codes and jclouds.zone.{@code zoneId}.iso3166-codes |
| 47 | * |
| 48 | * @author Adrian Cole |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class ProvideIso3166CodesByLocationIdViaProperties implements javax.inject.Provider<Map<String, Set<String>>> { |
| 52 | |
| 53 | private final Injector injector; |
| 54 | |
| 55 | @Inject |
| 56 | ProvideIso3166CodesByLocationIdViaProperties(Injector injector) { |
| 57 | this.injector = checkNotNull(injector, "injector"); |
| 58 | } |
| 59 | |
| 60 | @Singleton |
| 61 | @Iso3166 |
| 62 | @Override |
| 63 | public Map<String, Set<String>> get() { |
| 64 | Builder<String, Set<String>> codes = ImmutableMap.<String, Set<String>> builder(); |
| 65 | for (String key : ImmutableSet.of(PROPERTY_REGION, PROPERTY_ZONE)) |
| 66 | try { |
| 67 | String regionString = injector.getInstance(Key.get(String.class, named(key + "s"))); |
| 68 | for (String region : Splitter.on(',').split(regionString)) { |
| 69 | try { |
| 70 | codes.put(region, ImmutableSet.copyOf(Splitter.on(',') |
| 71 | .split( |
| 72 | injector.getInstance(Key.get(String.class, named(key + "." + region + "." |
| 73 | + ISO3166_CODES)))))); |
| 74 | } catch (ConfigurationException e) { |
| 75 | // this happens if regions property isn't set |
| 76 | // services not run by AWS may not have regions, so this is ok. |
| 77 | } |
| 78 | } |
| 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 | } |
| 83 | return codes.build(); |
| 84 | } |
| 85 | } |