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.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.naming.NamingException;
30  
31  import org.apache.directory.shared.ldap.schema.Normalizer;
32  import org.apache.directory.shared.ldap.schema.syntax.NormalizerDescription;
33  
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * The POJO implementation for the NormalizerRegistry service.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   * @version $Rev: 638228 $
43   */
44  public class DefaultNormalizerRegistry implements NormalizerRegistry
45  {
46      /** static class logger */
47      private static final Logger LOG = LoggerFactory.getLogger( DefaultNormalizerRegistry.class );
48      
49      /** A speedup for debug */
50      private static final boolean DEBUG = LOG.isDebugEnabled();
51      
52      /** a map of Normalizers looked up by OID */
53      private final Map<String,Normalizer> byOid;
54      
55      /** maps an OID to a normalizerDescription */
56      private final Map<String,NormalizerDescription> oidToDescription;
57  
58  
59      // ------------------------------------------------------------------------
60      // C O N S T R U C T O R S
61      // ------------------------------------------------------------------------
62  
63  
64      /**
65       * Creates a new default DefaultNormalizerRegistry.
66       */
67      public DefaultNormalizerRegistry()
68      {
69          this.byOid = new HashMap<String, Normalizer>();
70          this.oidToDescription = new HashMap<String, NormalizerDescription>();
71      }
72  
73  
74      // ------------------------------------------------------------------------
75      // Service Methods
76      // ------------------------------------------------------------------------
77  
78      
79      public void register( NormalizerDescription description, Normalizer normalizer ) throws NamingException
80      {
81          String oid = description.getNumericOid();
82          if ( byOid.containsKey( oid ) )
83          {
84              throw new NamingException( "Normalizer already " + "registered for OID " + oid );
85          }
86  
87          oidToDescription.put( oid, description );
88          byOid.put( oid, normalizer );
89          
90          if ( DEBUG )
91          {
92              LOG.debug( "registered normalizer with oid: " + oid );
93          }
94      }
95  
96  
97      public Normalizer lookup( String oid ) throws NamingException
98      {
99          if ( !byOid.containsKey( oid ) )
100         {
101             throw new NamingException( "Normalizer for OID " + oid + " does not exist!" );
102         }
103 
104         Normalizer normalizer = byOid.get( oid );
105         
106         if ( DEBUG )
107         {
108             LOG.debug( "registered normalizer with oid: " + oid );
109         }
110         
111         return normalizer;
112     }
113 
114 
115     public boolean hasNormalizer( String oid )
116     {
117         return byOid.containsKey( oid );
118     }
119 
120 
121     public String getSchemaName( String oid ) throws NamingException
122     {
123         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
124         {
125             throw new NamingException( "Looks like the arg is not a numeric OID" );
126         }
127 
128         if ( oidToDescription.containsKey( oid ) )
129         {
130             return getSchema( oidToDescription.get( oid ) );
131         }
132 
133         throw new NamingException( "OID " + oid + " not found in oid to " + "schema name map!" );
134     }
135 
136 
137     private static String getSchema( NormalizerDescription desc )
138     {
139         List values = desc.getExtensions().get( "X-SCHEMA" );
140         
141         if ( values == null || values.size() == 0 )
142         {
143             return "other";
144         }
145         
146         return desc.getExtensions().get( "X-SCHEMA" ).get( 0 );
147     }
148     
149 
150     public Iterator<String> oidIterator()
151     {
152         return byOid.keySet().iterator();
153     }
154 
155 
156     public void unregister( String oid ) throws NamingException
157     {
158         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
159         {
160             throw new NamingException( "OID " + oid + " is not a numeric OID" );
161         }
162 
163         this.byOid.remove( oid );
164         this.oidToDescription.remove( oid );
165     }
166     
167     
168     public void unregisterSchemaElements( String schemaName )
169     {
170         List<String> oids = new ArrayList<String>( byOid.keySet() );
171         for ( String oid : oids )
172         {
173             NormalizerDescription description = oidToDescription.get( oid );
174             String schemaNameForOid = getSchema( description );
175             if ( schemaNameForOid.equalsIgnoreCase( schemaName ) )
176             {
177                 byOid.remove( oid );
178                 oidToDescription.remove( oid );
179             }
180         }
181     }
182 
183 
184     public void renameSchema( String originalSchemaName, String newSchemaName )
185     {
186         List<String> oids = new ArrayList<String>( byOid.keySet() );
187         for ( String oid : oids )
188         {
189             NormalizerDescription description = oidToDescription.get( oid );
190             String schemaNameForOid = getSchema( description );
191             if ( schemaNameForOid.equalsIgnoreCase( originalSchemaName ) )
192             {
193                 List<String> schemaExt = description.getExtensions().get( "X-SCHEMA" );
194                 schemaExt.clear();
195                 schemaExt.add( newSchemaName );
196             }
197         }
198     }
199 
200 
201     public Iterator<NormalizerDescription> normalizerDescriptionIterator()
202     {
203         return oidToDescription.values().iterator();
204     }
205 }