View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.dns.store.jndi;
21  
22  
23  import org.apache.directory.server.core.CoreSession;
24  import org.apache.directory.server.core.DirectoryService;
25  import org.apache.directory.server.core.jndi.ServerLdapContext;
26  import org.apache.directory.server.dns.DnsException;
27  import org.apache.directory.server.dns.messages.QuestionRecord;
28  import org.apache.directory.server.dns.messages.ResourceRecord;
29  import org.apache.directory.server.dns.messages.ResponseCode;
30  import org.apache.directory.server.dns.store.jndi.operations.GetRecords;
31  import org.apache.directory.server.protocol.shared.ServiceConfigurationException;
32  import org.apache.directory.server.protocol.shared.catalog.Catalog;
33  import org.apache.directory.server.protocol.shared.catalog.GetCatalog;
34  import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException;
35  import org.apache.directory.shared.ldap.name.LdapDN;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import javax.naming.NamingException;
40  import javax.naming.directory.DirContext;
41  
42  import java.util.Map;
43  import java.util.Set;
44  
45  
46  /**
47   * A JNDI-backed search strategy implementation.  This search strategy builds a catalog
48   * from directory configuration to determine where zones are to search for
49   * resource records.
50   * 
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   * @version $Rev: 681578 $, $Date: 2008-08-01 03:20:21 +0200 (Fr, 01 Aug 2008) $
53   */
54  public class MultiBaseSearch implements SearchStrategy
55  {
56      /** the LOG for this class */
57      private static final Logger LOG = LoggerFactory.getLogger( MultiBaseSearch.class );
58  
59      private final Catalog catalog;
60      private final DirectoryService directoryService;
61  
62  
63      MultiBaseSearch( String catalogBaseDn, DirectoryService directoryService )
64      {
65          this.directoryService = directoryService;
66          try
67          {
68              CoreSession session = directoryService.getSession();
69              catalog = new DnsCatalog( ( Map<String, Object> ) new GetCatalog().execute( session, null ) );
70          }
71          catch ( Exception e )
72          {
73              LOG.error( e.getMessage(), e );
74              String message = "Failed to get catalog context " + catalogBaseDn;
75              throw new ServiceConfigurationException( message, e );
76          }
77      }
78  
79  
80      public Set<ResourceRecord> getRecords( QuestionRecord question ) throws DnsException
81      {
82          try
83          {
84              GetRecords getRecords = new GetRecords( question );
85              String baseDn = catalog.getBaseDn( question.getDomainName() );
86              CoreSession session = directoryService.getSession();
87              DirContext dirContext = new ServerLdapContext( directoryService, session, new LdapDN( baseDn ) );
88              return getRecords.execute( dirContext, null );
89          }
90          catch ( LdapNameNotFoundException lnnfe )
91          {
92              LOG.debug( "Name for DNS record search does not exist.", lnnfe );
93  
94              throw new DnsException( ResponseCode.NAME_ERROR );
95          }
96          catch ( NamingException ne )
97          {
98              LOG.error( ne.getMessage(), ne );
99              String message = "Failed to get initial context " + question.getDomainName();
100             throw new ServiceConfigurationException( message, ne );
101         }
102         catch ( Exception e )
103         {
104             LOG.debug( "Unexpected error retrieving DNS records.", e );
105             throw new DnsException( ResponseCode.SERVER_FAILURE );
106         }
107 
108     }
109 }