| 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.ec2.compute.config; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.toArray; |
| 22 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 23 | import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS; |
| 24 | |
| 25 | import javax.inject.Named; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.compute.ComputeServiceContext; |
| 29 | import org.jclouds.compute.config.BaseComputeServiceContextModule; |
| 30 | import org.jclouds.compute.domain.Image; |
| 31 | import org.jclouds.ec2.compute.EC2ComputeService; |
| 32 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 33 | import org.jclouds.ec2.compute.suppliers.RegionAndNameToImageSupplier; |
| 34 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 35 | |
| 36 | import com.google.common.base.Splitter; |
| 37 | import com.google.common.base.Supplier; |
| 38 | import com.google.common.cache.Cache; |
| 39 | import com.google.inject.Provides; |
| 40 | |
| 41 | /** |
| 42 | * Configures the {@link ComputeServiceContext}; requires {@link EC2ComputeService} bound. |
| 43 | * |
| 44 | * @author Adrian Cole |
| 45 | */ |
| 46 | public class EC2ComputeServiceContextModule extends BaseComputeServiceContextModule { |
| 47 | @Override |
| 48 | protected void configure() { |
| 49 | installDependencies(); |
| 50 | install(new EC2BindComputeStrategiesByClass()); |
| 51 | install(new EC2BindComputeSuppliersByClass()); |
| 52 | super.configure(); |
| 53 | } |
| 54 | |
| 55 | protected void installDependencies(){ |
| 56 | install(new EC2ComputeServiceDependenciesModule()); |
| 57 | } |
| 58 | |
| 59 | @Provides |
| 60 | @Singleton |
| 61 | protected Supplier<Cache<RegionAndName, ? extends Image>> provideRegionAndNameToImageSupplierCache( |
| 62 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, final RegionAndNameToImageSupplier supplier) { |
| 63 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Cache<RegionAndName, ? extends Image>>( |
| 64 | authException, seconds, new Supplier<Cache<RegionAndName, ? extends Image>>() { |
| 65 | @Override |
| 66 | public Cache<RegionAndName, ? extends Image> get() { |
| 67 | return supplier.get(); |
| 68 | } |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | @Provides |
| 73 | @Singleton |
| 74 | @Named(PROPERTY_EC2_AMI_OWNERS) |
| 75 | String[] amiOwners(@Named(PROPERTY_EC2_AMI_OWNERS) String amiOwners) { |
| 76 | if (amiOwners.trim().equals("")) |
| 77 | return new String[] {}; |
| 78 | return toArray(Splitter.on(',').split(amiOwners), String.class); |
| 79 | } |
| 80 | |
| 81 | } |
| 82 | |