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.predicates; |
20 | |
21 | import java.util.concurrent.TimeUnit; |
22 | |
23 | import javax.annotation.Resource; |
24 | import javax.inject.Inject; |
25 | import javax.inject.Named; |
26 | |
27 | import org.jclouds.compute.reference.ComputeServiceConstants; |
28 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
29 | import org.jclouds.logging.Logger; |
30 | import org.jclouds.net.IPSocket; |
31 | import org.jclouds.predicates.RetryablePredicate; |
32 | import org.jclouds.predicates.SocketOpen; |
33 | |
34 | import com.google.common.base.Predicate; |
35 | |
36 | /** |
37 | * |
38 | * |
39 | * Not singleton as seconds are mutable |
40 | * |
41 | * @author Adrian Cole |
42 | * |
43 | */ |
44 | public class RetryIfSocketNotYetOpen implements Predicate<IPSocket> { |
45 | @Resource |
46 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
47 | private Logger logger = Logger.NULL; |
48 | |
49 | private final SocketOpen socketTester; |
50 | private long seconds; |
51 | |
52 | public RetryIfSocketNotYetOpen seconds(long seconds) { |
53 | this.seconds = seconds; |
54 | return this; |
55 | } |
56 | |
57 | @Inject |
58 | public RetryIfSocketNotYetOpen(SocketOpen socketTester, Timeouts timeouts) { |
59 | this.socketTester = socketTester; |
60 | this.seconds = timeouts.portOpen; |
61 | } |
62 | |
63 | public RetryIfSocketNotYetOpen(SocketOpen socketTester, Logger logger, long seconds) { |
64 | this.socketTester = socketTester; |
65 | this.logger = logger; |
66 | this.seconds = seconds; |
67 | } |
68 | |
69 | @Override |
70 | public String toString() { |
71 | return "retryIfSocketNotYetOpen(" + seconds + ")"; |
72 | } |
73 | |
74 | @Override |
75 | public boolean apply(IPSocket socket) { |
76 | logger.debug(">> blocking on socket %s for %d seconds", socket, seconds); |
77 | RetryablePredicate<IPSocket> tester = new RetryablePredicate<IPSocket>(socketTester, seconds, 1, TimeUnit.SECONDS); |
78 | boolean passed = tester.apply(socket); |
79 | if (passed) |
80 | logger.debug("<< socket %s opened", socket); |
81 | else |
82 | logger.warn("<< socket %s didn't open after %d seconds", socket, seconds); |
83 | return passed; |
84 | } |
85 | } |