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 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.common.util.JoinIterator;
28  import org.apache.ldap.server.schema.bootstrap.BootstrapSyntaxRegistry;
29  
30  
31  /***
32   * A plain old java object implementation of an SyntaxRegistry.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 264732 $
36   */
37  public class GlobalSyntaxRegistry implements SyntaxRegistry
38  {
39      /*** maps an OID to an Syntax */
40      private final Map byOid;
41      /*** maps an OID to a schema name*/
42      private final Map oidToSchema;
43      /*** the registry used to resolve names to OIDs */
44      private final OidRegistry oidRegistry;
45      /*** monitor notified via callback events */
46      private SyntaxRegistryMonitor monitor;
47      /*** the underlying bootstrap registry to delegate on misses to */
48      private BootstrapSyntaxRegistry bootstrap;
49  
50  
51      // ------------------------------------------------------------------------
52      // C O N S T R U C T O R S
53      // ------------------------------------------------------------------------
54  
55  
56      /***
57       * Creates an empty BootstrapSyntaxRegistry.
58       */
59      public GlobalSyntaxRegistry( BootstrapSyntaxRegistry bootstrap, OidRegistry oidRegistry )
60      {
61          this.byOid = new HashMap();
62          this.oidToSchema = new HashMap();
63          this.oidRegistry = oidRegistry;
64          this.monitor = new SyntaxRegistryMonitorAdapter();
65  
66          this.bootstrap = bootstrap;
67          if ( this.bootstrap == null )
68          {
69              throw new NullPointerException( "the bootstrap registry cannot be null" ) ;
70          }
71      }
72  
73  
74      /***
75       * Sets the monitor that is to be notified via callback events.
76       *
77       * @param monitor the new monitor to notify of notable events
78       */
79      public void setMonitor( SyntaxRegistryMonitor monitor )
80      {
81          this.monitor = monitor;
82      }
83  
84  
85      // ------------------------------------------------------------------------
86      // Service Methods
87      // ------------------------------------------------------------------------
88  
89  
90      public void register( String schema, Syntax dITContentRule ) throws NamingException
91      {
92          if ( byOid.containsKey( dITContentRule.getOid() ) ||
93               bootstrap.hasSyntax( dITContentRule.getOid() ) )
94          {
95              NamingException e = new NamingException( "dITContentRule w/ OID " +
96                  dITContentRule.getOid() + " has already been registered!" );
97              monitor.registerFailed( dITContentRule, e );
98              throw e;
99          }
100 
101         oidRegistry.register( dITContentRule.getName(), dITContentRule.getOid() ) ;
102         byOid.put( dITContentRule.getOid(), dITContentRule );
103         oidToSchema.put( dITContentRule.getOid(), schema );
104         monitor.registered( dITContentRule );
105     }
106 
107 
108     public Syntax lookup( String id ) throws NamingException
109     {
110         id = oidRegistry.getOid( id );
111 
112         if ( byOid.containsKey( id ) )
113         {
114             Syntax dITContentRule = ( Syntax ) byOid.get( id );
115             monitor.lookedUp( dITContentRule );
116             return dITContentRule;
117         }
118 
119         if ( bootstrap.hasSyntax( id ) )
120         {
121             Syntax dITContentRule = bootstrap.lookup( id );
122             monitor.lookedUp( dITContentRule );
123             return dITContentRule;
124         }
125 
126         NamingException e = new NamingException( "dITContentRule w/ OID "
127             + id + " not registered!" );
128         monitor.lookupFailed( id, e );
129         throw e;
130     }
131 
132 
133     public boolean hasSyntax( String id )
134     {
135         if ( oidRegistry.hasOid( id ) )
136         {
137             try
138             {
139                 return byOid.containsKey( oidRegistry.getOid( id ) ) ||
140                        bootstrap.hasSyntax( id );
141             }
142             catch ( NamingException e )
143             {
144                 return false;
145             }
146         }
147 
148         return false;
149     }
150 
151 
152     public String getSchemaName( String id ) throws NamingException
153     {
154         id = oidRegistry.getOid( id );
155 
156         if ( oidToSchema.containsKey( id ) )
157         {
158             return ( String ) oidToSchema.get( id );
159         }
160 
161         if ( bootstrap.hasSyntax( id ) )
162         {
163             return bootstrap.getSchemaName( id );
164         }
165 
166         throw new NamingException( "OID " + id + " not found in oid to " +
167             "schema name map!" );
168     }
169 
170 
171     public Iterator list()
172     {
173         return new JoinIterator( new Iterator[]
174             { byOid.values().iterator(),bootstrap.list() } );
175     }
176 }