001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.schema.registries;
021    
022    
023    import java.util.HashMap;
024    import java.util.Iterator;
025    import java.util.Map;
026    
027    import javax.naming.NamingException;
028    
029    import org.apache.directory.shared.ldap.exception.LdapNamingException;
030    import org.apache.directory.shared.ldap.message.ResultCodeEnum;
031    import org.apache.directory.shared.ldap.schema.DITStructureRule;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    
036    /**
037     * A plain old java object implementation of an DITStructureRuleRegistry.
038     *
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 602007 $
041     */
042    public class DefaultDitStructureRuleRegistry implements DITStructureRuleRegistry
043    {
044        /** static class logger */
045        private static final Logger LOG = LoggerFactory.getLogger( DefaultDitStructureRuleRegistry.class );
046        /** maps an OID to an DITStructureRule */
047        private final Map<String,DITStructureRule> byOid;
048        /** maps an OID to an DITStructureRule */
049        private final Map<Integer,DITStructureRule> byRuleId;
050        /** the registry used to resolve names to OIDs */
051        private final OidRegistry oidRegistry;
052    
053    
054        // ------------------------------------------------------------------------
055        // C O N S T R U C T O R S
056        // ------------------------------------------------------------------------
057    
058        
059        /**
060         * Creates an empty DefaultDitStructureRuleRegistry.
061         *
062         * @param oidRegistry used by this registry for OID to name resolution of
063         * dependencies and to automatically register and unregister it's aliases and OIDs
064         */
065        public DefaultDitStructureRuleRegistry( OidRegistry oidRegistry )
066        {
067            this.byRuleId = new HashMap<Integer,DITStructureRule>();
068            this.byOid = new HashMap<String,DITStructureRule>();
069            this.oidRegistry = oidRegistry;
070        }
071    
072    
073        // ------------------------------------------------------------------------
074        // Service Methods
075        // ------------------------------------------------------------------------
076    
077        public void register( DITStructureRule dITStructureRule ) throws NamingException
078        {
079            if ( byOid.containsKey( dITStructureRule.getOid() ) )
080            {
081                throw new NamingException( "dITStructureRule w/ OID " + dITStructureRule.getOid()
082                    + " has already been registered!" );
083            }
084    
085            oidRegistry.register( dITStructureRule.getName(), dITStructureRule.getOid() );
086            byOid.put( dITStructureRule.getOid(), dITStructureRule );
087            byRuleId.put( dITStructureRule.getRuleId(), dITStructureRule );
088            if ( LOG.isDebugEnabled() )
089            {
090                LOG.debug( "registered dITStructureRule: " + dITStructureRule );
091            }
092        }
093    
094    
095        public DITStructureRule lookup( String id ) throws NamingException
096        {
097            id = oidRegistry.getOid( id );
098    
099            if ( !byOid.containsKey( id ) )
100            {
101                throw new NamingException( "dITStructureRule w/ OID " + id + " not registered!" );
102            }
103    
104            DITStructureRule dITStructureRule = byOid.get( id );
105            if ( LOG.isDebugEnabled() )
106            {
107                LOG.debug( "lookup with id '" + id + "' for dITStructureRule: " + dITStructureRule );
108            }
109            return dITStructureRule;
110        }
111    
112    
113        public boolean hasDITStructureRule( String id )
114        {
115            if ( oidRegistry.hasOid( id ) )
116            {
117                try
118                {
119                    return byOid.containsKey( oidRegistry.getOid( id ) );
120                }
121                catch ( NamingException e )
122                {
123                    return false;
124                }
125            }
126    
127            return false;
128        }
129    
130    
131        public String getSchemaName( String id ) throws NamingException
132        {
133            id = oidRegistry.getOid( id );
134            DITStructureRule dsr = byOid.get( id );
135            if ( dsr != null )
136            {
137                return dsr.getSchema();
138            }
139    
140            throw new NamingException( "OID " + id + " not found in oid to " + "DITStructureRule map!" );
141        }
142    
143    
144        public String getSchemaName( Integer ruleId ) throws NamingException
145        {
146            DITStructureRule dsr = byRuleId.get( ruleId );
147            if ( dsr != null )
148            {
149                return dsr.getSchema();
150            }
151    
152            throw new NamingException( "A DitStructureRule with ruleId " + ruleId 
153                + " not found in the DITStructureRule map!" );
154        }
155    
156    
157        public Iterator<DITStructureRule> iterator()
158        {
159            return byOid.values().iterator();
160        }
161    
162        
163        public void unregister( Integer ruleId ) throws NamingException
164        {
165            DITStructureRule dsr = byRuleId.remove( ruleId );
166            
167            if ( dsr == null )
168            {
169                if ( dsr == null )
170                {
171                    throw new LdapNamingException(
172                        "No such DITStructureRule for rule identifier: " + ruleId,
173                        ResultCodeEnum.OTHER );
174                }
175            }
176            
177            byOid.remove( dsr.getOid() );
178        }
179        
180        
181        public void unregister( String numericOid ) throws NamingException
182        {
183            if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
184            {
185                throw new NamingException( "Looks like the arg is not a numeric OID" );
186            }
187    
188            DITStructureRule dsr = byOid.remove( numericOid );
189            byRuleId.remove( dsr.getRuleId() );
190        }
191    
192    
193        public boolean hasDITStructureRule( Integer ruleId )
194        {
195            DITStructureRule dsr = byRuleId.get( ruleId );
196            return dsr != null;
197        }
198    
199    
200        public DITStructureRule lookup( Integer ruleId ) throws NamingException
201        {
202            DITStructureRule dsr = byRuleId.get( ruleId );
203            
204            if ( dsr == null )
205            {
206                throw new LdapNamingException(
207                    "No such DITStructureRule for rule identifier: " + ruleId,
208                    ResultCodeEnum.OTHER );
209            }
210    
211            return dsr;
212        }
213    }