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; |
| 21 | import org.apache.thrift.server.TSimpleServer; |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 22 | import org.apache.thrift.transport.TSSLTransportFactory; |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 23 | import org.apache.thrift.transport.TServerSocket; |
| 24 | import org.apache.thrift.transport.TServerTransport; |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 25 | import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters; |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 26 | |
| 27 | // Generated code |
| 28 | import tutorial.*; |
| 29 | import shared.*; |
| 30 | |
| 31 | import java.util.HashMap; |
| 32 | |
| 33 | public class JavaServer { |
| 34 | |
| 35 | public static class CalculatorHandler implements Calculator.Iface { |
| 36 | |
| 37 | private HashMap<Integer,SharedStruct> log; |
| 38 | |
| 39 | public CalculatorHandler() { |
| 40 | log = new HashMap<Integer, SharedStruct>(); |
| 41 | } |
| 42 | |
| 43 | public void ping() { |
| 44 | System.out.println("ping()"); |
| 45 | } |
| 46 | |
| 47 | public int add(int n1, int n2) { |
| 48 | System.out.println("add(" + n1 + "," + n2 + ")"); |
| 49 | return n1 + n2; |
| 50 | } |
| 51 | |
| 52 | public int calculate(int logid, Work work) throws InvalidOperation { |
| 53 | System.out.println("calculate(" + logid + ", {" + work.op + "," + work.num1 + "," + work.num2 + "})"); |
| 54 | int val = 0; |
| 55 | switch (work.op) { |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 56 | case ADD: |
| 57 | val = work.num1 + work.num2; |
| 58 | break; |
| 59 | case SUBTRACT: |
| 60 | val = work.num1 - work.num2; |
| 61 | break; |
| 62 | case MULTIPLY: |
| 63 | val = work.num1 * work.num2; |
| 64 | break; |
| 65 | case DIVIDE: |
| 66 | if (work.num2 == 0) { |
| 67 | InvalidOperation io = new InvalidOperation(); |
| 68 | io.what = work.op.getValue(); |
| 69 | io.why = "Cannot divide by 0"; |
| 70 | throw io; |
| 71 | } |
| 72 | val = work.num1 / work.num2; |
| 73 | break; |
| 74 | default: |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 75 | InvalidOperation io = new InvalidOperation(); |
Todd Lipcon | 515c238 | 2010-09-27 18:26:07 +0000 | [diff] [blame] | 76 | io.what = work.op.getValue(); |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 77 | io.why = "Unknown operation"; |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 78 | throw io; |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | SharedStruct entry = new SharedStruct(); |
| 82 | entry.key = logid; |
| 83 | entry.value = Integer.toString(val); |
| 84 | log.put(logid, entry); |
| 85 | |
| 86 | return val; |
| 87 | } |
| 88 | |
| 89 | public SharedStruct getStruct(int key) { |
| 90 | System.out.println("getStruct(" + key + ")"); |
| 91 | return log.get(key); |
| 92 | } |
| 93 | |
| 94 | public void zip() { |
| 95 | System.out.println("zip()"); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 100 | public static Calculator.Processor processor; |
| 101 | |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 102 | public static void main(String [] args) { |
| 103 | try { |
| 104 | CalculatorHandler handler = new CalculatorHandler(); |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 105 | processor = new Calculator.Processor(handler); |
| 106 | |
| 107 | Runnable simple = new Runnable() { |
| 108 | public void run() { |
| 109 | simple(processor); |
| 110 | } |
| 111 | }; |
| 112 | Runnable secure = new Runnable() { |
| 113 | public void run() { |
| 114 | secure(processor); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | new Thread(simple).start(); |
| 119 | new Thread(secure).start(); |
| 120 | } catch (Exception x) { |
| 121 | x.printStackTrace(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public static void simple(Calculator.Processor processor) { |
| 126 | try { |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 127 | TServerTransport serverTransport = new TServerSocket(9090); |
| 128 | TServer server = new TSimpleServer(processor, serverTransport); |
| 129 | |
| 130 | // Use this for a multithreaded server |
| 131 | // server = new TThreadPoolServer(processor, serverTransport); |
| 132 | |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 133 | System.out.println("Starting the simple server..."); |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 134 | server.serve(); |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 135 | } catch (Exception e) { |
| 136 | e.printStackTrace(); |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 137 | } |
Bryan Duxbury | 1b13083 | 2010-10-19 17:20:57 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public static void secure(Calculator.Processor processor) { |
| 141 | try { |
| 142 | /* |
| 143 | * Use TSSLTransportParameters to setup the required SSL parameters. In this example |
| 144 | * we are setting the keystore and the keystore password. Other things like algorithms, |
| 145 | * cipher suites, client auth etc can be set. |
| 146 | */ |
| 147 | TSSLTransportParameters params = new TSSLTransportParameters(); |
| 148 | // The Keystore contains the private key |
| 149 | params.setKeyStore("../../lib/java/test/.keystore", "thrift", null, null); |
| 150 | |
| 151 | /* |
| 152 | * Use any of the TSSLTransportFactory to get a server transport with the appropriate |
| 153 | * SSL configuration. You can use the default settings if properties are set in the command line. |
| 154 | * Ex: -Djavax.net.ssl.keyStore=.keystore and -Djavax.net.ssl.keyStorePassword=thrift |
| 155 | * |
| 156 | * Note: You need not explicitly call open(). The underlying server socket is bound on return |
| 157 | * from the factory class. |
| 158 | */ |
| 159 | TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(9091, 0, null, params); |
| 160 | TServer server = new TSimpleServer(processor, serverTransport); |
| 161 | |
| 162 | // Use this for a multi threaded server |
| 163 | // server = new TThreadPoolServer(processor, serverTransport); |
| 164 | |
| 165 | System.out.println("Starting the secure server..."); |
| 166 | server.serve(); |
| 167 | } catch (Exception e) { |
| 168 | e.printStackTrace(); |
| 169 | } |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 170 | } |
| 171 | } |