Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    *  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.studio.connection.core.io.jndi;
  
  
  import java.io.File;
  import java.util.HashMap;
  import java.util.List;
  
  import javax.naming.Name;
  
  import  org.apache.commons.io.FileUtils;
  import  org.apache.commons.lang.StringUtils;
  import  org.apache.directory.api.ldap.model.constants.SaslQoP;
  import  org.apache.directory.api.ldap.model.constants.SaslSecurityStrength;
  import  org.apache.directory.api.ldap.model.exception.LdapURLEncodingException;
  import  org.apache.directory.api.ldap.model.message.Referral;
  import  org.apache.directory.api.ldap.model.message.ReferralImpl;
  import  org.apache.directory.api.ldap.model.url.LdapUrl;
  import  org.apache.directory.studio.common.core.jobs.StudioProgressMonitor;
  import  org.eclipse.core.runtime.Preferences;
  import  org.eclipse.osgi.util.NLS;


A connection wrapper that uses JNDI. - asychron + cancelable - SSL certificate - manages broken/closed connections - delete old Rdn - exception handling - referral handling

Author(s):
Apache Directory Project
 
 public class JNDIConnectionWrapper implements ConnectionWrapper
 {
     private static final String JAVA_NAMING_LDAP_DELETE_RDN = "java.naming.ldap.deleteRDN"//$NON-NLS-1$
 
     private static final String AUTHMETHOD_NONE = "none"//$NON-NLS-1$
 
     private static final String AUTHMETHOD_SIMPLE = "simple"//$NON-NLS-1$
 
     private static final String AUTHMETHOD_DIGEST_MD5 = "DIGEST-MD5"//$NON-NLS-1$
 
     private static final String AUTHMETHOD_CRAM_MD5 = "CRAM-MD5"//$NON-NLS-1$
 
     private static final String AUTHMETHOD_GSSAPI = "GSSAPI"//$NON-NLS-1$
 
     private static final String NO_CONNECTION = "No connection"//$NON-NLS-1$
 
     private static final String JAVA_NAMING_SECURITY_SASL_REALM = "java.naming.security.sasl.realm"//$NON-NLS-1$
 
     private static final String JAVA_NAMING_LDAP_FACTORY_SOCKET = "java.naming.ldap.factory.socket"//$NON-NLS-1$
 
     private static final String COM_SUN_JNDI_DNS_TIMEOUT_RETRIES = "com.sun.jndi.dns.timeout.retries"//$NON-NLS-1$
 
     private static final String COM_SUN_JNDI_DNS_TIMEOUT_INITIAL = "com.sun.jndi.dns.timeout.initial"//$NON-NLS-1$
 
     private static final String COM_SUN_JNDI_LDAP_CONNECT_TIMEOUT = "com.sun.jndi.ldap.connect.timeout"//$NON-NLS-1$
 
     private static final String JAVA_NAMING_LDAP_VERSION = "java.naming.ldap.version"//$NON-NLS-1$
 
     private static final String JAVA_NAMING_LDAP_DEREF_ALIASES = "java.naming.ldap.derefAliases"//$NON-NLS-1$
 
     private static final String JAVA_NAMING_LDAP_ATTRIBUTES_BINARY = "java.naming.ldap.attributes.binary"//$NON-NLS-1$
 
     private static int SEARCH_RESQUEST_NUM = 0;
 
     private Connection connection;
 
     private boolean useLdaps;
 
     private boolean useStartTLS;
 
     private String authMethod;
 
     private String bindPrincipal;
 
     private String bindCredentials;
 
     private String saslRealm;
 
     private Hashtable<StringStringenvironment;
 
     private InitialLdapContext context;
 
     private boolean isConnected;
 
     private Thread jobThread;
 
     private Collection<StringbinaryAttributes;

    
JNDI constant for "throw" referrals handling
 
     public static final String REFERRAL_THROW = "throw"//$NON-NLS-1$
 
    
JNDI constant for "follow" referrals handling
 
     public static final String REFERRAL_FOLLOW = "follow"//$NON-NLS-1$
 
    
JNDI constant for "ignore" referrals handling
 
     public static final String REFERRAL_IGNORE = "ignore"//$NON-NLS-1$
 
    
JNDI constant for "searching" alias dereferencing
 
     public static final String ALIAS_SEARCHING = "searching"//$NON-NLS-1$
 
    
JNDI constant for "finding" alias dereferencing
 
     public static final String ALIAS_FINDING = "finding"//$NON-NLS-1$
 
    
JNDI constant for "always" alias dereferencing
 
     public static final String ALIAS_ALWAYS = "always"//$NON-NLS-1$
 
    
JNDI constant for "never" alias dereferencing
 
     public static final String ALIAS_NEVER = "never"//$NON-NLS-1$
 

    
Creates a new instance of JNDIConnectionContext.

Parameters:
connection the connection
 
     public JNDIConnectionWrapperConnection connection )
     {
         this. = connection;
     }


    
 
     public void connect( StudioProgressMonitor monitor )
     {
          = null;
          = false;
          = null;
 
         try
         {
             doConnectmonitor );
         }
         catch ( NamingException ne )
         {
             disconnect();
             monitor.reportError( ne );
         }
     }


    
 
     public void disconnect()
     {
         if (  != null )
         {
             Thread t = ;
              = null;
             t.interrupt();
         }
         if (  != null )
         {
             try
             {
                 .close();
             }
             catch ( NamingException e )
             {
                 // ignore
             }
              = null;
         }
          = false;
     }


    
 
     public void bind( StudioProgressMonitor monitor )
     {
         try
         {
             doBindmonitor );
         }
         catch ( NamingException ne )
         {
             disconnect();
             monitor.reportError( ne );
         }
     }


    
 
     public void unbind()
     {
         disconnect();
     }


    
 
     public boolean isConnected()
     {
         return  != null;
     }


    
