View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF 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   *    https://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   */
20  package org.apache.directory.ldap.client.api.future;
21  
22  
23  import java.util.concurrent.ExecutionException;
24  import java.util.concurrent.Future;
25  import java.util.concurrent.TimeUnit;
26  import java.util.concurrent.TimeoutException;
27  
28  
29  /**
30   * A Future to manage StartTLS handshake
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class HandshakeFuture implements Future<Boolean>
35  {
36      /** A flag set to TRUE when the handshake has been completed */
37      private volatile boolean done = false;
38  
39      /** flag to determine if this future is cancelled */
40      protected boolean cancelled = false;
41  
42      /**
43       * Creates a new instance of HandshakeFuture.
44       */
45      public HandshakeFuture()
46      {
47          // Nothing to initialize...
48      }
49  
50  
51      /**
52       * Cancel the Future
53       *
54       */
55      public synchronized void cancel()
56      {
57          // set the cancel flag first
58          cancelled = true;
59  
60          // Notify the future
61          notifyAll();
62      }
63  
64  
65      /**
66       * Set the Future to done when the TLS handshake has completed
67       */
68      public synchronized void secured()
69      {
70          done = true;
71  
72          notifyAll();
73      }
74  
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public synchronized boolean cancel( boolean mayInterruptIfRunning )
81      {
82          if ( cancelled )
83          {
84              return cancelled;
85          }
86  
87          // set the cancel flag first
88          cancelled = true;
89  
90          // Notify the future
91          notifyAll();
92  
93          return cancelled;
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public synchronized Boolean get() throws InterruptedException, ExecutionException
102     {
103         while ( !done && !cancelled )
104         {
105             wait();
106         }
107 
108         return done;
109     }
110 
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public synchronized Boolean get( long timeout, TimeUnit unit )
117         throws InterruptedException, ExecutionException, TimeoutException
118     {
119         // no need to wait if already done or cancelled
120         if ( !done && !cancelled )
121         {
122             wait( unit.toMillis( timeout ) );
123         }
124 
125         return done;
126     }
127 
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public boolean isCancelled()
134     {
135         return cancelled;
136     }
137 
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public boolean isDone()
144     {
145         return done;
146     }
147 
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public String toString()
154     {
155         StringBuilder sb = new StringBuilder();
156     
157         sb.append( "HandshakeFuture, completed: " ).append( done ).append( ", cancelled: " ).append( cancelled );
158     
159         return sb.toString();
160     }
161 }