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.message;
021    
022    
023    import java.util.Map;
024    
025    import org.apache.directory.shared.i18n.I18n;
026    import org.apache.directory.shared.ldap.constants.JndiPropertyConstants;
027    
028    
029    /**
030     * Type-safe derefAliases search parameter enumeration which determines the mode
031     * of alias handling. Note that the jndi values of these ValuedEnums correspond
032     * to the string value for the java.naming.ldap.derefAliases JNDI LDAP specific
033     * property.  The integer value represents the values used in the LDAP ASN.1 for
034     * different settings.
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Revision: 912436 $
038     */
039    public enum AliasDerefMode
040    {
041        /** Alias handling mode value that treats aliases like entries */
042        NEVER_DEREF_ALIASES( 0, "never" ),
043    
044        /** Alias handling mode value that dereferences only when searching */
045        DEREF_IN_SEARCHING( 1, "searching" ),
046    
047        /** Alias handling mode value that dereferences only in finding the base */
048        DEREF_FINDING_BASE_OBJ( 2, "finding" ),
049    
050        /** Alias handling mode value that dereferences always */
051        DEREF_ALWAYS( 3, "always" );
052    
053    
054        /** Stores the integer value of each element of the enumeration */
055        private int value;
056        /** Stores the integer value of each element of the enumeration */
057        private String jndiValue;
058    
059        
060        /**
061         * Private constructor so no other instances can be created other than the
062         * public static constants in this class.
063         * 
064         * @param value the integer value of the enumeration.
065         */
066        private AliasDerefMode( int value, String jndiValue )
067        {
068           this.value = value;
069           this.jndiValue = jndiValue;
070        }
071    
072        
073        /**
074         * @return The value associated with the current element.
075         */
076        public int getValue()
077        {
078            return value;
079        }
080    
081        /**
082         * Gets the enumeration from by extracting the value for the JNDI LDAP
083         * specific environment property, java.naming.ldap.derefAliases, from the
084         * environment.
085         * 
086         * @param env
087         *            the JNDI environment with a potential value for the
088         *            java.naming.ldap.derefAliases property
089         * @return the enumeration for the environment
090         */
091        public static AliasDerefMode getEnum( Map<String, Object> env )
092        {
093            String property = ( String ) env.get( JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES );
094            
095            if ( null == property )
096            {
097                return DEREF_ALWAYS;
098            }
099            else
100            {
101                if ( property.trim().equalsIgnoreCase( "always" ) )
102                {
103                    return DEREF_ALWAYS;
104                }
105                else if ( property.trim().equalsIgnoreCase( "never" ) )
106                {
107                    return NEVER_DEREF_ALIASES;
108                }
109                else if ( property.trim().equalsIgnoreCase( "finding" ) )
110                {
111                    return DEREF_FINDING_BASE_OBJ;
112                }
113                else if ( property.trim().equalsIgnoreCase( "searching" ) )
114                {
115                    return DEREF_IN_SEARCHING;
116                }
117                else
118                {
119                    throw new IllegalArgumentException( I18n.err( I18n.ERR_04186, property,
120                                    JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES ) );
121                }
122            }
123        }
124        
125        /**
126         * Checks to see if we dereference while searching and finding the base.
127         * 
128         * @return true if value is DEREF_ALWAYS, false otherwise
129         */
130        public boolean isDerefAlways()
131        {
132            return this == DEREF_ALWAYS;
133        }
134    
135    
136        /**
137         * Checks to see if we never dereference aliases.
138         * 
139         * @return true if value is NEVER_DEREF_ALIASES, false otherwise
140         */
141        public boolean isNeverDeref()
142        {
143            return this == NEVER_DEREF_ALIASES;
144        }
145    
146    
147        /**
148         * Checks to see if we dereference while searching.
149         * 
150         * @return true if value is DEREF_ALWAYS_VAL, or DEREF_IN_SEARCHING, and
151         *         false otherwise.
152         */
153        public boolean isDerefInSearching()
154        {
155            switch ( this )
156            {
157                case DEREF_ALWAYS :
158                    return true;
159                
160                case DEREF_FINDING_BASE_OBJ :
161                    return false;
162                
163                case DEREF_IN_SEARCHING :
164                    return true;
165                
166                case NEVER_DEREF_ALIASES :
167                    return false;
168                
169                default:
170                    throw new IllegalArgumentException( I18n.err( I18n.ERR_04187 ) );
171            }
172        }
173    
174    
175        /**
176         * Checks to see if we dereference while finding the base.
177         * 
178         * @return true if value is DEREF_ALWAYS, or DEREF_FINDING_BASE_OBJ, and
179         *         false otherwise.
180         */
181        public boolean isDerefFindingBase()
182        {
183            switch ( this )
184            {
185                case DEREF_ALWAYS :
186                    return true;
187                
188                case DEREF_FINDING_BASE_OBJ :
189                    return true;
190                
191                case DEREF_IN_SEARCHING :
192                    return false;
193                
194                case NEVER_DEREF_ALIASES :
195                    return false;
196                
197                default:
198                    throw new IllegalArgumentException( "Class has bug: check for valid enumeration values" );
199            }
200        }
201    
202    
203        public String getJndiValue()
204        {
205            return jndiValue;
206        }
207    }