David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 20 | import org.apache.thrift.server.TServer; |
Roger Meier | b942d57 | 2010-10-27 19:39:56 +0000 | [diff] [blame] | 21 | import org.apache.thrift.server.TServer.Args; |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 22 | import org.apache.thrift.server.TSimpleServer; |
Roger Meier | b942d57 | 2010-10-27 19:39:56 +0000 | [diff] [blame] | 23 | import org.apache.thrift.server.TThreadPoolServer; |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 24 | import org.apache.thrift.transport.TSSLTransportFactory; |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 25 | import org.apache.thrift.transport.TServerSocket; |
| 26 | import org.apache.thrift.transport.TServerTransport; |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 27 | import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters; |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 28 | |
| 29 | // Generated code |
| 30 | import tutorial.*; |
| 31 | import shared.*; |
| 32 | |
| 33 | import java.util.HashMap; |
| 34 | |
| 35 | public class JavaServer { |
| 36 | |
Roger Meier | b942d57 | 2010-10-27 19:39:56 +0000 | [diff] [blame] | 37 | public static CalculatorHandler handler; |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 38 | |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 39 | public static Calculator.Processor processor; |
| 40 | |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 41 | public static void main(String [] args) { |
| 42 | try { |
Roger Meier | b942d57 | 2010-10-27 19:39:56 +0000 | [diff] [blame] | 43 | handler = new CalculatorHandler(); |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 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 { |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 66 | TServerTransport serverTransport = new TServerSocket(9090); |
Roger Meier | b942d57 | 2010-10-27 19:39:56 +0000 | [diff] [blame] | 67 | TServer server = new TSimpleServer(new Args(serverTransport).processor(processor)); |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 68 | |
| 69 | // Use this for a multithreaded server |
Roger Meier | b942d57 | 2010-10-27 19:39:56 +0000 | [diff] [blame] | 70 | // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor)); |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 71 | |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 72 | System.out.println("Starting the simple server..."); |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 73 | server.serve(); |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 74 | } catch (Exception e) { |
| 75 | e.printStackTrace(); |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 76 | } |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 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); |
Roger Meier | b942d57 | 2010-10-27 19:39:56 +0000 | [diff] [blame] | 99 | TServer server = new TSimpleServer(new Args(serverTransport).processor(processor)); |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 100 | |
| 101 | // Use this for a multi threaded server |
Roger Meier | b942d57 | 2010-10-27 19:39:56 +0000 | [diff] [blame] | 102 | // TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor)); |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 103 | |
| 104 | System.out.println("Starting the secure server..."); |
| 105 | server.serve(); |
| 106 | } catch (Exception e) { |
| 107 | e.printStackTrace(); |
| 108 | } |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 109 | } |
| 110 | } |