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

COVERAGE SUMMARY FOR SOURCE FILE [AWSEC2ReviseParsedImage.java]

nameclass, %method, %block, %line, %
AWSEC2ReviseParsedImage.java100% (1/1)100% (4/4)84%  (154/184)82%  (27.1/33)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AWSEC2ReviseParsedImage100% (1/1)100% (4/4)84%  (154/184)82%  (27.1/33)
reviseParsedImage (Image, ImageBuilder, OsFamily, OperatingSystem$Builder): void 100% (1/1)76%  (80/105)74%  (14.1/19)
getMatcherAndFind (String): Matcher 100% (1/1)90%  (46/51)80%  (4/5)
<static initializer> 100% (1/1)100% (16/16)100% (5/5)
AWSEC2ReviseParsedImage (Map): void 100% (1/1)100% (12/12)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.aws.ec2.compute.strategy;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.Map;
24import java.util.NoSuchElementException;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27 
28import javax.annotation.Resource;
29import javax.inject.Inject;
30import javax.inject.Singleton;
31 
32import org.jclouds.compute.domain.ImageBuilder;
33import org.jclouds.compute.domain.OperatingSystem;
34import org.jclouds.compute.domain.OsFamily;
35import org.jclouds.compute.util.ComputeServiceUtils;
36import org.jclouds.ec2.compute.strategy.ReviseParsedImage;
37import org.jclouds.logging.Logger;
38 
39/**
40 * @author Adrian Cole
41 */
42@Singleton
43public class AWSEC2ReviseParsedImage implements ReviseParsedImage {
44 
45   // 137112412989/amzn-ami-0.9.7-beta.i386-ebs
46   // 137112412989/amzn-ami-0.9.7-beta.x86_64-ebs
47   // amzn-ami-us-east-1/amzn-ami-0.9.7-beta.x86_64.manifest.xml
48   // amzn-ami-us-east-1/amzn-ami-0.9.7-beta.i386.manifest.xml
49   public static final Pattern AMZN_PATTERN = Pattern
50            .compile(".*/(amzn-hvm-|amzn-)?ami-(.*)\\.(i386|x86_64)(-ebs|\\.manifest.xml)?");
51 
52   // amazon/EC2 CentOS 5.4 HVM AMI
53   public static final Pattern AMAZON_PATTERN = Pattern.compile("amazon/EC2 ([^ ]+) ([^ ]+).*");
54 
55   public static final Pattern CANONICAL_PATTERN = Pattern.compile(".*/([^-]*)-([^-]*)-.*-(.*)(\\.manifest.xml)?");
56 
57   // ex rightscale-us-east/CentOS_5.4_x64_v4.4.10.manifest.xml
58   public static final Pattern RIGHTSCALE_PATTERN = Pattern
59            .compile("[^/]*/([^_]*)_([^_]*)_[^vV]*[vV](.*)(\\.manifest.xml)?");
60 
61   // ex 411009282317/RightImage_Ubuntu_9.10_x64_v4.5.3_EBS_Alpha
62   // 411009282317/RightImage_Windows_2008_x64_v5.5.5
63   public static final Pattern RIGHTIMAGE_PATTERN = Pattern
64            .compile("[^/]*/RightImage[_ ]([^_]*)_([^_]*)_[^vV]*[vV](.*)(\\.manifest.xml)?");
65   private final Map<OsFamily, Map<String, String>> osVersionMap;
66 
67   @Resource
68   protected Logger logger = Logger.NULL;
69 
70   @Inject
71   public AWSEC2ReviseParsedImage(Map<OsFamily, Map<String, String>> osVersionMap) {
72      this.osVersionMap = checkNotNull(osVersionMap, "osVersionMap");
73   }
74 
75   @Override
76   public void reviseParsedImage(org.jclouds.ec2.domain.Image from, ImageBuilder builder, OsFamily family,
77            OperatingSystem.Builder osBuilder) {
78      try {
79         Matcher matcher = getMatcherAndFind(from.getImageLocation());
80         if (matcher.pattern() == AMZN_PATTERN) {
81            osBuilder.family(OsFamily.AMZN_LINUX);
82            osBuilder.version(matcher.group(2));
83            builder.version(matcher.group(2));
84         } else if (matcher.pattern() == AMAZON_PATTERN) {
85            family = OsFamily.fromValue(matcher.group(1));
86            osBuilder.family(family);
87            osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, matcher.group(2),
88                     osVersionMap));
89         } else {
90            family = OsFamily.fromValue(matcher.group(1));
91            osBuilder.family(family);
92            osBuilder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, matcher.group(2),
93                     osVersionMap));
94            builder.version(matcher.group(3).replace(".manifest.xml", ""));
95         }
96      } catch (IllegalArgumentException e) {
97         logger.debug("<< didn't match os(%s)", from.getImageLocation());
98      } catch (NoSuchElementException e) {
99         logger.trace("<< didn't match at all(%s)", from.getImageLocation());
100      }
101   }
102 
103   /**
104    * 
105    * @throws NoSuchElementException
106    *            if no configured matcher matches the manifest.
107    */
108   private Matcher getMatcherAndFind(String manifest) {
109      for (Pattern pattern : new Pattern[] { AMZN_PATTERN, AMAZON_PATTERN, CANONICAL_PATTERN, RIGHTIMAGE_PATTERN,
110               RIGHTSCALE_PATTERN }) {
111         Matcher matcher = pattern.matcher(manifest);
112         if (matcher.find())
113            return matcher;
114      }
115      throw new NoSuchElementException(manifest);
116   }
117}

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