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.schema.MatchingRuleUse;
030    
031    import org.slf4j.Logger;
032    import org.slf4j.LoggerFactory;
033    
034    
035    /**
036     * A plain old java object implementation of an MatchingRuleUseRegistry.
037     *
038     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039     * @version $Rev: 601982 $
040     */
041    public class DefaultMatchingRuleUseRegistry implements MatchingRuleUseRegistry
042    {
043        /** static class logger */
044        private static final Logger LOG = LoggerFactory.getLogger( DefaultMatchingRuleUseRegistry.class );
045        /** maps a name to an MatchingRuleUse */
046        private final Map<String,MatchingRuleUse> byName;
047    
048    
049        // ------------------------------------------------------------------------
050        // C O N S T R U C T O R S
051        // ------------------------------------------------------------------------
052    
053        
054        /**
055         * Creates an empty DefaultMatchingRuleUseRegistry.
056         */
057        public DefaultMatchingRuleUseRegistry()
058        {
059            this.byName = new HashMap<String,MatchingRuleUse>();
060        }
061    
062    
063        // ------------------------------------------------------------------------
064        // Service Methods
065        // ------------------------------------------------------------------------
066    
067        
068        public void register( MatchingRuleUse matchingRuleUse ) throws NamingException
069        {
070            if ( byName.containsKey( matchingRuleUse.getName() ) )
071            {
072                throw new NamingException( "matchingRuleUse w/ name " + matchingRuleUse.getName()
073                    + " has already been registered!" );
074            }
075    
076            byName.put( matchingRuleUse.getName(), matchingRuleUse );
077            if ( LOG.isDebugEnabled() )
078            {
079                LOG.debug( "registed matchingRuleUse: " + matchingRuleUse );
080            }
081        }
082    
083    
084        public MatchingRuleUse lookup( String name ) throws NamingException
085        {
086            if ( !byName.containsKey( name ) )
087            {
088                throw new NamingException( "matchingRuleUse w/ name " + name + " not registered!" );
089            }
090    
091            MatchingRuleUse matchingRuleUse = byName.get( name );
092            if ( LOG.isDebugEnabled() )
093            {
094                LOG.debug( "lookup with name '"+ name + "' of matchingRuleUse: " + matchingRuleUse );
095            }
096            return matchingRuleUse;
097        }
098    
099    
100        public boolean hasMatchingRuleUse( String name )
101        {
102            return byName.containsKey( name );
103        }
104    
105    
106        public String getSchemaName( String id ) throws NamingException
107        {
108            MatchingRuleUse mru = byName.get( id );
109            if ( mru != null )
110            {
111                return mru.getSchema();
112            }
113    
114            throw new NamingException( "Name " + id + " not found in name to " + "MatchingRuleUse map!" );
115        }
116    
117    
118        public Iterator<MatchingRuleUse> iterator()
119        {
120            return byName.values().iterator();
121        }
122        
123        
124        public void unregister( String name ) throws NamingException
125        {
126            byName.remove( name );
127        }
128    }