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   *    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   */
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       * @param connection the LDAP connection
46       * @param messageId The associated messageId
47       */
48      public HandshakeFuture()
49      {
50          // Nothing to initialize...
51      }
52  
53  
54      /**
55       * Cancel the Future
56       *
57       */
58      public synchronized void cancel()
59      {
60          // set the cancel flag first
61          cancelled = true;
62          
63          // Notify the future
64          notifyAll();
65      }
66  
67  
68      /**
69       * Set the Future to done when the TLS handshake has completed
70       * 
71       * @throws InterruptedException if the operation has been cancelled by client
72       */
73      public synchronized void secured()
74      {
75          done = true;
76          
77          notifyAll();
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public synchronized boolean cancel( boolean mayInterruptIfRunning )
86      {
87          if ( cancelled )
88          {
89              return cancelled;
90          }
91  
92          // set the cancel flag first
93          cancelled = true;
94  
95          // Notify the future
96          notifyAll();
97  
98          return cancelled;
99      }
100 
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public synchronized Boolean get() throws InterruptedException, ExecutionException
107     {
108         while ( !done && !cancelled )
109         {
110             wait();
111         }
112         
113         return done;
114     }
115 
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public synchronized Boolean get( long timeout, TimeUnit unit ) throws InterruptedException, ExecutionException, TimeoutException
122     {
123         wait( unit.toMillis( timeout ) );
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 }
162