| 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 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 | private String groupId; |
| 66 | |
| 67 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 68 | if (qName.equals("blockDeviceMapping")) { |
| 69 | inBlockDeviceMapping = true; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public void endElement(String uri, String name, String qName) { |
| 74 | if (qName.equals("blockDeviceMapping")) { |
| 75 | inBlockDeviceMapping = false; |
| 76 | } else if (qName.equals("item") && inBlockDeviceMapping) { |
| 77 | try { |
| 78 | builder.blockDeviceMapping(blockDeviceMappingBuilder.build()); |
| 79 | } finally { |
| 80 | blockDeviceMappingBuilder.clear(); |
| 81 | } |
| 82 | } else if (qName.equals("deviceName")) { |
| 83 | blockDeviceMappingBuilder.deviceName(currentOrNull()); |
| 84 | } else if (qName.equals("virtualName")) { |
| 85 | blockDeviceMappingBuilder.virtualName(currentOrNull()); |
| 86 | } else if (qName.equals("snapshotId")) { |
| 87 | blockDeviceMappingBuilder.snapshotId(currentOrNull()); |
| 88 | } else if (qName.equals("volumeSize")) { |
| 89 | String volumeSize = currentOrNull(); |
| 90 | if (volumeSize != null) |
| 91 | blockDeviceMappingBuilder.sizeInGib(Integer.parseInt(volumeSize)); |
| 92 | } else if (qName.equals("noDevice")) { |
| 93 | String noDevice = currentOrNull(); |
| 94 | if (noDevice != null) |
| 95 | blockDeviceMappingBuilder.noDevice(Boolean.parseBoolean(noDevice)); |
| 96 | } else if (qName.equals("deleteOnTermination")) { |
| 97 | String deleteOnTermination = currentOrNull(); |
| 98 | if (deleteOnTermination != null) |
| 99 | blockDeviceMappingBuilder.deleteOnTermination(Boolean.parseBoolean(deleteOnTermination)); |
| 100 | } else if (qName.equals("groupId")) { |
| 101 | groupId = currentOrNull(); |
| 102 | } else if (qName.equals("groupName")) { |
| 103 | builder.securityGroupIdToName(groupId, currentOrNull()); |
| 104 | groupId = null; |
| 105 | } else if (qName.equals("imageId")) { |
| 106 | builder.imageId(currentOrNull()); |
| 107 | } else if (qName.equals("instanceType")) { |
| 108 | builder.instanceType(currentOrNull()); |
| 109 | } else if (qName.equals("kernelId")) { |
| 110 | builder.kernelId(currentOrNull()); |
| 111 | } else if (qName.equals("keyName")) { |
| 112 | builder.keyName(currentOrNull()); |
| 113 | } else if (qName.equals("availabilityZone")) { |
| 114 | builder.availabilityZone(currentOrNull()); |
| 115 | } else if (qName.equals("ramdiskId")) { |
| 116 | builder.ramdiskId(currentOrNull()); |
| 117 | } else if (qName.equals("enabled")) { |
| 118 | String monitoringEnabled = currentOrNull(); |
| 119 | if (monitoringEnabled != null) |
| 120 | builder.monitoringEnabled(new Boolean(monitoringEnabled)); |
| 121 | } |
| 122 | currentText = new StringBuilder(); |
| 123 | } |
| 124 | |
| 125 | public void characters(char ch[], int start, int length) { |
| 126 | currentText.append(ch, start, length); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public LaunchSpecification getResult() { |
| 131 | try { |
| 132 | return builder.build(); |
| 133 | } finally { |
| 134 | builder.clear(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | } |