EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.ec2.compute.suppliers]

COVERAGE SUMMARY FOR SOURCE FILE [RegionAndNameToImageSupplier.java]

nameclass, %method, %block, %line, %
RegionAndNameToImageSupplier.java0%   (0/2)0%   (0/6)0%   (0/146)0%   (0/27)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class RegionAndNameToImageSupplier0%   (0/1)0%   (0/4)0%   (0/131)0%   (0/26)
RegionAndNameToImageSupplier (Set, DescribeImagesParallel, String [], EC2Imag... 0%   (0/1)0%   (0/21)0%   (0/8)
get (): Cache 0%   (0/1)0%   (0/64)0%   (0/9)
getDescribeQueriesForOwnersInRegions (Set, String []): Iterable 0%   (0/1)0%   (0/26)0%   (0/5)
getOptionsForOwners (String []): DescribeImagesOptions 0%   (0/1)0%   (0/20)0%   (0/4)
     
class RegionAndNameToImageSupplier$10%   (0/1)0%   (0/2)0%   (0/15)0%   (0/2)
RegionAndNameToImageSupplier$1 (RegionAndNameToImageSupplier): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (Image): RegionAndName 0%   (0/1)0%   (0/9)0%   (0/1)

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.suppliers;
20 
21import static com.google.common.collect.Iterables.filter;
22import static com.google.common.collect.Iterables.transform;
23import static com.google.common.collect.Maps.uniqueIndex;
24import static org.jclouds.ec2.options.DescribeImagesOptions.Builder.ownedBy;
25import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS;
26 
27import java.util.Set;
28import java.util.Map.Entry;
29 
30import javax.annotation.Resource;
31import javax.inject.Inject;
32import javax.inject.Named;
33import javax.inject.Singleton;
34 
35import org.jclouds.compute.domain.Image;
36import org.jclouds.compute.reference.ComputeServiceConstants;
37import org.jclouds.ec2.compute.domain.RegionAndName;
38import org.jclouds.ec2.compute.functions.EC2ImageParser;
39import org.jclouds.ec2.compute.strategy.DescribeImagesParallel;
40import org.jclouds.ec2.options.DescribeImagesOptions;
41import org.jclouds.location.Region;
42import org.jclouds.logging.Logger;
43 
44import com.google.common.base.Function;
45import com.google.common.base.Predicates;
46import com.google.common.base.Supplier;
47import com.google.common.cache.Cache;
48import com.google.common.cache.CacheBuilder;
49import com.google.common.cache.CacheLoader;
50import com.google.common.collect.ImmutableMap;
51import com.google.common.collect.ImmutableSet;
52import com.google.common.collect.ImmutableMap.Builder;
53 
54/**
55 * 
56 * @author Adrian Cole
57 */
58@Singleton
59public class RegionAndNameToImageSupplier implements Supplier<Cache<RegionAndName, ? extends Image>> {
60   @Resource
61   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
62   protected Logger logger = Logger.NULL;
63 
64   private final Set<String> regions;
65   private final DescribeImagesParallel describer;
66   private final String[] amiOwners;
67   private final EC2ImageParser parser;
68   private final CacheLoader<RegionAndName, Image> regionAndIdToImage;
69 
70   @Inject
71   protected RegionAndNameToImageSupplier(@Region Set<String> regions, DescribeImagesParallel describer,
72         @Named(PROPERTY_EC2_AMI_OWNERS) String[] amiOwners, EC2ImageParser parser, CacheLoader<RegionAndName, Image> regionAndIdToImage) {
73      this.regions = regions;
74      this.describer = describer;
75      this.amiOwners = amiOwners;
76      this.parser = parser;
77      this.regionAndIdToImage = regionAndIdToImage;
78   }
79 
80   @Override
81   public Cache<RegionAndName, ? extends Image> get() {
82      Cache<RegionAndName, Image> cache = CacheBuilder.newBuilder().build(regionAndIdToImage);
83 
84      if (amiOwners.length == 0) {
85         logger.debug(">> no owners specified, skipping image parsing");
86      } else {
87         logger.debug(">> providing images");
88 
89         Iterable<Entry<String, DescribeImagesOptions>> queries = getDescribeQueriesForOwnersInRegions(regions,
90                  amiOwners);
91 
92         Iterable<? extends Image> parsedImages = ImmutableSet.copyOf(filter(transform(describer.apply(queries), parser), Predicates
93                  .notNull()));
94 
95         cache.asMap().putAll(uniqueIndex(parsedImages, new Function<Image, RegionAndName>() {
96 
97            @Override
98            public RegionAndName apply(Image from) {
99               return new RegionAndName(from.getLocation().getId(), from.getProviderId());
100            }
101 
102         }));
103         logger.debug("<< images(%d)",  cache.asMap().size());
104      }
105      return cache;
106   }
107 
108   public Iterable<Entry<String, DescribeImagesOptions>> getDescribeQueriesForOwnersInRegions(Set<String> regions,
109         String[] amiOwners) {
110      DescribeImagesOptions options = getOptionsForOwners(amiOwners);
111      Builder<String, DescribeImagesOptions> builder = ImmutableMap.<String, DescribeImagesOptions> builder();
112      for (String region : regions)
113         builder.put(region, options);
114      return builder.build().entrySet();
115   }
116 
117   public DescribeImagesOptions getOptionsForOwners(String... amiOwners) {
118      DescribeImagesOptions options;
119      if (amiOwners.length == 1 && amiOwners[0].equals("*"))
120         options = new DescribeImagesOptions();
121      else
122         options = ownedBy(amiOwners);
123      return options;
124   }
125}

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