001/* CertPathValidatorException.java -- wraps an exception during validation 002 of a CertPath 003 Copyright (C) 2002, 2005 Free Software Foundation, Inc. 004 005This file is part of GNU Classpath. 006 007GNU Classpath is free software; you can redistribute it and/or modify 008it under the terms of the GNU General Public License as published by 009the Free Software Foundation; either version 2, or (at your option) 010any later version. 011 012GNU Classpath is distributed in the hope that it will be useful, but 013WITHOUT ANY WARRANTY; without even the implied warranty of 014MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015General Public License for more details. 016 017You should have received a copy of the GNU General Public License 018along with GNU Classpath; see the file COPYING. If not, write to the 019Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02002110-1301 USA. 021 022Linking this library statically or dynamically with other modules is 023making a combined work based on this library. Thus, the terms and 024conditions of the GNU General Public License cover the whole 025combination. 026 027As a special exception, the copyright holders of this library give you 028permission to link this library with independent modules to produce an 029executable, regardless of the license terms of these independent 030modules, and to copy and distribute the resulting executable under 031terms of your choice, provided that you also meet, for each linked 032independent module, the terms and conditions of the license of that 033module. An independent module is a module which is not derived from 034or based on this library. If you modify this library, you may extend 035this exception to your version of the library, but you are not 036obligated to do so. If you do not wish to do so, delete this 037exception statement from your version. */ 038 039 040package java.security.cert; 041 042import java.io.PrintStream; 043import java.io.PrintWriter; 044import java.security.GeneralSecurityException; 045 046/** 047 * Indicates a problem while validating a certification path. In addition, 048 * it can store the path an index in that path that caused the problem. This 049 * class is not thread-safe. 050 * 051 * @author Eric Blake (ebb9@email.byu.edu) 052 * @see CertPathValidator 053 * @since 1.4 054 * @status updated to 1.4 055*/ 056public class CertPathValidatorException extends GeneralSecurityException 057{ 058 /** 059 * Compatible with JDK 1.4+. 060 */ 061 private static final long serialVersionUID = -3083180014971893139L; 062 063 /** 064 * The index of the certificate path that failed, or -1. 065 * 066 * @serial the failed index 067 */ 068 private final int index; 069 070 /** 071 * The <code>CertPath</code> that failed. 072 * 073 * @serial the object being validated at time of failure 074 */ 075 private final CertPath certPath; 076 077 /** 078 * Create an exception without a message. The cause may be initialized. The 079 * index is set to -1 and the failed CertPath object to null. 080 */ 081 public CertPathValidatorException() 082 { 083 this((String) null); 084 } 085 086 /** 087 * Create an exception with a message. The cause may be initialized. The 088 * index is set to -1 and the failed CertPath object to null. 089 * 090 * @param msg a message to display with exception 091 */ 092 public CertPathValidatorException(String msg) 093 { 094 super(msg); 095 index = -1; 096 certPath = null; 097 } 098 099 /** 100 * Create an exception with a cause. The message will be 101 * <code>cause == null ? null : cause.toString()</code>. The index is set 102 * to -1 and the failed CertPath object to null. 103 * 104 * @param cause the cause 105 */ 106 public CertPathValidatorException(Throwable cause) 107 { 108 this(cause == null ? null : cause.toString(), cause, null, -1); 109 } 110 111 /** 112 * Create an exception with a cause and a message. The index is set to -1 113 * and the failed CertPath object to null. 114 * 115 * @param msg the message 116 * @param cause the cause 117 */ 118 public CertPathValidatorException(String msg, Throwable cause) 119 { 120 this(msg, cause, null, -1); 121 } 122 123 /** 124 * Create an exception with a cause, message, failed object, and index of 125 * failure in that CertPath. 126 * 127 * @param msg the message 128 * @param cause the cause 129 * @param certPath the path that was being validated, or null 130 * @param index the index of the path, or -1 131 * @throws IndexOutOfBoundsException if index is < -1 or 132 * > certPath.getCertificates().size() 133 * @throws IllegalArgumentException if certPath is null but index != -1 134 */ 135 public CertPathValidatorException(String msg, Throwable cause, 136 CertPath certPath, int index) 137 { 138 super(msg); 139 initCause(cause); 140 if (index < -1 || (certPath != null 141 && index >= certPath.getCertificates().size())) 142 throw new IndexOutOfBoundsException(); 143 if ((certPath == null) != (index == -1)) 144 throw new IllegalArgumentException(); 145 this.certPath = certPath; 146 this.index = index; 147 } 148 149 /** 150 * Get the detail message. 151 * 152 * @return the detail message 153 */ 154 public String getMessage() 155 { 156 return super.getMessage(); 157 } 158 159 /** 160 * Get the certificate path that had the failure, or null. 161 * 162 * @return the culprit path 163 */ 164 public CertPath getCertPath() 165 { 166 return certPath; 167 } 168 169 /** 170 * Get the index that failed, or -1. 171 * 172 * @return the colprit index 173 */ 174 public int getIndex() 175 { 176 return index; 177 } 178 179 /** 180 * Get the cause, null if unknown. 181 * 182 * @return the cause 183 */ 184 public Throwable getCause() 185 { 186 return super.getCause(); 187 } 188 189 /** 190 * Convert this to a string, including its cause. 191 * 192 * @return the string conversion 193 */ 194 public String toString() 195 { 196 return super.toString(); 197 } 198 199 /** 200 * Print the stack trace to <code>System.err</code>. 201 */ 202 public void printStackTrace() 203 { 204 super.printStackTrace(); 205 } 206 207 /** 208 * Print the stack trace to a stream. 209 * 210 * @param stream the stream 211 */ 212 public void printStackTrace(PrintStream stream) 213 { 214 super.printStackTrace(stream); 215 } 216 217 /** 218 * Print the stack trace to a stream. 219 * 220 * @param stream the stream 221 */ 222 public void printStackTrace(PrintWriter stream) 223 { 224 super.printStackTrace(stream); 225 } 226}