1 29 30 package nextapp.echo2.webcontainer; 31 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.WeakHashMap ; 36 37 import nextapp.echo2.app.ApplicationInstance; 38 import nextapp.echo2.app.Component; 39 import nextapp.echo2.app.TaskQueueHandle; 40 import nextapp.echo2.app.update.UpdateManager; 41 import nextapp.echo2.webcontainer.util.IdTable; 42 import nextapp.echo2.webrender.Connection; 43 import nextapp.echo2.webrender.UserInstance; 44 45 48 public class ContainerInstance extends UserInstance { 49 50 53 private static final int DEFAULT_CALLBACK_INTERVAL = 500; 54 55 62 public static String getElementId(Component component) { 63 return "c_" + component.getRenderId(); 64 } 65 66 74 public static void newInstance(Connection conn) { 75 new ContainerInstance(conn); 76 } 77 78 private ApplicationInstance applicationInstance; 79 private Map componentToRenderStateMap = new HashMap (); 80 private transient IdTable idTable; 81 private boolean initialized = false; 82 private Map initialRequestParameterMap; 83 private transient Map taskQueueToCallbackIntervalMap; 84 85 92 private ContainerInstance(Connection conn) { 93 super(conn); 94 setServerDelayMessage(DefaultServerDelayMessage.INSTANCE); 95 initialRequestParameterMap = new HashMap (conn.getRequest().getParameterMap()); 96 } 97 98 104 public ApplicationInstance getApplicationInstance() { 105 return applicationInstance; 106 } 107 108 119 public int getCallbackInterval() { 120 if (taskQueueToCallbackIntervalMap == null || taskQueueToCallbackIntervalMap.size() == 0) { 121 return DEFAULT_CALLBACK_INTERVAL; 122 } 123 Iterator it = taskQueueToCallbackIntervalMap.values().iterator(); 124 int returnInterval = Integer.MAX_VALUE; 125 while (it.hasNext()) { 126 int interval = ((Integer ) it.next()).intValue(); 127 if (interval < returnInterval) { 128 returnInterval = interval; 129 } 130 } 131 return returnInterval; 132 } 133 134 140 public Component getComponentByElementId(String elementId) { 141 try { 142 return applicationInstance.getComponentByRenderId(elementId.substring(2)); 143 } catch (IndexOutOfBoundsException ex) { 144 throw new IllegalArgumentException ("Invalid component element id: " + elementId); 145 } 146 } 147 148 155 public IdTable getIdTable() { 156 if (idTable == null) { 157 idTable = new IdTable(); 158 } 159 return idTable; 160 } 161 162 168 public Map getInitialRequestParameterMap() { 169 return initialRequestParameterMap; 170 } 171 172 179 public RenderState getRenderState(Component component) { 180 return (RenderState) componentToRenderStateMap.get(component); 181 } 182 183 192 public UpdateManager getUpdateManager() { 193 return applicationInstance.getUpdateManager(); 194 } 195 196 203 public void init(Connection conn) { 204 if (initialized) { 205 throw new IllegalStateException ("Attempt to invoke ContainerInstance.init() on initialized instance."); 206 } 207 WebContainerServlet servlet = (WebContainerServlet) conn.getServlet(); 208 applicationInstance = servlet.newApplicationInstance(); 209 210 ContainerContext containerContext = new ContainerContextImpl(this); 211 applicationInstance.setContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME, containerContext); 212 213 try { 214 ApplicationInstance.setActive(applicationInstance); 215 applicationInstance.doInit(); 216 } finally { 217 ApplicationInstance.setActive(null); 218 } 219 initialized = true; 220 } 221 222 228 boolean isInitialized() { 229 return initialized; 230 } 231 232 238 public void removeRenderState(Component component) { 239 componentToRenderStateMap.remove(component); 240 } 241 242 249 public void setRenderState(Component component, RenderState renderState) { 250 componentToRenderStateMap.put(component, renderState); 251 } 252 253 266 public void setTaskQueueCallbackInterval(TaskQueueHandle taskQueue, int ms) { 267 if (taskQueueToCallbackIntervalMap == null) { 268 taskQueueToCallbackIntervalMap = new WeakHashMap (); 269 } 270 taskQueueToCallbackIntervalMap.put(taskQueue, new Integer (ms)); 271 } 272 } 273 | Popular Tags |