| 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 | |
| 40 | import com.google.common.base.Supplier; |
| 41 | import com.google.common.cache.Cache; |
| 42 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | public class EC2TemplateBuilderImpl extends TemplateBuilderImpl { |
| 49 | |
| 50 | private final Supplier<Cache<RegionAndName, ? extends Image>> imageMap; |
| 51 | |
| 52 | @Inject |
| 53 | protected EC2TemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations, |
| 54 | @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes, |
| 55 | Supplier<Location> defaultLocation, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider, |
| 56 | @Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, Supplier<Cache<RegionAndName, ? extends Image>> imageMap) { |
| 57 | super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider); |
| 58 | this.imageMap = imageMap; |
| 59 | } |
| 60 | |
| 61 | final Provider<Image> lazyImageProvider = new Provider<Image>() { |
| 62 | |
| 63 | @Override |
| 64 | public Image get() { |
| 65 | if (imageId != null) { |
| 66 | String[] regionName = imageId.split("/"); |
| 67 | checkArgument(regionName.length == 2, |
| 68 | "amazon image ids must include the region ( ex. us-east-1/ami-7ea24a17 ) you specified: " + imageId); |
| 69 | RegionAndName key = new RegionAndName(regionName[0], regionName[1]); |
| 70 | try { |
| 71 | return imageMap.get().get(key); |
| 72 | } catch (ExecutionException e) { |
| 73 | throw new NoSuchElementException(String.format("could not get imageId(%s/%s)", key.getRegion(), key.getName())); |
| 74 | } catch (UncheckedExecutionException e) { |
| 75 | throw new NoSuchElementException(String.format("could not get imageId(%s/%s)", key.getRegion(), key.getName())); |
| 76 | } catch (NullPointerException nex) { |
| 77 | throw new NoSuchElementException(String.format("imageId(%s/%s) not found", key.getRegion(), key.getName())); |
| 78 | } |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * @throws NoSuchElementException |
| 87 | * if the image is not found |
| 88 | */ |
| 89 | @Override |
| 90 | protected Image resolveImage(Hardware size, Iterable<? extends Image> supportedImages) { |
| 91 | try { |
| 92 | return super.resolveImage(size, supportedImages); |
| 93 | } catch (NoSuchElementException e) { |
| 94 | Image returnVal = lazyImageProvider.get(); |
| 95 | if (returnVal != null) |
| 96 | return returnVal; |
| 97 | throw e; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @SuppressWarnings("unchecked") |
| 102 | @Override |
| 103 | protected Set<? extends Image> getImages() { |
| 104 | Set<Image> images = (Set<Image>) this.images.get(); |
| 105 | if (images.size() == 0) { |
| 106 | Image toReturn = lazyImageProvider.get(); |
| 107 | if (toReturn != null) { |
| 108 | images.add(toReturn); |
| 109 | } |
| 110 | } |
| 111 | return images; |
| 112 | } |
| 113 | |
| 114 | } |