KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Server


1 /*
2  * @(#)Server.java 1.3 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 import java.io.*;
38 import java.net.*;
39 import java.nio.channels.*;
40 import java.security.*;
41 import javax.net.ssl.*;
42
43 /**
44  * The main server base class.
45  * <P>
46  * This class is responsible for setting up most of the server state
47  * before the actual server subclasses take over.
48  *
49  * @author Mark Reinhold
50  * @author Brad R. Wetmore
51  * @version 1.3, 05/11/17
52  */

53 public abstract class Server {
54
55     ServerSocketChannel ssc;
56     SSLContext sslContext = null;
57
58     static private int PORT = 8000;
59     static private int BACKLOG = 1024;
60     static private boolean SECURE = false;
61
62     Server(int port, int backlog,
63         boolean secure) throws Exception JavaDoc {
64
65     if (secure) {
66         createSSLContext();
67     }
68
69     ssc = ServerSocketChannel.open();
70     ssc.socket().setReuseAddress(true);
71     ssc.socket().bind(new InetSocketAddress(port), backlog);
72     }
73
74     /*
75      * If this is a secure server, we now setup the SSLContext we'll
76      * use for creating the SSLEngines throughout the lifetime of
77      * this process.
78      */

79     private void createSSLContext() throws Exception JavaDoc {
80
81     char[] passphrase = "passphrase".toCharArray();
82
83     KeyStore ks = KeyStore.getInstance("JKS");
84     ks.load(new FileInputStream("testkeys"), passphrase);
85
86     KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
87     kmf.init(ks, passphrase);
88
89     TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90     tmf.init(ks);
91
92     sslContext = SSLContext.getInstance("TLS");
93     sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
94     }
95
96     abstract void runServer() throws Exception JavaDoc;
97
98     static private void usage() {
99     System.out.println(
100         "Usage: Server <type> [options]\n"
101         + " type:\n"
102         + " B1 Blocking/Single-threaded Server\n"
103         + " BN Blocking/Multi-threaded Server\n"
104         + " BP Blocking/Pooled-Thread Server\n"
105         + " N1 Nonblocking/Single-threaded Server\n"
106         + " N2 Nonblocking/Dual-threaded Server\n"
107         + "\n"
108         + " options:\n"
109         + " -port port port number\n"
110         + " default: " + PORT + "\n"
111         + " -backlog backlog backlog\n"
112         + " default: " + BACKLOG + "\n"
113         + " -secure encrypt with SSL/TLS");
114     System.exit(1);
115     }
116
117     /*
118      * Parse the arguments, decide what type of server to run,
119      * see if there are any defaults to change.
120      */

121     static private Server createServer(String JavaDoc args[]) throws Exception JavaDoc {
122     if (args.length < 1) {
123         usage();
124     }
125
126     int port = PORT;
127     int backlog = BACKLOG;
128     boolean secure = SECURE;
129
130     for (int i = 1; i < args.length; i++) {
131         if (args[i].equals("-port")) {
132         checkArgs(i, args.length);
133         port = Integer.valueOf(args[++i]);
134         } else if (args[i].equals("-backlog")) {
135         checkArgs(i, args.length);
136         backlog = Integer.valueOf(args[++i]);
137         } else if (args[i].equals("-secure")) {
138         secure = true;
139         } else {
140         usage();
141         }
142     }
143
144     Server server = null;
145
146     if (args[0].equals("B1")) {
147         server = new B1(port, backlog, secure);
148     } else if (args[0].equals("BN")) {
149         server = new BN(port, backlog, secure);
150     } else if (args[0].equals("BP")) {
151         server = new BP(port, backlog, secure);
152     } else if (args[0].equals("N1")) {
153         server = new N1(port, backlog, secure);
154     } else if (args[0].equals("N2")) {
155         server = new N2(port, backlog, secure);
156     }
157
158     return server;
159     }
160
161     static private void checkArgs(int i, int len) {
162     if ((i + 1) >= len) {
163        usage();
164     }
165     }
166
167     static public void main(String JavaDoc args[]) throws Exception JavaDoc {
168     Server server = createServer(args);
169
170     if (server == null) {
171         usage();
172     }
173
174     System.out.println("Server started.");
175     server.runServer();
176     }
177 }
178
Popular Tags