Sets the binary attributes.

Parameters:
binaryAttributes the binary attributes
 
     public void setBinaryAttributesCollection<StringbinaryAttributes )
     {
         this. = binaryAttributes;
         String binaryAttributesString = StringUtils.EMPTY;
         for ( String string : binaryAttributes )
         {
             binaryAttributesString += string + ' ';
         }
 
         if (  != null )
         {
             .putbinaryAttributesString );
         }
 
         if (  != null )
         {
             try
             {
                 .addToEnvironmentbinaryAttributesString );
             }
             catch ( NamingException e )
             {
                 // TODO: logging
                 e.printStackTrace();
             }
         }
     }


    
Search.

Parameters:
searchBase the search base
filter the filter
searchControls the controls
aliasesDereferencingMethod the aliases dereferencing method
referralsHandlingMethod the referrals handling method
controls the LDAP controls
monitor the progress monitor
referralsInfo the referrals info
Returns:
the naming enumeration or null if an exception occurs.
 
     public JndiStudioNamingEnumeration searchfinal String searchBasefinal String filter,
         final SearchControls searchControlsfinal AliasDereferencingMethod aliasesDereferencingMethod,
         final ReferralHandlingMethod referralsHandlingMethodfinal Control[] controls,
         final StudioProgressMonitor monitorfinal ReferralsInfo referralsInfo )
     {
         final long requestNum = ++;
 
         // start
         InnerRunnable runnable = new InnerRunnable()
         {
             public void run()
             {
                 LdapContext searchCtx = ;
                 try
                 {
                     // create the search context
                     searchCtx = .newInstancecontrols );
 
                     // translate alias dereferencing method
                     searchCtx.addToEnvironment,
                         translateDerefAliasMethodaliasesDereferencingMethod ) );
 
                     // use "throw" as we handle referrals manually
                     searchCtx.addToEnvironment. );
 
                     // perform the search
                     NamingEnumeration<SearchResultresult = searchCtx.search( JNDIConnectionWrapper
                         .getSaveJndiNamesearchBase ), filtersearchControls );
                      = new JndiStudioNamingEnumerationsearchCtxresultnull,
                         searchBase,
                         filtersearchControlsaliasesDereferencingMethodreferralsHandlingMethodcontrols,
                         requestNummonitorreferralsInfo );
                 }
                 catch ( PartialResultException e )
                 {
                      = new JndiStudioNamingEnumerationsearchCtxnullesearchBase,
                         filtersearchControlsaliasesDereferencingMethodreferralsHandlingMethodcontrols,
                         requestNummonitorreferralsInfo );
                 }
                 catch ( ReferralException e )
                 {
                      = new JndiStudioNamingEnumerationsearchCtxnullesearchBase,
                         filtersearchControlsaliasesDereferencingMethodreferralsHandlingMethodcontrols,
                         requestNummonitorreferralsInfo );
                 }
                 catch ( NamingException e )
                 {
                      = e;
                 }
 
                 for ( IJndiLogger logger : getJndiLoggers() )
                 {
                     if (  != null )
                     {
                         logger.logSearchRequestsearchBasefiltersearchControls,
                             aliasesDereferencingMethodcontrolsrequestNum );
                     }
                     else
                     {
                         logger.logSearchRequestsearchBasefiltersearchControls,
                             aliasesDereferencingMethodcontrolsrequestNum );
                         logger.logSearchResultDone, 0, requestNum );
                     }
                 }
             }
         };
 
         try
         {
             checkConnectionAndRunAndMonitorrunnablemonitor );
         }
         catch ( NamingException ne )
         {
             monitor.reportError( ne );
             return null;
         }
 
         if ( runnable.isCanceled() )
         {
             monitor.setCanceled( true );
         }
         if ( runnable.getException() != null )
         {
             monitor.reportError( runnable.getException() );
             return null;
         }
         else
         {
             return runnable.getResult();
         }
     }


    
