View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.schema.registries;
21  
22  
23  import java.util.ArrayList;
24  import java.util.Comparator;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.naming.NamingException;
31  
32  import org.apache.directory.shared.ldap.schema.syntax.ComparatorDescription;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * A simple POJO implementation of the ComparatorRegistry service interface.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev: 638228 $
42   */
43  public class DefaultComparatorRegistry implements ComparatorRegistry
44  {
45      /** static class logger */
46      private static final Logger LOG = LoggerFactory.getLogger( DefaultComparatorRegistry.class );
47      
48      /** the comparators in this registry */
49      private final Map<String,Comparator> byOid;
50      
51      /** maps oids to a comparator description */
52      private final Map<String,ComparatorDescription> oidToDescription;
53  
54      /** A speedup for debug */
55      private static final boolean DEBUG = LOG.isDebugEnabled();
56  
57      // ------------------------------------------------------------------------
58      // C O N S T R U C T O R S
59      // ------------------------------------------------------------------------
60  
61  
62      /**
63       * Creates a DefaultComparatorRegistry by initializing the map and the
64       * montior.
65       */
66      public DefaultComparatorRegistry()
67      {
68          this.byOid = new HashMap<String, Comparator>();
69          this.oidToDescription = new HashMap<String,ComparatorDescription>();
70      }
71  
72  
73      // ------------------------------------------------------------------------
74      // Service Methods
75      // ------------------------------------------------------------------------
76  
77      
78      public void register( ComparatorDescription description, Comparator comparator ) throws NamingException
79      {
80          if ( byOid.containsKey( description.getNumericOid() ) )
81          {
82              throw new NamingException( "Comparator with OID " + description.getNumericOid() 
83                  + " already registered!" );
84          }
85  
86          oidToDescription.put( description.getNumericOid(), description );
87          byOid.put( description.getNumericOid(), comparator );
88          
89          if ( DEBUG )
90          {
91              LOG.debug( "registed comparator with OID: " + description.getNumericOid() );
92          }
93      }
94  
95      
96      private static String getSchema( ComparatorDescription desc )
97      {
98          List values = desc.getExtensions().get( "X-SCHEMA" );
99          
100         if ( values == null || values.size() == 0 )
101         {
102             return "other";
103         }
104         
105         return desc.getExtensions().get( "X-SCHEMA" ).get( 0 );
106     }
107     
108 
109     public Comparator lookup( String oid ) throws NamingException
110     {
111         if ( byOid.containsKey( oid ) )
112         {
113             Comparator c = byOid.get( oid );
114             
115             if ( DEBUG )
116             {
117                 LOG.debug( "looked up comparator with OID: " + oid );
118             }
119             
120             return c;
121         }
122 
123         throw new NamingException( "Comparator not found for OID: " + oid );
124     }
125 
126 
127     public boolean hasComparator( String oid )
128     {
129         return byOid.containsKey( oid );
130     }
131 
132 
133     public String getSchemaName( String oid ) throws NamingException
134     {
135         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
136         {
137             throw new NamingException( "OID " + oid + " is not a numeric OID" );
138         }
139 
140         if ( oidToDescription.containsKey( oid ) )
141         {
142             return getSchema( oidToDescription.get( oid ) );
143         }
144 
145         throw new NamingException( "OID " + oid + " not found in oid to " + "description map!" );
146     }
147 
148 
149     public Iterator<String> oidIterator()
150     {
151         return byOid.keySet().iterator();
152     }
153 
154 
155     public void unregister( String oid ) throws NamingException
156     {
157         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
158         {
159             throw new NamingException( "OID " + oid + " is not a numeric OID" );
160         }
161 
162         this.byOid.remove( oid );
163         this.oidToDescription.remove( oid );
164     }
165     
166     
167     public void unregisterSchemaElements( String schemaName )
168     {
169         List<String> oids = new ArrayList<String>( byOid.keySet() );
170         for ( String oid : oids )
171         {
172             ComparatorDescription description = oidToDescription.get( oid );
173             String schemaNameForOid = getSchema( description );
174             if ( schemaNameForOid.equalsIgnoreCase( schemaName ) )
175             {
176                 byOid.remove( oid );
177                 oidToDescription.remove( oid );
178             }
179         }
180     }
181 
182 
183     public void renameSchema( String originalSchemaName, String newSchemaName )
184     {
185         List<String> oids = new ArrayList<String>( byOid.keySet() );
186         for ( String oid : oids )
187         {
188             ComparatorDescription description = oidToDescription.get( oid );
189             String schemaNameForOid = getSchema( description );
190             if ( schemaNameForOid.equalsIgnoreCase( originalSchemaName ) )
191             {
192                 List<String> schemaExt = description.getExtensions().get( "X-SCHEMA" );
193                 schemaExt.clear();
194                 schemaExt.add( newSchemaName );
195             }
196         }
197     }
198 
199 
200     public Iterator<ComparatorDescription> comparatorDescriptionIterator()
201     {
202         return oidToDescription.values().iterator();
203     }
204 }