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;
21  
22  
23  import java.io.IOException;
24  import java.net.InetSocketAddress;
25  
26  import org.apache.directory.server.dns.protocol.DnsProtocolHandler;
27  import org.apache.directory.server.dns.store.RecordStore;
28  import org.apache.directory.server.dns.store.jndi.JndiRecordStoreImpl;
29  import org.apache.directory.server.protocol.shared.DirectoryBackedService;
30  import org.apache.mina.transport.socket.nio.DatagramAcceptorConfig;
31  import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
32  
33  
34  /**
35   * Contains the configuration parameters for the DNS protocol provider.
36   *
37   * @org.apache.xbean.XBean
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev: 596832 $, $Date: 2007-11-20 22:43:50 +0100 (Di, 20 Nov 2007) $
41   */
42  public class DnsServer extends DirectoryBackedService
43  {
44      private static final long serialVersionUID = 6943138644427163149L;
45  
46      /** The default IP port. */
47      private static final int IP_PORT_DEFAULT = 53;
48  
49      /** The default service pid. */
50      private static final String SERVICE_PID_DEFAULT = "org.apache.directory.server.dns";
51  
52      /** The default service name. */
53      private static final String SERVICE_NAME_DEFAULT = "ApacheDS DNS Service";
54  
55  
56      /**
57       * Creates a new instance of DnsConfiguration.
58       */
59      public DnsServer()
60      {
61          super.setIpPort( IP_PORT_DEFAULT );
62          super.setServiceId( SERVICE_PID_DEFAULT );
63          super.setServiceName( SERVICE_NAME_DEFAULT );
64      }
65  
66  
67      /**
68       * @throws IOException if we cannot bind to the specified ports
69       */
70      public void start() throws IOException
71      {
72          RecordStore store = new JndiRecordStoreImpl( getSearchBaseDn(), getSearchBaseDn(), getDirectoryService() );
73  
74          if ( getDatagramAcceptor() != null )
75          {
76              DatagramAcceptorConfig udpConfig = new DatagramAcceptorConfig();
77              getDatagramAcceptor().bind( new InetSocketAddress( getIpPort() ), new DnsProtocolHandler( this, store ), udpConfig );
78          }
79  
80          if ( getSocketAcceptor() != null )
81          {
82              SocketAcceptorConfig tcpConfig = new SocketAcceptorConfig();
83              tcpConfig.setDisconnectOnUnbind( false );
84              tcpConfig.setReuseAddress( true );
85              getSocketAcceptor().bind( new InetSocketAddress( getIpPort() ), new DnsProtocolHandler( this, store ), tcpConfig );
86          }
87      }
88  
89  
90      public void stop() {
91          if ( getDatagramAcceptor() != null )
92          {
93              getDatagramAcceptor().unbind( new InetSocketAddress( getIpPort() ));
94          }
95          if ( getSocketAcceptor() != null )
96          {
97              getSocketAcceptor().unbind( new InetSocketAddress( getIpPort() ));
98          }
99      }
100 }