Modifies attributes of an entry.

Parameters:
dn the Dn
modificationItems the modification items
controls the controls
monitor the progress monitor
referralsInfo the referrals info
 
     public void modifyEntryfinal String dnfinal ModificationItem[] modificationItemsfinal Control[] controls,
         final StudioProgressMonitor monitorfinal ReferralsInfo referralsInfo )
     {
         if ( .isReadOnly() )
         {
             monitor
                 .reportError( new Exception( NLS.bind( ..getName() ) ) );
             return;
         }
 
         InnerRunnable runnable = new InnerRunnable()
         {
             public void run()
             {
                 boolean logModification = true;
                 try
                 {
                     // create modify context
                     LdapContext modCtx = .newInstancecontrols );
 
                     // use "throw" as we handle referrals manually
                     modCtx.addToEnvironment. );
 
                     // perform modification
                     modCtx.modifyAttributesgetSaveJndiNamedn ), modificationItems );
                 }
                 catch ( ReferralException re )
                 {
                     logModification = false;
                     try
                     {
                         ReferralsInfo newReferralsInfo = handleReferralExceptionrereferralsInfo );
                         Referral referral = newReferralsInfo.getNextReferral();
                         if ( referral != null )
                         {
                             Connection referralConnection = ConnectionWrapperUtils.getReferralConnectionreferral,
                                 monitorthis );
                             if ( referralConnection != null )
                             {
                                 List<Stringurls = new ArrayList<String>( referral.getLdapUrls() );
 
                                 String referralDn = new LdapUrl( urls.get( 0 ) ).getDn().getName();
                                 referralConnection.getConnectionWrapper().modifyEntryreferralDn,
                                     modificationItemscontrolsmonitornewReferralsInfo );
                             }
                             else
                             {
                                  = true;
                             }
                         }
 
                         return;
                     }
                     catch ( NamingException ne )
                     {
                          = ne;
                     }
                     catch ( LdapURLEncodingException e )
                     {
                          = new NamingExceptione.getMessage() );
                     }
                 }
                 catch ( NamingException ne )
                 {
                      = ne;
                 }
 
                 if ( logModification )
                 {
                     for ( IJndiLogger logger : getJndiLoggers() )
                     {
                         logger.logChangetypeModifydnmodificationItemscontrols );
                     }
                 }
             }
         };
 
         try
         {
             checkConnectionAndRunAndMonitorrunnablemonitor );
         }
         catch ( NamingException ne )
         {
             monitor.reportError( ne );
         }
 
         if ( runnable.isCanceled() )
         {
             monitor.setCanceled( true );
         }
         if ( runnable.getException() != null )
         {
             monitor.reportError( runnable.getException() );
         }
     }


    
