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