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     */
020    package org.apache.directory.shared.ldap.exception;
021    
022    
023    import javax.naming.ServiceUnavailableException;
024    
025    import org.apache.directory.shared.ldap.message.ResultCodeEnum;
026    
027    
028    /**
029     * LDAP specific ServiceUnavailableException that preserves resultCode
030     * resolution.
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev: 487348 $
034     */
035    public class LdapServiceUnavailableException extends ServiceUnavailableException implements LdapException
036    {
037        static final long serialVersionUID = -5058439476235675179L;
038    
039        /** the LDAP result code to be checked */
040        private final ResultCodeEnum resultCode;
041    
042    
043        /**
044         * Creates an LDAP specific ServiceUnavailableException that preserves
045         * resultCode resolution.
046         * 
047         * @param resultCode
048         *            the LDAP result code to be checked
049         * @throws IllegalArgumentException
050         *             if the result code is not within the set
051         *             {@link ResultCodeEnum#SERVICEUNAVAILABLE_CODES}.
052         */
053        public LdapServiceUnavailableException(ResultCodeEnum resultCode)
054        {
055            super();
056            checkResultCode( resultCode );
057            this.resultCode = resultCode;
058        }
059    
060    
061        /**
062         * Creates an LDAP specific ServiceUnavailableException that preserves
063         * resultCode resolution.
064         * 
065         * @param explanation
066         *            the reason for the exception to pass to super
067         * @param resultCode
068         *            the LDAP result code to be checked
069         * @throws IllegalArgumentException
070         *             if the result code is not within the set
071         *             {@link ResultCodeEnum#SERVICEUNAVAILABLE_CODES}.
072         */
073        public LdapServiceUnavailableException(String explanation, ResultCodeEnum resultCode)
074        {
075            super( explanation );
076            checkResultCode( resultCode );
077            this.resultCode = resultCode;
078        }
079    
080    
081        /**
082         * Checks to see if the LDAP result code is valid for this exception.
083         * 
084         * @param resultCode
085         *            the LDAP result code to be checked
086         * @throws IllegalArgumentException
087         *             if the result code is not within the set
088         *             {@link ResultCodeEnum#SERVICEUNAVAILABLE_CODES}.
089         */
090        private void checkResultCode( ResultCodeEnum result )
091        {
092            if ( !ResultCodeEnum.getServiceCodes().contains( result ) )
093            {
094                String msg = "Only the following LDAP result codes can be used: " + ResultCodeEnum.getSearchCodes();
095                throw new IllegalArgumentException( msg );
096            }
097        }
098    
099    
100        /**
101         * Returns one of the resultCodes within the set {@link
102         * ResultCodeEnum#SERVICEUNAVAILABLE_CODES}.
103         * 
104         * @see LdapException#getResultCode()
105         */
106        public final ResultCodeEnum getResultCode()
107        {
108            return resultCode;
109        }
110    }