Renames an entry.

Parameters:
oldDn the old Dn
newDn the new Dn
deleteOldRdn true to delete the old Rdn
controls the controls
monitor the progress monitor
referralsInfo the referrals info
 
     public void renameEntryfinal String oldDnfinal String newDnfinal boolean deleteOldRdn,
         final Control[] controlsfinal StudioProgressMonitor monitorfinal ReferralsInfo referralsInfo )
     {
         if ( .isReadOnly() )
         {
             monitor
                 .reportError( new Exception( NLS.bind( ..getName() ) ) );
             return;
         }
 
         InnerRunnable runnable = new InnerRunnable()
         {
             public void run()
             {
                 boolean logModification = true;
                 try
                 {
                     // create modify context
                     LdapContext modCtx = .newInstancecontrols );
 
                     // use "throw" as we handle referrals manually
                     modCtx.addToEnvironment. );
 
                     // delete old Rdn
                     if ( deleteOldRdn )
                     {
                         modCtx.addToEnvironment"true" ); //$NON-NLS-1$
                     }
                     else
                     {
                         modCtx.addToEnvironment"false" ); //$NON-NLS-1$
                     }
 
                     // rename entry
                     modCtx.renamegetSaveJndiNameoldDn ), getSaveJndiNamenewDn ) );
                 }
                 catch ( ReferralException re )
                 {
                     logModification = false;
                     try
                     {
                         ReferralsInfo newReferralsInfo = handleReferralExceptionrereferralsInfo );
                         Referral referral = newReferralsInfo.getNextReferral();
                         if ( referral != null )
                         {
                             Connection referralConnection = ConnectionWrapperUtils.getReferralConnectionreferral,
                                 monitorthis );
                             if ( referralConnection != null )
                             {
                                 referralConnection.getConnectionWrapper().renameEntryoldDnnewDndeleteOldRdn,
                                     controlsmonitornewReferralsInfo );
                             }
                             else
                             {
                                  = true;
                             }
                         }
                     }
                     catch ( NamingException ne )
                     {
                          = ne;
                     }
                 }
                 catch ( NamingException ne )
                 {
                      = ne;
                 }
 
                 if ( logModification )
                 {
                     for ( IJndiLogger logger : getJndiLoggers() )
                     {
                         logger.logChangetypeModDnoldDnnewDndeleteOldRdncontrols );
                     }
                 }
             }
         };
 
         try
         {
             checkConnectionAndRunAndMonitorrunnablemonitor );
         }
         catch ( NamingException ne )
         {
             monitor.reportError( ne );
         }
 
         if ( runnable.isCanceled() )
         {
             monitor.setCanceled( true );
         }
         if ( runnable.getException() != null )
         {
             monitor.reportError( runnable.getException() );
         }
     }


    
Creates an entry.

