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 * Portions Copyrighted 2016 ForgeRock AS.
026 */
027
028package com.sun.identity.sm;
029
030import com.sun.identity.shared.locale.Locale;
031import java.text.MessageFormat;
032import java.util.ResourceBundle;
033
034
035/**
036 * @see java.lang.Exception
037 * @see java.lang.Throwable
038 *
039 * @supported.all.api
040 */
041public class InvalidAttributeValueException extends SMSException {
042    private String resourceBundleName;
043
044    private String rbName;
045
046    private String attributeI18nKey;
047
048    private String errCode;
049
050    /**
051     * Constructs an <code>InvalidAttributeValueException</code> with no
052     * specified detail message.
053     */
054    public InvalidAttributeValueException() {
055        super();
056    }
057
058    /**
059     * Constructs an <code>InvalidAttributeValueException</code> with the
060     * specified detail message.
061     * 
062     * @param s
063     *            the detail message.
064     */
065    public InvalidAttributeValueException(String s) {
066        super(s);
067    }
068
069    /**
070     * Constructs an <code>InvalidAttributeValueException</code> with the
071     * specified error code. It can be used to pass localized error message.
072     * 
073     * @param rbName
074     *            Resource Bundle name where localized error message is located.
075     * @param errorCode
076     *            error code or message id to be used for
077     *            <code>ResourceBundle.getString()</code> to locate error
078     *            message
079     * @param args
080     *            any arguments to be used for error message formatting
081     *            <code>getMessage()</code> will construct error message using
082     *            English resource bundle.
083     */
084    public InvalidAttributeValueException(String rbName, String errorCode,
085            Object[] args) {
086        super(rbName, errorCode, args);
087        resourceBundleName = rbName;
088        errCode = errorCode;
089
090        if (args.length > 2) {
091            this.rbName = (String) args[1];
092            this.attributeI18nKey = (String) args[2];
093        }
094    }
095
096    /**
097     * Returns a localized error message
098     * 
099     * @param locale
100     *            Uses the locale object to create the appropriate localized
101     *            error message
102     * @return localized error message.
103     */
104    public String getL10NMessage(java.util.Locale locale) {
105        String message;
106
107        if ((resourceBundleName == null) || (locale == null) || (attributeI18nKey == null) || (rbName == null)) {
108            message = super.getL10NMessage(locale);
109        } else {
110            ResourceBundle bundle = amCache.getResBundle(resourceBundleName, locale);
111            String mid = Locale.getString(bundle, errCode, debug);
112            ResourceBundle serviceResouceBundle = amCache.getResBundle(rbName, locale);
113            String localizedAttributeName = Locale.getString(serviceResouceBundle, attributeI18nKey, debug);
114            if (localizedAttributeName.equals(attributeI18nKey)) {
115                return super.getL10NMessage(locale);
116            }
117            String[] argsEx = { localizedAttributeName };
118            message = MessageFormat.format(mid, (Object[])argsEx);
119        }
120
121        return message;
122    }
123}