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