protected void mergeTemplate ( Template template , InvocationContext context ) throws ResourceNotFoundException , ParseErrorException , MethodInvocationException , UnsupportedEncodingException , IOException , Exception { HttpServletResponse response = context . getResponse ( ) ; ServletOutputStream output = response . getOutputStream ( ) ; // ASSUMPTION: response.setContentType() has been called. String encoding = response . getCharacterEncoding ( ) ; VelocityWriter vw = null ; try { vw = ( VelocityWriter ) _writerPool . get ( ) ; if ( vw == null ) { vw = new VelocityWriter ( new OutputStreamWriter ( output , encoding ) , 4 * 1024 , true ) ; } else { vw . recycle ( new OutputStreamWriter ( output , encoding ) ) ; } template . merge ( context , vw ) ; } catch ( IOException ioe ) { // the client probably crashed or aborted the connection ungracefully, so use log.info log . info ( "Failed to write response" , "uri" , context . getRequest ( ) . getRequestURI ( ) , "error" , ioe ) ; } finally { if ( vw != null ) { try { // flush and put back into the pool don't close to allow us to play nicely with // others. vw . flush ( ) ; } catch ( IOException e ) { // do nothing } // Clear the VelocityWriter's reference to its internal OutputStreamWriter to allow // the latter to be GC'd while vw is pooled. vw . recycle ( null ) ; _writerPool . put ( vw ) ; } } }
Merges the template with the context .