| 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.compute.strategy; | 
| 20 |   | 
| 21 | import static com.google.common.base.Preconditions.checkNotNull; | 
| 22 | import static com.google.common.base.Preconditions.checkState; | 
| 23 | import static com.google.common.base.Throwables.getRootCause; | 
| 24 |   | 
| 25 | import java.util.Map; | 
| 26 | import java.util.concurrent.Callable; | 
| 27 |   | 
| 28 | import javax.annotation.Resource; | 
| 29 | import javax.inject.Named; | 
| 30 |   | 
| 31 | import org.jclouds.compute.callables.RunScriptOnNode; | 
| 32 | import org.jclouds.compute.domain.ExecResponse; | 
| 33 | import org.jclouds.compute.domain.NodeMetadata; | 
| 34 | import org.jclouds.compute.reference.ComputeServiceConstants; | 
| 35 | import org.jclouds.logging.Logger; | 
| 36 |   | 
| 37 | import com.google.common.base.Objects; | 
| 38 | import com.google.inject.assistedinject.AssistedInject; | 
| 39 |   | 
| 40 | /** | 
| 41 |  *  | 
| 42 |  * @author Adrian Cole | 
| 43 |  */ | 
| 44 | public class RunScriptOnNodeAndAddToGoodMapOrPutExceptionIntoBadMap implements Callable<ExecResponse> { | 
| 45 |   | 
| 46 |    @Resource | 
| 47 |    @Named(ComputeServiceConstants.COMPUTE_LOGGER) | 
| 48 |    protected Logger logger = Logger.NULL; | 
| 49 |    private final RunScriptOnNode runScriptOnNode; | 
| 50 |    private final Map<NodeMetadata, Exception> badNodes; | 
| 51 |    private final Map<NodeMetadata, ExecResponse> goodNodes; | 
| 52 |   | 
| 53 |    private transient boolean tainted; | 
| 54 |   | 
| 55 |    @AssistedInject | 
| 56 |    public RunScriptOnNodeAndAddToGoodMapOrPutExceptionIntoBadMap(RunScriptOnNode runScriptOnNode, | 
| 57 |             Map<NodeMetadata, ExecResponse> goodNodes, Map<NodeMetadata, Exception> badNodes) { | 
| 58 |       this.runScriptOnNode = checkNotNull(runScriptOnNode, "runScriptOnNode"); | 
| 59 |       this.badNodes = checkNotNull(badNodes, "badNodes"); | 
| 60 |       this.goodNodes = checkNotNull(goodNodes, "goodNodes"); | 
| 61 |    } | 
| 62 |   | 
| 63 |    @Override | 
| 64 |    public ExecResponse call() { | 
| 65 |       checkState(runScriptOnNode != null, "runScriptOnNode must be set"); | 
| 66 |       checkState(!tainted, "this object is not designed to be reused: %s", toString()); | 
| 67 |       tainted = true; | 
| 68 |       try { | 
| 69 |          ExecResponse exec = runScriptOnNode.call(); | 
| 70 |          logger.trace("<< script output for node(%s): %s", runScriptOnNode.getNode().getId(), exec); | 
| 71 |          logger.debug("<< options applied node(%s)", runScriptOnNode.getNode().getId()); | 
| 72 |          goodNodes.put(runScriptOnNode.getNode(), exec); | 
| 73 |          return exec; | 
| 74 |       } catch (Exception e) { | 
| 75 |          logger.error(e, "<< problem applying options to node(%s): ", runScriptOnNode.getNode().getId(), | 
| 76 |                   getRootCause(e).getMessage()); | 
| 77 |          badNodes.put(runScriptOnNode.getNode(), e); | 
| 78 |       } | 
| 79 |       return null; | 
| 80 |    } | 
| 81 |   | 
| 82 |    @Override | 
| 83 |    public String toString() { | 
| 84 |       return Objects.toStringHelper(this).add("runScriptOnNode", runScriptOnNode).add("goodNodes", goodNodes).add( | 
| 85 |                "badNodes", badNodes).toString(); | 
| 86 |    } | 
| 87 |   | 
| 88 | } |