001/* 002 * Copyright 2016-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2016-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) 2016-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.unboundidds.tools; 037 038 039 040import java.util.ArrayList; 041 042import com.unboundid.ldap.sdk.BindResult; 043import com.unboundid.ldap.sdk.LDAPConnection; 044import com.unboundid.ldap.sdk.LDAPConnectionPoolHealthCheck; 045import com.unboundid.ldap.sdk.LDAPException; 046import com.unboundid.ldap.sdk.ResultCode; 047import com.unboundid.util.CommandLineTool; 048import com.unboundid.util.StaticUtils; 049import com.unboundid.util.ThreadSafety; 050import com.unboundid.util.ThreadSafetyLevel; 051 052import static com.unboundid.ldap.sdk.unboundidds.tools.ToolMessages.*; 053 054 055 056/** 057 * This class provides an implementation of a connection pool health check that 058 * can display information about the result of a bind operation. It will always 059 * report information about an unsuccessful bind. It may optionally report 060 * information about a successful bind, and optionally only if the successful 061 * bind includes one or more response controls. 062 * <BR> 063 * <BLOCKQUOTE> 064 * <B>NOTE:</B> This class, and other classes within the 065 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 066 * supported for use against Ping Identity, UnboundID, and 067 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 068 * for proprietary functionality or for external specifications that are not 069 * considered stable or mature enough to be guaranteed to work in an 070 * interoperable way with other types of LDAP servers. 071 * </BLOCKQUOTE> 072 * <BR> 073 * Note that this health check is only intended to generate output when 074 * appropriate and will never throw an exception to indicate that a connection 075 * is unusable. If additional health checking is required, then this health 076 * check may be combined with others via an aggregate health check in a manner 077 * that ensures this health check will be invoked before any others that may 078 * throw an exception. 079 */ 080@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 081public final class ReportBindResultLDAPConnectionPoolHealthCheck 082 extends LDAPConnectionPoolHealthCheck 083{ 084 // Indicates whether to display result details for successful binds that 085 // include one or more response controls. 086 private final boolean displaySuccessResultWithControls; 087 088 // Indicates whether to display result details for successful binds that do 089 // not include any response controls. 090 private final boolean displaySuccessResultWithoutControls; 091 092 // The tool whose output and error streams will be used for displaying result 093 // details. 094 private final CommandLineTool tool; 095 096 // The column at which to wrap long lines. 097 private final int wrapColumn; 098 099 100 101 /** 102 * Creates a new instance of this health check with the provided information. 103 * 104 * @param tool The tool with which this 105 * health check is associated. 106 * Any success messages written 107 * will be sent to the tool's 108 * standard output stream. Any 109 * failure messages written will 110 * be sent to the tool's standard 111 * error stream. 112 * @param displaySuccessResultWithControls Indicates whether to display 113 * information about a bind 114 * result with a result code of 115 * {@code SUCCESS} that has one 116 * or more response controls. 117 * @param displaySuccessResultWithoutControls Indicates whether to display 118 * information about a bind 119 * result with a result code of 120 * {@code SUCCESS} that has no 121 * response controls. 122 */ 123 public ReportBindResultLDAPConnectionPoolHealthCheck( 124 final CommandLineTool tool, 125 final boolean displaySuccessResultWithControls, 126 final boolean displaySuccessResultWithoutControls) 127 { 128 this.tool = tool; 129 this.displaySuccessResultWithControls = displaySuccessResultWithControls; 130 this.displaySuccessResultWithoutControls = 131 displaySuccessResultWithoutControls; 132 133 wrapColumn = StaticUtils.TERMINAL_WIDTH_COLUMNS - 1; 134 } 135 136 137 138 /** 139 * {@inheritDoc} 140 */ 141 @Override() 142 public void ensureConnectionValidAfterAuthentication( 143 final LDAPConnection connection, 144 final BindResult bindResult) 145 throws LDAPException 146 { 147 if (bindResult.getResultCode() == ResultCode.SUCCESS) 148 { 149 final boolean displayResult; 150 if (bindResult.hasResponseControl()) 151 { 152 displayResult = displaySuccessResultWithControls; 153 } 154 else 155 { 156 displayResult = displaySuccessResultWithoutControls; 157 } 158 159 if (displayResult) 160 { 161 final ArrayList<String> lines = new ArrayList<>(10); 162 lines.add("# " + INFO_REPORT_BIND_RESULT_HEADER.get()); 163 164 ResultUtils.formatResult(lines, bindResult, true, false, 5, wrapColumn); 165 for (final String line : lines) 166 { 167 tool.out(line); 168 } 169 tool.out(); 170 } 171 } 172 else 173 { 174 final ArrayList<String> lines = new ArrayList<>(10); 175 lines.add("# " + INFO_REPORT_BIND_RESULT_HEADER.get()); 176 177 ResultUtils.formatResult(lines, bindResult, true, false, 0, wrapColumn); 178 for (final String line : lines) 179 { 180 tool.err(line); 181 } 182 tool.err(); 183 } 184 } 185 186 187 188 /** 189 * {@inheritDoc} 190 */ 191 @Override() 192 public void toString(final StringBuilder buffer) 193 { 194 buffer.append("ReportBindResultLDAPConnectionPoolHealthCheck(" + 195 "displaySuccessResultWithControls="); 196 buffer.append(displaySuccessResultWithControls); 197 buffer.append(", displaySuccessResultWithoutControls="); 198 buffer.append(displaySuccessResultWithoutControls); 199 buffer.append(')'); 200 } 201}