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