Parameters:
dn the entry's Dn
attributes the entry's attributes
controls the controls
monitor the progress monitor
referralsInfo the referrals info
 
     public void createEntryfinal String dnfinal Attributes attributesfinal Control[] controls,
         final StudioProgressMonitor monitorfinal ReferralsInfo referralsInfo )
     {
         if ( .isReadOnly() )
         {
             monitor
                 .reportError( new Exception( NLS.bind( ..getName() ) ) );
             return;
         }
 
         InnerRunnable runnable = new InnerRunnable()
         {
             public void run()
             {
                 boolean logModification = true;
                 try
                 {
                     // create modify context
                     LdapContext modCtx = .newInstancecontrols );
 
                     // use "throw" as we handle referrals manually
                     modCtx.addToEnvironment. );
 
                     // create entry
                     modCtx.createSubcontextgetSaveJndiNamedn ), attributes );
                 }
                 catch ( ReferralException re )
                 {
                     logModification = false;
                     try
                     {
                         ReferralsInfo newReferralsInfo = handleReferralExceptionrereferralsInfo );
                         Referral referral = newReferralsInfo.getNextReferral();
                         if ( referral != null )
                         {
                             Connection referralConnection = ConnectionWrapperUtils.getReferralConnectionreferral,
                                 monitorthis );
                             if ( referralConnection != null )
                             {
                                 List<Stringurls = new ArrayList<String>( referral.getLdapUrls() );
 
                                 String referralDn = new LdapUrl( urls.get( 0 ) ).getDn().getName();
                                 referralConnection.getConnectionWrapper().createEntryreferralDnattributes,
                                     controlsmonitornewReferralsInfo );
                             }
                             else
                             {
                                  = true;
                             }
                         }
                     }
                     catch ( NamingException ne )
                     {
                          = ne;
                     }
                     catch ( LdapURLEncodingException e )
                     {
                          = new NamingExceptione.getMessage() );
                     }
                 }
                 catch ( NamingException ne )
                 {
                      = ne;
                 }
 
                 if ( logModification )
                 {
                     for ( IJndiLogger logger : getJndiLoggers() )
                     {
                         logger.logChangetypeAdddnattributescontrols );
                     }
                 }
             }
         };
 
         try
         {
             checkConnectionAndRunAndMonitorrunnablemonitor );
         }
         catch ( NamingException ne )
         {
             monitor.reportError( ne );
         }
 
         if ( runnable.isCanceled() )
         {
             monitor.setCanceled( true );
         }
         if ( runnable.getException() != null )
         {
             monitor.reportError( runnable.getException() );
         }
     }


    
Deletes an entry.

