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.dns.store.jndi;
021    
022    
023    import java.util.Set;
024    
025    import org.apache.directory.server.core.DirectoryService;
026    import org.apache.directory.server.dns.DnsException;
027    import org.apache.directory.server.dns.messages.QuestionRecord;
028    import org.apache.directory.server.dns.messages.ResourceRecord;
029    import org.apache.directory.server.dns.store.RecordStore;
030    
031    
032    /**
033     * A DirectoryService-backed implementation of the RecordStore interface.  This RecordStore uses
034     * the Strategy pattern to either serve records based on a single base DN or to lookup
035     * catalog mappings from directory configuration.
036     *
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev: 583592 $, $Date: 2007-10-10 21:49:37 +0200 (Wed, 10 Oct 2007) $
039     */
040    public class JndiRecordStoreImpl implements RecordStore
041    {
042        /**
043         * a handle on the searchh strategy
044         */
045        private final SearchStrategy strategy;
046    
047    
048        /**
049         * Creates a new instance of JndiRecordStoreImpl.
050         *
051         * @param catalogBaseDn base of catalog of searchDns
052         * @param searchBaseDn single search base for when there is no catalog
053         * @param directoryService DirectoryService backend for the searches.
054         */
055        public JndiRecordStoreImpl( String catalogBaseDn, String searchBaseDn, DirectoryService directoryService )
056        {
057    
058            strategy = getSearchStrategy( catalogBaseDn, searchBaseDn, directoryService );
059        }
060    
061    
062        public Set<ResourceRecord> getRecords( QuestionRecord question ) throws DnsException
063        {
064            return strategy.getRecords( question );
065        }
066    
067    
068        private SearchStrategy getSearchStrategy( String catalogBaseDn, String searchBaseDn, DirectoryService directoryService )
069        {
070            if ( catalogBaseDn != null )
071            {
072                // build catalog from factory
073                return new MultiBaseSearch( catalogBaseDn, directoryService );
074            }
075    
076            // use config for catalog baseDN
077            return new SingleBaseSearch( searchBaseDn, directoryService );
078        }
079    }