tags:

views:

11

answers:

0

i want to create a comet application using dwr. here is my problem: i want to create a connection class as the comet connection between server and javascript. It should be unique among the session, so i want it to be singleton. i define the creator of this class as singleton however i can't get it work. any idea? here is my dwr.xml

<dwr>
<creator id="singleton" class=" org.directwebremoting.create.SingletonCreator" />
<allow>
    <create creator="singleton" javascript="TOLConnectionFactory">
        <param name="class" value="TOL.TOLConnectionFactory"/>
    </create>
</allow>

TOLConnectionFactory.java

package TOL;

public class TOLConnectionFactory { private static TOLConnectionFactory _instance = null; private static Hashtable _warehouse = null;

private TOLConnectionFactory(){

}

public static TOLConnectionFactory getInstance(){
    if(TOLConnectionFactory._instance == null){
        TOLConnectionFactory._instance = new TOLConnectionFactory();
    }
    return TOLConnectionFactory._instance;
}

public TOLConnection getConnection() throws Exception{
    ScriptSession session = WebContextFactory.get().getScriptSession();
    String loginId = (String)session.getAttribute("loginId");
    if(loginId != null && !loginId.equals("")){
        if(this._warehouse.containsKey(loginId)) { //first connection
            TOLConnection conn = new TOLConnection(loginId);
            this._warehouse.put(loginId, conn);
        }
        return (TOLConnection)this._warehouse.get(loginId);
    } else {
        throw new Exception("not yet login");
    }
}
public String hello(){
    return "hello";
}

}

index.jsp

<%@ page import="TOL.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

TOL_WPC function firstDwr(){ TOLConnectionFactory.getInstance(callBackHello); } function callBackHello(data){ data.hello(function callback(data2){alert(data2)}); }

        </script>
</head>


<body onload="dwr.engine.setActiveReverseAjax(true);firstDwr()">

  <label for="label1">wsds</label>
</body>