KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > resolver > CacheResolver


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.resolver;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.text.ParseException JavaDoc;
12 import java.util.Collections JavaDoc;
13
14 import fr.jayasoft.ivy.Artifact;
15 import fr.jayasoft.ivy.DependencyDescriptor;
16 import fr.jayasoft.ivy.Ivy;
17 import fr.jayasoft.ivy.IvyNode;
18 import fr.jayasoft.ivy.ModuleRevisionId;
19 import fr.jayasoft.ivy.ResolveData;
20 import fr.jayasoft.ivy.ResolvedModuleRevision;
21 import fr.jayasoft.ivy.report.ArtifactDownloadReport;
22 import fr.jayasoft.ivy.report.DownloadReport;
23 import fr.jayasoft.ivy.report.DownloadStatus;
24 import fr.jayasoft.ivy.util.Message;
25
26
27 public class CacheResolver extends FileSystemResolver {
28     private File JavaDoc _configured = null;
29     
30     public CacheResolver() {
31     }
32     
33     public CacheResolver(Ivy ivy) {
34         setIvy(ivy);
35         setName("cache");
36     }
37
38     public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException JavaDoc {
39         clearIvyAttempts();
40
41         ModuleRevisionId mrid = dd.getDependencyRevisionId();
42         // check revision
43

44         // if we do not have to check modified and if the revision is exact and not changing,
45
// we first search for it in cache
46
if (!getIvy().getVersionMatcher().isDynamic(mrid)) {
47             ResolvedModuleRevision rmr = data.getIvy().findModuleInCache(mrid, data.getCache(), doValidate(data));
48             if (rmr != null) {
49                 Message.verbose("\t"+getName()+": revision in cache: "+mrid);
50                 return rmr;
51             } else {
52                 Message.verbose("\t"+getName()+": no ivy file in cache found for "+mrid);
53                 return null;
54             }
55         } else {
56             ensureConfigured(data.getIvy(), data.getCache());
57             ResolvedResource ivyRef = findIvyFileRef(dd, data);
58             if (ivyRef != null) {
59                 Message.verbose("\t"+getName()+": found ivy file in cache for "+mrid);
60                 Message.verbose("\t\t=> "+ivyRef);
61
62                 ModuleRevisionId resolvedMrid = ModuleRevisionId.newInstance(mrid, ivyRef.getRevision());
63                 IvyNode node = data.getNode(resolvedMrid);
64                 if (node != null && node.getModuleRevision() != null) {
65                     // this revision has already be resolved : return it
66
Message.verbose("\t"+getName()+": revision already resolved: "+resolvedMrid);
67                     return searchedRmr(node.getModuleRevision());
68                 }
69                 ResolvedModuleRevision rmr = data.getIvy().findModuleInCache(resolvedMrid, data.getCache(), doValidate(data));
70                 if (rmr != null) {
71                     Message.verbose("\t"+getName()+": revision in cache: "+resolvedMrid);
72                     return searchedRmr(rmr);
73                 } else {
74                     Message.error("\t"+getName()+": inconsistent cache: clean it and resolve again");
75                     return null;
76                 }
77             } else {
78                 Message.verbose("\t"+getName()+": no ivy file in cache found for "+mrid);
79                 return null;
80             }
81         }
82     }
83     
84     public DownloadReport download(Artifact[] artifacts, Ivy ivy, File JavaDoc cache) {
85         clearArtifactAttempts();
86         DownloadReport dr = new DownloadReport();
87         for (int i = 0; i < artifacts.length; i++) {
88             final ArtifactDownloadReport adr = new ArtifactDownloadReport(artifacts[i]);
89             dr.addArtifactReport(adr);
90             File JavaDoc archiveFile = ivy.getArchiveFileInCache(cache, artifacts[i]);
91             if (archiveFile.exists()) {
92                 Message.verbose("\t[NOT REQUIRED] "+artifacts[i]);
93                 adr.setDownloadStatus(DownloadStatus.NO);
94                 adr.setSize(archiveFile.length());
95             } else {
96                     logArtifactNotFound(artifacts[i]);
97                     adr.setDownloadStatus(DownloadStatus.FAILED);
98             }
99         }
100         return dr;
101     }
102     public boolean exists(Artifact artifact) {
103         ensureConfigured();
104         return super.exists(artifact);
105     }
106     public void publish(Artifact artifact, File JavaDoc src, boolean overwrite) throws IOException JavaDoc {
107         ensureConfigured();
108         super.publish(artifact, src, overwrite);
109     }
110     
111     
112     public OrganisationEntry[] listOrganisations() {
113         ensureConfigured();
114         return super.listOrganisations();
115     }
116     public ModuleEntry[] listModules(OrganisationEntry org) {
117         ensureConfigured();
118         return super.listModules(org);
119     }
120     public RevisionEntry[] listRevisions(ModuleEntry module) {
121         ensureConfigured();
122         return super.listRevisions(module);
123     }
124     public void dumpConfig() {
125         Message.verbose("\t"+getName()+" [cache]");
126     }
127
128     private void ensureConfigured() {
129         if (getIvy() != null) {
130             ensureConfigured(getIvy(), getIvy().getDefaultCache());
131         }
132     }
133     private void ensureConfigured(Ivy ivy, File JavaDoc cache) {
134         if (ivy == null || cache == null || (_configured != null && _configured.equals(cache))) {
135             return;
136         }
137         setIvyPatterns(Collections.singletonList(cache.getAbsolutePath()+"/"+ivy.getCacheIvyPattern()));
138         setArtifactPatterns(Collections.singletonList(cache.getAbsolutePath()+"/"+ivy.getCacheArtifactPattern()));
139     }
140     public String JavaDoc getTypeName() {
141         return "cache";
142     }
143 }
144
Popular Tags