001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.ldap.client.api.future;
021
022
023import java.util.concurrent.ExecutionException;
024import java.util.concurrent.Future;
025import java.util.concurrent.TimeUnit;
026import java.util.concurrent.TimeoutException;
027
028
029/**
030 * A Future to manage StartTLS handshake
031 *
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034public class HandshakeFuture implements Future<Boolean>
035{
036    /** A flag set to TRUE when the handshake has been completed */
037    private volatile boolean done = false;
038
039    /** flag to determine if this future is cancelled */
040    protected boolean cancelled = false;
041
042    /**
043     * Creates a new instance of HandshakeFuture.
044     *
045     * @param connection the LDAP connection
046     * @param messageId The associated messageId
047     */
048    public HandshakeFuture()
049    {
050        // Nothing to initialize...
051    }
052
053
054    /**
055     * Cancel the Future
056     *
057     */
058    public synchronized void cancel()
059    {
060        // set the cancel flag first
061        cancelled = true;
062        
063        // Notify the future
064        notifyAll();
065    }
066
067
068    /**
069     * Set the Future to done when the TLS handshake has completed
070     * 
071     * @throws InterruptedException if the operation has been cancelled by client
072     */
073    public synchronized void secured()
074    {
075        done = true;
076        
077        notifyAll();
078    }
079
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public synchronized boolean cancel( boolean mayInterruptIfRunning )
086    {
087        if ( cancelled )
088        {
089            return cancelled;
090        }
091
092        // set the cancel flag first
093        cancelled = true;
094
095        // Notify the future
096        notifyAll();
097
098        return cancelled;
099    }
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