001
002/*
003 * The contents of this file are subject to the terms of the Common Development and
004 * Distribution License (the License). You may not use this file except in compliance with the
005 * License.
006 *
007 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
008 * specific language governing permission and limitations under the License.
009 *
010 * When distributing Covered Software, include this CDDL Header Notice in each file and include
011 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
012 * Header, with the fields enclosed by brackets [] replaced by your own identifying
013 * information: "Portions copyright [year] [name of copyright owner]".
014 *
015 * Copyright 2014-2015 ForgeRock AS.
016 */
017
018package com.sun.identity.idm;
019
020/**
021 * An exception type thrown when an {@link com.sun.identity.idm.IdRepo} is asked to
022 * create an object with a name that is already used.
023 *
024 * @supported.all.api
025 */
026public class IdRepoDuplicateObjectException extends IdRepoException {
027
028    /**
029     * This constructor is used to pass the localized error message At this
030     * level, the locale of the caller is not known and it is not possible to
031     * throw localized error message at this level. Instead this constructor
032     * provides Resource Bundle name and error code for correctly locating the
033     * error message. The default <code>getMessage()</code> will always return
034     * English messages only. This is in consistent with current JRE.
035     *
036     * @param rbName
037     *            Resource bundle Name to be used for getting localized error
038     *            message.
039     * @param errorCode
040     *            Key to resource bundle. You can use <code>ResourceBundle rb =
041     *        ResourceBunde.getBundle(rbName,locale);
042     *        String localizedStr = rb.getString(errorCode)</code>.
043     * @param args
044     *            arguments to message. If it is not present pass the as null.
045     */
046    private IdRepoDuplicateObjectException(String rbName, String errorCode, Object[] args) {
047        super(rbName, errorCode, args);
048    }
049
050    /**
051     * Create an instance using the localized {@link IdRepoErrorCode#NAME_ALREADY_EXISTS }
052     * error message populated with the provided name.
053     *
054     * @param name An identity name that is already taken
055     * @return exception with localized error message
056     */
057    public static IdRepoDuplicateObjectException nameAlreadyExists(String name) {
058        return new IdRepoDuplicateObjectException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.NAME_ALREADY_EXISTS,
059                new String[] { name });
060    }
061
062    /**
063     * Create an instance using the localized {@link IdRepoErrorCode#IDENTITY_OF_TYPE_ALREADY_EXISTS }
064     * error message populated with the provided name and type.
065     *
066     * @param name An identity name that is already taken
067     * @param type The identity type
068     * @return exception with localized error message
069     */
070    public static IdRepoDuplicateObjectException identityOfTypeAlreadyExists(String name, String type) {
071        return new IdRepoDuplicateObjectException(IdRepoBundle.BUNDLE_NAME,
072                IdRepoErrorCode.IDENTITY_OF_TYPE_ALREADY_EXISTS, new String[] { name, type });
073    }
074
075}