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