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  
21  package org.apache.directory.server.dns.store.jndi;
22  
23  
24  import java.util.Set;
25  
26  import javax.naming.directory.DirContext;
27  
28  import org.apache.directory.server.core.CoreSession;
29  import org.apache.directory.server.core.DirectoryService;
30  import org.apache.directory.server.core.jndi.ServerLdapContext;
31  import org.apache.directory.server.dns.DnsException;
32  import org.apache.directory.server.dns.messages.QuestionRecord;
33  import org.apache.directory.server.dns.messages.ResourceRecord;
34  import org.apache.directory.server.dns.messages.ResponseCode;
35  import org.apache.directory.server.dns.store.jndi.operations.GetRecords;
36  import org.apache.directory.server.protocol.shared.ServiceConfigurationException;
37  import org.apache.directory.shared.ldap.name.LdapDN;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  
42  /**
43   * A JNDI-backed search strategy implementation.  This search strategy searches a
44   * single base DN for resource records.
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   * @version $Rev: 662440 $, $Date: 2008-06-02 16:00:23 +0200 (Mo, 02 Jun 2008) $
48   */
49  public class SingleBaseSearch implements SearchStrategy
50  {
51      /**
52       * the LOG for this class
53       */
54      private static final Logger LOG = LoggerFactory.getLogger( SingleBaseSearch.class );
55  
56      private final DirContext ctx;
57  
58  
59      SingleBaseSearch( String searchBaseDn, DirectoryService directoryService )
60      {
61          try
62          {
63              CoreSession session = directoryService.getSession();
64              ctx = new ServerLdapContext( directoryService, session, new LdapDN( searchBaseDn ) );
65          } catch ( Exception e )
66          {
67              throw new ServiceConfigurationException( "Can't get context at" + searchBaseDn, e );
68          }
69  
70      }
71  
72  
73      public Set<ResourceRecord> getRecords( QuestionRecord question ) throws DnsException
74      {
75          try
76          {
77  
78              return new GetRecords( question ).execute( ctx, null );
79          }
80          catch ( Exception e )
81          {
82              LOG.debug( "Unexpected error retrieving DNS records.", e );
83              throw new DnsException( ResponseCode.SERVER_FAILURE );
84          }
85      }
86  
87  
88  }