KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > job > ui > JobUIUpdater


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * 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 JavaDoc;
27
28 import javax.servlet.ServletContext JavaDoc;
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 JavaDoc JOB_ID_ATTRIBUTE = JobUIUpdater.class.getName() + ".jobId";
42             
43     private ServletContext JavaDoc servletContext;
44         
45     public void setServletContext(ServletContext JavaDoc servletContext) {
46         this.servletContext = servletContext;
47     }
48
49     public void register(WebContext wctx, Long JavaDoc 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 JavaDoc jobId, String JavaDoc functionName, Object JavaDoc arg) {
62         ServerContext serverContext = ServerContextFactory.get(servletContext);
63         Iterator JavaDoc it = serverContext.getAllScriptSessions().iterator();
64         while (it.hasNext()) {
65             ScriptSession session = (ScriptSession) it.next();
66             Long JavaDoc pageJobId = (Long JavaDoc) 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