View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.schema.bootstrap;
18  
19  
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.naming.NamingException;
25  
26  import org.apache.ldap.common.schema.Syntax;
27  import org.apache.ldap.server.schema.OidRegistry;
28  import org.apache.ldap.server.schema.SyntaxRegistry;
29  import org.apache.ldap.server.schema.SyntaxRegistryMonitor;
30  import org.apache.ldap.server.schema.SyntaxRegistryMonitorAdapter;
31  
32  
33  /***
34   * A SyntaxRegistry service available during server startup when other resources
35   * like a syntax backing store is unavailable.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 264732 $
39   */
40  public class BootstrapSyntaxRegistry implements SyntaxRegistry
41  {
42      /*** a map of entries using an OID for the key and a Syntax for the value */
43      private final Map byOid;
44      /*** maps an OID to a schema name*/
45      private final Map oidToSchema;
46      /*** the OID oidRegistry this oidRegistry uses to register new syntax OIDs */
47      private final OidRegistry oidRegistry;
48      /*** a monitor used to track noteable oidRegistry events */
49      private SyntaxRegistryMonitor monitor = null;
50      
51      
52      // ------------------------------------------------------------------------
53      // C O N S T R U C T O R S
54      // ------------------------------------------------------------------------
55      
56      
57      /***
58       * Creates a BootstrapSyntaxRegistry.
59       */
60      public BootstrapSyntaxRegistry( OidRegistry registry )
61      {
62          this.oidRegistry = registry;
63          this.byOid = new HashMap();
64          this.oidToSchema = new HashMap();
65          this.monitor = new SyntaxRegistryMonitorAdapter();
66      }
67      
68  
69      // ------------------------------------------------------------------------
70      // SyntaxRegistry interface methods
71      // ------------------------------------------------------------------------
72      
73      
74      /***
75       * @see org.apache.ldap.server.schema.SyntaxRegistry#lookup(java.lang.String)
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 = ( Syntax ) byOid.get( id );
84              monitor.lookedUp( syntax );
85              return syntax;
86          }
87          
88          NamingException fault = new NamingException( "Unknown syntax OID " + id );
89          monitor.lookupFailed( id, fault );
90          throw fault;
91      }
92      
93  
94      /***
95       * @see org.apache.ldap.server.schema.SyntaxRegistry#register(String,Syntax)
96       */
97      public void register( String schema, Syntax syntax ) throws NamingException
98      {
99          if ( byOid.containsKey( syntax.getOid() ) )
100         {
101             NamingException e = new NamingException( "syntax w/ OID " +
102                 syntax.getOid() + " has already been registered!" );
103             monitor.registerFailed( syntax, e );
104             throw e;
105         }
106 
107         oidRegistry.register( syntax.getName(), syntax.getOid() );
108         byOid.put( syntax.getOid(), syntax );
109         oidToSchema.put( syntax.getOid(), schema );
110         monitor.registered( syntax );
111     }
112 
113     
114     /***
115      * @see org.apache.ldap.server.schema.SyntaxRegistry#hasSyntax(java.lang.String)
116      */
117     public boolean hasSyntax( String id )
118     {
119         if ( oidRegistry.hasOid( id ) )
120         {
121             try
122             {
123                 return byOid.containsKey( oidRegistry.getOid( id ) );
124             }
125             catch ( NamingException e )
126             {
127                 return false;
128             }
129         }
130 
131         return false;
132     }
133 
134 
135     public String getSchemaName( String id ) throws NamingException
136     {
137         id = oidRegistry.getOid( id );
138         if ( oidToSchema.containsKey( id ) )
139         {
140             return ( String ) oidToSchema.get( id );
141         }
142 
143         throw new NamingException( "OID " + id + " not found in oid to " +
144             "schema name map!" );
145     }
146 
147 
148     // ------------------------------------------------------------------------
149     // package friendly monitor methods
150     // ------------------------------------------------------------------------
151     
152     
153     /***
154      * Gets the monitor for this oidRegistry.
155      * 
156      * @return the monitor
157      */
158     SyntaxRegistryMonitor getMonitor()
159     {
160         return monitor;
161     }
162 
163     
164     /***
165      * Sets the monitor for this oidRegistry.
166      * 
167      * @param monitor the monitor to set
168      */
169     void setMonitor( SyntaxRegistryMonitor monitor )
170     {
171         this.monitor = monitor;
172     }
173 
174 
175     public Iterator list()
176     {
177         return byOid.values().iterator();
178     }
179 }