Parameters:
dn the Dn of the entry to delete
controls the controls
monitor the progress monitor
referralsInfo the referrals info
 
     public void deleteEntryfinal String dnfinal Control[] controlsfinal StudioProgressMonitor monitor,
         final ReferralsInfo referralsInfo )
     {
         if ( .isReadOnly() )
         {
             monitor
                 .reportError( new Exception( NLS.bind( ..getName() ) ) );
             return;
         }
 
         InnerRunnable runnable = new InnerRunnable()
         {
             public void run()
             {
                 boolean logModification = true;
                 try
                 {
                     // create modify context
                     LdapContext modCtx = .newInstancecontrols );
 
                     // use "throw" as we handle referrals manually
                     modCtx.addToEnvironment. );
 
                     // delete entry
                     modCtx.destroySubcontextgetSaveJndiNamedn ) );
                 }
                 catch ( ReferralException re )
                 {
                     logModification = false;
                     try
                     {
                         ReferralsInfo newReferralsInfo = handleReferralExceptionrereferralsInfo );
                         Referral referral = newReferralsInfo.getNextReferral();
                         if ( referral != null )
                         {
                             Connection referralConnection = ConnectionWrapperUtils.getReferralConnectionreferral,
                                 monitorthis );
                             if ( referralConnection != null )
                             {
                                 List<Stringurls = new ArrayList<String>( referral.getLdapUrls() );
 
                                 String referralDn = new LdapUrl( urls.get( 0 ) ).getDn().getName();
                                 referralConnection.getConnectionWrapper().deleteEntryreferralDncontrols,
                                     monitornewReferralsInfo );
                             }
                             else
                             {
                                  = true;
                             }
                         }
                     }
                     catch ( NamingException ne )
                     {
                          = ne;
                     }
                     catch ( LdapURLEncodingException e )
                     {
                          = new NamingExceptione.getMessage() );
                     }
                 }
                 catch ( NamingException ne )
                 {
                      = ne;
                 }
 
                 if ( logModification )
                 {
                     for ( IJndiLogger logger : getJndiLoggers() )
                     {
                         logger.logChangetypeDeletedncontrols );
                     }
                 }
             }
         };
 
         try
         {
             checkConnectionAndRunAndMonitorrunnablemonitor );
         }
         catch ( NamingException ne )
         {
             monitor.reportError( ne );
         }
 
         if ( runnable.isCanceled() )
         {
             monitor.setCanceled( true );
         }
         if ( runnable.getException() != null )
         {
             monitor.reportError( runnable.getException() );
         }
     }
 
 
     private void doConnectfinal StudioProgressMonitor monitor ) throws NamingException
     {
          = null;
          = true;
 
         // setup connection parameters
         String host = .getConnectionParameter().getHost();
         int port = .getConnectionParameter().getPort();
 
 
          = new Hashtable<StringString>();
         Preferences preferences = ConnectionCorePlugin.getDefault().getPluginPreferences();
         final boolean validateCertificates = preferences
             .getBoolean( . );
         String ldapCtxFactory = preferences.getString( . );
         .put.ldapCtxFactory );
         .put"3" ); //$NON-NLS-1$
 
         // timeouts
         // Don't use a timeout when using ldaps: JNDI throws a SocketException 
         // when setting a timeout on SSL connections.
         if ( ! )
         {
             .put"10000" ); //$NON-NLS-1$
         }
         .put"2000" ); //$NON-NLS-1$
         .put"3" ); //$NON-NLS-1$
 
         // ldaps://
         if (  )
         {
             .put., LdapUrl.LDAPS_SCHEME + host + ':' + port );
             .put."ssl" ); //$NON-NLS-1$
             // host name verification is done in StudioTrustManager
             .putvalidateCertificates ? StudioSSLSocketFactory.class
                 .getName() : DummySSLSocketFactory.class.getName() );
         }
         else
         {
             .put., LdapUrl.LDAP_SCHEME + host + ':' + port );
         }
 
         if (  != null )
         {
             setBinaryAttributes );
         }
 
         InnerRunnable runnable = new InnerRunnable()
         {
             public void run()
             {
                 try
                 {
                      = new InitialLdapContextnull );
 
                     if (  )
                     {
                         try
                         {
                             StartTlsResponse tls = ( StartTlsResponse ) 
                                 .extendedOperationnew StartTlsRequest() );
                             // deactivate host name verification at this level,
                             // host name verification is done in StudioTrustManager
                             tls.setHostnameVerifiernew HostnameVerifier()
                             {
                                 public boolean verifyString hostnameSSLSession session )
                                 {
                                     return true;
                                 }
                             } );
                             if ( validateCertificates )
                             {
                                 tls.negotiate( StudioSSLSocketFactory.getDefault() );
                             }
                             else
                             {
                                 tls.negotiate( DummySSLSocketFactory.getDefault() );
                             }
                         }
                         catch ( Exception e )
                         {
                              = new NamingExceptione.getMessage() != null ? e.getMessage()
                                 : "Error while establishing TLS session" ); //$NON-NLS-1$
                             .setRootCausee );
                             .close();
                         }
                     }
                 }
                 catch ( NamingException ne )
                 {
                      = ne;
                 }
             }
         };
 
         runAndMonitorrunnablemonitor );
 
         if ( runnable.getException() != null )
         {
             throw runnable.getException();
         }
         else if (  != null )
         {
             // all OK
         }
         else
         {
             throw new NamingException"???" ); //$NON-NLS-1$
         }
     }
 
 
     private void doBindfinal StudioProgressMonitor monitor ) throws NamingException
     {
         if (  != null &&  )
         {
             // setup authentication methdod
              = ;
             {
                  = ;
             }
             {
                  = ;
                  = .getConnectionParameter().getSaslRealm();
             }
             {
                  = ;
             }
             {
                  = ;
             }
 
             // No Authentication
             if (  ==  )
             {
                  = ""//$NON-NLS-1$
                  = ""//$NON-NLS-1$
             }
             else
             {
                 // setup credentials
                 IAuthHandler authHandler = ConnectionCorePlugin.getDefault().getAuthHandler();
                 if ( authHandler == null )
                 {
                     NamingException namingException = new NamingException. );
                     monitor.reportError( .namingException );
                     throw namingException;
                 }
                 ICredentials credentials = authHandler.getCredentials.getConnectionParameter() );
                 if ( credentials == null )
                 {
                    CancelException cancelException = new CancelException();
                    monitor.setCanceled( true );
                    monitor.reportError( .cancelException );
                    throw cancelException;
                }
                if ( credentials.getBindPrincipal() == null || credentials.getBindPassword() == null )
                {
                    NamingException namingException = new NamingException. );
                    monitor.reportError( .namingException );
                    throw namingException;
                }
                 = credentials.getBindPrincipal();
                 = credentials.getBindPassword();
            }
            InnerRunnable runnable = new InnerRunnable()
            {
                public void run()
                {
                    try
                    {
                        .removeFromEnvironment. );
                        .removeFromEnvironment. );
                        .removeFromEnvironment. );
                        .removeFromEnvironment );
                        .addToEnvironment. );
                        // SASL options
                        if ( .getConnectionParameter().getAuthMethod() == .
                            || .getConnectionParameter().getAuthMethod() == .
                            || .getConnectionParameter().getAuthMethod() == . )
                        {
                            // Request quality of protection
                            switch ( .getConnectionParameter().getSaslQop() )
                            {
                                case AUTH:
                                    .addToEnvironment., SaslQoP.AUTH.getValue() );
                                    break;
                                case AUTH_INT:
                                    .addToEnvironment., SaslQoP.AUTH_INT.getValue() );
                                    break;
                                case AUTH_CONF:
                                    .addToEnvironment., SaslQoP.AUTH_CONF.getValue() );
                                    break;
                            }
                            // Request mutual authentication
                            if ( .getConnectionParameter().isSaslMutualAuthentication() )
                            {
                                .addToEnvironment."true" ); //$NON-NLS-1$
                            }
                            else
                            {
                                .removeFromEnvironment. );
                            }
                            // Request cryptographic protection strength
                            switch ( .getConnectionParameter().getSaslSecurityStrength() )
                            {
                                case HIGH:
                                    .addToEnvironment., SaslSecurityStrength.HIGH.getValue() );
                                    break;
                                case MEDIUM:
                                    .addToEnvironment., SaslSecurityStrength.MEDIUM.getValue() );
                                    break;
                                case LOW:
                                    .addToEnvironment., SaslSecurityStrength.LOW.getValue() );
                                    break;
                            }
                        }
                        // Bind
                        if ( .getConnectionParameter().getAuthMethod() == .. )
                        {
                            // GSSAPI
                            doGssapiBindthis );
                        }
                        else
                        {
                            // no GSSAPI
                            .addToEnvironment. );
                            .addToEnvironment. );
                            if ( .getConnectionParameter().getAuthMethod() == ..
                                && StringUtils.isNotEmpty(  ) )
                            {
                                .addToEnvironment );
                            }
                            .reconnect.getConnectControls() );
                        }
                    }
                    catch ( NamingException ne )
                    {
                         = ne;
                    }
                }
            };
            runAndMonitorrunnablemonitor );
            if ( runnable.getException() != null )
            {
                throw runnable.getException();
            }
            else if (  != null )
            {