You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by dong rui <sm...@hotmail.com> on 2009/03/23 03:23:44 UTC

dwr get ServerContext problem working with tapestry5

Following is the Clock Object:


import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


import org.apache.tapestry5.annotations.Service;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ApplicationGlobals;
import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.ServerContext;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.extend.TaskDispatcher;
import org.directwebremoting.extend.TaskDispatcherFactory;
import org.directwebremoting.ui.dwr.Util;


public class Clock implements Runnable {
	@Inject 
	@Service("ApplicationGlobals") 
	private ApplicationGlobals applicationGlobals; 

    public Clock() {
        ScheduledThreadPoolExecutor executor = new
ScheduledThreadPoolExecutor(1);
        executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);
        System.out.println("In Clock"); //this line can be printed out
correctly
    }

    public void run() {
        if (active) {
            setClockDisplay(new Date().toString());
        }
    }

    /**
     * Called from the client to turn the clock on/off
     */
    public synchronized void toggle() {
        active = !active;
System.out.println("in toggle");//this line can be printed out correctly
        if (active) {
            setClockDisplay("Started");
        }
        else {
            setClockDisplay("Stopped");
        }
    }

    /**
     * Actually alter the clients.
     * @param output The string to display.
     */
    public void setClockDisplay(final String output) {
    	//System.out.println("setClockDisplay");
      //  String page = ServerContextFactory.get().getContextPath() + ;
      //  System.out.println("setClockDisplay:"+output);
    	//Following codes are for test using.
    	ServerContext
sc=ServerContextFactory.get(applicationGlobals.getServletContext()); //So th
problem must be in this line. IE always prompt an "Error" alert box.
    	System.out.println("serverContext"); //this line cannot be printed out
correctly
    	TaskDispatcher taskDispatcher = TaskDispatcherFactory.get(sc);
    	System.out.println("taskDispatcher");
        Collection<ScriptSession> sessions =
taskDispatcher.getTargetSessions();
        if (sessions != null)
        {
        	System.out.println("sessions:"+sessions.size());
       //     return sessions;
        }
        else{
	        WebContext webContext = WebContextFactory.get();
	        if (webContext != null)
	        {
	            sessions = new ArrayList<ScriptSession>();
	            sessions.add(webContext.getScriptSession());
	       //     return sessions;
	        }
        }
        //Collection<ScriptSession> sessions=Browser.getTargetSessions();
        System.out.println("getTargetSessions:"+sessions.size());
        Iterator<ScriptSession> iterator=sessions.iterator();
        while(iterator.hasNext()){
        	ScriptSession session=iterator.next();
        	System.out.println(session.getPage());
        }
    	//Following aren't for text, but it always has the IE prompting "Error"
alert box problem. so I add the for-test code above.
       
Browser.withPage(ServerContextFactory.get(applicationGlobals.getServletContext()),"/manageCar/Dwrtracker",
new Runnable() {
            public void run() {
               // Util.setValue("clockDisplay", output);
                ScriptSessions.addFunctionCall("updateClockValue", output);
       
            }
        });
    }

    protected transient boolean active = false;
}

The problem is everytime the setClockDisplay Method triggered, IE always
prompt out an "Error" alert box. and the text can't be displayed correctly.

Anyone can help me for this?
Thank you.
-- 
View this message in context: http://www.nabble.com/dwr-get-ServerContext-problem-working-with-tapestry5-tp22653370p22653370.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: dwr get ServerContext problem working with tapestry5

Posted by dong rui <sm...@hotmail.com>.
resolved.
I changed the setClockDisplay method to a tapestry page, and add the page as
a listener to the Clock object.
as the following:

@Inject 
        @Service("ApplicationGlobals") 
        private ApplicationGlobals applicationGlobals; 

	@SetupRender
	void setListener(){
		Clock.setListener(this);
	}

public void setClockDisplay(final String output) { 

Browser.withPage(ServerContextFactory.get(applicationGlobals.getServletContext()),"/manageCar/Dwrtracker",
new Runnable() { 
            public void run() { 
               // Util.setValue("clockDisplay", output); 
            System.out.println("updateClockValue"); 
                ScriptSessions.addFunctionCall("updateClockValue", output); 
        
            } 
        }); 
}

-- 
View this message in context: http://www.nabble.com/dwr-get-ServerContext-problem-working-with-tapestry5-tp22653370p22654774.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org