01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of 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,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.proxy;
17:
18: import java.util.ArrayList;
19: import java.util.Collection;
20: import java.util.Collections;
21: import java.util.List;
22:
23: import org.directwebremoting.ScriptSession;
24: import org.directwebremoting.WebContext;
25: import org.directwebremoting.WebContextFactory;
26:
27: /**
28: * A way to call functions in JavaScript that return data using a reverse ajax
29: * proxy.
30: * @author Joe Walker [joe at getahead dot ltd dot uk]
31: */
32: public abstract class Callback<T> {
33: /**
34: * Create a Callback from a DWR thread,
35: * i.e. where {@link WebContextFactory#get()} =! null
36: */
37: public Callback() {
38: WebContext context = WebContextFactory.get();
39: if (context == null) {
40: throw new IllegalStateException(
41: "Attempt to use Callback without any ScriptSessions, from a non DWR thread. There is nowhere for replies to go.");
42: }
43:
44: sessions.add(context.getScriptSession());
45: }
46:
47: /**
48: * Used when you need to specify the browser that will be providing the
49: * response
50: * @param session The browser to answer the question
51: */
52: public Callback(ScriptSession session) {
53: sessions.add(session);
54: }
55:
56: /**
57: * Used when you need to specify a group of browsers that will be providing
58: * the responses. The callback will be executed once per browser that
59: * replies.
60: * @param sessionList The browsers to answer the question
61: */
62: public Callback(Collection<ScriptSession> sessionList) {
63: sessions.addAll(sessionList);
64: }
65:
66: /**
67: * A browser has completed some remote call as has data for you
68: * @param data The data returned by the browser
69: */
70: public abstract void dataReturned(T data);
71:
72: /**
73: * Accessor for the ScriptSessions that will reply to the question.
74: * This method is generally for DWR internal use, but only because it's
75: * unlikely to be useful to others.
76: * @return An immutable list of browsers that may reply to the question.
77: */
78: public Collection<ScriptSession> getScriptSessions() {
79: return Collections.unmodifiableCollection(sessions);
80: }
81:
82: /**
83: * We store the ScriptSessions that we send replies to, here.
84: */
85: private final List<ScriptSession> sessions = new ArrayList<ScriptSession>();
86: }
|