| 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.aws.ec2.xml; |
| 20 | |
| 21 | import static org.jclouds.util.SaxUtils.*; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | |
| 25 | import org.jclouds.aws.ec2.domain.SpotInstanceRequest; |
| 26 | import org.jclouds.aws.ec2.domain.SpotInstanceRequest.Builder; |
| 27 | import org.jclouds.aws.util.AWSUtils; |
| 28 | import org.jclouds.date.DateService; |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 30 | import org.jclouds.location.Region; |
| 31 | import org.xml.sax.Attributes; |
| 32 | import org.xml.sax.SAXException; |
| 33 | |
| 34 | /** |
| 35 | * |
| 36 | * @author Adrian Cole |
| 37 | */ |
| 38 | public class SpotInstanceHandler extends ParseSax.HandlerForGeneratedRequestWithResult<SpotInstanceRequest> { |
| 39 | private StringBuilder currentText = new StringBuilder(); |
| 40 | |
| 41 | protected final DateService dateService; |
| 42 | protected final String defaultRegion; |
| 43 | protected final Builder builder; |
| 44 | protected boolean inLaunchSpecification; |
| 45 | protected final LaunchSpecificationHandler launchSpecificationHandler; |
| 46 | protected boolean inTagSet; |
| 47 | protected final TagSetHandler tagSetHandler; |
| 48 | |
| 49 | @Inject |
| 50 | public SpotInstanceHandler(DateService dateService, @Region String defaultRegion, |
| 51 | LaunchSpecificationHandler launchSpecificationHandler, TagSetHandler tagSetHandler, |
| 52 | SpotInstanceRequest.Builder builder) { |
| 53 | this.dateService = dateService; |
| 54 | this.defaultRegion = defaultRegion; |
| 55 | this.launchSpecificationHandler = launchSpecificationHandler; |
| 56 | this.tagSetHandler = tagSetHandler; |
| 57 | this.builder = builder; |
| 58 | } |
| 59 | |
| 60 | public SpotInstanceRequest getResult() { |
| 61 | try { |
| 62 | String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; |
| 63 | if (region == null) |
| 64 | region = defaultRegion; |
| 65 | return builder.region(region).build(); |
| 66 | } finally { |
| 67 | builder.clear(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void startElement(String uri, String name, String qName, Attributes attrs) throws SAXException { |
| 73 | if (equalsOrSuffix(qName, "launchSpecification")) { |
| 74 | inLaunchSpecification = true; |
| 75 | } else if (equalsOrSuffix(qName, "tagSet")) { |
| 76 | inTagSet = true; |
| 77 | } |
| 78 | if (inLaunchSpecification) { |
| 79 | launchSpecificationHandler.startElement(uri, name, qName, attrs); |
| 80 | } else if (inTagSet) { |
| 81 | tagSetHandler.startElement(uri, name, qName, attrs); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void endElement(String uri, String name, String qName) throws SAXException { |
| 87 | if (equalsOrSuffix(qName, "tagSet")) { |
| 88 | inTagSet = false; |
| 89 | builder.tags(tagSetHandler.getResult()); |
| 90 | } else if (inTagSet) { |
| 91 | tagSetHandler.endElement(uri, name, qName); |
| 92 | } |
| 93 | |
| 94 | if (qName.equals("launchSpecification")) { |
| 95 | inLaunchSpecification = false; |
| 96 | builder.launchSpecification(launchSpecificationHandler.getResult()); |
| 97 | } else if (inLaunchSpecification) { |
| 98 | launchSpecificationHandler.endElement(uri, name, qName); |
| 99 | } |
| 100 | |
| 101 | if (qName.equals("spotInstanceRequestId")) { |
| 102 | builder.id(currentOrNull(currentText)); |
| 103 | } else if (qName.equals("instanceId")) { |
| 104 | builder.instanceId(currentOrNull(currentText)); |
| 105 | } else if (qName.equals("launchedAvailabilityZone")) { |
| 106 | builder.launchedAvailabilityZone(currentOrNull(currentText)); |
| 107 | } else if (qName.equals("availabilityZoneGroup")) { |
| 108 | builder.availabilityZoneGroup(currentOrNull(currentText)); |
| 109 | } else if (qName.equals("launchGroup")) { |
| 110 | builder.launchGroup(currentOrNull(currentText)); |
| 111 | } else if (qName.equals("code")) { |
| 112 | builder.faultCode(currentOrNull(currentText)); |
| 113 | } else if (qName.equals("message")) { |
| 114 | builder.faultMessage(currentOrNull(currentText)); |
| 115 | } else if (qName.equals("spotPrice")) { |
| 116 | String price = currentOrNull(currentText); |
| 117 | if (price != null) |
| 118 | builder.spotPrice(Float.parseFloat(price)); |
| 119 | } else if (qName.equals("type")) { |
| 120 | String type = currentOrNull(currentText); |
| 121 | if (type != null) |
| 122 | builder.type(SpotInstanceRequest.Type.fromValue(type)); |
| 123 | } else if (qName.equals("state")) { |
| 124 | String state = currentOrNull(currentText); |
| 125 | if (state != null) |
| 126 | builder.state(SpotInstanceRequest.State.fromValue(state)); |
| 127 | } else if (qName.equals("createTime")) { |
| 128 | String createTime = currentOrNull(currentText); |
| 129 | if (createTime != null) |
| 130 | builder.createTime(dateService.iso8601DateParse(createTime)); |
| 131 | } else if (qName.equals("productDescription")) { |
| 132 | builder.productDescription(currentOrNull(currentText)); |
| 133 | } |
| 134 | currentText = new StringBuilder(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void characters(char ch[], int start, int length) { |
| 139 | if (inLaunchSpecification) { |
| 140 | launchSpecificationHandler.characters(ch, start, length); |
| 141 | } else if (inTagSet) { |
| 142 | tagSetHandler.characters(ch, start, length); |
| 143 | } else { |
| 144 | currentText.append(ch, start, length); |
| 145 | } |
| 146 | } |
| 147 | } |