001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.exception;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
025
026
027/**
028 * Makes a {@link LdapOperationException} unambiguous with respect to the result code
029 * it corresponds to by associating an LDAP specific result code with it.
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class LdapSchemaViolationException extends LdapOperationException
034{
035    /** The serial version UUID */
036    static final long serialVersionUID = 1L;
037
038
039    /**
040     * Creates a new instance of LdapSchemaViolationException.
041     *
042     * @param resultCode the ResultCodeEnum for this exception
043     * @param message The exception message
044     */
045    public LdapSchemaViolationException( ResultCodeEnum resultCode, String message )
046    {
047        super( message );
048        checkResultCode( resultCode );
049        this.resultCode = resultCode;
050    }
051
052
053    /**
054     *
055     * @param resultCode the ResultCodeEnum for this exception
056     * @param message The exception message
057     * @param cause The root cause for this exception
058     */
059    public LdapSchemaViolationException( ResultCodeEnum resultCode, String message, Throwable cause )
060    {
061        super( message, cause );
062        checkResultCode( resultCode );
063        this.resultCode = resultCode;
064    }
065
066
067    /**
068     * Creates a new instance of LdapSchemaViolationException.
069     * 
070     * @param resultCode the ResultCodeEnum for this exception
071     */
072    public LdapSchemaViolationException( ResultCodeEnum resultCode )
073    {
074        super( null );
075        checkResultCode( resultCode );
076        this.resultCode = resultCode;
077    }
078
079
080    /**
081     * Checks to make sure the resultCode value is right for this exception
082     * type.
083     * 
084     * @throws IllegalArgumentException
085     *             if the result code is not one of
086     *             {@link ResultCodeEnum#OBJECT_CLASS_VIOLATION},
087     *             {@link ResultCodeEnum#NOT_ALLOWED_ON_RDN}.
088     *             {@link org.apache.directory.api.ldap.model.message.ResultCodeEnum#OBJECT_CLASS_MODS_PROHIBITED}.
089     */
090    private void checkResultCode( ResultCodeEnum resultCode )
091    {
092        switch ( resultCode )
093        {
094            case OBJECT_CLASS_VIOLATION:
095            case NOT_ALLOWED_ON_RDN:
096            case OBJECT_CLASS_MODS_PROHIBITED:
097            case INVALID_ATTRIBUTE_SYNTAX: // may happen during schema processing
098                return;
099
100            default:
101                throw new IllegalArgumentException( I18n.err( I18n.ERR_04140_UNACCEPTABLE_RESULT_CODE, resultCode ) );
102        }
103    }
104}