Package anvil.server

Source Code of anvil.server.ScriptContentHandler

/*
* $Id: ScriptContentHandler.java,v 1.18 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.server;

import java.io.Writer;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;

import anvil.Log;
import anvil.core.Any;
import anvil.Product;
import anvil.script.Type;
import anvil.script.Module;
import anvil.script.Function;
import anvil.script.NamespaceImpl;
import anvil.parser.InputSource;
import anvil.parser.StreamInputSource;
import anvil.util.PrettyPrinter;

/**
* class ScriptContentHandler
*
* @author: Jani Lehtim�ki
* @author: Jaripekka Salminen
*/
public class ScriptContentHandler implements ContentHandler
{

  public static final ContentHandler INSTANCE = new ScriptContentHandler();
 

  public ScriptContentHandler()
  {
  }


  public boolean isSessionRequired()
  {
    return true;
  }
 

  public void service(Context context, Resource resource) throws Exception
  {
    Runtime runtime = Runtime.getRuntime();
    long used_start = (runtime.totalMemory() - runtime.freeMemory());
    long started = System.currentTimeMillis();
   
    try {

      context.log().debug(context.getRequest().getMethod()+' '+context.getAddress().getPathinfo());

      OutputStream scriptOutput;
      OutputStream finalOutput = context.getOutputStream();
      Zone zone = context.getZone();
      boolean printPretty = zone.printPretty() ||
        (context.getRequest().getParameter("anvil.pretty") != null);

      if (printPretty) {
        scriptOutput = new ByteArrayOutputStream();
      } else {
        scriptOutput = finalOutput;
      }

      anvil.core.net.AnyContext anyContext = new anvil.core.net.AnyContext(context);
      context.getResponse().setHeader("Cache-Control", "no-cache");
      Module script = zone.getServer().getCache().load(context.getAddress()).getModule();
      Product product = new Product(
        context.getAddress(), scriptOutput, context.getCitizen(), script);
      try {
        product.forge("service", anyContext);
      } finally {
        product.destroy();
        product = null;
      }

      if (printPretty) {
        ByteArrayOutputStream byteStream =(ByteArrayOutputStream)scriptOutput;
        InputSource inputSource =
          new StreamInputSource(new ByteArrayInputStream(byteStream.toByteArray()));
        Writer writer = new PrintWriter(finalOutput);
        PrettyPrinter prettyPrinter = new PrettyPrinter(inputSource, writer);
        prettyPrinter.print();
        writer.flush();

      }
    } finally {
      Log log = context.log();

      long ended = System.currentTimeMillis();
      log.debug(context.getAddress().getPathinfo() + ' ' + (ended - started) + " ms");

      long used_end = (runtime.totalMemory() - runtime.freeMemory());
      log.info("Memory (kB)  "+(used_start/1024)+" - "+(used_end/1024)+" = "+(used_end - used_start)/1024);
      //log.info("Used_end_kB   : "+);
      //log.info("Delta_kB      : "+);

    }
  }
}
TOP

Related Classes of anvil.server.ScriptContentHandler

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.