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.getahead.dwrdemo.chat;
17:
18: import java.util.Collection;
19: import java.util.LinkedList;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.commons.logging.LogFactory;
23: import org.directwebremoting.ScriptSession;
24: import org.directwebremoting.WebContext;
25: import org.directwebremoting.WebContextFactory;
26: import org.directwebremoting.proxy.ScriptProxy;
27:
28: /**
29: * @author Joe Walker [joe at getahead dot ltd dot uk]
30: */
31: public class JavascriptChat {
32: /**
33: * @param text The new message text to add
34: */
35: public void addMessage(String text) {
36: if (text != null && text.trim().length() > 0) {
37: messages.addFirst(new Message(text));
38: while (messages.size() > 10) {
39: messages.removeLast();
40: }
41: }
42:
43: WebContext wctx = WebContextFactory.get();
44: String currentPage = wctx.getCurrentPage();
45:
46: Collection<ScriptSession> sessions = wctx
47: .getScriptSessionsByPage(currentPage);
48: ScriptProxy s = new ScriptProxy(sessions);
49: s.addFunctionCall("receiveMessages", messages);
50: }
51:
52: /**
53: * The current set of messages
54: */
55: private LinkedList<Message> messages = new LinkedList<Message>();
56:
57: /**
58: * The log stream
59: */
60: protected static final Log log = LogFactory
61: .getLog(JavascriptChat.class);
62: }
|