THRIFT-3301 Java generated code uses imports that can lead to class name collisions...
[thrift.git] / tutorial / java / src / JavaServer.java
1 /*
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
20 import org.apache.thrift.server.TServer;
21 import org.apache.thrift.server.TServer.Args;
22 import org.apache.thrift.server.TSimpleServer;
23 import org.apache.thrift.server.TThreadPoolServer;
24 import org.apache.thrift.transport.TSSLTransportFactory;
25 import org.apache.thrift.transport.TServerSocket;
26 import org.apache.thrift.transport.TServerTransport;
27 import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
28
29 // Generated code
30 import tutorial.*;
31 import shared.*;
32
33 import java.util.HashMap;
34
35 public class JavaServer {
36
37 public static CalculatorHandler handler;
38
39 public static Calculator.Processor processor;
40
41 public static void main(String [] args) {
42 try {
43 handler = new CalculatorHandler();
44 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 {
66 TServerTransport serverTransport = new TServerSocket(9090);
67 TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
68
69 // Use this for a multithreaded server
70 // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
71
72 System.out.println("Starting the simple server...");
73 server.serve();
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77 }
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);
99 TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
100
101 // Use this for a multi threaded server
102 // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
103
104 System.out.println("Starting the secure server...");
105 server.serve();
106 } catch (Exception e) {
107 e.printStackTrace();
108 }
109 }
110 }

Copyright 2016, The Apache Software Foundation.