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.DITContentRule;
031    import org.slf4j.Logger;
032    import org.slf4j.LoggerFactory;
033    
034    
035    /**
036     * A plain old java object implementation of an DITContentRuleRegistry.
037     *
038     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039     * @version $Rev: 778781 $
040     */
041    public class DefaultDitContentRuleRegistry implements DITContentRuleRegistry
042    {
043        /** static class logger */
044        private static final Logger LOG = LoggerFactory.getLogger( DefaultDitContentRuleRegistry.class );
045        
046        /** maps an OID to an DITContentRule */
047        private final Map<String,DITContentRule> byOidDitContentRule;
048        
049        /** the registry used to resolve names to OIDs */
050        private final OidRegistry oidRegistry;
051    
052    
053        // ------------------------------------------------------------------------
054        // C O N S T R U C T O R S
055        // ------------------------------------------------------------------------
056    
057        /**
058         * Creates an empty DefaultDitContentRuleRegistry.
059         *
060         * @param oidRegistry used by this registry for OID to name resolution of
061         * dependencies and to automatically register and unregister it's aliases and OIDs
062         */
063        public DefaultDitContentRuleRegistry( OidRegistry oidRegistry )
064        {
065            byOidDitContentRule = new ConcurrentHashMap<String,DITContentRule>();
066            this.oidRegistry = oidRegistry;
067        }
068    
069    
070        // ------------------------------------------------------------------------
071        // Service Methods
072        // ------------------------------------------------------------------------
073        /**
074         * {@inheritDoc}
075         */
076        public void register( DITContentRule dITContentRule ) throws NamingException
077        {
078            String oid = dITContentRule.getOid();
079            
080            if ( byOidDitContentRule.containsKey( oid ) )
081            {
082                String msg = "dITContentRule w/ OID " + oid
083                    + " has already been registered!";
084                LOG.warn( msg );
085                throw new NamingException( msg );
086            }
087    
088            oidRegistry.register( dITContentRule.getName(), oid );
089            byOidDitContentRule.put( oid, dITContentRule );
090            
091            if ( LOG.isDebugEnabled() )
092            {
093                LOG.debug( "registed dITContentRule: {}", dITContentRule );
094            }
095        }
096    
097    
098        /**
099         * {@inheritDoc}
100         */
101        public DITContentRule lookup( String id ) throws NamingException
102        {
103            String oid = oidRegistry.getOid( id );
104    
105            DITContentRule dITContentRule = byOidDitContentRule.get( oid );
106    
107            if ( dITContentRule == null )
108            {
109                String msg = "dITContentRule w/ OID " + oid + " not registered!";
110                LOG.debug( msg );
111                throw new NamingException( msg );
112            }
113    
114            
115            if ( LOG.isDebugEnabled() )
116            {
117                LOG.debug( "lookup with id '{}' of dITContentRule: {}", oid, dITContentRule );
118            }
119            
120            return dITContentRule;
121        }
122    
123    
124        /**
125         * {@inheritDoc}
126         */
127        public boolean hasDITContentRule( String id )
128        {
129            try
130            {
131                String oid = oidRegistry.getOid( id );
132                
133                if ( oid != null )
134                {
135                    return byOidDitContentRule.containsKey( oid );
136                }
137                
138                return false;
139            }
140            catch ( NamingException e )
141            {
142                return false;
143            }
144        }
145    
146    
147        /**
148         * {@inheritDoc}
149         */
150        public String getSchemaName( String id ) throws NamingException
151        {
152            String oid = oidRegistry.getOid( id );
153            DITContentRule dcr = byOidDitContentRule.get( oid );
154            
155            if ( dcr != null )
156            {
157                return dcr.getSchema();
158            }
159    
160            String msg = "OID " + oid + " not found in oid to " + "DITContentRule map!";
161            LOG.error( msg );
162            throw new NamingException( msg );
163        }
164    
165    
166        /**
167         * {@inheritDoc}
168         */
169        public Iterator<DITContentRule> iterator()
170        {
171            return byOidDitContentRule.values().iterator();
172        }
173    
174    
175        /**
176         * {@inheritDoc}
177         */
178        public void unregister( String numericOid ) throws NamingException
179        {
180            if ( !OID.isOID( numericOid ) )
181            {
182                String msg = "OID " + numericOid + " is not a numeric OID";
183                LOG.error( msg );
184                throw new NamingException( msg );
185            }
186    
187            byOidDitContentRule.remove( numericOid );
188        }
189    }