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