Examples of VelocityWriter


Examples of org.apache.velocity.io.VelocityWriter

                                 HttpServletResponse response)
        throws ResourceNotFoundException, ParseErrorException,
               MethodInvocationException, IOException,
               UnsupportedEncodingException, Exception
    {
        VelocityWriter vw = null;
        Writer writer = getResponseWriter(response);
        try
        {
            vw = (VelocityWriter)writerPool.get();
            if (vw == null)
            {
                vw = new VelocityWriter(writer, 4 * 1024, true);
            }
            else
            {
                vw.recycle(writer);
            }
            template.merge(context, vw);
        }
        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();
                    /* This hack sets the VelocityWriter's internal ref to the
                     * PrintWriter to null to keep memory free while
                     * the writer is pooled. See bug report #18951 */
                    vw.recycle(null);
                    writerPool.put(vw);
                }
                catch (Exception e)
                {
                    Velocity.debug("VelocityViewServlet: " +
View Full Code Here

Examples of org.apache.velocity.io.VelocityWriter

                                 HttpServletResponse response)
        throws ResourceNotFoundException, ParseErrorException,
               MethodInvocationException, IOException,
               UnsupportedEncodingException, Exception
    {
        VelocityWriter vw = null;
        Writer writer = getResponseWriter(response);
        try
        {
            vw = (VelocityWriter)writerPool.get();
            if (vw == null)
            {
                vw = new VelocityWriter(writer, 4 * 1024, true);
            }
            else
            {
                vw.recycle(writer);
            }
            performMerge(template, context, vw);
        }
        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();
                    /* This hack sets the VelocityWriter's internal ref to the
                     * PrintWriter to null to keep memory free while
                     * the writer is pooled. See bug report #18951 */
                    vw.recycle(null);
                    writerPool.put(vw);
                }
                catch (Exception e)
                {
                    velocity.debug("VelocityViewServlet: " +
View Full Code Here

Examples of org.apache.velocity.io.VelocityWriter

     */
    protected void mergeTemplate( Template template, Context context, HttpServletResponse response )
        throws Exception
    {
        ServletOutputStream output = response.getOutputStream();
        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);
        }
        finally
        {
            try
            {
                if (vw != null)
                {
                    vw.flush();
                    writerPool.put(vw);
                    output.close();
                }               
            }
            catch (Exception e)
View Full Code Here

Examples of org.apache.velocity.io.VelocityWriter

        response.setCharacterEncoding(encoding==null ? "UTF-8" : encoding);
        // init context:
        Context context = new VelocityContext(model);
        afterContextPrepared(context);
        // render:
        VelocityWriter vw = new VelocityWriter(response.getWriter());
        try {
            template.merge(context, vw);
            vw.flush();
        }
        finally {
            vw.recycle(null);
        }
    }
View Full Code Here

Examples of org.apache.velocity.io.VelocityWriter

     * @param writer into which the content is rendered
     */
    public void merge(Template template, Context context, Writer writer)
        throws IOException
    {
        VelocityWriter vw = null;
        try
        {
            vw = (VelocityWriter)writerPool.get();
            if (vw == null)
            {
                vw = new VelocityWriter(writer, 4 * 1024, true);
            }
            else
            {
                vw.recycle(writer);
            }
            performMerge(template, context, vw);
        }
        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();
                    /* This hack sets the VelocityWriter's internal ref to the
                     * PrintWriter to null to keep memory free while
                     * the writer is pooled. See bug report #18951 */
                    vw.recycle(null);
                    writerPool.put(vw);
                }
                catch (Exception e)
                {
                    getLog().error("Trouble releasing VelocityWriter: " +
View Full Code Here

Examples of org.apache.velocity.io.VelocityWriter

    RenderResponseImpl resimp = (RenderResponseImpl) res;
    Context context = VelocityUtil.getWebContext(reqimp.getHttpServletRequest(), resimp.getHttpServletResponse());
   
    res.setContentType(req.getResponseContentType());

    VelocityWriter velocityWriter = null;

    try {
      velocityWriter = (VelocityWriter)writerPool.get();

      PrintWriter output = res.getWriter();

      if (velocityWriter == null) {
        velocityWriter = new VelocityWriter(output, 4 * 1024, true);
      }
      else {
        velocityWriter.recycle(output);
      }

      template.merge(context, velocityWriter);
    }finally {
      if (velocityWriter != null) {
        try{
          velocityWriter.flush();
        }catch (Exception e) {
          Logger.error(this,e.getMessage(), e);
        }
        try{
          velocityWriter.recycle(null);
        }catch (Exception e) {
          Logger.error(this,e.getMessage(), e);
        }
        writerPool.put(velocityWriter);
      }
View Full Code Here

Examples of org.apache.velocity.io.VelocityWriter

            IOException,
            UnsupportedEncodingException,
            Exception
    {
        PrintWriter pw = response.getWriter();
        VelocityWriter vw = null;

        try
        {
            vw = (VelocityWriter) writerPool.get();

            if (vw == null)
            {
                vw = new VelocityWriter(pw, 4 * 1024, true);
            }
            else
            {
                vw.recycle(pw);
            }
     
      // Place the VelocityWriter into the Context
      context.put(VELOCITY_WRITER_ATTR, vw);
            template.merge(context, vw);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            try
            {
                if (vw != null)
                {
                    // flush and put back into the pool
                    // don't close to allow us to play
                    // nicely with others.
                    vw.flush();
                    /* This hack sets the VelocityWriter's internal ref to the
                     * PrintWriter to null to keep memory free while
                     * the writer is pooled. See bug report #18951 */
                    vw.recycle(null);
                    writerPool.put(vw);
                }
            }
            catch (Exception e)
            {
View Full Code Here

Examples of org.apache.velocity.io.VelocityWriter

        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);
            }
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.