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.schema.normalizers;
021    
022    
023    import org.apache.directory.shared.ldap.entry.Value;
024    import org.apache.directory.shared.ldap.exception.LdapException;
025    import org.apache.directory.shared.ldap.schema.Normalizer;
026    import org.apache.directory.shared.ldap.schema.SchemaManager;
027    import org.apache.directory.shared.ldap.schema.registries.Registries;
028    //import org.apache.directory.shared.ldap.util.SynchronizedLRUMap;
029    
030    
031    /**
032     * Caches previously normalized values.
033     * 
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev: 928935 $
036     */
037    public class CachingNormalizer extends Normalizer
038    {
039        /** The serial UID */
040        public static final long serialVersionUID = 1L;
041    
042        /** Cache maximum size default */
043        public static final int CACHE_MAX = 250;
044    
045        /** Least recently used cache */
046        //private final SynchronizedLRUMap cache;
047    
048        /** The underlying decorated Normalizer */
049        protected final Normalizer normalizer;
050    
051    
052        // ------------------------------------------------------------------------
053        // C O N S T R U C T O R S
054        // ------------------------------------------------------------------------
055    
056        /**
057         * Creates a CachingNormalizer that decorates another normalizer using a
058         * default cache size.  This Normalizer delegates 
059         * 
060         * @param oid The MR OID to use with this Normalizer
061         * @param normalizer the underlying Normalizer being decorated
062         */
063        public CachingNormalizer( Normalizer normalizer )
064        {
065            this( normalizer, CACHE_MAX );
066        }
067    
068    
069        /**
070         * Creates a CachingNormalizer that decorates another normalizer using a
071         * specified cache size.
072         * 
073         * @param normalizer the underlying Normalizer being decorated
074         * @param cacheSz the maximum size of the name cache
075         */
076        public CachingNormalizer( Normalizer normalizer, int cacheSz )
077        {
078            super( normalizer.getOid() );
079            this.normalizer = normalizer;
080            //cache = new SynchronizedLRUMap( cacheSz );
081        }
082    
083    
084        /**
085         * Overrides default behavior by returning the OID of the wrapped 
086         * Normalizer.
087         */
088        @Override
089        public String getOid()
090        {
091            return normalizer.getOid();
092        }
093    
094    
095        /**
096         * Overrides default behavior by setting the OID of the wrapped Normalizer.
097         * 
098         * @param oid the object identifier to set
099         */
100        @Override
101        public void setOid( String oid )
102        {
103            super.setOid( oid );
104            normalizer.setOid( oid );
105        }
106    
107    
108        /**
109         * {@inheritDoc}
110         */
111        public Value<?> normalize( Value<?> value ) throws LdapException
112        {
113            if ( value == null )
114            {
115                return null;
116            }
117    
118            //Value<?> result = ( Value<?> ) cache.get( value );
119    
120            //if ( result != null )
121            //{
122            //    return result;
123            //}
124    
125            Value<?> normalized = normalizer.normalize( value );
126            //cache.put( value, normalized );
127            
128            return normalized;
129        }
130    
131    
132        /**
133         * {@inheritDoc}
134         */
135        public String normalize( String value ) throws LdapException
136        {
137            if ( value == null )
138            {
139                return null;
140            }
141    
142            //String normalized = ( String ) cache.get( value );
143    
144            //if ( normalized != null )
145            //{
146            //    return normalized;
147            //}
148    
149            String normalized = normalizer.normalize( value );
150            //cache.put( value, normalized );
151            return normalized;
152        }
153    
154    
155        /**
156         * {@inheritDoc}
157         */
158        public void setRegistries( Registries registries )
159        {
160            normalizer.setRegistries( registries );
161        }
162    
163    
164        /**
165         * Sets the SchemaManager
166         * 
167         * @param schemaManager The SchemaManager
168         */
169        public void setSchemaManager( SchemaManager schemaManager )
170        {
171            normalizer.setSchemaManager( schemaManager );
172        }
173    }