01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.sample.simplerpc.server;
17:
18: import com.google.gwt.sample.simplerpc.client.SimpleRPCException;
19: import com.google.gwt.sample.simplerpc.client.SimpleRPCService;
20: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
21:
22: import java.util.HashMap;
23: import java.util.List;
24: import java.util.Map;
25:
26: /**
27: * The implementation of the <code>SimpleRPCService</code> code. This code
28: * only runs on the server.
29: */
30: public class SimpleRPCServiceImpl extends RemoteServiceServlet
31: implements SimpleRPCService {
32:
33: /**
34: * The server strings used to supply the information to <code>getString</code>.
35: */
36: private static final String[] SERVER_STRINGS = new String[] {
37: "Hello World", "Bonjour monde", "Hola Espaņol" };
38:
39: /**
40: * Gets a map of strings associated with the given indexes.
41: */
42: public Map<Integer, String> getMultipleStrings(List<Integer> indexes)
43: throws SimpleRPCException {
44: Map<Integer, String> accum = new HashMap<Integer, String>();
45: for (int i = 0; i < indexes.size(); i++) {
46: Integer key = indexes.get(i);
47: String value = getString(key.intValue());
48: accum.put(key, value);
49: }
50: return accum;
51: }
52:
53: /**
54: * Gets a string associated with a given index. In a real world application,
55: * we would be consulting a database or some other server-side set of
56: * information. Here we are just accessing a server side array.
57: *
58: * @param index index of string
59: * @return the string associated with the given index
60: * @throws SimpleRPCException
61: */
62: public String getString(int index) throws SimpleRPCException {
63: try {
64: return SERVER_STRINGS[index];
65: } catch (RuntimeException e) {
66: throw new SimpleRPCException(e.getClass().getName() + ":"
67: + e.getMessage());
68: }
69: }
70: }
|