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.plugins.resolver;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.text.ParseException;
023: import java.util.ArrayList;
024:
025: import org.apache.ivy.core.cache.ArtifactOrigin;
026: import org.apache.ivy.core.cache.DefaultRepositoryCacheManager;
027: import org.apache.ivy.core.cache.RepositoryCacheManager;
028: import org.apache.ivy.core.module.descriptor.Artifact;
029: import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
030: import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
031: import org.apache.ivy.core.module.id.ModuleRevisionId;
032: import org.apache.ivy.core.report.ArtifactDownloadReport;
033: import org.apache.ivy.core.report.DownloadReport;
034: import org.apache.ivy.core.report.DownloadStatus;
035: import org.apache.ivy.core.resolve.DownloadOptions;
036: import org.apache.ivy.core.resolve.IvyNode;
037: import org.apache.ivy.core.resolve.ResolveData;
038: import org.apache.ivy.core.resolve.ResolvedModuleRevision;
039: import org.apache.ivy.core.search.ModuleEntry;
040: import org.apache.ivy.core.search.OrganisationEntry;
041: import org.apache.ivy.core.search.RevisionEntry;
042: import org.apache.ivy.plugins.repository.file.FileResource;
043: import org.apache.ivy.plugins.resolver.util.ResolvedResource;
044: import org.apache.ivy.util.Message;
045:
046: public class CacheResolver extends FileSystemResolver {
047: public CacheResolver() {
048: }
049:
050: public CacheResolver(ResolverSettings settings) {
051: setSettings(settings);
052: setName("cache");
053: }
054:
055: public ResolvedModuleRevision getDependency(
056: DependencyDescriptor dd, ResolveData data)
057: throws ParseException {
058: clearIvyAttempts();
059:
060: ModuleRevisionId mrid = dd.getDependencyRevisionId();
061: // check revision
062:
063: ResolvedModuleRevision rmr = getRepositoryCacheManager()
064: .findModuleInCache(dd, getCacheOptions(data), null);
065: if (rmr != null) {
066: Message.verbose("\t" + getName() + ": revision in cache: "
067: + mrid);
068: return rmr;
069: } else if (!getSettings().getVersionMatcher().isDynamic(mrid)) {
070: Message.verbose("\t" + getName()
071: + ": no ivy file in cache found for " + mrid);
072: return null;
073: } else {
074: ensureConfigured();
075: ResolvedResource ivyRef = findIvyFileRef(dd, data);
076: if (ivyRef != null) {
077: Message.verbose("\t" + getName()
078: + ": found ivy file in cache for " + mrid);
079: Message.verbose("\t\t=> " + ivyRef);
080:
081: ModuleRevisionId resolvedMrid = ModuleRevisionId
082: .newInstance(mrid, ivyRef.getRevision());
083: IvyNode node = data.getNode(resolvedMrid);
084: if (node != null && node.getModuleRevision() != null) {
085: // this revision has already be resolved : return it
086: Message.verbose("\t" + getName()
087: + ": revision already resolved: "
088: + resolvedMrid);
089: return node.getModuleRevision();
090: }
091: rmr = getRepositoryCacheManager().findModuleInCache(
092: new DefaultDependencyDescriptor(dd, ivyRef
093: .getRevision()), getCacheOptions(data),
094: null);
095: if (rmr != null) {
096: Message.verbose("\t" + getName()
097: + ": revision in cache: " + resolvedMrid);
098: return rmr;
099: } else {
100: Message
101: .error("\t"
102: + getName()
103: + ": inconsistent cache: clean it and resolve again");
104: return null;
105: }
106: } else {
107: Message.verbose("\t" + getName()
108: + ": no ivy file in cache found for " + mrid);
109: return null;
110: }
111: }
112: }
113:
114: public DownloadReport download(Artifact[] artifacts,
115: DownloadOptions options) {
116: clearArtifactAttempts();
117: DownloadReport dr = new DownloadReport();
118: for (int i = 0; i < artifacts.length; i++) {
119: final ArtifactDownloadReport adr = new ArtifactDownloadReport(
120: artifacts[i]);
121: dr.addArtifactReport(adr);
122: ResolvedResource artifactRef = getArtifactRef(artifacts[i],
123: null);
124: if (artifactRef != null) {
125: Message.verbose("\t[NOT REQUIRED] " + artifacts[i]);
126: ArtifactOrigin origin = new ArtifactOrigin(true,
127: artifactRef.getResource().getName());
128: File archiveFile = ((FileResource) artifactRef
129: .getResource()).getFile();
130: adr.setDownloadStatus(DownloadStatus.NO);
131: adr.setSize(archiveFile.length());
132: adr.setArtifactOrigin(origin);
133: adr.setLocalFile(archiveFile);
134: } else {
135: adr.setDownloadStatus(DownloadStatus.FAILED);
136: }
137: }
138: return dr;
139: }
140:
141: public boolean exists(Artifact artifact) {
142: ensureConfigured();
143: return super .exists(artifact);
144: }
145:
146: public void publish(Artifact artifact, File src, boolean overwrite)
147: throws IOException {
148: ensureConfigured();
149: super .publish(artifact, src, overwrite);
150: }
151:
152: public OrganisationEntry[] listOrganisations() {
153: ensureConfigured();
154: return super .listOrganisations();
155: }
156:
157: public ModuleEntry[] listModules(OrganisationEntry org) {
158: ensureConfigured();
159: return super .listModules(org);
160: }
161:
162: public RevisionEntry[] listRevisions(ModuleEntry module) {
163: ensureConfigured();
164: return super .listRevisions(module);
165: }
166:
167: public void dumpSettings() {
168: Message.verbose("\t" + getName() + " [cache]");
169: }
170:
171: private void ensureConfigured() {
172: if (getIvyPatterns().isEmpty()) {
173: setIvyPatterns(new ArrayList());
174: setArtifactPatterns(new ArrayList());
175: RepositoryCacheManager[] caches = getSettings()
176: .getRepositoryCacheManagers();
177: for (int i = 0; i < caches.length; i++) {
178: if (caches[i] instanceof DefaultRepositoryCacheManager) {
179: DefaultRepositoryCacheManager c = (DefaultRepositoryCacheManager) caches[i];
180: addIvyPattern(c.getBasedir().getAbsolutePath()
181: + "/" + c.getIvyPattern());
182: addArtifactPattern(c.getBasedir().getAbsolutePath()
183: + "/" + c.getArtifactPattern());
184: } else {
185: Message
186: .verbose(caches[i]
187: + ": cache implementation is not a DefaultRepositoryCacheManager:"
188: + " unable to configure cache resolver with it");
189: }
190: }
191: }
192: }
193:
194: public String getTypeName() {
195: return "cache";
196: }
197: }
|