| 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.functions; | 
| 20 |  | 
| 21 | import static com.google.common.base.Preconditions.checkArgument; | 
| 22 | import static com.google.common.base.Preconditions.checkNotNull; | 
| 23 | import static com.google.common.base.Preconditions.checkState; | 
| 24 |  | 
| 25 | import java.net.URI; | 
| 26 | import java.util.Map; | 
| 27 |  | 
| 28 | import org.jclouds.javax.annotation.Nullable; | 
| 29 | import javax.inject.Inject; | 
| 30 | import javax.inject.Singleton; | 
| 31 |  | 
| 32 | import org.jclouds.location.Provider; | 
| 33 | import org.jclouds.location.Region; | 
| 34 |  | 
| 35 | import com.google.common.base.Function; | 
| 36 |  | 
| 37 | /** | 
| 38 | * If a mapping of regions to endpoints exists, return a uri corresponding to the name of the region | 
| 39 | * (passed argument). Otherwise, return the default location. | 
| 40 | * | 
| 41 | * @author Adrian Cole | 
| 42 | */ | 
| 43 | @Singleton | 
| 44 | public class RegionToEndpointOrProviderIfNull implements Function<Object, URI> { | 
| 45 | private final URI defaultUri; | 
| 46 | private final String defaultProvider; | 
| 47 | private final Map<String, URI> regionToEndpoint; | 
| 48 |  | 
| 49 | @Inject | 
| 50 | public RegionToEndpointOrProviderIfNull(@Provider URI defaultUri, @Provider String defaultProvider, | 
| 51 | @Nullable @Region Map<String, URI> regionToEndpoint) { | 
| 52 | this.defaultUri = checkNotNull(defaultUri, "defaultUri"); | 
| 53 | this.defaultProvider = checkNotNull(defaultProvider, "defaultProvider"); | 
| 54 | this.regionToEndpoint = regionToEndpoint; | 
| 55 | } | 
| 56 |  | 
| 57 | @Override | 
| 58 | public URI apply(@Nullable Object from) { | 
| 59 | if (from == null || from.equals(defaultProvider)) | 
| 60 | return defaultUri; | 
| 61 | checkState(from.equals(defaultProvider) || regionToEndpoint != null, "requested location " + from | 
| 62 | + ", but only the default location " + defaultProvider + " is configured"); | 
| 63 | checkArgument(from.equals(defaultProvider) || (regionToEndpoint != null && regionToEndpoint.containsKey(from)), | 
| 64 | "requested location %s, which is not in the configured locations: %s", from, regionToEndpoint); | 
| 65 | return regionToEndpoint.get(from); | 
| 66 | } | 
| 67 | } |