| 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.deltacloud.xml; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | |
| 25 | import org.jclouds.deltacloud.domain.Transition; |
| 26 | import org.jclouds.deltacloud.domain.TransitionAutomatically; |
| 27 | import org.jclouds.deltacloud.domain.TransitionOnAction; |
| 28 | import org.jclouds.deltacloud.domain.Instance.Action; |
| 29 | import org.jclouds.deltacloud.domain.Instance.State; |
| 30 | import org.jclouds.http.functions.ParseSax; |
| 31 | import org.jclouds.logging.Logger; |
| 32 | import org.jclouds.util.SaxUtils; |
| 33 | import org.xml.sax.Attributes; |
| 34 | import org.xml.sax.SAXException; |
| 35 | |
| 36 | import com.google.common.collect.LinkedHashMultimap; |
| 37 | import com.google.common.collect.Multimap; |
| 38 | |
| 39 | /** |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | public class InstanceStatesHandler extends ParseSax.HandlerWithResult<Multimap<State, ? extends Transition>> { |
| 43 | |
| 44 | @Resource |
| 45 | protected Logger logger = Logger.NULL; |
| 46 | |
| 47 | private Multimap<State, Transition> states = LinkedHashMultimap.create(); |
| 48 | private State state; |
| 49 | |
| 50 | public Multimap<State, ? extends Transition> getResult() { |
| 51 | return states; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 56 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 57 | if (qName.equals("state")) { |
| 58 | state = instanceStateWarningOnUnrecognized(attributes.get("name")); |
| 59 | } else if (qName.equals("transition")) { |
| 60 | if (attributes.containsKey("auto")) |
| 61 | states.put(state, new TransitionAutomatically(instanceStateWarningOnUnrecognized(attributes.get("to")))); |
| 62 | else |
| 63 | states.put(state, new TransitionOnAction(instanceActionWarningOnUnrecognized(attributes.get("action")), |
| 64 | instanceStateWarningOnUnrecognized(attributes.get("to")))); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | State instanceStateWarningOnUnrecognized(String input) { |
| 69 | State state = State.fromValue(input); |
| 70 | if (state == State.UNRECOGNIZED) |
| 71 | logger.warn("unrecognized state: %s", input); |
| 72 | return state; |
| 73 | } |
| 74 | |
| 75 | Action instanceActionWarningOnUnrecognized(String input) { |
| 76 | Action action = Action.fromValue(input); |
| 77 | if (action == Action.UNRECOGNIZED) |
| 78 | logger.warn("unrecognized action: %s", input); |
| 79 | return action; |
| 80 | } |
| 81 | |
| 82 | } |