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.Map;
024 import java.util.Set;
025
026 import javax.naming.NamingException;
027 import javax.naming.directory.DirContext;
028 import javax.naming.ldap.LdapName;
029
030 import org.apache.directory.server.core.CoreSession;
031 import org.apache.directory.server.core.DirectoryService;
032 import org.apache.directory.server.core.jndi.ServerLdapContext;
033 import org.apache.directory.server.dns.DnsException;
034 import org.apache.directory.server.dns.messages.QuestionRecord;
035 import org.apache.directory.server.dns.messages.ResourceRecord;
036 import org.apache.directory.server.dns.messages.ResponseCode;
037 import org.apache.directory.server.dns.store.jndi.operations.GetRecords;
038 import org.apache.directory.server.i18n.I18n;
039 import org.apache.directory.server.protocol.shared.ServiceConfigurationException;
040 import org.apache.directory.server.protocol.shared.catalog.Catalog;
041 import org.apache.directory.server.protocol.shared.catalog.GetCatalog;
042 import org.apache.directory.shared.ldap.exception.LdapNoSuchObjectException;
043 import org.slf4j.Logger;
044 import org.slf4j.LoggerFactory;
045
046
047 /**
048 * A JNDI-backed search strategy implementation. This search strategy builds a catalog
049 * from directory configuration to determine where zones are to search for
050 * resource records.
051 *
052 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
053 * @version $Rev: 925887 $, $Date: 2010-03-21 22:36:55 +0200 (Sun, 21 Mar 2010) $
054 */
055 public class MultiBaseSearch implements SearchStrategy
056 {
057 /** the LOG for this class */
058 private static final Logger LOG = LoggerFactory.getLogger( MultiBaseSearch.class );
059
060 private final Catalog catalog;
061 private final DirectoryService directoryService;
062
063
064 MultiBaseSearch( String catalogBaseDn, DirectoryService directoryService )
065 {
066 this.directoryService = directoryService;
067 try
068 {
069 CoreSession session = directoryService.getSession();
070 catalog = new DnsCatalog( ( Map<String, Object> ) new GetCatalog().execute( session, null ) );
071 }
072 catch ( Exception e )
073 {
074 LOG.error( e.getLocalizedMessage(), e );
075 String message = I18n.err( I18n.ERR_156, catalogBaseDn );
076 throw new ServiceConfigurationException( message, e );
077 }
078 }
079
080
081 public Set<ResourceRecord> getRecords( QuestionRecord question ) throws DnsException
082 {
083 try
084 {
085 GetRecords getRecords = new GetRecords( question );
086 String baseDn = catalog.getBaseDn( question.getDomainName() );
087 CoreSession session = directoryService.getSession();
088 DirContext dirContext = new ServerLdapContext( directoryService, session, new LdapName( baseDn ) );
089 return getRecords.execute( dirContext, null );
090 }
091 catch ( LdapNoSuchObjectException lnnfe )
092 {
093 LOG.debug( "Name for DNS record search does not exist.", lnnfe );
094
095 throw new DnsException( ResponseCode.NAME_ERROR );
096 }
097 catch ( NamingException ne )
098 {
099 LOG.error( ne.getLocalizedMessage(), ne );
100 String message = I18n.err( I18n.ERR_157, question.getDomainName() );
101 throw new ServiceConfigurationException( message, ne );
102 }
103 catch ( Exception e )
104 {
105 LOG.debug( "Unexpected error retrieving DNS records.", e );
106 throw new DnsException( ResponseCode.SERVER_FAILURE );
107 }
108
109 }
110 }