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