/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.servlet;

import java.io.IOException;
import java.lang.ref.SoftReference;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.config.RootConfig;

public class StyleSheetServlet extends BaseServlet
{
  /*--------------------------------------------------------------------------------
   * Internal class for returning the stylesheet data.
   *--------------------------------------------------------------------------------
   */

  static class StyleSheetOutput implements ContentExecute
  {
    private String data;  // the data to output

    StyleSheetOutput(String data)
    {
      this.data = data;

    } // end constructor

    public void execute(RequestExec req) throws IOException
    {
      if (data!=null)  // send the data
	req.sendText("text/css",data);
      else  // No stylesheet present - send back an error
	req.error(HttpServletResponse.SC_NOT_FOUND,"stylesheets not enabled");

    } // end execute

  } // end class StyleSheetOutput

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private SoftReference ref_stylesheet = null;  // reference to stylesheet data

  /*--------------------------------------------------------------------------------
   * Implementations from class BaseServlet
   *--------------------------------------------------------------------------------
   */

  public Object process(RequestInput req)
  {
    String data = null;
    ServletContext ctxt = getServletContext();
    RootConfig config = getRootConfig(ctxt);

    if (config.usingStyleSheet())
    { // we need to load the stylesheet data...
      synchronized (this)
      { // see if the stylesheet data has already been loaded
	if (ref_stylesheet!=null)
	  data = (String)(ref_stylesheet.get());
	if ((data==null) || config.hasStyleSheetChanged())
	{ // construct or reconstruct the stylesheet data, save a soft reference to it
	  if (ref_stylesheet!=null)
	    ref_stylesheet.clear();
	  try
	  { // retrieve the stylesheet data
	    data = config.loadStyleSheetData();
	    ref_stylesheet = new SoftReference(data);

	  } // end try
	  catch (IOException e)
	  { // can't load the stylesheet
	    data = null;

	  } // end catch

	} // end if

      } // end synchronized block

    } // end if
    // else not using stylesheet data at all...

    return new StyleSheetOutput(data);

  } // end process

} // end class StyleSheetServlet
