blob: 788473a8dc56f0c6e47424c9f3b742533fc77c1d [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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
Bryan Duxburyafa80ea2009-01-15 23:47:51 +000020import org.apache.thrift.server.TServer;
Roger Meierb942d572010-10-27 19:39:56 +000021import org.apache.thrift.server.TServer.Args;
Bryan Duxburyafa80ea2009-01-15 23:47:51 +000022import org.apache.thrift.server.TSimpleServer;
Roger Meierb942d572010-10-27 19:39:56 +000023import org.apache.thrift.server.TThreadPoolServer;
Bryan Duxbury1b130832010-10-19 17:20:57 +000024import org.apache.thrift.transport.TSSLTransportFactory;
Bryan Duxburyafa80ea2009-01-15 23:47:51 +000025import org.apache.thrift.transport.TServerSocket;
26import org.apache.thrift.transport.TServerTransport;
Bryan Duxbury1b130832010-10-19 17:20:57 +000027import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
Mark Sleecb39f082007-04-10 02:30:30 +000028
29// Generated code
30import tutorial.*;
31import shared.*;
32
33import java.util.HashMap;
34
35public class JavaServer {
36
Roger Meierb942d572010-10-27 19:39:56 +000037 public static CalculatorHandler handler;
Mark Sleecb39f082007-04-10 02:30:30 +000038
Bryan Duxbury1b130832010-10-19 17:20:57 +000039 public static Calculator.Processor processor;
40
Mark Sleecb39f082007-04-10 02:30:30 +000041 public static void main(String [] args) {
42 try {
Roger Meierb942d572010-10-27 19:39:56 +000043 handler = new CalculatorHandler();
Bryan Duxbury1b130832010-10-19 17:20:57 +000044 processor = new Calculator.Processor(handler);
45
46 Runnable simple = new Runnable() {
47 public void run() {
48 simple(processor);
49 }
50 };
51 Runnable secure = new Runnable() {
52 public void run() {
53 secure(processor);
54 }
55 };
56
57 new Thread(simple).start();
58 new Thread(secure).start();
59 } catch (Exception x) {
60 x.printStackTrace();
61 }
62 }
63
64 public static void simple(Calculator.Processor processor) {
65 try {
Mark Sleecb39f082007-04-10 02:30:30 +000066 TServerTransport serverTransport = new TServerSocket(9090);
Roger Meierb942d572010-10-27 19:39:56 +000067 TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
Mark Sleecb39f082007-04-10 02:30:30 +000068
69 // Use this for a multithreaded server
Roger Meierb942d572010-10-27 19:39:56 +000070 // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
Mark Sleecb39f082007-04-10 02:30:30 +000071
Bryan Duxbury1b130832010-10-19 17:20:57 +000072 System.out.println("Starting the simple server...");
Mark Sleecb39f082007-04-10 02:30:30 +000073 server.serve();
Bryan Duxbury1b130832010-10-19 17:20:57 +000074 } catch (Exception e) {
75 e.printStackTrace();
Mark Sleecb39f082007-04-10 02:30:30 +000076 }
Bryan Duxbury1b130832010-10-19 17:20:57 +000077 }
78
79 public static void secure(Calculator.Processor processor) {
80 try {
81 /*
82 * Use TSSLTransportParameters to setup the required SSL parameters. In this example
83 * we are setting the keystore and the keystore password. Other things like algorithms,
84 * cipher suites, client auth etc can be set.
85 */
86 TSSLTransportParameters params = new TSSLTransportParameters();
87 // The Keystore contains the private key
88 params.setKeyStore("../../lib/java/test/.keystore", "thrift", null, null);
89
90 /*
91 * Use any of the TSSLTransportFactory to get a server transport with the appropriate
92 * SSL configuration. You can use the default settings if properties are set in the command line.
93 * Ex: -Djavax.net.ssl.keyStore=.keystore and -Djavax.net.ssl.keyStorePassword=thrift
94 *
95 * Note: You need not explicitly call open(). The underlying server socket is bound on return
96 * from the factory class.
97 */
98 TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(9091, 0, null, params);
Roger Meierb942d572010-10-27 19:39:56 +000099 TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
Bryan Duxbury1b130832010-10-19 17:20:57 +0000100
101 // Use this for a multi threaded server
Roger Meierb942d572010-10-27 19:39:56 +0000102 // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
Bryan Duxbury1b130832010-10-19 17:20:57 +0000103
104 System.out.println("Starting the secure server...");
105 server.serve();
106 } catch (Exception e) {
107 e.printStackTrace();
108 }
Mark Sleecb39f082007-04-10 02:30:30 +0000109 }
110}