001/* 002 * Copyright 2016-2019 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2016-2019 Ping Identity Corporation 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.ldap.sdk.unboundidds.tools; 022 023 024 025import java.util.ArrayList; 026 027import com.unboundid.ldap.sdk.BindResult; 028import com.unboundid.ldap.sdk.LDAPConnection; 029import com.unboundid.ldap.sdk.LDAPConnectionPoolHealthCheck; 030import com.unboundid.ldap.sdk.LDAPException; 031import com.unboundid.ldap.sdk.ResultCode; 032import com.unboundid.util.CommandLineTool; 033import com.unboundid.util.StaticUtils; 034import com.unboundid.util.ThreadSafety; 035import com.unboundid.util.ThreadSafetyLevel; 036 037import static com.unboundid.ldap.sdk.unboundidds.tools.ToolMessages.*; 038 039 040 041/** 042 * This class provides an implementation of a connection pool health check that 043 * can display information about the result of a bind operation. It will always 044 * report information about an unsuccessful bind. It may optionally report 045 * information about a successful bind, and optionally only if the successful 046 * bind includes one or more response controls. 047 * <BR> 048 * <BLOCKQUOTE> 049 * <B>NOTE:</B> This class, and other classes within the 050 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 051 * supported for use against Ping Identity, UnboundID, and 052 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 053 * for proprietary functionality or for external specifications that are not 054 * considered stable or mature enough to be guaranteed to work in an 055 * interoperable way with other types of LDAP servers. 056 * </BLOCKQUOTE> 057 * <BR> 058 * Note that this health check is only intended to generate output when 059 * appropriate and will never throw an exception to indicate that a connection 060 * is unusable. If additional health checking is required, then this health 061 * check may be combined with others via an aggregate health check in a manner 062 * that ensures this health check will be invoked before any others that may 063 * throw an exception. 064 */ 065@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 066public final class ReportBindResultLDAPConnectionPoolHealthCheck 067 extends LDAPConnectionPoolHealthCheck 068{ 069 // Indicates whether to display result details for successful binds that 070 // include one or more response controls. 071 private final boolean displaySuccessResultWithControls; 072 073 // Indicates whether to display result details for successful binds that do 074 // not include any response controls. 075 private final boolean displaySuccessResultWithoutControls; 076 077 // The tool whose output and error streams will be used for displaying result 078 // details. 079 private final CommandLineTool tool; 080 081 // The column at which to wrap long lines. 082 private final int wrapColumn; 083 084 085 086 /** 087 * Creates a new instance of this health check with the provided information. 088 * 089 * @param tool The tool with which this 090 * health check is associated. 091 * Any success messages written 092 * will be sent to the tool's 093 * standard output stream. Any 094 * failure messages written will 095 * be sent to the tool's standard 096 * error stream. 097 * @param displaySuccessResultWithControls Indicates whether to display 098 * information about a bind 099 * result with a result code of 100 * {@code SUCCESS} that has one 101 * or more response controls. 102 * @param displaySuccessResultWithoutControls Indicates whether to display 103 * information about a bind 104 * result with a result code of 105 * {@code SUCCESS} that has no 106 * response controls. 107 */ 108 public ReportBindResultLDAPConnectionPoolHealthCheck( 109 final CommandLineTool tool, 110 final boolean displaySuccessResultWithControls, 111 final boolean displaySuccessResultWithoutControls) 112 { 113 this.tool = tool; 114 this.displaySuccessResultWithControls = displaySuccessResultWithControls; 115 this.displaySuccessResultWithoutControls = 116 displaySuccessResultWithoutControls; 117 118 wrapColumn = StaticUtils.TERMINAL_WIDTH_COLUMNS - 1; 119 } 120 121 122 123 /** 124 * {@inheritDoc} 125 */ 126 @Override() 127 public void ensureConnectionValidAfterAuthentication( 128 final LDAPConnection connection, 129 final BindResult bindResult) 130 throws LDAPException 131 { 132 if (bindResult.getResultCode() == ResultCode.SUCCESS) 133 { 134 final boolean displayResult; 135 if (bindResult.hasResponseControl()) 136 { 137 displayResult = displaySuccessResultWithControls; 138 } 139 else 140 { 141 displayResult = displaySuccessResultWithoutControls; 142 } 143 144 if (displayResult) 145 { 146 final ArrayList<String> lines = new ArrayList<>(10); 147 lines.add("# " + INFO_REPORT_BIND_RESULT_HEADER.get()); 148 149 ResultUtils.formatResult(lines, bindResult, true, false, 5, wrapColumn); 150 for (final String line : lines) 151 { 152 tool.out(line); 153 } 154 tool.out(); 155 } 156 } 157 else 158 { 159 final ArrayList<String> lines = new ArrayList<>(10); 160 lines.add("# " + INFO_REPORT_BIND_RESULT_HEADER.get()); 161 162 ResultUtils.formatResult(lines, bindResult, true, false, 0, wrapColumn); 163 for (final String line : lines) 164 { 165 tool.err(line); 166 } 167 tool.err(); 168 } 169 } 170 171 172 173 /** 174 * {@inheritDoc} 175 */ 176 @Override() 177 public void toString(final StringBuilder buffer) 178 { 179 buffer.append("ReportBindResultLDAPConnectionPoolHealthCheck(" + 180 "displaySuccessResultWithControls="); 181 buffer.append(displaySuccessResultWithControls); 182 buffer.append(", displaySuccessResultWithoutControls="); 183 buffer.append(displaySuccessResultWithoutControls); 184 buffer.append(')'); 185 } 186}