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.NameForm;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    
034    /**
035     * A plain old java object implementation of an NameFormRegistry.
036     *
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev: 602007 $
039     */
040    public class DefaultNameFormRegistry implements NameFormRegistry
041    {
042        /** static class logger */
043        private static final Logger LOG = LoggerFactory.getLogger( DefaultNameFormRegistry.class );
044        /** maps an OID to an NameForm */
045        private final Map<String,NameForm> byOid;
046        /** the registry used to resolve names to OIDs */
047        private final OidRegistry oidRegistry;
048    
049    
050        // ------------------------------------------------------------------------
051        // C O N S T R U C T O R S
052        // ------------------------------------------------------------------------
053    
054    
055        /**
056         * Creates an empty DefaultNameFormRegistry.
057         *
058         * @param oidRegistry used by this registry for OID to name resolution of
059         * dependencies and to automatically register and unregister it's aliases and OIDs
060         */
061        public DefaultNameFormRegistry( OidRegistry oidRegistry )
062        {
063            this.byOid = new HashMap<String,NameForm>();
064            this.oidRegistry = oidRegistry;
065        }
066    
067    
068        // ------------------------------------------------------------------------
069        // Service Methods
070        // ------------------------------------------------------------------------
071    
072        public void register( NameForm nameForm ) throws NamingException
073        {
074            if ( byOid.containsKey( nameForm.getOid() ) )
075            {
076                throw new NamingException( "nameForm w/ OID " + nameForm.getOid()
077                    + " has already been registered!" );
078            }
079    
080            oidRegistry.register( nameForm.getName(), nameForm.getOid() );
081            byOid.put( nameForm.getOid(), nameForm );
082            if ( LOG.isDebugEnabled() )
083            {
084                LOG.debug( "registered nameForm: " + nameForm );
085            }
086        }
087    
088    
089        public NameForm lookup( String id ) throws NamingException
090        {
091            id = oidRegistry.getOid( id );
092    
093            if ( !byOid.containsKey( id ) )
094            {
095                throw new NamingException( "nameForm w/ OID " + id + " not registered!" );
096            }
097    
098            NameForm nameForm = byOid.get( id );
099            if ( LOG.isDebugEnabled() )
100            {
101                LOG.debug( "lookup with id '"+ id + "' of nameForm: " + nameForm );
102            }
103            return nameForm;
104        }
105    
106    
107        public boolean hasNameForm( 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            NameForm nf = byOid.get( id );
129            if ( nf != null )
130            {
131                return nf.getSchema();
132            }
133    
134            throw new NamingException( "OID " + id + " not found in oid to " + "NameForm map!" );
135        }
136    
137    
138        public Iterator<NameForm> 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    }