Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util.resource;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 
A collection of resources (dirs). Allows webapps to have multiple (static) sources. The first resource in the collection is the main resource. If a resource is not found in the main resource, it looks it up in the order the resources were constructed.
 
 public class ResourceCollection extends Resource
 {
     private Resource[] _resources;
 
     /* ------------------------------------------------------------ */
    
Instantiates an empty resource collection. This constructor is used when configuring jetty-maven-plugin.
 
     public ResourceCollection()
     {
          = new Resource[0];
     }
     
     /* ------------------------------------------------------------ */
    
Instantiates a new resource collection.

Parameters:
resources the resources to be added to collection
 
     public ResourceCollection(Resource... resources)
     {
         List<Resourcelist = new ArrayList<Resource>();
         for (Resource r : resources)
         {
             if (r==null)
                 continue;
             if (r instanceof ResourceCollection)
             {
                 for (Resource r2 : ((ResourceCollection)r).getResources())
                     list.add(r2);
             }
             else
                 list.add(r);
         }
          = list.toArray(new Resource[list.size()]);
         for(Resource r : )
         {
             if(!r.exists() || !r.isDirectory())
                 throw new IllegalArgumentException(r + " is not an existing directory.");
         }
     }
     
     
     /* ------------------------------------------------------------ */
    
Instantiates a new resource collection.

Parameters:
resources the resource strings to be added to collection
 
     public ResourceCollection(String[] resources)
     {
          = new Resource[resources.length];
         try
        {
            for(int i=0; i<resources.lengthi++)
            {
                [i] = Resource.newResource(resources[i]);
                if(![i].exists() || ![i].isDirectory())
                    throw new IllegalArgumentException([i] + " is not an existing directory.");
            }
        }
        catch(IllegalArgumentException e)
        {
            throw e;
        }
        catch(Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    
    /* ------------------------------------------------------------ */
    
Instantiates a new resource collection.

Parameters:
csvResources the string containing comma-separated resource strings
    public ResourceCollection(String csvResources)
    {
        setResourcesAsCSV(csvResources);
    }
    
    /* ------------------------------------------------------------ */
    
Retrieves the resource collection's resources.

Returns:
the resource array
    public Resource[] getResources()
    {
        return ;
    }
    
    /* ------------------------------------------------------------ */
    
Sets the resource collection's resources.

Parameters:
resources the new resource array
    public void setResources(Resource[] resources)
    {
         = resources != null ? resources : new Resource[0];
    }
    /* ------------------------------------------------------------ */
    
Sets the resources as string of comma-separated values. This method should be used when configuring jetty-maven-plugin.

Parameters:
csvResources the comma-separated string containing one or more resource strings.
    public void setResourcesAsCSV(String csvResources)
    {
        StringTokenizer tokenizer = new StringTokenizer(csvResources",;");
        int len = tokenizer.countTokens();
        if(len==0)
        {
            throw new IllegalArgumentException("ResourceCollection@setResourcesAsCSV(String) " +
                    " argument must be a string containing one or more comma-separated resource strings.");
        }
        
         = new Resource[len];
        try
        {            
            for(int i=0; tokenizer.hasMoreTokens(); i++)
            {
                [i] = Resource.newResource(tokenizer.nextToken().trim());
                if(![i].exists() || ![i].isDirectory())
                    throw new IllegalArgumentException([i] + " is not an existing directory.");
            }
        }
        catch(Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    
    /* ------------------------------------------------------------ */
    

Parameters:
path The path segment to add
Returns:
The contained resource (found first) in the collection of resources
    @Override
    public Resource addPath(String paththrows IOExceptionMalformedURLException
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        if(path==null)
            throw new MalformedURLException();
        
        if(path.length()==0 || ..equals(path))
            return this;
        
        Resource resource=null;
        ArrayList<Resourceresources = null;
        int i=0;
        for(; i<.i++)
        {
            resource = [i].addPath(path);  
            if (resource.exists())
            {
                if (resource.isDirectory())
                    break;       
                return resource;
            }
        }  
        for(i++; i<.i++)
        {
            Resource r = [i].addPath(path); 
            if (r.exists() && r.isDirectory())
            {
                if (resource!=null)
                {
                    resources = new ArrayList<Resource>();
                    resources.add(resource);
                    resource=null;
                }
                resources.add(r);
            }
        }
        if (resource!=null)
            return resource;
        if (resources!=null)
            return new ResourceCollection(resources.toArray(new Resource[resources.size()]));
        return null;
    }
    
    /* ------------------------------------------------------------ */
    

Parameters:
path
Returns:
the resource(file) if found, returns a list of resource dirs if its a dir, else null.
Throws:
java.io.IOException
java.net.MalformedURLException
    protected Object findResource(String paththrows IOExceptionMalformedURLException
    {        
        Resource resource=null;
        ArrayList<Resourceresources = null;
        int i=0;
        for(; i<.i++)
        {
            resource = [i].addPath(path);  
            if (resource.exists())
            {
                if (resource.isDirectory())
                    break;
               
                return resource;
            }
        }  
        for(i++; i<.i++)
        {
            Resource r = [i].addPath(path); 
            if (r.exists() && r.isDirectory())
            {
                if (resource!=null)
                {
                    resources = new ArrayList<Resource>();
                    resources.add(resource);
                }
                resources.add(r);
            }
        }
        
        if (resource!=null)
            return resource;
        if (resources!=null)
            return resources;
        return null;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public boolean delete() throws SecurityException
    {
        throw new UnsupportedOperationException();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public boolean exists()
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        return true;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public File getFile() throws IOException
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        for(Resource r : )
        {
            File f = r.getFile();
            if(f!=null)
                return f;
        }
        return null;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public InputStream getInputStream() throws IOException
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        for(Resource r : )
        {
            InputStream is = r.getInputStream();
            if(is!=null)
                return is;
        }
        return null;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public String getName()
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        for(Resource r : )
        {
            String name = r.getName();
            if(name!=null)
                return name;
        }
        return null;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        for(Resource r : )
        {
            OutputStream os = r.getOutputStream();
            if(os!=null)
                return os;
        }
        return null;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public URL getURL()
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        for(Resource r : )
        {
            URL url = r.getURL();
            if(url!=null)
                return url;
        }
        return null;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public boolean isDirectory()
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        return true;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public long lastModified()
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        for(Resource r : )
        {
            long lm = r.lastModified();
            if (lm!=-1)
                return lm;
        }
        return -1;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public long length()
    {
        return -1;
    }    
    
    /* ------------------------------------------------------------ */
    

Returns:
The list of resource names(merged) contained in the collection of resources.
    
    @Override
    public String[] list()
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        HashSet<Stringset = new HashSet<String>();
        for(Resource r : )
        {
            for(String s : r.list())
                set.add(s);
        }
        String[] result=set.toArray(new String[set.size()]);
        Arrays.sort(result);
        return result;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void release()
    {
        if(==null)
            throw new IllegalStateException("*resources* not set.");
        
        for(Resource r : )
            r.release();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public boolean renameTo(Resource destthrows SecurityException
    {
        throw new UnsupportedOperationException();
    }
    /* ------------------------------------------------------------ */
    @Override
    public void copyTo(File destination)
        throws IOException
    {
        for (int r=.;r-->0;)
            [r].copyTo(destination);
    }
    
    /* ------------------------------------------------------------ */
    

Returns:
the list of resources separated by a path separator
    @Override
    public String toString()
    {
        if(==null)
            return "[]";
        
        return String.valueOf(Arrays.asList());
    }
    /* ------------------------------------------------------------ */
    @Override
    public boolean isContainedIn(Resource rthrows MalformedURLException
    {
        // TODO could look at implementing the semantic of is this collection a subset of the Resource r?
        return false;
    }
New to GrepCode? Check out our FAQ X