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