EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[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)72%  (130/180)69%  (20/29)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EC2TemplateBuilderImpl$1100% (1/1)100% (2/2)66%  (74/112)69%  (9/13)
get (): Image 100% (1/1)64%  (68/106)67%  (8/12)
EC2TemplateBuilderImpl$1 (EC2TemplateBuilderImpl): void 100% (1/1)100% (6/6)100% (1/1)
     
class EC2TemplateBuilderImpl100% (1/1)100% (7/7)82%  (56/68)71%  (12/17)
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% (21/21)100% (6/6)

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;
39 
40import com.google.common.base.Supplier;
41import com.google.common.cache.Cache;
42import com.google.common.util.concurrent.UncheckedExecutionException;
43 
44/**
45 * 
46 * @author Adrian Cole
47 */
48public 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}

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