01: /*
02: * Created on 09.10.2004
03: *
04: * To change the template for this generated file go to
05: * Window - Preferences - Java - Code Generation - Code and Comments
06: */
07: package de.schlund.pfixcore.example.webservices.chat;
08:
09: import java.util.ArrayList;
10:
11: import org.w3c.dom.Element;
12:
13: import de.schlund.pfixcore.workflow.Context;
14: import de.schlund.pfixcore.workflow.ContextResource;
15: import de.schlund.pfixxml.ResultDocument;
16:
17: /**
18: * @author ml
19: *
20: * To change the template for this generated type comment go to
21: * Window - Preferences - Java - Code Generation - Code and Comments
22: */
23: public class ContextChatImpl implements ContextResource, ContextChat {
24:
25: private boolean loggedIn;
26: private String nickName;
27: private ArrayList<Message> messageCache;
28: private ArrayList<Message> lastMessages;
29: private int maxCache = 100;
30:
31: public void init(Context context) {
32: reset();
33: }
34:
35: public void reset() {
36: loggedIn = false;
37: messageCache = new ArrayList<Message>();
38: lastMessages = new ArrayList<Message>();
39: nickName = null;
40: }
41:
42: public boolean needsData() {
43: return false;
44: }
45:
46: public void insertStatus(ResultDocument resdoc, Element node)
47: throws Exception {
48: node.setAttribute("loggedin", Boolean.toString(loggedIn));
49: if (loggedIn)
50: node.setAttribute("nickname", nickName);
51: }
52:
53: public synchronized void login(String nickName) throws Exception {
54: if (loggedIn)
55: throw new Exception("You're already logged in");
56: ChatServer.getInstance().register(nickName, this );
57: this .nickName = nickName;
58: loggedIn = true;
59: }
60:
61: public synchronized void logout() throws Exception {
62: if (!loggedIn)
63: throw new Exception("You're already logged out");
64: ChatServer.getInstance().deregister(nickName);
65: loggedIn = false;
66: }
67:
68: public synchronized void sendMessage(String text,
69: String[] recipients) throws Exception {
70: if (!loggedIn)
71: throw new Exception("You're not logged in");
72: ChatServer.getInstance().publish(nickName, text, recipients);
73: }
74:
75: public synchronized void addMessage(Message msg) {
76: if (messageCache.size() == maxCache)
77: messageCache.remove(0);
78: messageCache.add(msg);
79: lastMessages.add(msg);
80: }
81:
82: public synchronized Message[] getMessages() {
83: Message[] msgs = new Message[messageCache.size()];
84: messageCache.toArray(msgs);
85: return msgs;
86: }
87:
88: public synchronized Message[] getLastMessages() {
89: Message[] msgs = new Message[lastMessages.size()];
90: lastMessages.toArray(msgs);
91: lastMessages.clear();
92: return msgs;
93: }
94:
95: public synchronized String[] getNickNames() {
96: return ChatServer.getInstance().getRegisteredNames();
97: }
98:
99: }
|