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 org.codehaus.plexus.components.io.attributes.FileAttributes;
20  import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
21  import org.codehaus.plexus.components.io.attributes.SimpleResourceAttributes;
22  import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;
23  import org.codehaus.plexus.util.DirectoryScanner;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  import java.io.Closeable;
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.concurrent.Callable;
35  import java.util.concurrent.ExecutorService;
36  
37  /**
38   * Implementation of {@link PlexusIoResourceCollection} for the set
39   * of files in a common directory.
40   */
41  public class PlexusIoFileResourceCollection
42      extends AbstractPlexusIoResourceCollectionWithAttributes
43  {
44      /**
45       * Role hint of this component
46       */
47      public static final String ROLE_HINT = "files";
48  
49      private File baseDir;
50  
51      private boolean isFollowingSymLinks = true;
52  
53      public PlexusIoFileResourceCollection()
54      {
55      }
56  
57  
58      public PlexusIoResource resolve( final PlexusIoResource resource )
59          throws IOException
60      {
61          return resource;
62      }
63  
64  
65      @Override
66      public InputStream getInputStream( PlexusIoResource resource )
67          throws IOException
68      {
69          return resource.getContents();
70      }
71  
72  
73      @Override
74      public String getName( PlexusIoResource resource )
75      {
76          return resource.getName();
77      }
78  
79      /**
80       * @param baseDir The base directory of the file collection
81       */
82      public void setBaseDir( File baseDir )
83      {
84          this.baseDir = baseDir;
85      }
86  
87      /**
88       * @return Returns the file collections base directory.
89       */
90      public File getBaseDir()
91      {
92          return baseDir;
93      }
94  
95      /**
96       * @return Returns, whether symbolic links should be followed.
97       * Defaults to true.
98       */
99      public boolean isFollowingSymLinks()
100     {
101         return isFollowingSymLinks;
102     }
103 
104     /**
105      * @param pIsFollowingSymLinks whether symbolic links should be followed
106      */
107     @SuppressWarnings({ "UnusedDeclaration" })
108     public void setFollowingSymLinks( boolean pIsFollowingSymLinks )
109     {
110         isFollowingSymLinks = pIsFollowingSymLinks;
111     }
112 
113     public void setDefaultAttributes( final int uid, final String userName, final int gid, final String groupName,
114                                       final int fileMode, final int dirMode )
115     {
116         setDefaultFileAttributes( createDefaults( uid, userName, gid, groupName, fileMode ) );
117 
118         setDefaultDirAttributes( createDefaults( uid, userName, gid, groupName, dirMode ) );
119     }
120 
121     public void setOverrideAttributes( final int uid, final String userName, final int gid, final String groupName,
122                                        final int fileMode, final int dirMode )
123     {
124         setOverrideFileAttributes( createDefaults( uid, userName, gid, groupName, fileMode ) );
125 
126         setOverrideDirAttributes( createDefaults( uid, userName, gid, groupName, dirMode ) );
127     }
128 
129     private static PlexusIoResourceAttributes createDefaults( final int uid, final String userName, final int gid,
130                                                               final String groupName, final int mode )
131     {
132         return new SimpleResourceAttributes( uid, userName, gid, groupName,
133                 mode >= 0 ? mode : PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE );
134     }
135 
136 
137     @Override
138     public void setPrefix( String prefix )
139     {
140         char nonSeparator = File.separatorChar == '/' ? '\\' : '/';
141         super.setPrefix( StringUtils.replace( prefix, nonSeparator, File.separatorChar ) );
142     }
143 
144     private void addResources( List<PlexusIoResource> result, String[] resources )
145         throws IOException
146     {
147 
148         final File dir = getBaseDir();
149         final HashMap<Integer, String> cache1 = new HashMap<>();
150         final HashMap<Integer, String> cache2 = new HashMap<>();
151         for ( String name : resources )
152         {
153             String sourceDir = name.replace( '\\', '/' );
154             File f = new File( dir, sourceDir );
155 
156             PlexusIoResourceAttributes attrs = new FileAttributes( f, cache1, cache2 );
157             attrs = mergeAttributes( attrs, f.isDirectory() );
158 
159             String remappedName = getName( name );
160 
161             PlexusIoResource resource =
162                 ResourceFactory.createResource( f, remappedName, null, getStreamTransformer(), attrs );
163 
164             if ( isSelected( resource ) )
165             {
166                 result.add( resource );
167             }
168         }
169     }
170 
171     public Stream stream()
172     {
173         return new Stream()
174         {
175             public void forEach( PlexusIoResourceConsumer resourceConsumer )
176                 throws IOException
177             {
178                 Iterator<PlexusIoResource> resources = getResources();
179                 while ( resources.hasNext() )
180                 {
181                     PlexusIoResource next = resources.next();
182                     if ( isSelected( next ) )
183                     {
184                         resourceConsumer.accept( next );
185                     }
186                 }
187                 if ( resources instanceof Closeable )
188                 {
189                     ( (Closeable) resources ).close();
190                 }
191 
192             }
193 
194             public void forEach( ExecutorService es, final PlexusIoResourceConsumer resourceConsumer )
195                 throws IOException
196             {
197                 Iterator<PlexusIoResource> resources = getResources();
198                 while ( resources.hasNext() )
199                 {
200                     final PlexusIoResource next = resources.next();
201                     Callable future = new Callable()
202                     {
203                         public Object call()
204                             throws Exception
205                         {
206                             resourceConsumer.accept( next );
207                             return this;
208                         }
209                     };
210                     es.submit( future );
211                 }
212                 if ( resources instanceof Closeable )
213                 {
214                     ( (Closeable) resources ).close();
215                 }
216 
217             }
218         };
219 
220     }
221 
222     public Iterator<PlexusIoResource> getResources()
223         throws IOException
224     {
225         final DirectoryScanner ds = new DirectoryScanner();
226         final File dir = getBaseDir();
227         ds.setBasedir( dir );
228         final String[] inc = getIncludes();
229         if ( inc != null && inc.length > 0 )
230         {
231             ds.setIncludes( inc );
232         }
233         final String[] exc = getExcludes();
234         if ( exc != null && exc.length > 0 )
235         {
236             ds.setExcludes( exc );
237         }
238         if ( isUsingDefaultExcludes() )
239         {
240             ds.addDefaultExcludes();
241         }
242         ds.setCaseSensitive( isCaseSensitive() );
243         ds.setFollowSymlinks( isFollowingSymLinks() );
244         ds.scan();
245 
246         final List<PlexusIoResource> result = new ArrayList<>();
247         if ( isIncludingEmptyDirectories() )
248         {
249             String[] dirs = ds.getIncludedDirectories();
250             addResources( result, dirs );
251         }
252 
253         String[] files = ds.getIncludedFiles();
254         addResources( result, files );
255         return result.iterator();
256     }
257 
258     public boolean isConcurrentAccessSupported()
259     {
260         return true;
261     }
262 }