KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > webapp > http > core > SendUpdates


1 package com.icesoft.faces.webapp.http.core;
2
3 import com.icesoft.faces.webapp.command.Command;
4 import com.icesoft.faces.webapp.command.CommandQueue;
5 import com.icesoft.faces.webapp.command.Macro;
6 import com.icesoft.faces.webapp.command.NOOP;
7 import com.icesoft.faces.webapp.http.common.Request;
8 import com.icesoft.faces.webapp.http.common.Server;
9 import com.icesoft.faces.webapp.http.common.standard.FixedXMLContentHandler;
10
11 import java.io.IOException JavaDoc;
12 import java.io.Writer JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 public class SendUpdates implements Server {
20     private static final Command NOOP = new NOOP();
21     private Map JavaDoc commandQueues;
22
23     public SendUpdates(Map JavaDoc commandQueues) {
24         this.commandQueues = commandQueues;
25     }
26
27     public void service(final Request request) throws Exception JavaDoc {
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 JavaDoc commandQueues;
37
38         public Handler(Map JavaDoc commandQueues, Request request) {
39             this.commandQueues = commandQueues;
40             this.request = request;
41         }
42
43         public void writeTo(Writer JavaDoc writer) throws IOException JavaDoc {
44             HashSet JavaDoc viewIdentifierSet = new HashSet JavaDoc(Arrays.asList(request.getParameterAsStrings("viewNumber")));
45             Iterator JavaDoc viewIdentifiers = viewIdentifierSet.iterator();
46             ArrayList JavaDoc commandList = new ArrayList JavaDoc(viewIdentifierSet.size());
47             while (viewIdentifiers.hasNext()) {
48                 Object JavaDoc viewIdentifier = viewIdentifiers.next();
49                 if (commandQueues.containsKey(viewIdentifier)) {
50                     CommandQueue queue = (CommandQueue) commandQueues.get(viewIdentifier);
51                     commandList.add(queue.take());
52                 }
53             }
54
55             if (commandList.size() > 1) {
56                 Command[] commands = (Command[]) commandList.toArray(new Command[commandList.size()]);
57                 new Macro(commands).serializeTo(writer);
58             } else if (commandList.size() == 1) {
59                 Command command = (Command) commandList.get(0);
60                 command.serializeTo(writer);
61             } else {
62                 NOOP.serializeTo(writer);
63             }
64         }
65     }
66 }
67
Popular Tags