EMMA Coverage Report (generated Fri Apr 27 15:03:37 EDT 2012)
[all classes][org.jclouds.ec2.compute.internal]

COVERAGE SUMMARY FOR SOURCE FILE [EC2TemplateBuilderImpl.java]

nameclass, %method, %block, %line, %
EC2TemplateBuilderImpl.java100% (2/2)100% (9/9)75%  (157/208)70%  (21/30)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EC2TemplateBuilderImpl$1100% (1/1)100% (2/2)73%  (106/145)75%  (12/16)
get (): Image 100% (1/1)72%  (100/139)73%  (11/15)
EC2TemplateBuilderImpl$1 (EC2TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class EC2TemplateBuilderImpl100% (1/1)100% (7/7)81%  (51/63)67%  (10/15)
resolveImage (Hardware, Iterable): Image 100% (1/1)29%  (5/17)17%  (1/6)
EC2TemplateBuilderImpl (Supplier, Supplier, Supplier, Supplier, Provider, Pro... 100% (1/1)100% (18/18)100% (4/4)
access$000 (EC2TemplateBuilderImpl): String 100% (1/1)100% (3/3)100% (1/1)
access$100 (EC2TemplateBuilderImpl): String 100% (1/1)100% (3/3)100% (1/1)
access$200 (EC2TemplateBuilderImpl): String 100% (1/1)100% (3/3)100% (1/1)
access$300 (EC2TemplateBuilderImpl): Supplier 100% (1/1)100% (3/3)100% (1/1)
getImages (): Set 100% (1/1)100% (16/16)100% (4/4)

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 */
19package org.jclouds.ec2.compute.internal;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22 
23import java.util.NoSuchElementException;
24import java.util.Set;
25import java.util.concurrent.ExecutionException;
26 
27import javax.inject.Inject;
28import javax.inject.Named;
29import javax.inject.Provider;
30 
31import org.jclouds.collect.Memoized;
32import org.jclouds.compute.domain.Hardware;
33import org.jclouds.compute.domain.Image;
34import org.jclouds.compute.domain.TemplateBuilder;
35import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
36import org.jclouds.compute.options.TemplateOptions;
37import org.jclouds.domain.Location;
38import org.jclouds.ec2.compute.domain.RegionAndName;
39import org.jclouds.util.Throwables2;
40 
41import com.google.common.base.Supplier;
42import com.google.common.cache.CacheLoader;
43import com.google.common.cache.LoadingCache;
44import com.google.common.collect.ImmutableSet;
45import com.google.common.util.concurrent.UncheckedExecutionException;
46 
47/**
48 * 
49 * @author Adrian Cole
50 */
51public 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}

[all classes][org.jclouds.ec2.compute.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov