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

COVERAGE SUMMARY FOR SOURCE FILE [DescribeImagesResponseHandler.java]

nameclass, %method, %block, %line, %
DescribeImagesResponseHandler.java100% (1/1)100% (5/5)95%  (470/495)97%  (97.6/101)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DescribeImagesResponseHandler100% (1/1)100% (5/5)95%  (470/495)97%  (97.6/101)
endElement (String, String, String): void 100% (1/1)94%  (408/433)96%  (77.6/81)
DescribeImagesResponseHandler (String): void 100% (1/1)100% (35/35)100% (12/12)
characters (char [], int, int): void 100% (1/1)100% (8/8)100% (2/2)
getResult (): Set 100% (1/1)100% (3/3)100% (1/1)
startElement (String, String, String, Attributes): void 100% (1/1)100% (16/16)100% (5/5)

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.xml;
20 
21import java.util.Map;
22import java.util.Set;
23 
24import javax.annotation.Resource;
25import javax.inject.Inject;
26 
27import org.jclouds.aws.util.AWSUtils;
28import org.jclouds.ec2.domain.Hypervisor;
29import org.jclouds.ec2.domain.Image;
30import org.jclouds.ec2.domain.RootDeviceType;
31import org.jclouds.ec2.domain.VirtualizationType;
32import org.jclouds.ec2.domain.Image.Architecture;
33import org.jclouds.ec2.domain.Image.EbsBlockDevice;
34import org.jclouds.ec2.domain.Image.ImageState;
35import org.jclouds.ec2.domain.Image.ImageType;
36import org.jclouds.http.functions.ParseSax;
37import org.jclouds.location.Region;
38import org.jclouds.logging.Logger;
39import org.xml.sax.Attributes;
40 
41import com.google.common.collect.Maps;
42import com.google.common.collect.Sets;
43 
44/**
45 * Parses the following XML document:
46 * <p/>
47 * DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2010-06-15/"
48 * 
49 * @author Adrian Cole
50 * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeImages.html"
51 *      />
52 */
53public class DescribeImagesResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Set<Image>> {
54 
55   @Inject
56   public DescribeImagesResponseHandler(@Region String defaultRegion) {
57      this.defaultRegion = defaultRegion;
58   }
59 
60   @Resource
61   protected Logger logger = Logger.NULL;
62 
63   private Set<Image> contents = Sets.newLinkedHashSet();
64   private StringBuilder currentText = new StringBuilder();
65   private final String defaultRegion;
66 
67   private Architecture architecture;
68   private String name;
69   private String description;
70   private String imageId;
71   private String imageLocation;
72   private String imageOwnerId;
73   private ImageState imageState;
74   private ImageType imageType;
75   private boolean isPublic;
76   private String kernelId;
77   private String platform;
78   private Set<String> productCodes = Sets.newHashSet();
79   private String ramdiskId;
80   private boolean inProductCodes;
81   private boolean inBlockDeviceMapping;
82   /**
83    * Eucalyptus 1.6 doesn't set rootDeviceType
84    */
85   private RootDeviceType rootDeviceType = RootDeviceType.INSTANCE_STORE;
86   private Map<String, EbsBlockDevice> ebsBlockDevices = Maps.newHashMap();
87   private String deviceName;
88   private String snapshotId;
89   private VirtualizationType virtualizationType = VirtualizationType.PARAVIRTUAL;
90   private Hypervisor hypervisor = Hypervisor.XEN;
91 
92   private int volumeSize;
93   private boolean deleteOnTermination = true;// correct default is true.
94 
95   private String rootDeviceName;
96 
97   public Set<Image> getResult() {
98      return contents;
99   }
100 
101   public void startElement(String uri, String name, String qName, Attributes attrs) {
102      if (qName.equals("productCodes")) {
103         inProductCodes = true;
104      } else if (qName.equals("blockDeviceMapping")) {
105         inBlockDeviceMapping = true;
106      }
107   }
108 
109   public void endElement(String uri, String name, String qName) {
110      if (qName.equals("architecture")) {
111         architecture = Architecture.fromValue(currentText.toString().trim());
112      } else if (qName.equals("name")) {
113         this.name = currentText.toString().trim();
114      } else if (qName.equals("description")) {
115         description = currentText.toString().trim();
116      } else if (qName.equals("imageId")) {
117         imageId = currentText.toString().trim();
118      } else if (qName.equals("deviceName")) {
119         deviceName = currentText.toString().trim();
120      } else if (qName.equals("imageLocation")) {
121         imageLocation = currentText.toString().trim();
122      } else if (qName.equals("imageOwnerId")) {
123         imageOwnerId = currentText.toString().trim();
124      } else if (qName.equals("imageState")) {
125         imageState = ImageState.fromValue(currentText.toString().trim());
126         // eucalyptus
127      } else if (qName.equals("imageType") || qName.equals("type")) {
128         imageType = ImageType.fromValue(currentText.toString().trim());
129      } else if (qName.equals("isPublic")) {
130         isPublic = Boolean.parseBoolean(currentText.toString().trim());
131      } else if (qName.equals("kernelId")) {
132         kernelId = currentText.toString().trim();
133      } else if (qName.equals("platform")) {
134         platform = currentText.toString().trim();
135      } else if (qName.equals("productCode")) {
136         productCodes.add(currentText.toString().trim());
137      } else if (qName.equals("productCodes")) {
138         inProductCodes = false;
139      } else if (qName.equals("blockDeviceMapping")) {
140         inBlockDeviceMapping = false;
141      } else if (qName.equals("snapshotId")) {
142         snapshotId = currentText.toString().trim();
143      } else if (qName.equals("volumeSize")) {
144         volumeSize = Integer.parseInt(currentText.toString().trim());
145      } else if (qName.equals("deleteOnTermination")) {
146         deleteOnTermination = Boolean.parseBoolean(currentText.toString().trim());
147      } else if (qName.equals("ramdiskId")) {
148         ramdiskId = currentText.toString().trim();
149      } else if (qName.equals("rootDeviceType")) {
150         rootDeviceType = RootDeviceType.fromValue(currentText.toString().trim());
151      } else if (qName.equals("rootDeviceName")) {
152         rootDeviceName = currentText.toString().trim();
153      } else if (qName.equals("virtualizationType")) {
154         virtualizationType = VirtualizationType.fromValue(currentText.toString().trim());
155      } else if (qName.equals("hypervisor")) {
156         hypervisor = Hypervisor.fromValue(currentText.toString().trim());
157      } else if (qName.equals("item")) {
158         if (inBlockDeviceMapping) {
159            ebsBlockDevices.put(deviceName, new Image.EbsBlockDevice(snapshotId, volumeSize, deleteOnTermination));
160            this.deviceName = null;
161            this.snapshotId = null;
162            this.volumeSize = 0;
163            this.deleteOnTermination = true;
164         } else if (!inProductCodes) {
165            try {
166               String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null;
167               if (region == null)
168                  region = defaultRegion;
169               contents.add(new Image(region, architecture, this.name, description, imageId, imageLocation,
170                        imageOwnerId, imageState, imageType, isPublic, productCodes, kernelId, platform, ramdiskId,
171                        rootDeviceType, rootDeviceName, ebsBlockDevices, virtualizationType, hypervisor));
172            } catch (NullPointerException e) {
173               logger.warn(e, "malformed image: %s", imageId);
174            }
175            this.name = null;
176            this.description = null;
177            this.architecture = null;
178            this.imageId = null;
179            this.imageLocation = null;
180            this.imageOwnerId = null;
181            this.imageState = null;
182            this.imageType = null;
183            this.isPublic = false;
184            this.kernelId = null;
185            this.platform = null;
186            this.productCodes = Sets.newHashSet();
187            this.ramdiskId = null;
188            this.rootDeviceType = RootDeviceType.INSTANCE_STORE;
189            this.rootDeviceName = null;
190            this.ebsBlockDevices = Maps.newHashMap();
191            this.virtualizationType = VirtualizationType.PARAVIRTUAL;
192            this.hypervisor = Hypervisor.XEN;
193         }
194 
195      }
196      currentText = new StringBuilder();
197   }
198 
199   public void characters(char ch[], int start, int length) {
200      currentText.append(ch, start, length);
201   }
202}

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