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

import java.io.InputStream;
import java.io.IOException;
import com.silverwrist.dynamo.except.NoSuchResourceException;
import com.silverwrist.dynamo.iface.ResourceProvider;

class WrappedResourceProvider
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ComponentResName m_mount_point;
  private ResourceProvider m_provider;

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

  WrappedResourceProvider(ComponentResName mount_point, ResourceProvider provider)
  {
    m_mount_point = mount_point;
    m_provider = provider;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  public boolean equals(Object obj)
  {
    if ((obj==null) || !(obj instanceof WrappedResourceProvider))
      return false;
    WrappedResourceProvider other = (WrappedResourceProvider)obj;
    return m_mount_point.equals(other.m_mount_point);

  } // end equals

  public int hashCode()
  {
    return m_mount_point.hashCode();

  } // end hashCode

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  int getMatchCount(ComponentResName crn)
  {
    int rc = m_mount_point.getMatchCount(crn);
    if (rc!=m_mount_point.getComponentCount())
      return -1;
    return rc;

  } // end getMatchCount

  InputStream getResource(ComponentResName crn) throws IOException
  {
    if (m_mount_point.getMatchCount(crn)!=m_mount_point.getComponentCount())
      throw new NoSuchResourceException(crn.getStringValue(0,true));
    return m_provider.getResource(crn.getStringValue(m_mount_point.getComponentCount(),true));

  } // end getResource

  long getResourceModTime(ComponentResName crn)
  {
    if (m_mount_point.getMatchCount(crn)!=m_mount_point.getComponentCount())
      return 0;
    return m_provider.getResourceModTime(crn.getStringValue(m_mount_point.getComponentCount(),true));

  } // end getResourceModTime

} // end class WrappedResourceProvider

