You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2018/04/19 07:40:59 UTC

[directory-ldap-api] 02/02: Added teh HandShakeFuture class

This is an automated email from the ASF dual-hosted git repository.

elecharny pushed a commit to branch 1.0.1
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git

commit f2822cdf3475f5d8182ef461173c912bbb982bdf
Author: Emmanuel Lécharny <el...@symas.com>
AuthorDate: Thu Apr 19 09:40:50 2018 +0200

    Added teh HandShakeFuture class
---
 .../ldap/client/api/future/HandshakeFuture.java    | 162 +++++++++++++++++++++
 1 file changed, 162 insertions(+)

diff --git a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/future/HandshakeFuture.java b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/future/HandshakeFuture.java
new file mode 100644
index 0000000..b14e15a
--- /dev/null
+++ b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/future/HandshakeFuture.java
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.directory.ldap.client.api.future;
+
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+
+/**
+ * A Future to manage StartTLS handshake
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class HandshakeFuture implements Future<Boolean>
+{
+    /** A flag set to TRUE when the handshake has been completed */
+    private volatile boolean done = false;
+
+    /** flag to determine if this future is cancelled */
+    protected boolean cancelled = false;
+
+    /**
+     * Creates a new instance of HandshakeFuture.
+     *
+     * @param connection the LDAP connection
+     * @param messageId The associated messageId
+     */
+    public HandshakeFuture()
+    {
+        // Nothing to initialize...
+    }
+
+
+    /**
+     * Cancel the Future
+     *
+     */
+    public synchronized void cancel()
+    {
+        // set the cancel flag first
+        cancelled = true;
+        
+        // Notify the future
+        notifyAll();
+    }
+
+
+    /**
+     * Set the Future to done when the TLS handshake has completed
+     * 
+     * @throws InterruptedException if the operation has been cancelled by client
+     */
+    public synchronized void secured()
+    {
+        done = true;
+        
+        notifyAll();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public synchronized boolean cancel( boolean mayInterruptIfRunning )
+    {
+        if ( cancelled )
+        {
+            return cancelled;
+        }
+
+        // set the cancel flag first
+        cancelled = true;
+
+        // Notify the future
+        notifyAll();
+
+        return cancelled;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public synchronized Boolean get() throws InterruptedException, ExecutionException
+    {
+        while ( !done && !cancelled )
+        {
+            wait();
+        }
+        
+        return done;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public synchronized Boolean get( long timeout, TimeUnit unit ) throws InterruptedException, ExecutionException, TimeoutException
+    {
+        wait( unit.toMillis( timeout ) );
+        
+        return done;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean isCancelled()
+    {
+        return cancelled;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean isDone()
+    {
+        return done;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+    
+        sb.append( "HandshakeFuture, completed: " ).append( done ).append( ", cancelled: " ).append( cancelled );
+    
+        return sb.toString();
+    }
+}
+

-- 
To stop receiving notification emails like this one, please contact
elecharny@apache.org.