1 /*
2  * @(#) $Id: JSTKServerSocket.java,v 1.2 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.ssl;
11
12import java.nio.channels.ServerSocketChannel;
13import java.net.ServerSocket;
14import javax.net.ssl.SSLServerSocket;
15import java.io.IOException;
16
17public abstract class JSTKServerSocket {
18    private static class ServerSocketWrapper extends JSTKServerSocket {
19        private ServerSocket ss = null;
20        private ServerSocketWrapper(ServerSocket ss){
21            this.ss = ss;
22        }
23        public JSTKSocket accept() throws IOException{
24            return JSTKSocket.getInstance(ss.accept());
25        }
26        public void setNeedClientAuth(boolean flag){
27            if (ss instanceof SSLServerSocket){
28                ((SSLServerSocket)ss).setNeedClientAuth(flag);
29            }
30        }
31        public void setWantClientAuth(boolean flag){
32            if (ss instanceof SSLServerSocket){
33                ((SSLServerSocket)ss).setWantClientAuth(flag);
34            }
35        }
36    }
37
38    private static class ServerSocketChannelWrapper extends JSTKServerSocket {
39        private ServerSocketChannel ssc = null;
40        private ServerSocketChannelWrapper(ServerSocketChannel ssc){
41            this.ssc = ssc;
42        }
43        public JSTKSocket accept() throws IOException{
44            return JSTKSocket.getInstance(ssc.accept());
45        }
46        public void setNeedClientAuth(boolean flag){
47            // Underlying socket can't be SSLServerSocket. Do nothing.
48        }
49        public void setWantClientAuth(boolean flag){
50            // Underlying socket can't be SSLServerSocket. Do nothing.
51        }
52    }
53
54    public static JSTKServerSocket getInstance(ServerSocket ss){
55        return new ServerSocketWrapper(ss);
56    }
57
58    public static JSTKServerSocket getInstance(ServerSocketChannel ssc){
59        return new ServerSocketChannelWrapper(ssc);
60    }
61
62    public abstract JSTKSocket accept() throws IOException;
63    public abstract void setNeedClientAuth(boolean flag);
64    public abstract void setWantClientAuth(boolean flag);
65}
66