| 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.config; | 
| 20 |   | 
| 21 | import static com.google.common.base.Predicates.not; | 
| 22 |   | 
| 23 | import javax.inject.Named; | 
| 24 | import javax.inject.Singleton; | 
| 25 |   | 
| 26 | import org.jclouds.compute.domain.NodeMetadata; | 
| 27 | import org.jclouds.compute.predicates.NodeRunning; | 
| 28 | import org.jclouds.compute.predicates.NodeSuspended; | 
| 29 | import org.jclouds.compute.predicates.NodeTerminated; | 
| 30 | import org.jclouds.compute.predicates.ScriptStatusReturnsZero; | 
| 31 | import org.jclouds.compute.predicates.ScriptStatusReturnsZero.CommandUsingClient; | 
| 32 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; | 
| 33 | import org.jclouds.predicates.RetryablePredicate; | 
| 34 |   | 
| 35 | import com.google.common.base.Predicate; | 
| 36 | import com.google.inject.AbstractModule; | 
| 37 | import com.google.inject.Provides; | 
| 38 |   | 
| 39 | /** | 
| 40 |  *  | 
| 41 |  * @author Adrian Cole | 
| 42 |  *  | 
| 43 |  */ | 
| 44 | public class ComputeServiceTimeoutsModule extends AbstractModule { | 
| 45 |   | 
| 46 |    @Provides | 
| 47 |    @Singleton | 
| 48 |    @Named("NODE_RUNNING") | 
| 49 |    protected Predicate<NodeMetadata> nodeRunning(NodeRunning stateRunning, Timeouts timeouts) { | 
| 50 |       return timeouts.nodeRunning == 0 ? stateRunning : new RetryablePredicate<NodeMetadata>(stateRunning, | 
| 51 |             timeouts.nodeRunning); | 
| 52 |    } | 
| 53 |   | 
| 54 |    @Provides | 
| 55 |    @Singleton | 
| 56 |    @Named("NODE_TERMINATED") | 
| 57 |    protected Predicate<NodeMetadata> serverTerminated(NodeTerminated stateTerminated, Timeouts timeouts) { | 
| 58 |       return timeouts.nodeTerminated == 0 ? stateTerminated : new RetryablePredicate<NodeMetadata>(stateTerminated, | 
| 59 |             timeouts.nodeTerminated); | 
| 60 |    } | 
| 61 |     | 
| 62 |   | 
| 63 |    @Provides | 
| 64 |    @Singleton | 
| 65 |    @Named("NODE_SUSPENDED") | 
| 66 |    protected Predicate<NodeMetadata> serverSuspended(NodeSuspended stateSuspended, Timeouts timeouts) { | 
| 67 |       return timeouts.nodeSuspended == 0 ? stateSuspended : new RetryablePredicate<NodeMetadata>(stateSuspended, | 
| 68 |             timeouts.nodeSuspended); | 
| 69 |    } | 
| 70 |   | 
| 71 |    @Provides | 
| 72 |    @Singleton | 
| 73 |    @Named("SCRIPT_COMPLETE") | 
| 74 |    protected Predicate<CommandUsingClient> runScriptRunning(ScriptStatusReturnsZero stateRunning, Timeouts timeouts) { | 
| 75 |       return timeouts.scriptComplete == 0 ? not(stateRunning) : new RetryablePredicate<CommandUsingClient>( | 
| 76 |             not(stateRunning), timeouts.scriptComplete); | 
| 77 |    } | 
| 78 |   | 
| 79 |    @Override | 
| 80 |    protected void configure() { | 
| 81 |   | 
| 82 |    } | 
| 83 | } |