| 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 | */ |
| 19 | package org.jclouds.ec2.xml; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | import javax.inject.Inject; |
| 25 | |
| 26 | import org.jclouds.ec2.domain.AvailabilityZoneInfo; |
| 27 | import org.jclouds.http.functions.ParseSax; |
| 28 | import org.jclouds.location.Region; |
| 29 | import org.jclouds.logging.Logger; |
| 30 | import org.xml.sax.Attributes; |
| 31 | |
| 32 | import com.google.common.base.Supplier; |
| 33 | import com.google.common.collect.Sets; |
| 34 | |
| 35 | /** |
| 36 | * |
| 37 | * @author Adrian Cole |
| 38 | */ |
| 39 | public class DescribeAvailabilityZonesResponseHandler extends ParseSax.HandlerWithResult<Set<AvailabilityZoneInfo>> { |
| 40 | private StringBuilder currentText = new StringBuilder(); |
| 41 | private final Supplier<String> defaultRegion; |
| 42 | |
| 43 | private Set<AvailabilityZoneInfo> availablilityZones = Sets.newLinkedHashSet(); |
| 44 | private String zone; |
| 45 | @Resource |
| 46 | protected Logger logger = Logger.NULL; |
| 47 | private String region; |
| 48 | private String zoneState; |
| 49 | private boolean inMessageSet; |
| 50 | private Set<String> messages = Sets.newHashSet(); |
| 51 | |
| 52 | /** |
| 53 | * Eucalyptus 1.6 doesn't return region in the XML output |
| 54 | */ |
| 55 | @Inject |
| 56 | DescribeAvailabilityZonesResponseHandler(@Region Supplier<String> defaultRegion) { |
| 57 | this.defaultRegion = defaultRegion; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void startDocument() { |
| 62 | region = defaultRegion.get(); |
| 63 | } |
| 64 | |
| 65 | public Set<AvailabilityZoneInfo> getResult() { |
| 66 | return availablilityZones; |
| 67 | } |
| 68 | |
| 69 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 70 | if (qName.equals("messageSet")) { |
| 71 | inMessageSet = true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public void endElement(String uri, String name, String qName) { |
| 76 | if (qName.equals("zoneName")) { |
| 77 | zone = currentText.toString().trim(); |
| 78 | } else if (qName.equals("regionName")) { |
| 79 | try { |
| 80 | region = currentText.toString().trim(); |
| 81 | } catch (IllegalArgumentException e) { |
| 82 | logger.warn(e, "unsupported region: %s", currentText.toString().trim()); |
| 83 | region = "UNKNOWN"; |
| 84 | } |
| 85 | } else if (qName.equals("zoneState")) { |
| 86 | zoneState = currentText.toString().trim(); |
| 87 | } else if (qName.equals("message")) { |
| 88 | messages.add(currentText.toString().trim()); |
| 89 | } else if (qName.equals("messageSet")) { |
| 90 | inMessageSet = false; |
| 91 | } else if (qName.equals("item") && !inMessageSet) { |
| 92 | availablilityZones.add(new AvailabilityZoneInfo(zone, zoneState, region, messages)); |
| 93 | this.zone = null; |
| 94 | this.region = defaultRegion.get(); |
| 95 | this.zoneState = null; |
| 96 | this.messages = Sets.newHashSet(); |
| 97 | } |
| 98 | currentText = new StringBuilder(); |
| 99 | } |
| 100 | |
| 101 | public void characters(char ch[], int start, int length) { |
| 102 | currentText.append(ch, start, length); |
| 103 | } |
| 104 | |
| 105 | } |