KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > sockets > TLSServerSocketFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.cornerstone.blocks.sockets;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23 import javax.net.ssl.SSLServerSocket;
24 import javax.net.ssl.SSLServerSocketFactory;
25 import org.apache.avalon.cornerstone.services.sockets.ServerSocketFactory;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28
29 /**
30  * Manufactures TLS server sockets. Configuration element inside a
31  * SocketManager would look like:
32  * <pre>
33  * &lt;factory name="secure"
34  * class="org.apache.avalon.cornerstone.blocks.sockets.TLSServerSocketFactory" &gt;
35  * &lt;ssl-factory /&gt; &lt;!-- see {@link SSLFactoryBuilder} --&gt;
36  * &lt;timeout&gt; 0 &lt;/timeout&gt;
37  * &lt;!-- With this option set to a non-zero timeout, a call to
38  * accept() for this ServerSocket will block for only this amount of
39  * time. If the timeout expires, a java.io.InterruptedIOException is
40  * raised, though the ServerSocket is still valid. Default value is 0. --&gt;
41  * &lt;authenticate-client&gt;false&lt;/authenticate-client&gt;
42  * &lt;!-- Whether or not the client must present a certificate to
43  * confirm its identity. Defaults to false. --&gt;
44  * &lt;/factory&gt;
45  * </pre>
46  *
47  * @author Peter Donald
48  * @author <a HREF="mailto:fede@apache.org">Federico Barbieri</a>
49  * @author <a HREF="mailto:charles@benett1.demon.co.uk">Charles Benett</a>
50  * @author <a HREF="mailto:">Harish Prabandham</a>
51  * @author <a HREF="mailto:">Costin Manolache</a>
52  * @author <a HREF="mailto:">Craig McClanahan</a>
53  * @author <a HREF="mailto:myfam@surfeu.fi">Andrei Ivanov</a>
54  * @author <a HREF="mailto:greg-avalon-apps at nest.cx">Greg Steuck</a>
55  */

56 public class TLSServerSocketFactory
57     extends AbstractTLSSocketFactory
58     implements ServerSocketFactory
59 {
60     private SSLServerSocketFactory m_factory;
61     protected boolean m_keyStoreAuthenticateClients;
62
63     /**
64      * Configures the factory.
65      *
66      * @param configuration the Configuration
67      * @exception ConfigurationException if an error occurs
68      */

69     public void configure( final Configuration configuration )
70         throws ConfigurationException
71     {
72         super.configure( configuration );
73         m_keyStoreAuthenticateClients =
74             configuration.getChild( "authenticate-client" ).getValueAsBoolean( false );
75     }
76
77     protected void visitBuilder( SSLFactoryBuilder builder )
78     {
79         m_factory = builder.buildServerSocketFactory();
80     }
81
82     /**
83      * Creates a socket on specified port.
84      *
85      * @param port the port
86      * @return the created ServerSocket
87      * @exception IOException if an error occurs
88      */

89     public ServerSocket JavaDoc createServerSocket( final int port )
90         throws IOException JavaDoc
91     {
92         final ServerSocket JavaDoc serverSocket = m_factory.createServerSocket( port );
93         initServerSocket( serverSocket );
94         return serverSocket;
95     }
96
97     /**
98      * Creates a socket on specified port with a specified backLog.
99      *
100      * @param port the port
101      * @param backLog the backLog
102      * @return the created ServerSocket
103      * @exception IOException if an error occurs
104      */

105     public ServerSocket JavaDoc createServerSocket( int port, int backLog )
106         throws IOException JavaDoc
107     {
108         final ServerSocket JavaDoc serverSocket = m_factory.createServerSocket( port, backLog );
109         initServerSocket( serverSocket );
110         return serverSocket;
111     }
112
113     /**
114      * Creates a socket on a particular network interface on specified port
115      * with a specified backLog.
116      *
117      * @param port the port
118      * @param backLog the backLog
119      * @param bindAddress the network interface to bind to.
120      * @return the created ServerSocket
121      * @exception IOException if an error occurs
122      */

123     public ServerSocket JavaDoc createServerSocket( int port, int backLog, InetAddress JavaDoc bindAddress )
124         throws IOException JavaDoc
125     {
126         final ServerSocket JavaDoc serverSocket =
127             m_factory.createServerSocket( port, backLog, bindAddress );
128         initServerSocket( serverSocket );
129         return serverSocket;
130     }
131
132     protected void initServerSocket( final ServerSocket JavaDoc serverSocket )
133         throws IOException JavaDoc
134     {
135         final SSLServerSocket socket = (SSLServerSocket)serverSocket;
136
137         // Set client authentication if necessary
138
socket.setNeedClientAuth( m_keyStoreAuthenticateClients );
139         // Sets socket timeout
140
socket.setSoTimeout( m_socketTimeOut );
141     }
142 }
143
144
Popular Tags