blob: 29c4d58787c93769fc81fe1872775fe25e82f64d [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;
21import org.apache.thrift.server.TSimpleServer;
Bryan Duxbury1b130832010-10-19 17:20:57 +000022import org.apache.thrift.transport.TSSLTransportFactory;
Bryan Duxburyafa80ea2009-01-15 23:47:51 +000023import org.apache.thrift.transport.TServerSocket;
24import org.apache.thrift.transport.TServerTransport;
Bryan Duxbury1b130832010-10-19 17:20:57 +000025import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
Mark Sleecb39f082007-04-10 02:30:30 +000026
27// Generated code
28import tutorial.*;
29import shared.*;
30
31import java.util.HashMap;
32
33public 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 Duxbury1b130832010-10-19 17:20:57 +000056 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 Sleecb39f082007-04-10 02:30:30 +000075 InvalidOperation io = new InvalidOperation();
Todd Lipcon515c2382010-09-27 18:26:07 +000076 io.what = work.op.getValue();
Bryan Duxbury1b130832010-10-19 17:20:57 +000077 io.why = "Unknown operation";
Mark Sleecb39f082007-04-10 02:30:30 +000078 throw io;
Mark Sleecb39f082007-04-10 02:30:30 +000079 }
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 Duxbury1b130832010-10-19 17:20:57 +0000100 public static Calculator.Processor processor;
101
Mark Sleecb39f082007-04-10 02:30:30 +0000102 public static void main(String [] args) {
103 try {
104 CalculatorHandler handler = new CalculatorHandler();
Bryan Duxbury1b130832010-10-19 17:20:57 +0000105 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 Sleecb39f082007-04-10 02:30:30 +0000127 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 Duxbury1b130832010-10-19 17:20:57 +0000133 System.out.println("Starting the simple server...");
Mark Sleecb39f082007-04-10 02:30:30 +0000134 server.serve();
Bryan Duxbury1b130832010-10-19 17:20:57 +0000135 } catch (Exception e) {
136 e.printStackTrace();
Mark Sleecb39f082007-04-10 02:30:30 +0000137 }
Bryan Duxbury1b130832010-10-19 17:20:57 +0000138 }
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 Sleecb39f082007-04-10 02:30:30 +0000170 }
171}