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;
18  
19  
20  import org.apache.ldap.common.schema.SyntaxChecker;
21  import org.apache.ldap.server.SystemPartition;
22  import org.apache.ldap.server.schema.bootstrap.BootstrapSyntaxCheckerRegistry;
23  
24  import javax.naming.NamingException;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  
29  /***
30   * A simple POJO implementation of the SyntaxCheckerRegistry service interface.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 159259 $
34   */
35  public class GlobalSyntaxCheckerRegistry implements SyntaxCheckerRegistry
36  {
37      /*** the syntaxCheckers in this registry */
38      private final Map syntaxCheckers;
39      /*** maps an OID to a schema name*/
40      private final Map oidToSchema;
41      /*** the monitor for delivering callback events */
42      private SyntaxCheckerRegistryMonitor monitor;
43      /*** the underlying bootstrap registry to delegate on misses to */
44      private BootstrapSyntaxCheckerRegistry bootstrap;
45      /*** the system partition where we keep attributeType updates */
46      private SystemPartition systemPartition;
47  
48  
49      // ------------------------------------------------------------------------
50      // C O N S T R U C T O R S
51      // ------------------------------------------------------------------------
52  
53  
54      /***
55       * Creates a default SyntaxCheckerRegistry by initializing the map and the
56       * montior.
57       */
58      public GlobalSyntaxCheckerRegistry( SystemPartition systemPartition,
59              BootstrapSyntaxCheckerRegistry bootstrap )
60      {
61          this.oidToSchema = new HashMap();
62          this.syntaxCheckers = new HashMap();
63          this.monitor = new SyntaxCheckerRegistryMonitorAdapter();
64  
65          this.bootstrap = bootstrap;
66          if ( this.bootstrap == null )
67          {
68              throw new NullPointerException( "the bootstrap registry cannot be null" ) ;
69          }
70  
71          this.systemPartition = systemPartition;
72          if ( this.systemPartition == null )
73          {
74              throw new NullPointerException( "the system partition cannot be null" ) ;
75          }
76      }
77  
78  
79      /***
80       * Sets the monitor used by this registry.
81       *
82       * @param monitor the monitor to set for registry event callbacks
83       */
84      public void setMonitor( SyntaxCheckerRegistryMonitor monitor )
85      {
86          this.monitor = monitor;
87      }
88  
89  
90      // ------------------------------------------------------------------------
91      // Service Methods
92      // ------------------------------------------------------------------------
93  
94  
95      public void register( String schema, String oid, SyntaxChecker syntaxChecker )
96              throws NamingException
97      {
98          if ( syntaxCheckers.containsKey( oid ) || bootstrap.hasSyntaxChecker( oid ) )
99          {
100             NamingException e = new NamingException( "SyntaxChecker with OID "
101                 + oid + " already registered!" );
102             monitor.registerFailed( oid, syntaxChecker, e );
103             throw e;
104         }
105 
106         oidToSchema.put( oid, schema );
107         syntaxCheckers.put( oid, syntaxChecker );
108         monitor.registered( oid, syntaxChecker );
109     }
110 
111 
112     public SyntaxChecker lookup( String oid ) throws NamingException
113     {
114         SyntaxChecker c;
115         NamingException e;
116 
117         if ( syntaxCheckers.containsKey( oid ) )
118         {
119             c = ( SyntaxChecker ) syntaxCheckers.get( oid );
120             monitor.lookedUp( oid, c );
121             return c;
122         }
123 
124         if ( bootstrap.hasSyntaxChecker( oid ) )
125         {
126             c = bootstrap.lookup( oid );
127             monitor.lookedUp( oid, c );
128             return c;
129         }
130 
131         e = new NamingException( "SyntaxChecker not found for OID: " + oid );
132         monitor.lookupFailed( oid, e );
133         throw e;
134     }
135 
136 
137     public boolean hasSyntaxChecker( String oid )
138     {
139         return syntaxCheckers.containsKey( oid ) || bootstrap.hasSyntaxChecker( oid );
140     }
141 
142 
143     public String getSchemaName( String oid ) throws NamingException
144     {
145         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
146         {
147             throw new NamingException( "OID " + oid + " is not a numeric OID" );
148         }
149         
150         if ( oidToSchema.containsKey( oid ) )
151         {
152             return ( String ) oidToSchema.get( oid );
153         }
154 
155         if ( bootstrap.hasSyntaxChecker( oid ) )
156         {
157             return bootstrap.getSchemaName( oid );
158         }
159 
160         throw new NamingException( "OID " + oid + " not found in oid to " +
161             "schema name map!" );
162     }
163 }