| 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.Constants.PROPERTY_SESSION_INTERVAL; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.atomic.AtomicReference; |
| 26 | |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.collect.Memoized; |
| 31 | import org.jclouds.domain.Location; |
| 32 | import org.jclouds.rest.AuthorizationException; |
| 33 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | import com.google.common.base.Supplier; |
| 37 | import com.google.common.base.Suppliers; |
| 38 | import com.google.common.collect.Maps; |
| 39 | import com.google.inject.AbstractModule; |
| 40 | import com.google.inject.Provides; |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | * @author Adrian Cole |
| 45 | */ |
| 46 | public class LocationModule extends AbstractModule { |
| 47 | protected final AtomicReference<AuthorizationException> authException; |
| 48 | |
| 49 | public LocationModule() { |
| 50 | this(new AtomicReference<AuthorizationException>()); |
| 51 | } |
| 52 | |
| 53 | public LocationModule(AtomicReference<AuthorizationException> authException) { |
| 54 | this.authException = authException; |
| 55 | } |
| 56 | |
| 57 | @Provides |
| 58 | @Singleton |
| 59 | protected Supplier<Map<String, ? extends Location>> provideLocationMap( |
| 60 | @Memoized Supplier<Set<? extends Location>> locations) { |
| 61 | return Suppliers.compose(new Function<Set<? extends Location>, Map<String, ? extends Location>>() { |
| 62 | |
| 63 | @Override |
| 64 | public Map<String, ? extends Location> apply(Set<? extends Location> from) { |
| 65 | return Maps.uniqueIndex(from, new Function<Location, String>() { |
| 66 | |
| 67 | @Override |
| 68 | public String apply(Location from) { |
| 69 | return from.getId(); |
| 70 | } |
| 71 | |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | }, locations); |
| 76 | } |
| 77 | |
| 78 | @Provides |
| 79 | @Singleton |
| 80 | @Memoized |
| 81 | protected Supplier<Set<? extends Location>> supplyLocationCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 82 | final Supplier<Set<? extends Location>> locationSupplier) { |
| 83 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Set<? extends Location>>(authException, seconds, |
| 84 | new Supplier<Set<? extends Location>>() { |
| 85 | @Override |
| 86 | public Set<? extends Location> get() { |
| 87 | return locationSupplier.get(); |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected void configure() { |
| 94 | } |
| 95 | } |