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.io.Serializable;
024    import java.util.Comparator;
025    
026    import javax.naming.NamingException;
027    
028    import jdbm.helper.StringComparator;
029    
030    import org.apache.directory.server.schema.bootstrap.ProducerTypeEnum;
031    import org.apache.directory.server.schema.registries.Registries;
032    import org.apache.directory.shared.ldap.schema.comparators.NormalizingComparator;
033    import org.apache.directory.shared.ldap.schema.normalizers.DeepTrimToLowerNormalizer;
034    import org.apache.directory.shared.ldap.util.StringTools;
035    
036    
037    
038    /**
039     * A producer of Comparator objects for the apachemeta schema.
040     * Modified by hand from generated code
041     *
042     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043     * @version $Rev$
044     */
045    public class ApachemetaComparatorProducer extends AbstractBootstrapProducer
046    {
047        public ApachemetaComparatorProducer()
048        {
049            super( ProducerTypeEnum.COMPARATOR_PRODUCER );
050        }
051    
052        
053        public static class DeepTrimToLowerNormalizingComparator extends NormalizingComparator
054        {
055            public DeepTrimToLowerNormalizingComparator()
056            {
057                super( new DeepTrimToLowerNormalizer(), new StringComparator() );
058            }
059        }
060    
061        
062        // ------------------------------------------------------------------------
063        // BootstrapProducer Methods
064        // We need comparators for 
065        // - nameOrNumericIdMatch      1.3.6.1.4.1.18060.0.4.0.1.0  (NameOrNumericIdComparator)
066        // - objectClassTypeMatch      1.3.6.1.4.1.18060.0.4.0.1.1  (ObjectClassTypeComparator)
067        // - numericOidMatch           1.3.6.1.4.1.18060.0.4.0.1.2  (StringComparator)
068        // - supDITStructureRuleMatch  1.3.6.1.4.1.18060.0.4.0.1.3  (DeepTrimToLowerNormalizingComparator)
069        // - ruleIDMatch               1.3.6.1.4.1.18060.0.4.0.1.4  (DeepTrimToLowerNormalizingComparator)
070        // ------------------------------------------------------------------------
071    
072    
073        /**
074         * {@inheritDoc}
075         */
076        public void produce( Registries registries, ProducerCallback cb )
077            throws NamingException
078        {
079            Comparator comparator = null;
080            
081            comparator = new NameOrNumericIdComparator();
082            cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.0", comparator );
083    
084            comparator = new ObjectClassTypeComparator();
085            cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.1", comparator );
086            
087            comparator = new StringComparator();
088            cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.2", comparator );
089            
090            comparator = new DeepTrimToLowerNormalizingComparator();
091            cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.3", comparator );
092            
093            comparator = new DeepTrimToLowerNormalizingComparator();
094            cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.4", comparator );
095        }
096    
097    
098        public static class ObjectClassTypeComparator implements Comparator<Object>, Serializable
099        {
100            private static final long serialVersionUID = 1L;
101    
102    
103            public int compare( Object o1, Object o2 )
104            {
105                String s1 = getString( o1 );
106                String s2 = getString( o2 );
107                
108                if ( s1 == null && s2 == null )
109                {
110                    return 0;
111                }
112                
113                if ( s1 == null )
114                {
115                    return -1;
116                }
117                
118                if ( s2 == null )
119                {
120                    return 1;
121                }
122                
123                return s1.compareTo( s2 );
124            }
125            
126            
127            String getString( Object obj )
128            {
129                String strValue;
130    
131                if ( obj == null )
132                {
133                    return null;
134                }
135                
136                if ( obj instanceof String )
137                {
138                    strValue = ( String ) obj;
139                }
140                else if ( obj instanceof byte[] )
141                {
142                    strValue = StringTools.utf8ToString( ( byte[] ) obj ); 
143                }
144                else
145                {
146                    strValue = obj.toString();
147                }
148    
149                return strValue;
150            }
151        }
152    }