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.SyntaxChecker;
21  import org.apache.ldap.server.schema.SyntaxCheckerRegistry;
22  import org.apache.ldap.server.schema.SyntaxCheckerRegistryMonitor;
23  import org.apache.ldap.server.schema.SyntaxCheckerRegistryMonitorAdapter;
24  
25  import javax.naming.NamingException;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  
30  /***
31   * The POJO implementation for the SyntaxCheckerRegistry service.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev: 159259 $
35   */
36  public class BootstrapSyntaxCheckerRegistry implements SyntaxCheckerRegistry
37  {
38      /*** a map by OID of SyntaxCheckers */
39      private final Map byOid;
40      /*** maps an OID to a schema name*/
41      private final Map oidToSchema;
42      /*** the monitor to use for callback event notifications */
43      private SyntaxCheckerRegistryMonitor monitor;
44  
45  
46      // ------------------------------------------------------------------------
47      // C O N S T R U C T O R S
48      // ------------------------------------------------------------------------
49  
50  
51      /***
52       * Creates an instance of a BootstrapSyntaxRegistry.
53       */
54      public BootstrapSyntaxCheckerRegistry()
55      {
56          this.byOid = new HashMap();
57          this.oidToSchema = new HashMap();
58          this.monitor = new SyntaxCheckerRegistryMonitorAdapter();
59      }
60  
61  
62      /***
63       * Sets the monitor used to deliver notification events to via callbacks.
64       *
65       * @param monitor the monitor to recieve callback events
66       */
67      public void setMonitor( SyntaxCheckerRegistryMonitor monitor )
68      {
69          this.monitor = monitor;
70      }
71  
72  
73      // ------------------------------------------------------------------------
74      // Service Methods
75      // ------------------------------------------------------------------------
76  
77  
78      public void register( String schema, String oid, SyntaxChecker syntaxChecker )
79          throws NamingException
80      {
81          if ( byOid.containsKey( oid ) )
82          {
83              NamingException e = new NamingException( "SyntaxChecker with OID " +
84                  oid + " already registered!" );
85              monitor.registerFailed( oid, syntaxChecker,e );
86              throw e;
87          }
88  
89          byOid.put( oid, syntaxChecker );
90          oidToSchema.put( oid, schema );
91          monitor.registered( oid, syntaxChecker );
92      }
93  
94  
95      public SyntaxChecker lookup( String oid ) throws NamingException
96      {
97          if ( ! byOid.containsKey( oid ) )
98          {
99              NamingException e = new NamingException( "SyntaxChecker for OID "
100                 + oid + " not found!" );
101             monitor.lookupFailed( oid, e );
102             throw e;
103         }
104 
105         SyntaxChecker syntaxChecker = ( SyntaxChecker ) byOid.get( oid );
106         monitor.lookedUp( oid, syntaxChecker );
107         return null;
108     }
109 
110 
111     public boolean hasSyntaxChecker( String oid )
112     {
113         return byOid.containsKey( oid );
114     }
115 
116 
117     public String getSchemaName( String oid ) throws NamingException
118     {
119         if ( Character.isDigit( oid.charAt( 0 ) ) )
120         {
121             throw new NamingException( "Looks like the arg is not a numeric OID" );
122         }
123 
124         if ( oidToSchema.containsKey( oid ) )
125         {
126             return ( String ) oidToSchema.get( oid );
127         }
128 
129         throw new NamingException( "OID " + oid + " not found in oid to " +
130             "schema name map!" );
131     }
132 }