01: /* ***** BEGIN LICENSE BLOCK *****
02: * Version: MPL 1.1
03: * The contents of this file are subject to the Mozilla Public License Version
04: * 1.1 (the "License"); you may not use this file except in compliance with
05: * the License. You may obtain a copy of the License at
06: * http://www.mozilla.org/MPL/
07: *
08: * Software distributed under the License is distributed on an "AS IS" basis,
09: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10: * for the specific language governing rights and limitations under the
11: * License.
12: *
13: * The Original Code is Riot.
14: *
15: * The Initial Developer of the Original Code is
16: * Neteye GmbH.
17: * Portions created by the Initial Developer are Copyright (C) 2006
18: * the Initial Developer. All Rights Reserved.
19: *
20: * Contributor(s):
21: * Felix Gnass [fgnass at neteye dot de]
22: *
23: * ***** END LICENSE BLOCK ***** */
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
42: .getName()
43: + ".jobId";
44:
45: private ServletContext servletContext;
46:
47: public void setServletContext(ServletContext servletContext) {
48: this .servletContext = servletContext;
49: }
50:
51: public void register(WebContext wctx, Long jobId) {
52: wctx.getScriptSession().setAttribute(JOB_ID_ATTRIBUTE, jobId);
53: }
54:
55: public void log(JobLogEntry entry) {
56: send(entry.getJobId(), "addLogEntry", entry);
57: }
58:
59: public void updateJob(JobDetail jd) {
60: send(jd.getId(), "updateJob", jd);
61: }
62:
63: private void send(Long jobId, String functionName, Object arg) {
64: ServerContext serverContext = ServerContextFactory
65: .get(servletContext);
66: Iterator it = serverContext.getAllScriptSessions().iterator();
67: while (it.hasNext()) {
68: ScriptSession session = (ScriptSession) it.next();
69: Long pageJobId = (Long) session
70: .getAttribute(JOB_ID_ATTRIBUTE);
71: if (jobId.equals(pageJobId)) {
72: ScriptBuffer script = new ScriptBuffer();
73: script.appendScript(functionName).appendScript("(")
74: .appendData(arg).appendScript(");");
75:
76: session.addScript(script);
77: }
78: }
79: }
80:
81: }
|