/*
 * 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.velocity;

import java.io.*;
import org.apache.commons.collections.*;
import org.apache.velocity.exception.*;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

public class VelocityDynamoResourceLoader extends ResourceLoader
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ResourceProvider m_prov = null;     // Dynamo's ResourceProvider
  private String m_prefix = null;             // prefix to use for resources

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

  public VelocityDynamoResourceLoader()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class ResourceLoader
   *--------------------------------------------------------------------------------
   */

  public void init(ExtendedProperties configuration)
  {
    // pick up the resource provider where we left it
    m_prov = (ResourceProvider)(rsvc.getApplicationAttribute(ResourceProvider.class.getName()));
    m_prefix = (String)(rsvc.getApplicationAttribute(VelocityRenderer.PREFIX_NAME));

  } // end init

  public InputStream getResourceStream(String template_name) throws ResourceNotFoundException
  {
    if (m_prov==null)
      throw new ResourceNotFoundException("Dynamo resource loader not initialized");
    try
    { // bounce request into Dynamo resource provider
      return m_prov.getResource(m_prefix + template_name);

    } // end try
    catch (NoSuchResourceException e)
    { // translate to Velocity's exception before we leave
      throw new ResourceNotFoundException("resource not found: " + template_name);

    } // end catch
    catch (IOException e)
    { // translate this exception, too
      throw new ResourceNotFoundException("resource not found: " + template_name);

    } // end catch

  } // end getResourceStream

  public boolean isSourceModified(Resource resource)
  {
    if (m_prov==null)
      return false;
    long time = m_prov.getResourceModTime(m_prefix + resource.getName());
    return (resource.getLastModified()!=time);

  } // end isSourceModified

  public long getLastModified(Resource resource)
  {
    if (m_prov!=null)
      return m_prov.getResourceModTime(m_prefix + resource.getName());
    else
      return 0;

  } // end getLastModified

} // end class VelocityDynamoResourceLoader
