01: package com.icesoft.faces.webapp.http.core;
02:
03: import com.icesoft.faces.webapp.command.Command;
04: import com.icesoft.faces.webapp.command.CommandQueue;
05: import com.icesoft.faces.webapp.command.Macro;
06: import com.icesoft.faces.webapp.command.NOOP;
07: import com.icesoft.faces.webapp.http.common.Request;
08: import com.icesoft.faces.webapp.http.common.Server;
09: import com.icesoft.faces.webapp.http.common.standard.FixedXMLContentHandler;
10:
11: import java.io.IOException;
12: import java.io.Writer;
13: import java.util.ArrayList;
14: import java.util.Arrays;
15: import java.util.HashSet;
16: import java.util.Iterator;
17: import java.util.Map;
18:
19: public class SendUpdates implements Server {
20: private static final Command NOOP = new NOOP();
21: private Map commandQueues;
22:
23: public SendUpdates(Map commandQueues) {
24: this .commandQueues = commandQueues;
25: }
26:
27: public void service(final Request request) throws Exception {
28: request.respondWith(new Handler(commandQueues, request));
29: }
30:
31: public void shutdown() {
32: }
33:
34: public static class Handler extends FixedXMLContentHandler {
35: private final Request request;
36: private Map commandQueues;
37:
38: public Handler(Map commandQueues, Request request) {
39: this .commandQueues = commandQueues;
40: this .request = request;
41: }
42:
43: public void writeTo(Writer writer) throws IOException {
44: HashSet viewIdentifierSet = new HashSet(Arrays
45: .asList(request
46: .getParameterAsStrings("ice.view.all")));
47: Iterator viewIdentifiers = viewIdentifierSet.iterator();
48: ArrayList commandList = new ArrayList(viewIdentifierSet
49: .size());
50: while (viewIdentifiers.hasNext()) {
51: Object viewIdentifier = viewIdentifiers.next();
52: if (commandQueues.containsKey(viewIdentifier)) {
53: CommandQueue queue = (CommandQueue) commandQueues
54: .get(viewIdentifier);
55: commandList.add(queue.take());
56: }
57: }
58:
59: if (commandList.size() > 1) {
60: Command[] commands = (Command[]) commandList
61: .toArray(new Command[commandList.size()]);
62: new Macro(commands).serializeTo(writer);
63: } else if (commandList.size() == 1) {
64: Command command = (Command) commandList.get(0);
65: command.serializeTo(writer);
66: } else {
67: NOOP.serializeTo(writer);
68: }
69: }
70: }
71: }
|