1 24 package org.riotfamily.riot.job.ui; 25 26 import java.util.Iterator ; 27 28 import javax.servlet.ServletContext ; 29 30 import org.directwebremoting.ScriptBuffer; 31 import org.directwebremoting.ScriptSession; 32 import org.directwebremoting.ServerContext; 33 import org.directwebremoting.ServerContextFactory; 34 import org.directwebremoting.WebContext; 35 import org.riotfamily.riot.job.persistence.JobDetail; 36 import org.riotfamily.riot.job.persistence.JobLogEntry; 37 import org.springframework.web.context.ServletContextAware; 38 39 public class JobUIUpdater implements ServletContextAware { 40 41 public static final String JOB_ID_ATTRIBUTE = JobUIUpdater.class.getName() + ".jobId"; 42 43 private ServletContext servletContext; 44 45 public void setServletContext(ServletContext servletContext) { 46 this.servletContext = servletContext; 47 } 48 49 public void register(WebContext wctx, Long jobId) { 50 wctx.getScriptSession().setAttribute(JOB_ID_ATTRIBUTE, jobId); 51 } 52 53 public void log(JobLogEntry entry) { 54 send(entry.getJobId(), "addLogEntry", entry); 55 } 56 57 public void updateJob(JobDetail jd) { 58 send(jd.getId(), "updateJob", jd); 59 } 60 61 private void send(Long jobId, String functionName, Object arg) { 62 ServerContext serverContext = ServerContextFactory.get(servletContext); 63 Iterator it = serverContext.getAllScriptSessions().iterator(); 64 while (it.hasNext()) { 65 ScriptSession session = (ScriptSession) it.next(); 66 Long pageJobId = (Long ) session.getAttribute(JOB_ID_ATTRIBUTE); 67 if (jobId.equals(pageJobId)) { 68 ScriptBuffer script = new ScriptBuffer(); 69 script.appendScript(functionName).appendScript("(") 70 .appendData(arg).appendScript(");"); 71 72 session.addScript(script); 73 } 74 } 75 } 76 77 } 78 | Popular Tags |