001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.ant;
019:
020: import java.io.File;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.ivy.core.report.ArtifactDownloadReport;
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.types.FileSet;
027: import org.apache.tools.ant.types.PatternSet.NameEntry;
028:
029: /**
030: * Creates an ant fileset consisting in all artifacts found during a resolve. Note that this task is
031: * not compatible with the useOrigin mode.
032: */
033: public class IvyCacheFileset extends IvyCacheTask {
034: private String setid;
035:
036: public String getSetid() {
037: return setid;
038: }
039:
040: public void setSetid(String id) {
041: setid = id;
042: }
043:
044: public void setUseOrigin(boolean useOrigin) {
045: if (useOrigin) {
046: throw new UnsupportedOperationException(
047: "the cachefileset task does not support the useOrigin mode, since filesets "
048: + "require to have only one root directory. Please use the the cachepath "
049: + "task instead");
050: }
051: }
052:
053: public void doExecute() throws BuildException {
054: prepareAndCheck();
055: if (setid == null) {
056: throw new BuildException(
057: "setid is required in ivy cachefileset");
058: }
059: try {
060: FileSet fileset = new FileSet();
061: fileset.setProject(getProject());
062: getProject().addReference(setid, fileset);
063:
064: List paths = getArtifactReports();
065: File base = null;
066: for (Iterator iter = paths.iterator(); iter.hasNext();) {
067: ArtifactDownloadReport a = (ArtifactDownloadReport) iter
068: .next();
069: if (a.getLocalFile() != null) {
070: base = getBaseDir(base, a.getLocalFile());
071: }
072: }
073: if (base == null) {
074: fileset.setDir(new File("."));
075: NameEntry ne = fileset.createExclude();
076: ne.setName("**/*");
077: } else {
078: fileset.setDir(base);
079: for (Iterator iter = paths.iterator(); iter.hasNext();) {
080: ArtifactDownloadReport a = (ArtifactDownloadReport) iter
081: .next();
082: if (a.getLocalFile() != null) {
083: NameEntry ne = fileset.createInclude();
084: ne.setName(getPath(base, a.getLocalFile()));
085: }
086: }
087: }
088: } catch (Exception ex) {
089: throw new BuildException(
090: "impossible to build ivy cache fileset: " + ex, ex);
091: }
092: }
093:
094: /**
095: * Returns the path of the file relative to the given base directory.
096: *
097: * @param base the parent directory to which the file must be evaluated.
098: * @param file the file for which the path should be returned
099: * @returnthe path of the file relative to the given base directory.
100: */
101: private String getPath(File base, File file) {
102: return file.getAbsolutePath().substring(
103: base.getAbsolutePath().length() + 1);
104: }
105:
106: /**
107: * Returns the common base directory between a current base directory and a given file.
108: * <p>
109: * The returned base directory must be a parent of both the current base and the given file.
110: * </p>
111: *
112: * @param base
113: * the current base directory, may be null.
114: * @param file
115: * the file for which the new base directory should be returned.
116: * @return the common base directory between a current base directory and a given file.
117: */
118: private File getBaseDir(File base, File file) {
119: if (base == null) {
120: return file.getParentFile();
121: } else {
122: String basePath = base.getAbsolutePath();
123: String filePath = file.getAbsolutePath();
124: for (int i = 0; i < basePath.length(); i++) {
125: if (i >= filePath.length()) {
126: return file.getParentFile();
127: }
128: if (basePath.charAt(i) != filePath.charAt(i)) {
129: return new File(basePath.substring(0, i));
130: }
131: }
132: return base;
133: }
134: }
135:
136: }
|