View Javadoc
1   package org.codehaus.plexus.components.io.resources;
2   
3   /*
4    * Copyright 2007 The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.Closeable;
20  import java.io.File;
21  import java.io.IOException;
22  import java.util.Iterator;
23  
24  import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;
25  
26  /**
27   * Default implementation of {@link PlexusIoFileResourceCollection} for
28   * zip files, tar files, etc.
29   * @author jwi
30   *
31   */
32  public abstract class AbstractPlexusIoArchiveResourceCollection extends AbstractPlexusIoResourceCollection
33      implements PlexusIoArchivedResourceCollection
34  {
35  
36      private File file;
37  
38      protected AbstractPlexusIoArchiveResourceCollection()
39      {
40      }
41  
42      /**
43       * Sets the zip file
44       */
45      public void setFile( File file )
46      {
47          this.file = file;
48      }
49  
50      /**
51       * Returns the zip file
52       */
53      public File getFile()
54      {
55          return file;
56      }
57  
58      /**
59       * Returns an iterator over the archives entries.
60       * @return An iterator, may be java.io.Closeable
61       * @throws java.io.IOException an IOException, doh
62       */
63      protected abstract Iterator<PlexusIoResource> getEntries() throws IOException;
64  
65      public Iterator<PlexusIoResource> getResources() throws IOException
66      {
67          return new FilteringIterator();
68      }
69  
70      class FilteringIterator
71          implements Iterator<PlexusIoResource>, Closeable
72      {
73          final Iterator<PlexusIoResource> it = getEntries();
74  
75          PlexusIoResource next;
76  
77          public FilteringIterator()
78              throws IOException
79          {
80          }
81  
82          boolean doNext()
83          {
84              while ( it.hasNext() )
85              {
86                  PlexusIoResource candidate = it.next();
87                  try
88                  {
89                      if ( isSelected( candidate ) )
90                      {
91                          next = candidate;
92                          return true;
93                      }
94                  }
95                  catch ( IOException e )
96                  {
97                      throw new RuntimeException( e );
98                  }
99              }
100             return false;
101         }
102         public boolean hasNext()
103         {
104             return doNext();
105         }
106 
107         public PlexusIoResource next()
108         {
109             if ( next == null )
110                 doNext();
111             PlexusIoResource res = next;
112             next = null;
113             return res;
114         }
115 
116         public void remove()
117         {
118             throw new UnsupportedOperationException();
119         }
120 
121         public void close()
122             throws IOException
123         {
124             if ( it instanceof Closeable )
125             {
126                 ( (Closeable) it ).close();
127             }
128         }
129     }
130     public Stream stream()
131     {
132         return new Stream()
133         {
134             public void forEach( PlexusIoResourceConsumer resourceConsumer )
135                 throws IOException
136             {
137 
138                 final Iterator<PlexusIoResource> it = getEntries();
139                 while ( it.hasNext() )
140                 {
141                     final PlexusIoResource res = it.next();
142                     if ( isSelected( res ) )
143                     {
144                         resourceConsumer.accept( res );
145                     }
146                 }
147                 if ( it instanceof Closeable )
148                 {
149                     ( (Closeable) it ).close();
150                 }
151             }
152         };
153     }
154 
155     public long getLastModified() throws IOException
156     {
157         File f = getFile();
158         return f == null ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : f.lastModified();
159     }
160 }