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.Iterator;
024    import java.util.Map;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    import javax.naming.NamingException;
028    
029    import org.apache.directory.shared.asn1.primitives.OID;
030    import org.apache.directory.shared.ldap.schema.MatchingRule;
031    
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    
036    /**
037     * A MatchingRuleRegistry service used to lookup matching rules by OID.
038     *
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 778788 $
041     */
042    public class DefaultMatchingRuleRegistry implements MatchingRuleRegistry
043    {
044        /** static class logger */
045        private static final Logger LOG = LoggerFactory.getLogger( DefaultMatchingRuleRegistry.class );
046    
047        /** a map using an OID for the key and a MatchingRule for the value */
048        private final Map<String,MatchingRule> byOidMatchingRule;
049        
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         * Creates a DefaultMatchingRuleRegistry using existing MatchingRulees
059         * for lookups.
060         *
061         * @param oidRegistry used by this registry for OID to name resolution of
062         * dependencies and to automatically register and unregister it's aliases and OIDs
063         */
064        public DefaultMatchingRuleRegistry( OidRegistry oidRegistry )
065        {
066            this.oidRegistry = oidRegistry;
067            byOidMatchingRule = new ConcurrentHashMap<String,MatchingRule>();
068        }
069    
070    
071        // ------------------------------------------------------------------------
072        // MatchingRuleRegistry interface methods
073        // ------------------------------------------------------------------------
074        /**
075         * {@inheritDoc}
076         */
077        public MatchingRule lookup( String id ) throws NamingException
078        {
079            String oid = oidRegistry.getOid( id );
080    
081            MatchingRule matchingRule = byOidMatchingRule.get( oid );
082            
083            if ( matchingRule != null )
084            {
085                if ( LOG.isDebugEnabled() )
086                {
087                    LOG.debug( "lookup with id '{}' of matchingRule: {}", oid, matchingRule );
088                }
089    
090                return matchingRule;
091            }
092    
093            String msg = "Unknown MatchingRule OID " + oid;
094            LOG.error( msg );
095            throw new NamingException( msg );
096        }
097    
098    
099        /**
100         * {@inheritDoc}
101         */
102        public void register( MatchingRule matchingRule ) throws NamingException
103        {
104            String oid = matchingRule.getOid();
105            
106            if ( byOidMatchingRule.containsKey( oid ) )
107            {
108                String msg = "matchingRule w/ OID " + oid
109                    + " has already been registered!";
110                LOG.warn( msg );
111                throw new NamingException( msg );
112            }
113    
114            String[] names = matchingRule.getNamesRef();
115            
116            for ( String name : names )
117            {
118                oidRegistry.register( name, oid );
119            }
120            
121            oidRegistry.register( oid, oid );
122            byOidMatchingRule.put( oid, matchingRule );
123            
124            if ( LOG.isDebugEnabled() )
125            {
126                LOG.debug( "registed matchingRule: {}", matchingRule);
127            }
128        }
129    
130    
131        /**
132         * {@inheritDoc}
133         */
134        public boolean hasMatchingRule( String id )
135        {
136            try
137            {
138                String oid = oidRegistry.getOid( id );
139                
140                if ( oid != null )
141                {
142                    return byOidMatchingRule.containsKey( oid );
143                }
144                
145                return false;
146            }
147            catch ( NamingException e )
148            {
149                return false;
150            }
151        }
152    
153    
154        /**
155         * {@inheritDoc}
156         */
157        public String getSchemaName( String id ) throws NamingException
158        {
159            String oid = oidRegistry.getOid( id );
160            MatchingRule mr = byOidMatchingRule.get( oid );
161           
162            if ( mr != null )
163            {
164                return mr.getSchema();
165            }
166    
167            String msg = "OID " + oid + " not found in oid to " + "MatchingRule name map!";
168            LOG.warn( msg );
169            throw new NamingException( msg );
170        }
171    
172    
173        /**
174         * {@inheritDoc}
175         */
176        public Iterator<MatchingRule> iterator()
177        {
178            return byOidMatchingRule.values().iterator();
179        }
180        
181        
182        /**
183         * {@inheritDoc}
184         */
185        public void unregister( String numericOid ) throws NamingException
186        {
187            if ( !OID.isOID( numericOid ) )
188            {
189                String msg = "OID " + numericOid + " is not a numeric OID";
190                LOG.error( msg );
191                throw new NamingException( msg );
192            }
193    
194            byOidMatchingRule.remove( numericOid );
195        }
196    }