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.io.IOException;
022: import java.text.ParseException;
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.LinkedHashSet;
028: import java.util.List;
029: import java.util.Set;
030:
031: import org.apache.ivy.core.cache.ResolutionCacheManager;
032: import org.apache.ivy.core.module.id.ModuleRevisionId;
033: import org.apache.ivy.core.report.ArtifactDownloadReport;
034: import org.apache.ivy.core.report.ConfigurationResolveReport;
035: import org.apache.ivy.core.report.ResolveReport;
036: import org.apache.ivy.core.resolve.ResolveOptions;
037: import org.apache.ivy.plugins.report.XmlReportParser;
038: import org.apache.ivy.util.Message;
039: import org.apache.tools.ant.BuildException;
040:
041: /**
042: * Base class for the cache path related classes: cachepath and cachefileset. Most of the behviour
043: * is common to the two, since only the produced element differs.
044: */
045: public abstract class IvyCacheTask extends IvyPostResolveTask {
046:
047: protected List getArtifactReports() throws BuildException,
048: ParseException, IOException {
049: Collection artifacts = getAllArtifactReports();
050: List ret = new ArrayList();
051: for (Iterator iter = artifacts.iterator(); iter.hasNext();) {
052: ArtifactDownloadReport artifactReport = (ArtifactDownloadReport) iter
053: .next();
054: if (getArtifactFilter()
055: .accept(artifactReport.getArtifact())) {
056: ret.add(artifactReport);
057: }
058: }
059:
060: return ret;
061: }
062:
063: private Collection getAllArtifactReports() throws ParseException,
064: IOException {
065: String[] confs = splitConfs(getConf());
066: Collection all = new LinkedHashSet();
067:
068: ResolveReport report = getResolvedReport();
069: if (report != null) {
070: Message
071: .debug("using internal report instance to get artifacts list");
072: for (int i = 0; i < confs.length; i++) {
073: ConfigurationResolveReport configurationReport = report
074: .getConfigurationReport(confs[i]);
075: if (configurationReport == null) {
076: throw new BuildException("bad confs provided: "
077: + confs[i] + " not found among "
078: + Arrays.asList(report.getConfigurations()));
079: }
080: Set revisions = configurationReport
081: .getModuleRevisionIds();
082: for (Iterator it = revisions.iterator(); it.hasNext();) {
083: ModuleRevisionId revId = (ModuleRevisionId) it
084: .next();
085: ArtifactDownloadReport[] aReports = configurationReport
086: .getDownloadReports(revId);
087: all.addAll(Arrays.asList(aReports));
088: }
089: }
090: } else {
091: Message.debug("using stored report to get artifacts list");
092:
093: XmlReportParser parser = new XmlReportParser();
094: ResolutionCacheManager cacheMgr = getIvyInstance()
095: .getResolutionCacheManager();
096: String resolvedId = getResolveId();
097: if (resolvedId == null) {
098: resolvedId = ResolveOptions
099: .getDefaultResolveId(getResolvedModuleId());
100: }
101: for (int i = 0; i < confs.length; i++) {
102: File reportFile = cacheMgr
103: .getConfigurationResolveReportInCache(
104: resolvedId, confs[i]);
105: parser.parse(reportFile);
106:
107: ArtifactDownloadReport[] aReports = parser
108: .getArtifactReports();
109: all.addAll(Arrays.asList(aReports));
110: }
111: }
112: return all;
113: }
114: }
|