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

COVERAGE SUMMARY FOR SOURCE FILE [EC2ImageSupplier.java]

nameclass, %method, %block, %line, %
EC2ImageSupplier.java100% (1/1)25%  (1/4)15%  (21/136)29%  (8/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EC2ImageSupplier100% (1/1)25%  (1/4)15%  (21/136)29%  (8/28)
get (): Set 0%   (0/1)0%   (0/69)0%   (0/11)
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)
EC2ImageSupplier (Supplier, DescribeImagesParallel, String [], Supplier, EC2I... 100% (1/1)100% (21/21)100% (8/8)

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

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