| 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.annotation.Resource; |
| 22 | import javax.inject.Inject; |
| 23 | |
| 24 | import org.jclouds.aws.ec2.domain.LaunchSpecification; |
| 25 | import org.jclouds.aws.ec2.domain.LaunchSpecification.Builder; |
| 26 | import org.jclouds.date.DateService; |
| 27 | import org.jclouds.ec2.domain.BlockDeviceMapping; |
| 28 | import org.jclouds.http.functions.ParseSax.HandlerForGeneratedRequestWithResult; |
| 29 | import org.jclouds.location.Region; |
| 30 | import org.jclouds.logging.Logger; |
| 31 | import org.xml.sax.Attributes; |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | * @author Adrian Cole |
| 36 | */ |
| 37 | public class LaunchSpecificationHandler extends HandlerForGeneratedRequestWithResult<LaunchSpecification> { |
| 38 | |
| 39 | @Resource |
| 40 | protected Logger logger = Logger.NULL; |
| 41 | |
| 42 | protected final DateService dateService; |
| 43 | protected final String defaultRegion; |
| 44 | protected final Builder builder; |
| 45 | protected final BlockDeviceMapping.Builder blockDeviceMappingBuilder; |
| 46 | |
| 47 | @Inject |
| 48 | public LaunchSpecificationHandler(DateService dateService, @Region String defaultRegion, |
| 49 | LaunchSpecification.Builder builder, BlockDeviceMapping.Builder blockDeviceMappingBuilder) { |
| 50 | this.dateService = dateService; |
| 51 | this.defaultRegion = defaultRegion; |
| 52 | this.builder = builder; |
| 53 | this.blockDeviceMappingBuilder = blockDeviceMappingBuilder; |
| 54 | } |
| 55 | |
| 56 | protected String currentOrNull() { |
| 57 | String returnVal = currentText.toString().trim(); |
| 58 | return returnVal.equals("") ? null : returnVal; |
| 59 | } |
| 60 | |
| 61 | protected StringBuilder currentText = new StringBuilder(); |
| 62 | |
| 63 | private boolean inBlockDeviceMapping; |
| 64 | |
| 65 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 66 | if (qName.equals("blockDeviceMapping")) { |
| 67 | inBlockDeviceMapping = true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public void endElement(String uri, String name, String qName) { |
| 72 | if (qName.equals("blockDeviceMapping")) { |
| 73 | inBlockDeviceMapping = false; |
| 74 | } else if (qName.equals("item") && inBlockDeviceMapping) { |
| 75 | try { |
| 76 | builder.blockDeviceMapping(blockDeviceMappingBuilder.build()); |
| 77 | } finally { |
| 78 | blockDeviceMappingBuilder.clear(); |
| 79 | } |
| 80 | } else if (qName.equals("deviceName")) { |
| 81 | blockDeviceMappingBuilder.deviceName(currentOrNull()); |
| 82 | } else if (qName.equals("virtualName")) { |
| 83 | blockDeviceMappingBuilder.virtualName(currentOrNull()); |
| 84 | } else if (qName.equals("snapshotId")) { |
| 85 | blockDeviceMappingBuilder.snapshotId(currentOrNull()); |
| 86 | } else if (qName.equals("volumeSize")) { |
| 87 | String volumeSize = currentOrNull(); |
| 88 | if (volumeSize != null) |
| 89 | blockDeviceMappingBuilder.sizeInGib(Integer.parseInt(volumeSize)); |
| 90 | } else if (qName.equals("noDevice")) { |
| 91 | String noDevice = currentOrNull(); |
| 92 | if (noDevice != null) |
| 93 | blockDeviceMappingBuilder.noDevice(Boolean.parseBoolean(noDevice)); |
| 94 | } else if (qName.equals("deleteOnTermination")) { |
| 95 | String deleteOnTermination = currentOrNull(); |
| 96 | if (deleteOnTermination != null) |
| 97 | blockDeviceMappingBuilder.deleteOnTermination(Boolean.parseBoolean(deleteOnTermination)); |
| 98 | } else if (qName.equals("groupId")) { |
| 99 | builder.groupId(currentOrNull()); |
| 100 | } else if (qName.equals("imageId")) { |
| 101 | builder.imageId(currentOrNull()); |
| 102 | } else if (qName.equals("instanceType")) { |
| 103 | builder.instanceType(currentOrNull()); |
| 104 | } else if (qName.equals("kernelId")) { |
| 105 | builder.kernelId(currentOrNull()); |
| 106 | } else if (qName.equals("keyName")) { |
| 107 | builder.keyName(currentOrNull()); |
| 108 | } else if (qName.equals("availabilityZone")) { |
| 109 | builder.availabilityZone(currentOrNull()); |
| 110 | } else if (qName.equals("ramdiskId")) { |
| 111 | builder.ramdiskId(currentOrNull()); |
| 112 | } else if (qName.equals("enabled")) { |
| 113 | String monitoringEnabled = currentOrNull(); |
| 114 | if (monitoringEnabled != null) |
| 115 | builder.monitoringEnabled(new Boolean(monitoringEnabled)); |
| 116 | } |
| 117 | currentText = new StringBuilder(); |
| 118 | } |
| 119 | |
| 120 | public void characters(char ch[], int start, int length) { |
| 121 | currentText.append(ch, start, length); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public LaunchSpecification getResult() { |
| 126 | try { |
| 127 | return builder.build(); |
| 128 | } finally { |
| 129 | builder.clear(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } |