ResourceFinderGroup.java :  » Web-Framework » rife-1.6.1 » com » uwyn » rife » resources » Java Open Source

Java Open Source » Web Framework » rife 1.6.1 
rife 1.6.1 » com » uwyn » rife » resources » ResourceFinderGroup.java
/*
 * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
 * Distributed under the terms of either:
 * - the common development and distribution license (CDDL), v1.0; or
 * - the GNU Lesser General Public License, v2.1 or later
 * $Id: ResourceFinderGroup.java 3634 2007-01-08 21:42:24Z gbevin $
 */
package com.uwyn.rife.resources;

import com.uwyn.rife.resources.ResourceFinder;
import com.uwyn.rife.resources.exceptions.CantOpenResourceStreamException;
import com.uwyn.rife.resources.exceptions.ResourceFinderErrorException;
import com.uwyn.rife.tools.InputStreamUser;
import java.net.URL;
import java.util.ArrayList;

/**
 * Allows a group of resource finders to acts as if they are one single
 * resource finders. They will be consecutively used in their order of addition
 * to the group.
 *
 * @author Geert Bevin (gbevin[remove] at uwyn dot com)
 * @version $Revision: 3634 $
 * @see com.uwyn.rife.resources.ResourceFinder
 * @since 1.0
 */
public class ResourceFinderGroup extends AbstractResourceFinder
{
  private ArrayList<ResourceFinder>  mResourceFinders = new ArrayList<ResourceFinder>();
  
  public ResourceFinderGroup add(ResourceFinder resourceFinder)
  {
    mResourceFinders.add(resourceFinder);
    
    return this;
  }
  
  public URL getResource(String name)
  {
    URL result = null;
    for (ResourceFinder resource_finder : mResourceFinders)
    {
      result = resource_finder.getResource(name);
      if (result != null)
      {
        return result;
      }
    }
    
    return null;
  }
  
  public <ResultType> ResultType useStream(URL resource, InputStreamUser user)
  throws ResourceFinderErrorException
  {
    ResultType result = null;
    for (ResourceFinder resource_finder : mResourceFinders)
    {
      try
      {
        result = (ResultType)resource_finder.useStream(resource, user);
      }
      catch (CantOpenResourceStreamException e)
      {
        continue;
      }
      
      return result;
    }
    
    throw new CantOpenResourceStreamException(resource, null);
  }
  
  public String getContent(URL resource, String encoding)
  throws ResourceFinderErrorException
  {
    String result = null;
    for (ResourceFinder resource_finder : mResourceFinders)
    {
      result = resource_finder.getContent(resource, encoding);
      if (result != null)
      {
        return result;
      }
    }
    
    return null;
  }
  
  public long getModificationTime(URL resource)
  throws ResourceFinderErrorException
  {
    long result = -1;
    for (ResourceFinder resource_finder : mResourceFinders)
    {
      result = resource_finder.getModificationTime(resource);
      if (result != -1)
      {
        return result;
      }
    }
    
    return -1;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.