/*
 * 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) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.script;

import java.io.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

class ScriptExecuteHelper implements ScriptExecute
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ScriptExecute m_actual;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  ScriptExecuteHelper(ScriptExecute actual)
  {
    m_actual = actual;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ScriptExecute
   *--------------------------------------------------------------------------------
   */

  public Object executeScript(Request request, String language, String source_name, String code)
      throws ScriptingException
  {
    return m_actual.executeScript(request,language,source_name,code);

  } // end executeScript

  public Object executeScript(Request request, String language, String source_name, InputStream code)
      throws ScriptingException
  {
    try
    { // load the script data and prepare to execute it
      InputStreamReader rdr = new InputStreamReader(code);
      StringWriter wr = new StringWriter();
      IOUtils.copy(rdr,wr);
      IOUtils.shutdown(rdr);
      return this.executeScript(request,language,source_name,wr.toString());

    } // end try
    catch (IOException e)
    { // the script load failed
      throw new ScriptingException(source_name,language,ScriptController.class,"ScriptEngineMessages",
				   "script.loadFail",e);
						     
    } // end catch

  } // end executeScript

  public Object executeScript(Request request, String language, String resource_name)
      throws ScriptingException
  {
    try
    { // get the resource provider and load the resource
      ResourceProvider prov = (ResourceProvider)(request.queryService(ResourceProvider.class));
      return this.executeScript(request,language,resource_name,prov.getResource(resource_name));

    } // end try
    catch (IOException e)
    { // the script load failed
      throw new ScriptingException(resource_name,language,ScriptController.class,"ScriptEngineMessages",
				   "script.loadFail",e);
						     
    } // end catch
    catch (NoSuchResourceException e)
    { // script not found!
      throw new ScriptingException(resource_name,language,ScriptController.class,"ScriptEngineMessages",
				   "script.notFound",e);
						     
    } // end catch

  } // end executeScript

  public Object executeScript(Request request, String resource_name) throws ScriptingException
  {
    return this.executeScript(request,ScriptEngine.getLanguageFromSourceName(resource_name),resource_name);

  } // end executeScript

} // end class ScriptExecuteHelper
