| 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.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | |
| 23 | import java.util.NoSuchElementException; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.ExecutionException; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Provider; |
| 30 | |
| 31 | import org.jclouds.collect.Memoized; |
| 32 | import org.jclouds.compute.domain.Hardware; |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.domain.TemplateBuilder; |
| 35 | import org.jclouds.compute.domain.internal.TemplateBuilderImpl; |
| 36 | import org.jclouds.compute.options.TemplateOptions; |
| 37 | import org.jclouds.domain.Location; |
| 38 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 39 | import org.jclouds.util.Throwables2; |
| 40 | |
| 41 | import com.google.common.base.Supplier; |
| 42 | import com.google.common.cache.CacheLoader; |
| 43 | import com.google.common.cache.LoadingCache; |
| 44 | import com.google.common.collect.ImmutableSet; |
| 45 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 46 | |
| 47 | /** |
| 48 | * |
| 49 | * @author Adrian Cole |
| 50 | */ |
| 51 | public class EC2TemplateBuilderImpl extends TemplateBuilderImpl { |
| 52 | |
| 53 | private final Supplier<LoadingCache<RegionAndName, ? extends Image>> lazyImageCache; |
| 54 | |
| 55 | @Inject |
| 56 | protected EC2TemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations, |
| 57 | @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes, |
| 58 | Supplier<Location> defaultLocation, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider, |
| 59 | @Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, Supplier<LoadingCache<RegionAndName, ? extends Image>> imageMap) { |
| 60 | super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider); |
| 61 | this.lazyImageCache = imageMap; |
| 62 | } |
| 63 | |
| 64 | final Provider<Image> lazyImageProvider = new Provider<Image>() { |
| 65 | |
| 66 | @Override |
| 67 | public Image get() { |
| 68 | if (imageId != null) { |
| 69 | String[] regionName = imageId.split("/"); |
| 70 | checkArgument(regionName.length == 2, |
| 71 | "amazon image ids must include the region ( ex. us-east-1/ami-7ea24a17 ) you specified: " + imageId); |
| 72 | RegionAndName key = new RegionAndName(regionName[0], regionName[1]); |
| 73 | try { |
| 74 | return lazyImageCache.get().get(key); |
| 75 | } catch (ExecutionException e) { |
| 76 | throw new NoSuchElementException(String.format("could not get imageId(%s/%s)", key.getRegion(), key.getName())); |
| 77 | } catch (UncheckedExecutionException e) { |
| 78 | // Primarily for testing: if cache is backed by a map, can get IllegalArgumentException instead of NPE |
| 79 | IllegalArgumentException e2 = Throwables2.getFirstThrowableOfType(e, IllegalArgumentException.class); |
| 80 | if (e2 != null && e2.getMessage() != null && e2.getMessage().contains("not present in")) { |
| 81 | throw new NoSuchElementException(String.format("imageId(%s/%s) not found", key.getRegion(), key.getName())); |
| 82 | } |
| 83 | throw new NoSuchElementException(String.format("could not get imageId(%s/%s)", key.getRegion(), key.getName())); |
| 84 | } catch (CacheLoader.InvalidCacheLoadException nex) { |
| 85 | throw new NoSuchElementException(String.format("imageId(%s/%s) not found", key.getRegion(), key.getName())); |
| 86 | } |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * @throws NoSuchElementException |
| 95 | * if the image is not found |
| 96 | */ |
| 97 | @Override |
| 98 | protected Image resolveImage(Hardware size, Iterable<? extends Image> supportedImages) { |
| 99 | try { |
| 100 | return super.resolveImage(size, supportedImages); |
| 101 | } catch (NoSuchElementException e) { |
| 102 | Image returnVal = lazyImageProvider.get(); |
| 103 | if (returnVal != null) |
| 104 | return returnVal; |
| 105 | throw e; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @SuppressWarnings("unchecked") |
| 110 | @Override |
| 111 | protected Set<? extends Image> getImages() { |
| 112 | if (imageId != null) { |
| 113 | Image image = lazyImageProvider.get(); |
| 114 | return ImmutableSet.of(image); |
| 115 | } else { |
| 116 | return (Set<Image>) this.images.get(); |
| 117 | } |
| 118 | } |
| 119 | } |