| 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 ; 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.asList(request.getParameterAsStrings("viewNumber"))); 45 Iterator viewIdentifiers = viewIdentifierSet.iterator(); 46 ArrayList commandList = new ArrayList (viewIdentifierSet.size()); 47 while (viewIdentifiers.hasNext()) { 48 Object 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 |