blob: 5a2cc9b79ebb73ee7335ca5937e4c17286df59ec [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Sun Microsystems nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32import java.io.*;
33import java.net.*;
34import java.nio.channels.*;
35import java.security.*;
36import javax.net.ssl.*;
37
38/**
39 * The main server base class.
40 * <P>
41 * This class is responsible for setting up most of the server state
42 * before the actual server subclasses take over.
43 *
44 * @author Mark Reinhold
45 * @author Brad R. Wetmore
46 */
47public abstract class Server {
48
49 ServerSocketChannel ssc;
50 SSLContext sslContext = null;
51
52 static private int PORT = 8000;
53 static private int BACKLOG = 1024;
54 static private boolean SECURE = false;
55
56 Server(int port, int backlog,
57 boolean secure) throws Exception {
58
59 if (secure) {
60 createSSLContext();
61 }
62
63 ssc = ServerSocketChannel.open();
64 ssc.socket().setReuseAddress(true);
65 ssc.socket().bind(new InetSocketAddress(port), backlog);
66 }
67
68 /*
69 * If this is a secure server, we now setup the SSLContext we'll
70 * use for creating the SSLEngines throughout the lifetime of
71 * this process.
72 */
73 private void createSSLContext() throws Exception {
74
75 char[] passphrase = "passphrase".toCharArray();
76
77 KeyStore ks = KeyStore.getInstance("JKS");
78 ks.load(new FileInputStream("testkeys"), passphrase);
79
80 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
81 kmf.init(ks, passphrase);
82
83 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
84 tmf.init(ks);
85
86 sslContext = SSLContext.getInstance("TLS");
87 sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
88 }
89
90 abstract void runServer() throws Exception;
91
92 static private void usage() {
93 System.out.println(
94 "Usage: Server <type> [options]\n"
95 + " type:\n"
96 + " B1 Blocking/Single-threaded Server\n"
97 + " BN Blocking/Multi-threaded Server\n"
98 + " BP Blocking/Pooled-Thread Server\n"
99 + " N1 Nonblocking/Single-threaded Server\n"
100 + " N2 Nonblocking/Dual-threaded Server\n"
101 + "\n"
102 + " options:\n"
103 + " -port port port number\n"
104 + " default: " + PORT + "\n"
105 + " -backlog backlog backlog\n"
106 + " default: " + BACKLOG + "\n"
107 + " -secure encrypt with SSL/TLS");
108 System.exit(1);
109 }
110
111 /*
112 * Parse the arguments, decide what type of server to run,
113 * see if there are any defaults to change.
114 */
115 static private Server createServer(String args[]) throws Exception {
116 if (args.length < 1) {
117 usage();
118 }
119
120 int port = PORT;
121 int backlog = BACKLOG;
122 boolean secure = SECURE;
123
124 for (int i = 1; i < args.length; i++) {
125 if (args[i].equals("-port")) {
126 checkArgs(i, args.length);
127 port = Integer.valueOf(args[++i]);
128 } else if (args[i].equals("-backlog")) {
129 checkArgs(i, args.length);
130 backlog = Integer.valueOf(args[++i]);
131 } else if (args[i].equals("-secure")) {
132 secure = true;
133 } else {
134 usage();
135 }
136 }
137
138 Server server = null;
139
140 if (args[0].equals("B1")) {
141 server = new B1(port, backlog, secure);
142 } else if (args[0].equals("BN")) {
143 server = new BN(port, backlog, secure);
144 } else if (args[0].equals("BP")) {
145 server = new BP(port, backlog, secure);
146 } else if (args[0].equals("N1")) {
147 server = new N1(port, backlog, secure);
148 } else if (args[0].equals("N2")) {
149 server = new N2(port, backlog, secure);
150 }
151
152 return server;
153 }
154
155 static private void checkArgs(int i, int len) {
156 if ((i + 1) >= len) {
157 usage();
158 }
159 }
160
161 static public void main(String args[]) throws Exception {
162 Server server = createServer(args);
163
164 if (server == null) {
165 usage();
166 }
167
168 System.out.println("Server started.");
169 server.runServer();
170 }
171}