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.suppliers; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | import static com.google.common.collect.Iterables.getOnlyElement; |
23 | |
24 | import java.util.NoSuchElementException; |
25 | import java.util.Set; |
26 | |
27 | import javax.inject.Inject; |
28 | import javax.inject.Singleton; |
29 | |
30 | import org.jclouds.collect.Memoized; |
31 | import org.jclouds.domain.Location; |
32 | import org.jclouds.domain.LocationScope; |
33 | import org.jclouds.location.Region; |
34 | |
35 | import com.google.common.base.Predicate; |
36 | import com.google.common.base.Supplier; |
37 | import com.google.common.collect.Iterables; |
38 | import com.google.inject.Injector; |
39 | import com.google.inject.Key; |
40 | |
41 | /** |
42 | * @author Adrian Cole |
43 | */ |
44 | @Singleton |
45 | public class OnlyLocationOrFirstZoneOrRegionMatchingRegionId implements Supplier<Location> { |
46 | @Singleton |
47 | public static final class IsRegionAndIdEqualsOrIsZoneParentIdEquals implements Predicate<Location> { |
48 | |
49 | private final String region; |
50 | |
51 | @Inject |
52 | IsRegionAndIdEqualsOrIsZoneParentIdEquals(@Region String region) { |
53 | this.region = checkNotNull(region, "region"); |
54 | } |
55 | |
56 | @Override |
57 | public boolean apply(Location input) { |
58 | switch (input.getScope()) { |
59 | case ZONE: |
60 | return input.getParent().getId().equals(region); |
61 | case REGION: |
62 | return input.getId().equals(region); |
63 | default: |
64 | return false; |
65 | } |
66 | } |
67 | |
68 | @Override |
69 | public String toString() { |
70 | return "isRegionAndIdEqualsOrIsZoneParentIdEquals(" + region + ")"; |
71 | } |
72 | } |
73 | |
74 | private final Injector injector; |
75 | private final Supplier<Set<? extends Location>> locationsSupplier; |
76 | |
77 | @Inject |
78 | OnlyLocationOrFirstZoneOrRegionMatchingRegionId(Injector injector, |
79 | @Memoized Supplier<Set<? extends Location>> locationsSupplier) { |
80 | this.injector = checkNotNull(injector, "injector"); |
81 | this.locationsSupplier = checkNotNull(locationsSupplier, "locationsSupplier"); |
82 | } |
83 | |
84 | @Override |
85 | @Singleton |
86 | public Location get() { |
87 | Set<? extends Location> locations = locationsSupplier.get(); |
88 | if (locationsSupplier.get().size() == 1) |
89 | return getOnlyElement(locationsSupplier.get()); |
90 | IsRegionAndIdEqualsOrIsZoneParentIdEquals matcher = null; |
91 | try { |
92 | String region = injector.getInstance(Key.get(String.class, Region.class)); |
93 | if (region == null) |
94 | return Iterables.get(locationsSupplier.get(), 0); |
95 | matcher = injector.getInstance(IsRegionAndIdEqualsOrIsZoneParentIdEquals.class); |
96 | Location toReturn = Iterables.find(locations, matcher); |
97 | return toReturn.getScope() == LocationScope.REGION ? toReturn : toReturn.getParent(); |
98 | } catch (NoSuchElementException e) { |
99 | throw new IllegalStateException(String.format("region %s not found in %s", matcher, locations)); |
100 | } |
101 | } |
102 | } |