View Javadoc
1   package org.codehaus.plexus.components.io.resources.proxy;
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.PlexusIoResourceAttributes;
20  import org.codehaus.plexus.components.io.attributes.SimpleResourceAttributes;
21  import org.codehaus.plexus.components.io.filemappers.FileMapper;
22  import org.codehaus.plexus.components.io.fileselectors.FileSelector;
23  import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
24  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
25  import org.codehaus.plexus.components.io.functions.NameSupplier;
26  import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
27  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResourceCollection;
28  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResourceCollectionWithAttributes;
29  import org.codehaus.plexus.components.io.resources.EncodingSupported;
30  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
31  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
32  import org.codehaus.plexus.components.io.resources.Stream;
33  
34  import javax.annotation.Nonnull;
35  import java.io.IOException;
36  import java.nio.charset.Charset;
37  import java.util.Iterator;
38  
39  /**
40   * Implementation of {@link PlexusIoResourceCollection} for an archives contents.
41   */
42  public class PlexusIoProxyResourceCollection
43      extends AbstractPlexusIoResourceCollectionWithAttributes implements EncodingSupported
44  {
45      private PlexusIoResourceCollection src;
46  
47  
48      public PlexusIoProxyResourceCollection( @Nonnull PlexusIoResourceCollection src )
49      {
50          this.src = src;
51      }
52  
53      /**
54       * Returns the archive to read.
55       */
56      public PlexusIoResourceCollection getSrc()
57      {
58          return src;
59      }
60  
61      public void setDefaultAttributes( final int uid, final String userName, final int gid, final String groupName,
62                                        final int fileMode, final int dirMode )
63      {
64          setDefaultFileAttributes( new SimpleResourceAttributes( uid, userName, gid, groupName, fileMode ) );
65  
66          setDefaultDirAttributes( new SimpleResourceAttributes( uid, userName, gid, groupName, dirMode ) );
67      }
68  
69      public void setOverrideAttributes( final int uid, final String userName, final int gid, final String groupName,
70                                         final int fileMode, final int dirMode )
71      {
72          setOverrideFileAttributes( new SimpleResourceAttributes( uid, userName, gid, groupName, fileMode ) );
73  
74          setOverrideDirAttributes( new SimpleResourceAttributes( uid, userName, gid, groupName, dirMode ) );
75      }
76  
77      @Override
78      public void setStreamTransformer( InputStreamTransformer streamTransformer )
79      {
80          if ( src instanceof AbstractPlexusIoResourceCollection )
81          {
82              ( (AbstractPlexusIoResourceCollection) src ).setStreamTransformer( streamTransformer );
83          }
84          super.setStreamTransformer( streamTransformer );
85      }
86  
87      protected FileSelector getDefaultFileSelector()
88      {
89          final IncludeExcludeFileSelector fileSelector = new IncludeExcludeFileSelector();
90          fileSelector.setIncludes( getIncludes() );
91          fileSelector.setExcludes( getExcludes() );
92          fileSelector.setCaseSensitive( isCaseSensitive() );
93          fileSelector.setUseDefaultExcludes( isUsingDefaultExcludes() );
94          return fileSelector;
95      }
96  
97      private String getNonEmptyPrfix()
98      {
99          String prefix = getPrefix();
100         if ( prefix != null && prefix.length() == 0 )
101         {
102             return null;
103         }
104         return prefix;
105 
106     }
107 
108     class FwdIterator
109         extends ForwardingIterator
110     {
111         Iterator<PlexusIoResource> iter;
112 
113         private final FileSelector fileSelector = getDefaultFileSelector();
114 
115         private final String prefix = getNonEmptyPrfix();
116 
117         FwdIterator( Iterator<PlexusIoResource> resources )
118         {
119             super( resources );
120             this.iter = resources;
121         }
122 
123         /**
124          * Returns the next resource or null if no next resource;
125          */
126         protected PlexusIoResource getNextResource()
127             throws IOException
128         {
129             if ( !iter.hasNext() )
130                 return null;
131             PlexusIoResource plexusIoResource = iter.next();
132 
133             while ( ( !fileSelector.isSelected( plexusIoResource ) || !isSelected( plexusIoResource ) )
134                             || ( plexusIoResource.isDirectory() && !isIncludingEmptyDirectories() ) )
135             {
136                 if ( !iter.hasNext() )
137                     return null;
138                 plexusIoResource = iter.next();
139             }
140 
141             PlexusIoResourceAttributes attrs = null;
142             if ( plexusIoResource instanceof ResourceAttributeSupplier )
143             {
144                 attrs = ( (ResourceAttributeSupplier) plexusIoResource ).getAttributes();
145             }
146             if ( attrs == null )
147             {
148                 attrs = SimpleResourceAttributes.lastResortDummyAttributesForBrokenOS();
149             }
150 
151             attrs = mergeAttributes( attrs, plexusIoResource.isDirectory() );
152 
153             if ( prefix != null )
154             {
155                 final String name = plexusIoResource.getName();
156 
157                 final PlexusIoResourceAttributes attrs2 = attrs;
158                 DualSupplier supplier = new DualSupplier()
159                 {
160                     public String getName()
161                     {
162                         return prefix + name;
163                     }
164 
165                     public PlexusIoResourceAttributes getAttributes()
166                     {
167                         return attrs2;
168                     }
169                 };
170                 plexusIoResource = ProxyFactory.createProxy( plexusIoResource, supplier );
171             }
172             return plexusIoResource;
173         }
174     }
175 
176     public Stream stream()
177     {
178         return getSrc().stream();
179     }
180 
181     public Iterator<PlexusIoResource> getResources()
182         throws IOException
183     {
184         return new FwdIterator( getSrc().getResources() );
185     }
186 
187     abstract static class DualSupplier
188         implements NameSupplier, ResourceAttributeSupplier
189     {
190 
191     }
192 
193     public String getName( final PlexusIoResource resource )
194     {
195         String name = resource.getName();
196         final FileMapper[] mappers = getFileMappers();
197         if ( mappers != null )
198         {
199             for ( FileMapper mapper : mappers )
200             {
201                 name = mapper.getMappedFileName( name );
202             }
203         }
204         /*
205          * The prefix is applied when creating the resource. return PrefixFileMapper.getMappedFileName( getPrefix(),
206          * name );
207          */
208         return name;
209     }
210 
211     public long getLastModified()
212         throws IOException
213     {
214         return src.getLastModified();
215     }
216 
217     public void setEncoding( Charset charset )
218     {
219         if ( src instanceof EncodingSupported )
220         {
221             ( (EncodingSupported) src ).setEncoding( charset );
222         }
223     }
224 
225     public boolean isConcurrentAccessSupported()
226     {
227         return src.isConcurrentAccessSupported();
228     }
229 }