001/* 002 * Copyright 2018-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2018-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2018-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.sdk; 037 038 039 040import com.unboundid.util.Debug; 041import com.unboundid.util.ThreadSafety; 042import com.unboundid.util.ThreadSafetyLevel; 043 044 045 046/** 047 * This class provides an implementation of a referral connector that will 048 * retain the exception encountered on the last attempt to establish a 049 * connection for the purpose of following a referral. 050 * <BR><BR> 051 * Note that although this class is technically safe to be used concurrently by 052 * multiple threads in that it won't result in a deadlock or concurrent 053 * modification exception or any other kind of obvious failure, it only retains 054 * a single exception, and only from the last attempt made to establish a 055 * connection for the purpose of following a referral. If multiple threads try 056 * to use the same instance of this connector concurrently, a call to the 057 * {@link #getExceptionFromLastConnectAttempt()} method may return the result 058 * from the last attempt made on another thread. It is therefore recommended 059 * that this connector only be used in contexts where it can be safely assumed 060 * that it will not be used concurrently across multiple threads. For example, 061 * if a connection is not expected to be concurrently shared by multiple 062 * threads, then it may be desirable to use the 063 * {@link LDAPConnection#setReferralConnector(ReferralConnector)} to set a 064 * different instance of this connector for each connection. Alternately, the 065 * {@link LDAPRequest#setReferralConnector(ReferralConnector)} method may be 066 * used to specify a connector that should be used for an individual request. 067 */ 068@ThreadSafety(level=ThreadSafetyLevel.MOSTLY_NOT_THREADSAFE) 069public final class RetainConnectExceptionReferralConnector 070 implements ReferralConnector 071{ 072 // The wrapped referral connector that will actually be used to establish the 073 // connection. 074 private final ReferralConnector wrappedReferralConnector; 075 076 // The exception caught in the last attempt to establish a connection for the 077 // purpose of following a referral. 078 private volatile LDAPException connectExceptionFromLastAttempt; 079 080 081 082 /** 083 * Creates a new instance of this referral connector that will use the 084 * connection's default referral handler to actually attempt to establish a 085 * connection. 086 */ 087 public RetainConnectExceptionReferralConnector() 088 { 089 this(null); 090 } 091 092 093 094 /** 095 * Creates a new instance of this referral connector that will use the 096 * provided connector to actually attempt to establish a connection. 097 * 098 * @param wrappedReferralConnector The referral connector that will be used 099 * to actually attempt to establish a 100 * connection for the purpose of following a 101 * referral. This may be {@code null} to 102 * use the default referral connector for 103 * the connection on which the referral was 104 * received. 105 */ 106 public RetainConnectExceptionReferralConnector( 107 final ReferralConnector wrappedReferralConnector) 108 { 109 this.wrappedReferralConnector = wrappedReferralConnector; 110 111 connectExceptionFromLastAttempt = null; 112 } 113 114 115 116 /** 117 * Retrieves the exception that was caught in the last attempt to establish a 118 * connection for the purpose of following a referral, if any. 119 * 120 * @return The exception that was caught in the last attempt to establish a 121 * connection for the purpose of following a referral, or 122 * {@code null} if the last connection attempt was successful or if 123 * there have not yet been any connection attempts. 124 */ 125 public LDAPException getExceptionFromLastConnectAttempt() 126 { 127 return connectExceptionFromLastAttempt; 128 } 129 130 131 132 /** 133 * {@inheritDoc} 134 */ 135 @Override() 136 public LDAPConnection getReferralConnection(final LDAPURL referralURL, 137 final LDAPConnection connection) 138 throws LDAPException 139 { 140 final ReferralConnector connector; 141 if (wrappedReferralConnector == null) 142 { 143 connector = connection.getReferralConnector(); 144 } 145 else 146 { 147 connector = wrappedReferralConnector; 148 } 149 150 LDAPException connectException = null; 151 try 152 { 153 return connector.getReferralConnection(referralURL, connection); 154 } 155 catch (final LDAPException e) 156 { 157 Debug.debugException(e); 158 connectException = e; 159 throw e; 160 } 161 finally 162 { 163 connectExceptionFromLastAttempt = connectException; 164 } 165 } 166}