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.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import javax.naming.NamingException;
28  
29  import org.apache.directory.shared.ldap.schema.Syntax;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A SyntaxRegistry service available during server startup when other resources
36   * like a syntax backing store is unavailable.
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   * @version $Rev: 602007 $
40   */
41  public class DefaultSyntaxRegistry implements SyntaxRegistry
42  {
43      /** static class logger */
44      private static final Logger LOG = LoggerFactory.getLogger( DefaultSyntaxRegistry.class );
45      
46      /** Speedup for DEBUG mode */
47      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
48      
49      /** a map of entries using an OID for the key and a Syntax for the value */
50      private final Map<String,Syntax> byOid;
51      /** the OID oidRegistry this oidRegistry uses to register new syntax OIDs */
52      private final OidRegistry oidRegistry;
53  
54  
55      // ------------------------------------------------------------------------
56      // C O N S T R U C T O R S
57      // ------------------------------------------------------------------------
58  
59      /**
60       * Creates a DefaultSyntaxRegistry.
61       *
62       * @param registry used by this registry for OID to name resolution of
63       * dependencies and to automatically register and unregister it's aliases and OIDs
64       */
65      public DefaultSyntaxRegistry( OidRegistry registry )
66      {
67          this.oidRegistry = registry;
68          this.byOid = new HashMap<String,Syntax>();
69      }
70  
71  
72      // ------------------------------------------------------------------------
73      // SyntaxRegistry interface methods
74      // ------------------------------------------------------------------------
75  
76      
77      public Syntax lookup( String id ) throws NamingException
78      {
79          id = oidRegistry.getOid( id );
80  
81          if ( byOid.containsKey( id ) )
82          {
83              Syntax syntax = byOid.get( id );
84              
85              if ( IS_DEBUG )
86              {
87                  LOG.debug( "looked up using id '" + id + "': " + syntax );
88              }
89              
90              return syntax;
91          }
92  
93          throw new NamingException( "Unknown syntax OID " + id );
94      }
95  
96  
97      public void register( Syntax syntax ) throws NamingException
98      {
99          if ( byOid.containsKey( syntax.getOid() ) )
100         {
101             throw new NamingException( "syntax w/ OID " + syntax.getOid()
102                 + " has already been registered!" );
103         }
104 
105         if ( syntax.getName() != null )
106         {
107             oidRegistry.register( syntax.getName(), syntax.getOid() );
108         }
109         else
110         {
111             oidRegistry.register( syntax.getOid(), syntax.getOid() );
112         }
113 
114         byOid.put( syntax.getOid(), syntax );
115         
116         if ( IS_DEBUG )
117         {
118             LOG.debug( "registered syntax: " + syntax );
119         }
120     }
121 
122 
123     public boolean hasSyntax( String id )
124     {
125         if ( oidRegistry.hasOid( id ) )
126         {
127             try
128             {
129                 return byOid.containsKey( oidRegistry.getOid( id ) );
130             }
131             catch ( NamingException e )
132             {
133                 return false;
134             }
135         }
136 
137         return false;
138     }
139 
140 
141     public String getSchemaName( String id ) throws NamingException
142     {
143         if ( ! Character.isDigit( id.charAt( 0 ) ) )
144         {
145             throw new NamingException( "Looks like the arg is not a numeric OID" );
146         }
147 
148         id = oidRegistry.getOid( id );
149         Syntax syntax = byOid.get( id );
150         if ( syntax != null )
151         {
152             return syntax.getSchema();
153         }
154 
155         throw new NamingException( "OID " + id + " not found in oid to " + "Syntax map!" );
156     }
157 
158 
159     public Iterator<Syntax> iterator()
160     {
161         return byOid.values().iterator();
162     }
163 
164 
165     public void unregister( String numericOid ) throws NamingException
166     {
167         if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
168         {
169             throw new NamingException( "Looks like the arg is not a numeric OID" );
170         }
171 
172         byOid.remove( numericOid );
173     }
174 }