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.server.schema.bootstrap;
021    
022    
023    import java.lang.reflect.InvocationTargetException;
024    import java.lang.reflect.Method;
025    
026    import javax.naming.NamingException;
027    
028    import org.apache.directory.server.schema.DnNormalizer;
029    import org.apache.directory.server.schema.NameAndOptionalUIDNormalizer;
030    import org.apache.directory.server.schema.registries.Registries;
031    import org.apache.directory.shared.ldap.constants.SchemaConstants;
032    import org.apache.directory.shared.ldap.schema.Normalizer;
033    import org.apache.directory.shared.ldap.schema.normalizers.BooleanNormalizer;
034    import org.apache.directory.shared.ldap.schema.normalizers.CachingNormalizer;
035    import org.apache.directory.shared.ldap.schema.normalizers.DeepTrimNormalizer;
036    import org.apache.directory.shared.ldap.schema.normalizers.DeepTrimToLowerNormalizer;
037    import org.apache.directory.shared.ldap.schema.normalizers.NoOpNormalizer;
038    import org.apache.directory.shared.ldap.schema.normalizers.ObjectIdentifierNormalizer;
039    
040    
041    /**
042     * A bootstrap producer which creates and announces newly created Normalizers
043     * for various matchingRules in the core schema.
044     *
045     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046     * @version $Rev: 798550 $
047     */
048    public class SystemNormalizerProducer extends AbstractBootstrapProducer
049    {
050        public SystemNormalizerProducer()
051        {
052            super( ProducerTypeEnum.NORMALIZER_PRODUCER );
053        }
054    
055        
056        public static class CachingDeepTrimToLowerNormalizer extends CachingNormalizer
057        {
058            private static final long serialVersionUID = 1L;
059    
060            public CachingDeepTrimToLowerNormalizer()
061            {
062                super( new DeepTrimToLowerNormalizer() );
063            }
064        }
065        
066        
067        public static class CachingDeepTrimNormalizer extends CachingNormalizer
068        {
069            private static final long serialVersionUID = 1L;
070    
071            public CachingDeepTrimNormalizer()
072            {
073                super( new DeepTrimNormalizer() );
074            }
075        }
076        
077        
078        public static class CachingDnNormalizer extends CachingNormalizer
079        {
080            private static final long serialVersionUID = 1L;
081    
082            /** Used for looking up the setRegistries(Registries) method */
083            private final static Class<?>[] parameterTypes = new Class<?>[] { Registries.class };
084    
085            
086            public CachingDnNormalizer()
087            {
088                super( DnNormalizer.INSTANCE );
089            }
090    
091            
092            public void setRegistries( Registries registries ) throws NamingException
093            {
094                injectRegistries( super.normalizer, registries );
095            }
096            
097            
098            private void injectRegistries( Object obj, Registries registries ) throws NamingException
099            {
100                String className = obj.getClass().getName();
101                
102                try
103                {
104                    Method method = obj.getClass().getMethod( "setRegistries", parameterTypes );
105                    
106                    if ( method == null )
107                    {
108                        return;
109                    }
110                    
111                    Object[] args = new Object[] { registries };
112                    method.invoke( obj, args );
113                }
114                catch ( SecurityException e )
115                {
116                    NamingException ne = new NamingException( "SyntaxChecker class "+ className 
117                        + " could not have the Registries dependency injected." );
118                    ne.setRootCause( e );
119                    throw ne;
120                }
121                catch ( NoSuchMethodException e )
122                {
123                    // this is ok since not every object may have setRegistries()
124                }
125                catch ( IllegalArgumentException e )
126                {
127                    NamingException ne = new NamingException( "SyntaxChecker class "+ className 
128                        + " could not have the Registries dependency injected." );
129                    ne.setRootCause( e );
130                    throw ne;
131                }
132                catch ( IllegalAccessException e )
133                {
134                    NamingException ne = new NamingException( "SyntaxChecker class "+ className 
135                        + " could not have the Registries dependency injected." );
136                    ne.setRootCause( e );
137                    throw ne;
138                }
139                catch ( InvocationTargetException e )
140                {
141                    NamingException ne = new NamingException( "SyntaxChecker class "+ className 
142                        + " could not have the Registries dependency injected." );
143                    ne.setRootCause( e );
144                    throw ne;
145                }
146            }
147        }
148        
149    
150        public void produce( Registries registries, ProducerCallback cb ) throws NamingException
151        {
152            Normalizer normalizer;
153    
154            /*
155             * Straight out of RFC 4517
156             * =======================================
157             */
158            
159            /*
160             * ( 2.5.13.0 NAME 'objectIdentifierMatch'
161             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )
162             */
163            normalizer = new ObjectIdentifierNormalizer();
164            cb.schemaObjectProduced( this, SchemaConstants.OBJECT_IDENTIFIER_MATCH_MR_OID, normalizer );
165    
166            /*
167             * ( 2.5.13.1 NAME 'distinguishedNameMatch'
168             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )
169             */
170            normalizer = new CachingDnNormalizer();
171            ( ( CachingDnNormalizer ) normalizer ).setRegistries( registries );
172            cb.schemaObjectProduced( this, SchemaConstants.DISTINGUISHED_NAME_MATCH_MR_OID, normalizer );
173    
174            /*
175             * ( 2.5.13.2 NAME 'caseIgnoreMatch'
176             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
177             */
178            normalizer = new CachingDeepTrimToLowerNormalizer();
179            cb.schemaObjectProduced( this, SchemaConstants.CASE_IGNORE_MATCH_MR_OID, normalizer );
180    
181            /*
182             * ( 2.5.13.3 NAME 'caseIgnoreOrderingMatch'
183             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
184             */
185            normalizer = new CachingDeepTrimToLowerNormalizer();
186            cb.schemaObjectProduced( this, SchemaConstants.CASE_IGNORE_ORDERING_MATCH_MR_OID, normalizer );
187    
188            /*
189             * ( 2.5.13.4 NAME 'caseIgnoreSubstringsMatch'
190             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )
191             */
192            normalizer = new CachingDeepTrimToLowerNormalizer();
193            cb.schemaObjectProduced( this, SchemaConstants.CASE_IGNORE_SUBSTRING_MATCH_MR_OID, normalizer );
194    
195            /*
196             * ( 2.5.13.5 NAME 'caseExactMatch'
197             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
198             */
199            normalizer = new CachingDeepTrimNormalizer();
200            cb.schemaObjectProduced( this, SchemaConstants.CASE_EXACT_MATCH_MR_OID, normalizer );
201            
202            /*
203             * ( 2.5.13.6 NAME 'caseExactOrderingMatch'
204             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
205             */
206            normalizer = new NoOpNormalizer();
207            cb.schemaObjectProduced( this, SchemaConstants.CASE_EXACT_ORDERING_MATCH_MR_OID, normalizer );
208    
209            /*
210             * ( 2.5.13.7 NAME 'caseExactSubstringsMatch'
211             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )
212             */
213            normalizer = new CachingDeepTrimNormalizer();
214            cb.schemaObjectProduced( this, SchemaConstants.CASE_EXACT_SUBSTRING_MATCH_MR_OID, normalizer );
215    
216            /*
217             * ( 2.5.13.8 NAME 'numericStringMatch'
218             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )
219             */
220            normalizer = new NoOpNormalizer();
221            cb.schemaObjectProduced( this, SchemaConstants.NUMERIC_STRING_MATCH_MR_OID, normalizer );
222    
223            /*
224             * ( 2.5.13.9 NAME 'numericStringOrderingMatch'
225             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )
226             */
227            normalizer = new NoOpNormalizer();
228            cb.schemaObjectProduced( this, SchemaConstants.NUMERIC_STRING_ORDERING_MATCH_MR_OID, normalizer );
229    
230            /*
231             * ( 2.5.13.10 NAME 'numericStringSubstringsMatch'
232             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )
233             */
234            normalizer = new NoOpNormalizer();
235            cb.schemaObjectProduced( this, SchemaConstants.NUMERIC_STRING_SUBSTRINGS_MATCH_MR_OID, normalizer );
236    
237            /*
238             * ( 2.5.13.11 NAME 'caseIgnoreListMatch'
239             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.41 )
240             */
241            normalizer = new CachingDeepTrimToLowerNormalizer();
242            cb.schemaObjectProduced( this, SchemaConstants.CASE_IGNORE_LIST_MATCH_MR_OID, normalizer );
243    
244            /*
245             * ( 2.5.13.12 NAME 'caseIgnoreListSubstringsMatch'
246             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )
247             */
248            normalizer = new CachingDeepTrimToLowerNormalizer();
249            cb.schemaObjectProduced( this, SchemaConstants.CASE_IGNORE_LIST_SUBSTRINGS_MATCH_MR_OID, normalizer );
250    
251            /*
252             * ( 2.5.13.13 NAME 'booleanMatch'
253             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 )
254             */
255            normalizer = new BooleanNormalizer();
256            cb.schemaObjectProduced( this, SchemaConstants.BOOLEAN_MATCH_MR_OID, normalizer );
257    
258            /*
259             * ( 2.5.13.14 NAME 'integerMatch'
260             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )
261             */
262            normalizer = new NoOpNormalizer();
263            cb.schemaObjectProduced( this, SchemaConstants.INTEGER_MATCH_MR_OID, normalizer );
264    
265            /*
266             * ( 2.5.13.15 NAME 'integerOrderingMatch'
267             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )
268             */
269            normalizer = new NoOpNormalizer();
270            cb.schemaObjectProduced( this, SchemaConstants.INTEGER_ORDERING_MATCH_MR_OID, normalizer );
271    
272            /*
273             * ( 2.5.13.16 NAME 'bitStringMatch'
274             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.6 )
275             */
276            normalizer = new NoOpNormalizer();
277            cb.schemaObjectProduced( this, SchemaConstants.BIT_STRING_MATCH_MR_OID, normalizer );
278    
279            /*
280             * ( 2.5.13.17 NAME 'octetStringMatch'
281             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )
282             */
283            normalizer = new NoOpNormalizer();
284            cb.schemaObjectProduced( this, SchemaConstants.OCTET_STRING_MATCH_MR_OID, normalizer );
285    
286            /*
287             * ( 2.5.13.18 NAME 'octetStringOrderingMatch'
288             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )
289             */
290            normalizer = new NoOpNormalizer();
291            cb.schemaObjectProduced( this, SchemaConstants.OCTET_STRING_ORDERING_MATCH_MR_OID, normalizer );
292    
293            /*
294             * ( 2.5.13.19 NAME 'octetStringSubstringsMatch'
295             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )
296             */
297            normalizer = new NoOpNormalizer();
298            cb.schemaObjectProduced( this, SchemaConstants.OCTET_STRING_SUBSTRINGS_MATCH_MR_OID, normalizer );
299            
300            /*
301             * ( 2.5.13.20 NAME 'telephoneNumberMatch'
302             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.50 )
303             */
304            normalizer = new NoOpNormalizer();
305            cb.schemaObjectProduced( this, SchemaConstants.TELEPHONE_NUMBER_MATCH_MR_OID, normalizer );
306    
307            /*
308             * ( 2.5.13.21 NAME 'telephoneNumberSubstringsMatch'
309             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )
310             */
311            normalizer = new NoOpNormalizer();
312            cb.schemaObjectProduced( this, SchemaConstants.TELEPHONE_NUMBER_SUBSTRINGS_MATCH_MR_OID, normalizer );
313    
314            /*
315             * ( 2.5.13.22 NAME 'presentationAddressMatch'
316             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.43 )
317             */
318            normalizer = new NoOpNormalizer();
319            cb.schemaObjectProduced( this, SchemaConstants.PRESENTATION_ADDRESS_MATCH_MATCH_MR_OID, normalizer );
320    
321            /*
322             * ( 2.5.13.23 NAME 'uniqueMemberMatch'
323             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.34 )
324             */
325            normalizer = NameAndOptionalUIDNormalizer.INSTANCE;
326            cb.schemaObjectProduced( this, SchemaConstants.UNIQUE_MEMBER_MATCH_MATCH_MR_OID, normalizer );
327    
328            /*
329             * ( 2.5.13.24 NAME 'protocolInformationMatch'
330             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.42 )
331             * 
332             * This MatchingRule has been removed from RFC 4517
333             */
334            normalizer = new CachingDeepTrimNormalizer();
335            cb.schemaObjectProduced( this, SchemaConstants.PROTOCOL_INFORMATION_MATCH_MATCH_MR_OID, normalizer );
336    
337            // 2.5.13.25 is not defined ...
338            
339            // 2.5.13.26 is not defined ...
340            
341            /*
342             * ( 2.5.13.27 NAME 'generalizedTimeMatch'
343             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )
344             */
345            normalizer = new CachingDeepTrimNormalizer();
346            cb.schemaObjectProduced( this, SchemaConstants.GENERALIZED_TIME_MATCH_MR_OID, normalizer );
347    
348            /*
349             * ( 2.5.13.28 NAME 'generalizedTimeOrderingMatch'
350             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )
351             */
352            normalizer = new CachingDeepTrimNormalizer();
353            cb.schemaObjectProduced( this, SchemaConstants.GENERALIZED_TIME_ORDERING_MATCH_MR_OID, normalizer );
354    
355            /*
356             * ( 2.5.13.29 NAME 'integerFirstComponentMatch'
357             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )
358             */
359            normalizer = new NoOpNormalizer();
360            cb.schemaObjectProduced( this, SchemaConstants.INTEGER_FIRST_COMPONENT_MATCH_MR_OID, normalizer );
361    
362            /*
363             * ( 2.5.13.30 NAME 'objectIdentifierFirstComponentMatch'
364             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )
365             */
366            normalizer = new NoOpNormalizer();
367            cb.schemaObjectProduced( this, SchemaConstants.OBJECT_IDENTIFIER_FIRST_COMPONENT_MATCH_MR_OID, normalizer );
368    
369            /*
370             * ( 2.5.13.31 NAME 'directoryStringFirstComponentMatch'
371             *   SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
372             */
373            normalizer = new NoOpNormalizer();
374            cb.schemaObjectProduced( this, SchemaConstants.DIRECTORY_STRING_FIRST_COMPONENT_MATCH_MR_OID, normalizer );
375    
376            /*
377             * ( 2.5.13.32 NAME 'wordMatch'
378             *   SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
379             */
380            normalizer = new NoOpNormalizer();
381            cb.schemaObjectProduced( this, SchemaConstants.WORD_MATCH_MR_OID, normalizer );
382    
383            /*
384             * ( 2.5.13.33 NAME 'keywordMatch'
385             *   SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
386             */
387            normalizer = new NoOpNormalizer();
388            cb.schemaObjectProduced( this, SchemaConstants.KEYWORD_MATCH_MR_OID, normalizer );
389    
390            /*
391             * ( 1.3.6.1.4.1.1466.109.114.1 NAME 'caseExactIA5Match'
392             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
393             */
394            normalizer = new CachingDeepTrimNormalizer();
395            cb.schemaObjectProduced( this, SchemaConstants.CASE_EXACT_IA5_MATCH_MR_OID, normalizer );
396    
397            /*
398             * ( 1.3.6.1.4.1.1466.109.114.2 NAME 'caseIgnoreIA5Match'
399             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
400             */
401            normalizer = new CachingDeepTrimToLowerNormalizer();
402            cb.schemaObjectProduced( this, SchemaConstants.CASE_IGNORE_IA5_MATCH_MR_OID, normalizer );
403    
404            /*
405             * ( 1.3.6.1.4.1.1466.109.114.3 NAME 'caseIgnoreIA5SubstringsMatch'
406             * SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )
407             */
408    
409            normalizer = new CachingDeepTrimToLowerNormalizer();
410            cb.schemaObjectProduced( this, SchemaConstants.CASE_IGNORE_IA5_SUBSTRINGS_MATCH_MR_OID, normalizer );
411        }
412    }