CODE:
ChatAction.java
package org.prism.seample.session;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.sip.Address;
import javax.servlet.sip.ServletParseException;
import javax.servlet.sip.SipApplicationSession;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipURI;
import javax.servlet.sip.URI;
import org.jboss.seam.annotations.AutoCreate;
import
org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import
org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
@Name("chatAction")
//@Scope(ScopeType.STATELESS)
@AutoCreate
public class ChatAction {
@Logger
private static Log log;
@Resource
SipFactory sipFactory;
void sendToUser(String to, String message)
throws ServletParseException, IOException {
SipApplicationSession session= sipFactory.createApplicationSession
();
SipURI from = sipFactory.createSipURI("zubeen","localhost");
SipServletRequest request = sipFactory.createRequest(session,
"MESSAGE",from.toString(), to);
request.setContent(message.getBytes(), "text/plain");
request.send();
}
}
SharedStaticStore.java
package org.prism.seample.session;
import java.io.IOException;
import java.util.EventListener;
import java.util.EventObject;
import java.util.LinkedList;
import javax.annotation.Resource;
import javax.servlet.sip.ServletParseException;
import javax.servlet.sip.SipApplicationSession;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipSession;
import javax.servlet.sip.SipURI;
import org.ajax4jsf.event.PushEventListener;
import org.jboss.seam.ScopeType;
import
org.jboss.seam.annotations.In;
import
org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.prism.seample.session.ChatAction;
@Name("sharedStore")
@Scope(ScopeType.STATELESS)
public class StaticSharedStore {
public static LinkedList<String> chat = new LinkedList<String>();
public static String message = new String ();
public static String text = new String();
private static LinkedList<PushEventListener> listeners = new
LinkedList<PushEventListener>();
/*@In (required = false)
SipApplicationSession session;
@In (required = false)*/
@Resource
SipFactory factory;
public void addListener(EventListener eventListener) {
listeners.add((PushEventListener) eventListener);
}
public LinkedList<String> getChat() {
return chat;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void sendText()
{
System.out.println("send : "+this.text);
StaticSharedStore.chat.offer("me:"+this.text);
// makeDirty is what actually causes the refresh
StaticSharedStore.makeDirty();
try {
ChatAction ca=new ChatAction();
ca.sendToUser("sip:zubeen@localhost:5062",this.text);
} catch (ServletParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getMessage() {
return message;
}
public void setChat(String chat) {
this.chat.offer(chat);
}
public void reset() {
chat = new LinkedList<String>();
}
/*
* This method causes the update in the Web Page through Server Push.
* It just notifies the subscribed listeners. The listeners come from
* the a4j:push control everytime someone loads the page from a
broswer.
*
* We start a new thread everytime because otherwise the Seam
contexts
* will be propagated in the Web Pages, which will cause confusion in
* multiuser enviroments where the Web Page would have session-
speific
* data (the session is part of the the Seam context).
*/
public static void makeDirty() {
for(final PushEventListener listener : listeners) {
new Thread() {
public void run() {
listener.onEvent(new EventObject(this));
}
}.start();
}
}
}