~ubuntu-branches/ubuntu/trusty/ivy/trusty-proposed

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/ant/IvyCacheFileset.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2009-03-06 22:04:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090306220456-5v37luqiuqda8ewp
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 *  contributor license agreements.  See the NOTICE file distributed with
 
4
 *  this work for additional information regarding copyright ownership.
 
5
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 *  (the "License"); you may not use this file except in compliance with
 
7
 *  the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *  Unless required by applicable law or agreed to in writing, software
 
12
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *  See the License for the specific language governing permissions and
 
15
 *  limitations under the License.
 
16
 *
 
17
 */
 
18
package org.apache.ivy.ant;
 
19
 
 
20
import java.io.File;
 
21
import java.util.Iterator;
 
22
import java.util.LinkedList;
 
23
import java.util.List;
 
24
 
 
25
import org.apache.ivy.core.report.ArtifactDownloadReport;
 
26
import org.apache.tools.ant.BuildException;
 
27
import org.apache.tools.ant.types.FileSet;
 
28
import org.apache.tools.ant.types.PatternSet.NameEntry;
 
29
 
 
30
/**
 
31
 * Creates an ant fileset consisting in all artifacts found during a resolve. Note that this task is
 
32
 * not compatible with the useOrigin mode.
 
33
 */
 
34
public class IvyCacheFileset extends IvyCacheTask {
 
35
    private String setid;
 
36
 
 
37
    public String getSetid() {
 
38
        return setid;
 
39
    }
 
40
 
 
41
    public void setSetid(String id) {
 
42
        setid = id;
 
43
    }
 
44
 
 
45
    public void setUseOrigin(boolean useOrigin) {
 
46
        if (useOrigin) {
 
47
            throw new UnsupportedOperationException(
 
48
                    "the cachefileset task does not support the useOrigin mode, since filesets "
 
49
                    + "require to have only one root directory. Please use the the cachepath "
 
50
                    + "task instead");
 
51
        }
 
52
    }
 
53
 
 
54
    public void doExecute() throws BuildException {
 
55
        prepareAndCheck();
 
56
        if (setid == null) {
 
57
            throw new BuildException("setid is required in ivy cachefileset");
 
58
        }
 
59
        try {
 
60
            FileSet fileset = new FileSet();
 
61
            fileset.setProject(getProject());
 
62
            getProject().addReference(setid, fileset);
 
63
 
 
64
            List paths = getArtifactReports();
 
65
            File base = null;
 
66
            for (Iterator iter = paths.iterator(); iter.hasNext();) {
 
67
                ArtifactDownloadReport a = (ArtifactDownloadReport) iter.next();
 
68
                if (a.getLocalFile() != null) {
 
69
                    base = getBaseDir(base, a.getLocalFile());
 
70
                }
 
71
            }
 
72
            if (base == null) {
 
73
                fileset.setDir(new File("."));
 
74
                NameEntry ne = fileset.createExclude();
 
75
                ne.setName("**/*");
 
76
            } else {
 
77
                fileset.setDir(base);
 
78
                for (Iterator iter = paths.iterator(); iter.hasNext();) {
 
79
                    ArtifactDownloadReport a = (ArtifactDownloadReport) iter.next();
 
80
                    if (a.getLocalFile() != null) {
 
81
                        NameEntry ne = fileset.createInclude();
 
82
                        ne.setName(getPath(base, a.getLocalFile()));
 
83
                    }
 
84
                }
 
85
            }
 
86
        } catch (Exception ex) {
 
87
            throw new BuildException("impossible to build ivy cache fileset: " + ex, ex);
 
88
        }
 
89
    }
 
90
 
 
91
    /**
 
92
     * Returns the path of the file relative to the given base directory.
 
93
     * 
 
94
     * @param base the parent directory to which the file must be evaluated.
 
95
     * @param file the file for which the path should be returned
 
96
     * @return the path of the file relative to the given base directory.
 
97
     */
 
98
    private String getPath(File base, File file) {
 
99
        String absoluteBasePath = base.getAbsolutePath();
 
100
        
 
101
        int beginIndex = absoluteBasePath.length();
 
102
        
 
103
        // checks if the basePath ends with the file separator (which can for instance
 
104
        // happen if the basePath is the root on unix)
 
105
        if (!absoluteBasePath.endsWith(File.separator)) {
 
106
            beginIndex++; // skip the seperator char as well
 
107
        }
 
108
        
 
109
        return file.getAbsolutePath().substring(beginIndex);
 
110
    }
 
111
 
 
112
    /**
 
113
     * Returns the common base directory between a current base directory and a given file.
 
114
     * <p>
 
115
     * The returned base directory must be a parent of both the current base and the given file.
 
116
     * </p>
 
117
     * 
 
118
     * @param base
 
119
     *            the current base directory, may be null.
 
120
     * @param file
 
121
     *            the file for which the new base directory should be returned.
 
122
     * @return the common base directory between a current base directory and a given file.
 
123
     */
 
124
    File getBaseDir(File base, File file) {
 
125
        if (base == null) {
 
126
            return file.getParentFile().getAbsoluteFile();
 
127
        } else {
 
128
            Iterator bases = getParents(base).iterator();
 
129
            Iterator fileParents = getParents(file.getAbsoluteFile()).iterator();
 
130
            File result = null;
 
131
            while (bases.hasNext() && fileParents.hasNext()) {
 
132
                File next = (File) bases.next();
 
133
                if (next.equals(fileParents.next())) {
 
134
                    result = next; 
 
135
                } else {
 
136
                    break;
 
137
                }
 
138
            }
 
139
            return result;
 
140
        }
 
141
    }
 
142
 
 
143
    /**
 
144
     * @return a list of files, starting with the root and ending with the file itself
 
145
     */
 
146
    private LinkedList/*<File>*/ getParents(File file) {
 
147
        LinkedList r = new LinkedList();
 
148
        while (file != null) {
 
149
            r.addFirst(file);
 
150
            file = file.getParentFile();
 
151
        }
 
152
        return r;
 
153
    }
 
154
 
 
155
}