View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.schema.registries;
21  
22  
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import javax.naming.NamingException;
28  
29  import org.apache.directory.shared.ldap.schema.DITContentRule;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A plain old java object implementation of an DITContentRuleRegistry.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 602007 $
39   */
40  public class DefaultDitContentRuleRegistry implements DITContentRuleRegistry
41  {
42      /** static class logger */
43      private static final Logger LOG = LoggerFactory.getLogger( DefaultDitContentRuleRegistry.class );
44      /** maps an OID to an DITContentRule */
45      private final Map<String,DITContentRule> byOid;
46      /** the registry used to resolve names to OIDs */
47      private final OidRegistry oidRegistry;
48  
49  
50      // ------------------------------------------------------------------------
51      // C O N S T R U C T O R S
52      // ------------------------------------------------------------------------
53  
54      /**
55       * Creates an empty DefaultDitContentRuleRegistry.
56       *
57       * @param oidRegistry used by this registry for OID to name resolution of
58       * dependencies and to automatically register and unregister it's aliases and OIDs
59       */
60      public DefaultDitContentRuleRegistry( OidRegistry oidRegistry )
61      {
62          this.byOid = new HashMap<String,DITContentRule>();
63          this.oidRegistry = oidRegistry;
64      }
65  
66  
67      // ------------------------------------------------------------------------
68      // Service Methods
69      // ------------------------------------------------------------------------
70  
71      
72      public void register( DITContentRule dITContentRule ) throws NamingException
73      {
74          if ( byOid.containsKey( dITContentRule.getOid() ) )
75          {
76              throw new NamingException( "dITContentRule w/ OID " + dITContentRule.getOid()
77                  + " has already been registered!" );
78          }
79  
80          oidRegistry.register( dITContentRule.getName(), dITContentRule.getOid() );
81          byOid.put( dITContentRule.getOid(), dITContentRule );
82          if ( LOG.isDebugEnabled() )
83          {
84              LOG.debug( "registed dITContentRule: " + dITContentRule );
85          }
86      }
87  
88  
89      public DITContentRule lookup( String id ) throws NamingException
90      {
91          id = oidRegistry.getOid( id );
92  
93          if ( !byOid.containsKey( id ) )
94          {
95              throw new NamingException( "dITContentRule w/ OID " + id + " not registered!" );
96          }
97  
98          DITContentRule dITContentRule = byOid.get( id );
99          if ( LOG.isDebugEnabled() )
100         {
101             LOG.debug( "lookup with id '" + id + "' of dITContentRule: " + dITContentRule );
102         }
103         return dITContentRule;
104     }
105 
106 
107     public boolean hasDITContentRule( String id )
108     {
109         if ( oidRegistry.hasOid( id ) )
110         {
111             try
112             {
113                 return byOid.containsKey( oidRegistry.getOid( id ) );
114             }
115             catch ( NamingException e )
116             {
117                 return false;
118             }
119         }
120 
121         return false;
122     }
123 
124 
125     public String getSchemaName( String id ) throws NamingException
126     {
127         id = oidRegistry.getOid( id );
128         DITContentRule dcr = byOid.get( id );
129         if ( dcr != null )
130         {
131             return dcr.getSchema();
132         }
133 
134         throw new NamingException( "OID " + id + " not found in oid to " + "DITContentRule map!" );
135     }
136 
137 
138     public Iterator<DITContentRule> iterator()
139     {
140         return byOid.values().iterator();
141     }
142 
143 
144     public void unregister( String numericOid ) throws NamingException
145     {
146         if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
147         {
148             throw new NamingException( "Looks like the arg is not a numeric OID" );
149         }
150 
151         byOid.remove( numericOid );
152     }
153 }