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: AMException.java,v 1.7 2009/01/28 05:34:47 ww203982 Exp $
026 *
027 * Portions Copyright 2015 ForgeRock AS.
028 */
029
030package com.iplanet.am.sdk;
031
032import com.iplanet.sso.SSOToken;
033import com.iplanet.ums.UMSException;
034import com.sun.identity.shared.locale.L10NMessage;
035import java.text.MessageFormat;
036import java.util.Locale;
037import java.util.ResourceBundle;
038import org.forgerock.opendj.ldap.LdapException;
039
040/**
041 * The <code>AMException</code> is thrown whenever an error is is encountered
042 * while performing an operation on the data store.
043 *
044 * @deprecated  As of Sun Java System Access Manager 7.1.
045 * @supported.all.api
046 */
047public class AMException extends Exception implements L10NMessage {
048
049    private static final long serialVersionUID = -660487903675407220L;
050
051    private String localizedMsg = null;
052
053    private String errorCode = null;
054
055    private Object args[] = null;
056
057    private LdapException rootCause = null;
058
059    private String ldapErrorMsg = null;
060
061    private String ldapErrCode = null;
062
063    /**
064     * Constructs a new <code>AMException</code> with detailed message.
065     * 
066     * @param msg
067     *            The detailed message
068     * @param errorCode
069     *            Matches the appropriate entry in
070     *            <code>amProfile.properties</code>.
071     */
072    public AMException(String msg, String errorCode) {
073        super(msg);
074        this.localizedMsg = msg;
075        this.errorCode = errorCode;
076    }
077
078    /**
079     * Convenience method (protected)
080     */
081    public AMException(SSOToken token, String errorCode) {
082        this.localizedMsg = AMSDKBundle.getString(errorCode, AMCommonUtils
083                .getUserLocale(token));
084        this.errorCode = errorCode;
085    }
086
087    /**
088     * Constructs a new <code>AMException</code> with detailed message.
089     * 
090     * @param msg
091     *            The detailed message.
092     * @param errorCode
093     *            Matches the appropriate entry in
094     *            <code>amProfile.properties</code>.
095     * @param ue
096     *            if the root cause is a <code>UMSException</code>.
097     */
098    public AMException(String msg, String errorCode, UMSException ue) {
099        this(msg, errorCode, null, ue);
100    }
101
102    /**
103     * Constructs a new <code>AMException</code> with detailed message.
104     * 
105     * @param token
106     *            a valid single sign on token of the user performing the
107     *            operation.
108     * @param errorCode
109     *            Matches the appropriate entry in
110     *            <code>amProfile.properties</code>.
111     * @param ue
112     *            if the root cause is a <code>UMSException</code>.
113     */
114    public AMException(SSOToken token, String errorCode, UMSException ue) {
115        try {
116            rootCause = (LdapException) ue.getRootCause();
117            ldapErrCode = Integer.toString(rootCause.getResult().getResultCode().intValue());
118            ldapErrorMsg = AMSDKBundle.getString(ldapErrCode);
119        } catch (Exception e) {
120        }
121        String message = AMSDKBundle.getString(errorCode, AMCommonUtils
122                .getUserLocale(token));
123        if (ldapErrorMsg != null) {
124            localizedMsg = message + "::" + ldapErrorMsg;
125        } else {
126            localizedMsg = message;
127        }
128        this.errorCode = errorCode;
129    }
130
131    /**
132     * Constructs a new <code>AMException</code> with detailed message.
133     * 
134     * @param msg
135     *            The detailed message.
136     * @param errorCode
137     *            Matches the appropriate entry in
138     *            <code>amProfile.properties</code>.
139     * @param args
140     *            arguments if the error message needs specific values to be
141     *            set.
142     */
143    public AMException(String msg, String errorCode, Object[] args) {
144        super(msg);
145        this.localizedMsg = msg;
146        this.errorCode = errorCode;
147        this.args = args;
148    }
149
150    /**
151     * Constructs a new <code>AMException</code> with detailed message.
152     * 
153     * @param msg
154     *            The detailed message
155     * @param errorCode
156     *            Matches the appropriate entry in
157     *            <code>amProfile.properties</code>.
158     * @param args
159     *            if the error message needs specific values to be set.
160     * @param ue
161     *            if the root cause is a <code>UMSException</code>.
162     */
163    public AMException(String msg, String errorCode, Object args[],
164            UMSException ue) {
165        try {
166            rootCause = (LdapException) ue.getRootCause();
167            ldapErrCode = Integer.toString(rootCause.getResult().getResultCode().intValue());
168            ldapErrorMsg = AMSDKBundle.getString(ldapErrCode);
169        } catch (Exception e) {
170            // Ignore
171        }
172        if (ldapErrorMsg != null) {
173            localizedMsg = msg + "::" + ldapErrorMsg;
174        } else {
175            localizedMsg = msg;
176        }
177        this.errorCode = errorCode;
178        this.args = args;
179    }
180
181    /**
182     * Returns the error code. This error code can be used with the arguments to
183     * construct a localized message.
184     * 
185     * @return the error code which can be used to map the message to a user
186     *         specific locale.
187     */
188    public String getErrorCode() {
189        return errorCode;
190    }
191
192    /**
193     * Returns the arguments corresponding to the error code.
194     * 
195     * @return the arguments corresponding to the error code or null if no
196     *         arguments are need to construct the message.
197     */
198    public Object[] getMessageArgs() {
199        return args;
200    }
201
202    /**
203     * Overrides the default <code>getMessage()</code> method of super class
204     * Exception.
205     * 
206     * @return The error message string.
207     */
208    public String getMessage() {
209        return localizedMsg;
210    }
211
212    /**
213     * Method to obtain the LDAP error code.
214     * 
215     * @return The error code, which can be used to map the message to a
216     *         specific locale. Returns a null, if not an LDAP error.
217     */
218    public String getLDAPErrorCode() {
219        return ldapErrCode;
220    }
221
222    /**
223     * Returns the root <code>LDAPException</code> of this
224     * <code>AMException</code>, if any.
225     * 
226     * @return The {@link LdapException} that caused this
227     *         <code>AMException</code>. If null, it means no root
228     *         <code>LDAPException</code> has been set.
229     */
230    public LdapException getLDAPException() {
231        return rootCause;
232    }
233
234    /**
235     * Returns localized error message.
236     * 
237     * @param locale
238     *            locale of the error message.
239     * @return Localized error message.
240     */
241    public String getL10NMessage(Locale locale) {
242        String result = errorCode;
243        if (locale != null) {
244            ResourceBundle rb = AMSDKBundle.getBundleFromHash(locale);
245            String mid = com.sun.identity.shared.locale.Locale.getString(
246                rb, errorCode, AMCommonUtils.debug);
247            result = ((args == null) || (args.length == 0)) ? mid
248                    : MessageFormat.format(mid, args);
249        }
250        return result;
251    }
252
253    /**
254     * Returns ResourceBundle Name associated with this exception
255     * 
256     * @return ResourceBundle Name associated with this exception.
257     */
258    public String getResourceBundleName() {
259        return AMSDKBundle.BUNDLE_NAME;
260    }
261}