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.ntp;
21
22
23 import org.apache.directory.server.ntp.protocol.NtpProtocolHandler;
24 import org.apache.directory.server.protocol.shared.AbstractProtocolService;
25 import org.apache.mina.transport.socket.nio.DatagramAcceptorConfig;
26 import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
27
28 import java.io.IOException;
29 import java.net.InetSocketAddress;
30
31
32 /**
33 * Contains the configuration parameters for the NTP protocol provider.
34 *
35 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36 * @version $Rev: 596939 $, $Date: 2007-11-21 06:07:19 +0100 (Mi, 21 Nov 2007) $
37 * @org.apache.xbean.XBean
38 */
39 public class NtpServer extends AbstractProtocolService
40 {
41 /**
42 * The default IP port.
43 */
44 private static final int IP_PORT_DEFAULT = 123;
45
46 /**
47 * The default service pid.
48 */
49 private static final String SERVICE_PID_DEFAULT = "org.apache.directory.server.ntp";
50
51 /**
52 * The default service name.
53 */
54 private static final String SERVICE_NAME_DEFAULT = "ApacheDS NTP Service";
55
56
57 /**
58 * Creates a new instance of NtpConfiguration.
59 */
60 public NtpServer()
61 {
62 super.setIpPort( IP_PORT_DEFAULT );
63 super.setServiceId( SERVICE_PID_DEFAULT );
64 super.setServiceName( SERVICE_NAME_DEFAULT );
65 }
66
67
68 /**
69 * @throws IOException if there are issues binding
70 */
71 public void start() throws IOException
72 {
73 //If appropriate, the udp and tcp servers could be enabled with boolean flags.
74 if ( getDatagramAcceptor() != null )
75 {
76 DatagramAcceptorConfig udpConfig = new DatagramAcceptorConfig();
77 getDatagramAcceptor().bind( new InetSocketAddress( getIpPort() ), new NtpProtocolHandler(), 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 NtpProtocolHandler(), tcpConfig );
86 }
87 }
88
89
90 public void stop()
91 {
92 if ( getDatagramAcceptor() != null )
93 {
94 getDatagramAcceptor().unbind( new InetSocketAddress( getIpPort() ));
95 }
96
97 if ( getSocketAcceptor() != null )
98 {
99 getSocketAcceptor().unbind( new InetSocketAddress( getIpPort() ));
100 }
101 }
102 }