1 20 package org.enhydra.barracuda.core.event.helper; 21 22 import java.io.*; 23 import java.util.*; 24 import javax.servlet.*; 25 26 import org.apache.log4j.*; 27 import org.w3c.dom.*; 28 29 import org.enhydra.barracuda.core.comp.*; 30 import org.enhydra.barracuda.core.util.dom.*; 31 import org.enhydra.barracuda.core.event.*; 32 import org.enhydra.barracuda.core.helper.servlet.*; 33 34 43 public abstract class DefaultViewHandler extends DefaultBaseEventListener { 44 45 protected static final Logger localLogger = Logger.getLogger(DefaultViewHandler.class.getName()); 47 48 protected boolean recycleChildren = false; 58 protected ViewContext vc = null; 60 63 public DefaultViewHandler() {} 64 65 66 79 public abstract Document handleViewEvent(BComponent root) throws EventException, ServletException, IOException; 82 90 public void preCompRender(BComponent root) { } 94 95 103 public void postCompRender(BComponent root) { } 107 108 114 public DOMWriter getDOMWriter() { 115 return new DefaultDOMWriter(); 125 } 126 127 134 public void setViewContext(ViewContext ivc) { 135 vc = ivc; 136 } 137 138 141 public ViewContext getViewContext() { 142 return vc; 143 } 144 146 147 151 public void handleViewEvent(ViewEventContext vec) throws EventException, ServletException, IOException { 152 long bmillis = 0; 153 long smillis = 0; 154 long emillis = 0; 155 long elapsed1 = 0; 156 long elapsed2 = 0; 157 if (localLogger.isInfoEnabled()) bmillis = System.currentTimeMillis(); 158 if (localLogger.isInfoEnabled()) localLogger.info("Handling ViewEvent in "+this); 159 160 try { 161 if (localLogger.isDebugEnabled()) localLogger.debug("Create the ViewContext"); 163 ViewContext vc = new DefaultViewContext(vec); 164 setViewContext(vc); 166 if (localLogger.isDebugEnabled()) localLogger.debug("Create component root"); 168 BComponent broot = new BComponent(); 169 broot.setName("Root"); 170 171 if (localLogger.isDebugEnabled()) localLogger.debug("Handling default"); 173 Document doc = handleViewEvent(broot); 176 if (localLogger.isDebugEnabled()) localLogger.debug("Invoking initCycle on component hierarchy"); 178 broot.initCycle(); 179 180 if (localLogger.isDebugEnabled()) localLogger.debug("Pre-component render"); 182 if (localLogger.isDebugEnabled()) smillis = System.currentTimeMillis(); 183 preCompRender(broot); if (localLogger.isDebugEnabled()) elapsed1 = System.currentTimeMillis()-smillis; 186 187 if (localLogger.isDebugEnabled()) localLogger.debug("Rendering component hierarchy"); 189 if (localLogger.isDebugEnabled()) smillis = System.currentTimeMillis(); 190 broot.render(vc); 191 if (localLogger.isDebugEnabled()) elapsed1 = System.currentTimeMillis()-smillis; 192 193 if (localLogger.isDebugEnabled()) localLogger.debug("Post-component render"); 195 if (localLogger.isDebugEnabled()) smillis = System.currentTimeMillis(); 196 postCompRender(broot); if (localLogger.isDebugEnabled()) elapsed1 = System.currentTimeMillis()-smillis; 199 200 if (recycleChildren) { 203 if (localLogger.isDebugEnabled()) localLogger.debug("Recycling child components"); 204 List children = broot.getChildren(); 205 if (children!=null) { 206 for (int i=children.size()-1; i>=0; i--) { 207 broot.removeChild(i); 208 } 209 } 210 } 211 212 if (localLogger.isDebugEnabled()) localLogger.debug("Invoking destroyCycle on component hierarchy"); 214 broot.destroyCycle(); 215 216 ScriptDetector.prepareClientResp(doc, vc); 220 221 if (localLogger.isDebugEnabled()) localLogger.debug("Rendering the DOM"); 223 if (localLogger.isInfoEnabled()) smillis = System.currentTimeMillis(); 224 this.getDOMWriter().write(doc, vec.getResponse()); 225 if (localLogger.isInfoEnabled()) { 226 elapsed2 = System.currentTimeMillis()-smillis; 227 emillis = System.currentTimeMillis(); 228 } 229 } catch (RenderException e) { 230 if (localLogger.isInfoEnabled()) localLogger.info("Unexpected RenderException:"+e); 232 throw new EventException ("Unexpected RenderException:"+e, e); 233 234 } catch (RuntimeException re) { 235 if (localLogger.isInfoEnabled()) localLogger.info("Unexpected RuntimeException:"+re, re); 237 throw re; 238 239 } finally { 240 setViewContext(null); } 242 243 if (localLogger.isInfoEnabled()) localLogger.info("ViewEvent handled! (rendered in "+(elapsed1)+"/written in "+(elapsed2)+" of "+(emillis-bmillis)+" millis)"); 244 } 245 246 247 } 248 | Popular Tags |