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