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
021package org.apache.directory.server.dns.store.jndi;
022
023
024import java.util.Set;
025
026import javax.naming.directory.DirContext;
027import javax.naming.ldap.LdapName;
028
029import org.apache.directory.server.core.api.CoreSession;
030import org.apache.directory.server.core.api.DirectoryService;
031import org.apache.directory.server.core.jndi.ServerLdapContext;
032import org.apache.directory.server.dns.DnsException;
033import org.apache.directory.server.dns.messages.QuestionRecord;
034import org.apache.directory.server.dns.messages.ResourceRecord;
035import org.apache.directory.server.dns.messages.ResponseCode;
036import org.apache.directory.server.dns.store.jndi.operations.GetRecords;
037import org.apache.directory.server.i18n.I18n;
038import org.apache.directory.server.protocol.shared.ServiceConfigurationException;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042
043/**
044 * A JNDI-backed search strategy implementation.  This search strategy searches a
045 * single base Dn for resource records.
046 *
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049public class SingleBaseSearch implements SearchStrategy
050{
051    /**
052     * the LOG for this class
053     */
054    private static final Logger LOG = LoggerFactory.getLogger( SingleBaseSearch.class );
055
056    private final DirContext ctx;
057
058
059    SingleBaseSearch( String searchBaseDn, DirectoryService directoryService )
060    {
061        try
062        {
063            CoreSession session = directoryService.getSession();
064            ctx = new ServerLdapContext( directoryService, session, new LdapName( searchBaseDn ) );
065        }
066        catch ( Exception e )
067        {
068            throw new ServiceConfigurationException( I18n.err( I18n.ERR_649, searchBaseDn ), e );
069        }
070
071    }
072
073
074    public Set<ResourceRecord> getRecords( QuestionRecord question ) throws DnsException
075    {
076        try
077        {
078
079            return new GetRecords( question ).execute( ctx, null );
080        }
081        catch ( Exception e )
082        {
083            LOG.debug( "Unexpected error retrieving DNS records.", e );
084            throw new DnsException( ResponseCode.SERVER_FAILURE );
085        }
086    }
087
088}