001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: IdRepoException.java,v 1.8 2009/11/19 18:18:47 bhavnab Exp $ 026 * 027 */ 028 029/* 030 * Portions Copyrighted [2011] [ForgeRock AS] 031 */ 032package com.sun.identity.idm; 033 034import com.sun.identity.shared.debug.Debug; 035import com.sun.identity.shared.locale.AMResourceBundleCache; 036import com.sun.identity.shared.locale.L10NMessage; 037import com.sun.identity.shared.locale.Locale; 038import com.sun.identity.shared.ldap.LDAPException; 039import java.text.MessageFormat; 040import java.util.ResourceBundle; 041 042/** 043 * The exception class whose instance is thrown if there is any error during the 044 * operation of objects of the <code>com.sun.identity.sms</code> package. This 045 * class maps the exception that occurred at a lower level to a high level 046 * error. Using the exception status code <code>getExceptionCode()</code> the 047 * errors are categorized as a <code>ABORT</code>, <code>RETRY</code>, 048 * <code>CONFIG_PROBLEM</code> or <code>LDAP_OP_FAILED</code> (typically a 049 * bug). 050 * 051 * @supported.all.api 052 */ 053public class IdRepoException extends Exception implements L10NMessage { 054 055 // Static variable 056 private transient AMResourceBundleCache amCache = AMResourceBundleCache 057 .getInstance(); 058 059 private transient Debug debug = AMIdentityRepository.debug; 060 061 // Instance variables 062 private String message; 063 064 private String bundleName; 065 066 private String errorCode; 067 068 private Object[] args; 069 070 private LDAPException rootCause = null; 071 072 private String ldapErrCode = null; 073 074 public IdRepoException() { 075 } 076 077 /** 078 * @param msg 079 * The message provided by the object which is throwing the 080 * exception 081 */ 082 public IdRepoException(String msg) { 083 message = msg; 084 } 085 086 public IdRepoException(String msg, String errorCode) { 087 message = msg; 088 this.errorCode = errorCode; 089 } 090 091 /** 092 * This constructor is used to pass the localized error message At this 093 * level, the locale of the caller is not known and it is not possible to 094 * throw localized error message at this level. Instead this constructor 095 * provides Resource Bundle name ,error code and LDAP error code ( in case 096 * of LDAP related exception for correctly locating the 097 * error message. The default <code>getMessage()</code> will always return 098 * English messages only. This is in consistent with current JRE. 099 * 100 * @param rbName 101 * Resource bundle Name to be used for getting localized error 102 * message. 103 * @param errorCode 104 * Key to resource bundle. You can use <code>ResourceBundle rb = 105 * ResourceBunde.getBundle(rbName,locale); 106 * String localizedStr = rb.getString(errorCode)</code>. 107 * @param ldapErrCode 108 * ldap error code 109 * @param args 110 * arguments to message. If it is not present pass the as null. 111 */ 112 public IdRepoException(String rbName, String errorCode, 113 String ldapErrCode,Object[] args) 114 { 115 this.bundleName = rbName; 116 this.errorCode = errorCode; 117 this.ldapErrCode = ldapErrCode; 118 this.args = args; 119 this.message = getL10NMessage(java.util.Locale.ENGLISH); 120 } 121 122 /** 123 * This constructor is used to pass the localized error message At this 124 * level, the locale of the caller is not known and it is not possible to 125 * throw localized error message at this level. Instead this constructor 126 * provides Resource Bundle name and error code for correctly locating the 127 * error message. The default <code>getMessage()</code> will always return 128 * English messages only. This is in consistent with current JRE. 129 * 130 * @param rbName 131 * Resource bundle Name to be used for getting localized error 132 * message. 133 * @param errorCode 134 * Key to resource bundle. You can use <code>ResourceBundle rb = 135 * ResourceBunde.getBundle(rbName,locale); 136 * String localizedStr = rb.getString(errorCode)</code>. 137 * @param args 138 * arguments to message. If it is not present pass the as null. 139 */ 140 public IdRepoException(String rbName, String errorCode, Object[] args) { 141 this.bundleName = rbName; 142 this.errorCode = errorCode; 143 this.args = args; 144 this.message = getL10NMessage(java.util.Locale.ENGLISH); 145 } 146 147 /** 148 * Returns a localized error message 149 * 150 * @param locale 151 * Uses the locale object to create the appropriate localized 152 * error message 153 * @return localized error message. 154 * @see #IdRepoException(String, String, Object[]) 155 */ 156 public String getL10NMessage(java.util.Locale locale) { 157 String result = errorCode; 158 if (bundleName != null && locale != null) { 159 ResourceBundle bundle = amCache.getResBundle(bundleName, locale); 160 String mid = Locale.getString(bundle, errorCode, debug); 161 if (args == null || args.length == 0) { 162 result = mid; 163 } else { 164 result = MessageFormat.format(mid, args); 165 } 166 } 167 return result; 168 } 169 170 /** 171 * Returns <code>ResourceBundle</code> Name associated with this error 172 * message. 173 * 174 * @return <code>ResourceBundle</code> name associated with this error 175 * message. 176 * @see #IdRepoException(String, String, Object[]) 177 */ 178 public String getResourceBundleName() { 179 return bundleName; 180 } 181 182 /** 183 * Returns error code associated with this error message. 184 * 185 * @return Error code associated with this error message. 186 * @see #IdRepoException(String, String, Object[]) 187 */ 188 public String getErrorCode() { 189 return errorCode; 190 } 191 192 /** 193 * Returns the LDAP error code associated with this error message. 194 * 195 * @return Error code associated with this error message and null if 196 * not caused by <code>LDAPException</code>. 197 * @see #IdRepoException(String, String, Object[]) 198 */ 199 public String getLDAPErrorCode() { 200 return ldapErrCode; 201 } 202 203 /** 204 * Replace the LDAP error code associated with this error message. 205 * 206 * @see #IdRepoException(String, String, Object[]) 207 */ 208 public void setLDAPErrorCode(String errorCode) { 209 ldapErrCode = errorCode; 210 } 211 212 /** 213 * Returns arguments for formatting this error message. 214 * 215 * @return arguments for formatting this error message. You need to use 216 * <code>MessageFormat</code> class to format the message It can 217 * be null. 218 * @see #IdRepoException(String, String, Object[]) 219 */ 220 public Object[] getMessageArgs() { 221 return args; 222 } 223 224 public String toString() { 225 StringBuilder buf = new StringBuilder(); 226 String msg = message; 227 if (msg != null && msg.length() > 0) { 228 buf.append("Message:"); 229 buf.append(msg); 230 buf.append("\n"); 231 } 232 return buf.toString(); 233 } 234 235 /** 236 * Returns the error message of this exception. 237 * 238 * @return String representing the error message 239 */ 240 public String getMessage() { 241 return message; 242 } 243}