1 7 package com.inversoft.savant; 8 9 10 import java.io.File ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 20 28 public class DependencyMediator { 29 30 private static Set projectsBuilt = new HashSet (); 31 private static Map artifactsFetched = new HashMap (); 32 33 private List listeners = new ArrayList (); 34 private Dependencies dependencies = new Dependencies(); 35 private LocalCacheStore localCacheStore; 36 private Workflow workflow; 37 38 39 42 public DependencyMediator() { 43 } 44 45 46 50 public File getLocalCacheDir() { 51 return (localCacheStore == null) ? null : localCacheStore.getLocation(); 52 } 53 54 61 public void setLocalCacheDir(File localCacheDir) throws SavantException { 62 this.localCacheStore = new LocalCacheStore(localCacheDir); 63 } 64 65 70 public void addListener(DependencyListener listener) { 71 listeners.add(listener); 72 } 73 74 79 public void removeListener(DependencyListener listener) { 80 listeners.remove(listener); 81 } 82 83 88 public Dependencies getDependencies() { 89 return dependencies; 90 } 91 92 97 public void setDependencies(Dependencies dependencies) { 98 this.dependencies = dependencies; 99 } 100 101 104 public Workflow getWorkflow() { 105 return workflow; 106 } 107 108 111 public void setWorkflow(Workflow workflow) { 112 this.workflow = workflow; 113 } 114 115 118 public void mediate() throws SavantException { 119 workflow.validate(); 120 121 handleGroups(dependencies.getArtifactGroups()); 122 123 Iterator aIter = dependencies.getArtifacts().iterator(); 124 while (aIter.hasNext()) { 125 Artifact artifact = (Artifact) aIter.next(); 126 handleArtifact(artifact, null); 127 } 128 } 129 130 138 protected void handleGroups(List groups) throws SavantException { 139 for (int i = 0; i < groups.size(); i++) { 141 ArtifactGroup group = (ArtifactGroup) groups.get(i); 142 List artifacts = group.getArtifacts(); 143 for (int j = 0; j < artifacts.size(); j++) { 144 Artifact artifact = (Artifact) artifacts.get(j); 145 handleArtifact(artifact, group); 146 } 147 148 List subGroups = group.getArtifactGroups(); 149 if (subGroups.size() > 0) { 150 handleGroups(subGroups); 151 } 152 } 153 } 154 155 162 protected void handleArtifact(Artifact artifact, ArtifactGroup group) 163 throws SavantException { 164 workflow.resolveArtifactDependencies(artifact, localCacheStore); 166 167 File file = (File ) artifactsFetched.get(artifact); 169 if (file == null) { 170 String artifactProject = artifact.getProjectname(); 171 String artifactGroup = artifact.getGroup(); 172 173 String projectID = LocalProject.makeProjectID(artifactGroup, 175 artifactProject); 176 LocalProject project = dependencies.getProject(projectID); 177 if (project != null && !projectsBuilt.contains(project)) { 178 project.build(); 179 projectsBuilt.add(project); 180 181 for (int i = 0; i < listeners.size(); i++) { 182 DependencyListener listener = (DependencyListener) listeners.get(i); 183 listener.projectBuilt(project); 184 } 185 } 186 187 file = workflow.findArtifact(artifact, localCacheStore); 190 if (file == null) { 191 throw new SavantException("Unable to locate dependency [" + 192 artifact.toString() + "]"); 193 } 194 195 artifactsFetched.put(artifact, file); 196 } 197 198 for (int i = 0; i < listeners.size(); i++) { 199 DependencyListener listener = (DependencyListener) listeners.get(i); 200 listener.artifactFound(file, artifact, group); 201 } 202 203 List deps = artifact.getDependencies(); 205 if (deps.size() > 0) { 206 for (int i = 0; i < deps.size(); i++) { 207 handleArtifact((Artifact) deps.get(i), group); 208 } 209 } 210 } 211 } | Popular Tags |