001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.simplerpc.client;
017:
018: import com.google.gwt.core.client.EntryPoint;
019: import com.google.gwt.core.client.GWT;
020: import com.google.gwt.user.client.Window;
021: import com.google.gwt.user.client.rpc.AsyncCallback;
022: import com.google.gwt.user.client.rpc.ServiceDefTarget;
023: import com.google.gwt.user.client.ui.FlexTable;
024: import com.google.gwt.user.client.ui.HTML;
025: import com.google.gwt.user.client.ui.Panel;
026: import com.google.gwt.user.client.ui.RootPanel;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Map.Entry;
032:
033: /**
034: * Demonstrates a simple use of the RPC mechanism.
035: */
036: public class SimpleRPC implements EntryPoint {
037:
038: public void onModuleLoad() {
039: final Panel root = RootPanel.get();
040:
041: // Create the RPC client.
042: SimpleRPCServiceAsync simpleRPCService = createSimpleRPCServiceAsync();
043:
044: // Collection of very simple RPC calls to "getString".
045: callGetString(root, simpleRPCService);
046:
047: // A single simple call to "getMultipleStrings".
048: callGetMultipleStrings(root, simpleRPCService);
049: }
050:
051: /**
052: * Creates a single call to <code>getMultipleStrings</code>.
053: */
054: private void callGetMultipleStrings(final Panel root,
055: SimpleRPCServiceAsync simpleRPCService) {
056: AsyncCallback<Map<Integer, String>> getMultipleStringsCallback = createGetMultipleStringsCallback(root);
057:
058: // Should print a table of key value pairs.
059: List<Integer> indexes = new ArrayList<Integer>();
060: indexes.add(new Integer(0));
061: indexes.add(new Integer(2));
062: simpleRPCService.getMultipleStrings(indexes,
063: getMultipleStringsCallback);
064: }
065:
066: /**
067: * Calls <code>getString</code> three times, the first two should return
068: * valid answers, the third should give back an error. <p/> Control flow will
069: * continue after making each call. Later the 'callback' onSuccess or
070: * onFailure method will be invoked when the RPC completes. There is no order
071: * guarantee here, the three results could appear on the page in any order.
072: */
073: private void callGetString(final Panel root,
074: SimpleRPCServiceAsync simpleRPCService) {
075: // Create a callback to use.
076: AsyncCallback<String> singleGetStringCallback = createGetStringCallback(root);
077:
078: // Should print 'Hello World'.
079: simpleRPCService.getString(0, singleGetStringCallback);
080:
081: // Should print 'Bonjour monde'.
082: simpleRPCService.getString(1, singleGetStringCallback);
083:
084: // Should print an IndexOutOfBoundsException.
085: simpleRPCService.getString(3, singleGetStringCallback);
086: }
087:
088: /**
089: * Create an asynchronous callback for the <code>getMultipleStrings</code>
090: * RPC call. The same callback can be used for many RPC calls or customized
091: * for a single one.
092: */
093: private AsyncCallback<Map<Integer, String>> createGetMultipleStringsCallback(
094: final Panel root) {
095: return new AsyncCallback<Map<Integer, String>>() {
096:
097: public void onFailure(Throwable caught) {
098: Window.alert("error: " + caught);
099: }
100:
101: public void onSuccess(Map<Integer, String> result) {
102: FlexTable t = new FlexTable();
103: t.setBorderWidth(2);
104: t.setHTML(0, 0, "<b>Map Key</b>");
105: t.setHTML(0, 1, "<b>Map Value</b>");
106: int index = 1;
107: for (Entry<Integer, String> element : result.entrySet()) {
108: Integer key = element.getKey();
109: String value = element.getValue();
110: t.setText(index, 0, key.toString());
111: t.setText(index, 1, value);
112: ++index;
113: }
114: root.add(new HTML("<h3>Result(on success)</h3>"));
115: root.add(t);
116: }
117: };
118: }
119:
120: /**
121: * Create an asynchronous callback for the <code>getString</code> RPC call.
122: * The same callback can be used for many RPC calls or customized for a single
123: * one.
124: */
125: private AsyncCallback<String> createGetStringCallback(
126: final Panel root) {
127: return new AsyncCallback<String>() {
128: public void onFailure(Throwable caught) {
129: root.add(new HTML("<h3>Result (on failure) </h3>"));
130: root
131: .add(new HTML("<i>" + caught.getMessage()
132: + "</i>"));
133: }
134:
135: public void onSuccess(String result) {
136: root.add(new HTML("<h3>Result (on success) </h3>"
137: + result));
138: }
139: };
140: }
141:
142: /**
143: * Returns an configured instance of the <code>SimpleRPCService</code>
144: * client proxy. <p/> Note that although you are creating the service
145: * interface proper, you cast the result to the asynchronous version of the
146: * interface. The cast is always safe because the generated proxy implements
147: * the asynchronous interface automatically.
148: */
149: private SimpleRPCServiceAsync createSimpleRPCServiceAsync() {
150: SimpleRPCServiceAsync simpleRPCService = GWT
151: .create(SimpleRPCService.class);
152: setServiceURL(simpleRPCService);
153: return simpleRPCService;
154: }
155:
156: /**
157: * Sets the URL where our service implementation is running.
158: * <p>
159: * Note that due to the same origin security policy enforced by the browser
160: * implementations the target URL must reside on the same domain, port and
161: * protocol from which the host page was served.
162: */
163: private void setServiceURL(SimpleRPCServiceAsync simpleRPCService) {
164: ServiceDefTarget endpoint = (ServiceDefTarget) simpleRPCService;
165: String moduleRelativeURL = GWT.getModuleBaseURL() + "simpleRPC";
166: endpoint.setServiceEntryPoint(moduleRelativeURL);
167: }
168: }
|