EMMA Coverage Report (generated Wed Jun 22 19:47:49 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)96%  (458/475)98%  (94.6/97)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DescribeImagesResponseHandler100% (1/1)100% (5/5)96%  (458/475)98%  (94.6/97)
endElement (String, String, String): void 100% (1/1)96%  (399/416)97%  (75.6/78)
DescribeImagesResponseHandler (String): void 100% (1/1)100% (32/32)100% (11/11)
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 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
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.Image;
29import org.jclouds.ec2.domain.RootDeviceType;
30import org.jclouds.ec2.domain.VirtualizationType;
31import org.jclouds.ec2.domain.Image.Architecture;
32import org.jclouds.ec2.domain.Image.EbsBlockDevice;
33import org.jclouds.ec2.domain.Image.ImageState;
34import org.jclouds.ec2.domain.Image.ImageType;
35import org.jclouds.http.functions.ParseSax;
36import org.jclouds.location.Region;
37import org.jclouds.logging.Logger;
38import org.xml.sax.Attributes;
39 
40import com.google.common.collect.Maps;
41import com.google.common.collect.Sets;
42 
43/**
44 * Parses the following XML document:
45 * <p/>
46 * DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2010-06-15/"
47 * 
48 * @author Adrian Cole
49 * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeImages.html"
50 *      />
51 */
52public class DescribeImagesResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Set<Image>> {
53 
54   @Inject
55   public DescribeImagesResponseHandler(@Region String defaultRegion) {
56      this.defaultRegion = defaultRegion;
57   }
58 
59   @Resource
60   protected Logger logger = Logger.NULL;
61 
62   private Set<Image> contents = Sets.newLinkedHashSet();
63   private StringBuilder currentText = new StringBuilder();
64   private final String defaultRegion;
65 
66   private Architecture architecture;
67   private String name;
68   private String description;
69   private String imageId;
70   private String imageLocation;
71   private String imageOwnerId;
72   private ImageState imageState;
73   private ImageType imageType;
74   private boolean isPublic;
75   private String kernelId;
76   private String platform;
77   private Set<String> productCodes = Sets.newHashSet();
78   private String ramdiskId;
79   private boolean inProductCodes;
80   private boolean inBlockDeviceMapping;
81   /**
82    * Eucalyptus 1.6 doesn't set rootDeviceType
83    */
84   private RootDeviceType rootDeviceType = RootDeviceType.INSTANCE_STORE;
85   private Map<String, EbsBlockDevice> ebsBlockDevices = Maps.newHashMap();
86   private String deviceName;
87   private String snapshotId;
88   private VirtualizationType virtualizationType = VirtualizationType.PARAVIRTUAL;
89 
90   private int volumeSize;
91   private boolean deleteOnTermination = true;// correct default is true.
92 
93   private String rootDeviceName;
94 
95   public Set<Image> getResult() {
96      return contents;
97   }
98 
99   public void startElement(String uri, String name, String qName, Attributes attrs) {
100      if (qName.equals("productCodes")) {
101         inProductCodes = true;
102      } else if (qName.equals("blockDeviceMapping")) {
103         inBlockDeviceMapping = true;
104      }
105   }
106 
107   public void endElement(String uri, String name, String qName) {
108      if (qName.equals("architecture")) {
109         architecture = Architecture.fromValue(currentText.toString().trim());
110      } else if (qName.equals("name")) {
111         this.name = currentText.toString().trim();
112      } else if (qName.equals("description")) {
113         description = currentText.toString().trim();
114      } else if (qName.equals("imageId")) {
115         imageId = currentText.toString().trim();
116      } else if (qName.equals("deviceName")) {
117         deviceName = currentText.toString().trim();
118      } else if (qName.equals("imageLocation")) {
119         imageLocation = currentText.toString().trim();
120      } else if (qName.equals("imageOwnerId")) {
121         imageOwnerId = currentText.toString().trim();
122      } else if (qName.equals("imageState")) {
123         imageState = ImageState.fromValue(currentText.toString().trim());
124         // eucalyptus
125      } else if (qName.equals("imageType") || qName.equals("type")) {
126         imageType = ImageType.fromValue(currentText.toString().trim());
127      } else if (qName.equals("isPublic")) {
128         isPublic = Boolean.parseBoolean(currentText.toString().trim());
129      } else if (qName.equals("kernelId")) {
130         kernelId = currentText.toString().trim();
131      } else if (qName.equals("platform")) {
132         platform = currentText.toString().trim();
133      } else if (qName.equals("productCode")) {
134         productCodes.add(currentText.toString().trim());
135      } else if (qName.equals("productCodes")) {
136         inProductCodes = false;
137      } else if (qName.equals("blockDeviceMapping")) {
138         inBlockDeviceMapping = false;
139      } else if (qName.equals("snapshotId")) {
140         snapshotId = currentText.toString().trim();
141      } else if (qName.equals("volumeSize")) {
142         volumeSize = Integer.parseInt(currentText.toString().trim());
143      } else if (qName.equals("deleteOnTermination")) {
144         deleteOnTermination = Boolean.parseBoolean(currentText.toString().trim());
145      } else if (qName.equals("ramdiskId")) {
146         ramdiskId = currentText.toString().trim();
147      } else if (qName.equals("rootDeviceType")) {
148         rootDeviceType = RootDeviceType.fromValue(currentText.toString().trim());
149      } else if (qName.equals("rootDeviceName")) {
150         rootDeviceName = currentText.toString().trim();
151      } else if (qName.equals("virtualizationType")) {
152         virtualizationType = VirtualizationType.fromValue(currentText.toString().trim());
153      } else if (qName.equals("item")) {
154         if (inBlockDeviceMapping) {
155            ebsBlockDevices.put(deviceName, new Image.EbsBlockDevice(snapshotId, volumeSize, deleteOnTermination));
156            this.deviceName = null;
157            this.snapshotId = null;
158            this.volumeSize = 0;
159            this.deleteOnTermination = true;
160         } else if (!inProductCodes) {
161            try {
162               String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null;
163               if (region == null)
164                  region = defaultRegion;
165               contents.add(new Image(region, architecture, this.name, description, imageId, imageLocation,
166                        imageOwnerId, imageState, imageType, isPublic, productCodes, kernelId, platform, ramdiskId,
167                        rootDeviceType, rootDeviceName, ebsBlockDevices, virtualizationType));
168            } catch (NullPointerException e) {
169               logger.warn(e, "malformed image: %s", imageId);
170            }
171            this.name = null;
172            this.description = null;
173            this.architecture = null;
174            this.imageId = null;
175            this.imageLocation = null;
176            this.imageOwnerId = null;
177            this.imageState = null;
178            this.imageType = null;
179            this.isPublic = false;
180            this.kernelId = null;
181            this.platform = null;
182            this.productCodes = Sets.newHashSet();
183            this.ramdiskId = null;
184            this.rootDeviceType = RootDeviceType.INSTANCE_STORE;
185            this.rootDeviceName = null;
186            this.ebsBlockDevices = Maps.newHashMap();
187            this.virtualizationType = VirtualizationType.PARAVIRTUAL;
188         }
189 
190      }
191      currentText = new StringBuilder();
192   }
193 
194   public void characters(char ch[], int start, int length) {
195      currentText.append(ch, start, length);
196   }
197}

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