View Javadoc

1   /*
2    * Copyright 2008 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jdeb.producers;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  
23  import org.apache.tools.ant.DirectoryScanner;
24  import org.apache.tools.ant.taskdefs.Tar;
25  import org.apache.tools.ant.types.FileSet;
26  import org.apache.tools.tar.TarEntry;
27  import org.vafer.jdeb.DataConsumer;
28  import org.vafer.jdeb.DataProducer;
29  
30  /**
31   * DataProducer providing data from an Ant fileset. TarFileSets are also
32   * supported with their permissions.
33   *
34   * @author Emmanuel Bourg
35   */
36  public final class FileSetDataProducer implements DataProducer {
37  
38  	private final FileSet fileset;
39  
40  	public FileSetDataProducer( final FileSet pFileset ) {
41  		fileset = pFileset;
42  	}
43  
44  	public void produce( final DataConsumer pReceiver ) throws IOException {
45  		String user = "root";
46  		int uid = 0;
47  		String group = "root";
48  		int gid = 0;
49  		int filemode = TarEntry.DEFAULT_FILE_MODE;
50  		int dirmode = TarEntry.DEFAULT_DIR_MODE;
51  		String prefix = "";
52  
53  		if (fileset instanceof Tar.TarFileSet) {
54  			Tar.TarFileSet tarfileset = (Tar.TarFileSet) fileset;
55  			user = tarfileset.getUserName();
56  			uid = tarfileset.getUid();
57  			group = tarfileset.getGroup();
58  			gid = tarfileset.getGid();
59  			filemode = tarfileset.getMode();
60  			dirmode = tarfileset.getDirMode();
61  			prefix = tarfileset.getPrefix();
62  		}
63  
64  		final DirectoryScanner scanner = fileset.getDirectoryScanner(fileset.getProject());
65  		scanner.scan();
66  
67  		final File basedir = scanner.getBasedir();
68  
69  		final String[] directories = scanner.getIncludedDirectories();
70  		for (int i = 0; i < directories.length; i++) {
71  			final String name = directories[i];
72  
73  			pReceiver.onEachDir(prefix + "/" + name, null, user, uid, group, gid, dirmode, 0);
74  		}
75  
76  		final String[] files = scanner.getIncludedFiles();
77  		for (int i = 0; i < files.length; i++) {
78  			final String name = files[i];
79  			final File file = new File(basedir, name);
80  
81  			final InputStream inputStream = new FileInputStream(file);
82  			try {
83  				pReceiver.onEachFile(inputStream, prefix + "/" + name, null, user, uid, group, gid,filemode, file.length());
84  			} finally {
85  				inputStream.close();
86  			}
87  
88  		}
89  	}
90  }