1 17 package org.apache.geronimo.kernel.repository; 18 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.LinkedHashSet ; 23 import java.util.SortedSet ; 24 import java.util.TreeSet ; 25 import java.util.Map ; 26 27 import org.apache.geronimo.gbean.GBeanInfo; 28 import org.apache.geronimo.gbean.GBeanInfoBuilder; 29 import org.apache.geronimo.kernel.config.Configuration; 30 31 34 public class DefaultArtifactResolver implements ArtifactResolver { 35 private final ArtifactManager artifactManager; 36 private final Collection repositories; 37 private final Map explicitResolution; 38 39 public DefaultArtifactResolver(ArtifactManager artifactManager, ListableRepository repository) { 40 this.artifactManager = artifactManager; 41 this.repositories = Collections.singleton(repository); 42 this.explicitResolution = Collections.EMPTY_MAP; 43 } 44 45 public DefaultArtifactResolver(ArtifactManager artifactManager, Collection repositories, Map explicitResolution) { 46 this.artifactManager = artifactManager; 47 this.repositories = repositories; 48 this.explicitResolution = explicitResolution == null? Collections.EMPTY_MAP: explicitResolution; 49 } 50 51 52 public Artifact generateArtifact(Artifact source, String defaultType) { 53 if(source.isResolved()) { 54 Artifact deAliased = (Artifact) explicitResolution.get(source); 55 if (deAliased != null) { 56 return deAliased; 57 } 58 return source; 59 } 60 String groupId = source.getGroupId() == null ? Artifact.DEFAULT_GROUP_ID : source.getGroupId(); 61 String artifactId = source.getArtifactId(); 62 String type = source.getType() == null ? defaultType : source.getType(); 63 Version version = source.getVersion() == null ? new Version(Long.toString(System.currentTimeMillis())) : source.getVersion(); 64 65 return new Artifact(groupId, artifactId, version, type); 66 } 67 68 public Artifact queryArtifact(Artifact artifact) throws MultipleMatchesException { 69 Artifact[] all = queryArtifacts(artifact); 70 if(all.length > 1) { 71 throw new MultipleMatchesException(artifact); 72 } 73 return all.length == 0 ? null : all[0]; 74 } 75 76 public Artifact[] queryArtifacts(Artifact artifact) { 77 LinkedHashSet set = new LinkedHashSet (); 78 for (Iterator iterator = repositories.iterator(); iterator.hasNext();) { 79 ListableRepository repository = (ListableRepository) iterator.next(); 80 set.addAll(repository.list(artifact)); 81 } 82 return (Artifact[]) set.toArray(new Artifact[set.size()]); 83 } 84 85 public LinkedHashSet resolveInClassLoader(Collection artifacts) throws MissingDependencyException { 86 return resolveInClassLoader(artifacts, Collections.EMPTY_SET); 87 } 88 89 public LinkedHashSet resolveInClassLoader(Collection artifacts, Collection parentConfigurations) throws MissingDependencyException { 90 LinkedHashSet resolvedArtifacts = new LinkedHashSet (); 91 for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) { 92 Artifact artifact = (Artifact) iterator.next(); 93 if (!artifact.isResolved()) { 94 artifact = resolveInClassLoader(artifact, parentConfigurations); 95 } 96 resolvedArtifacts.add(artifact); 97 } 98 return resolvedArtifacts; 99 } 100 101 public Artifact resolveInClassLoader(Artifact source) throws MissingDependencyException { 102 return resolveInClassLoader(source, Collections.EMPTY_SET); 103 } 104 105 public Artifact resolveInClassLoader(Artifact source, Collection parentConfigurations) throws MissingDependencyException { 106 if(source.isResolved()) { 108 return source; 109 } 110 119 121 Artifact working = resolveVersion(parentConfigurations, source); 122 if (working == null || !working.isResolved()) { 123 throw new MissingDependencyException("Unable to resolve dependency " + source); 124 } 125 126 return working; 127 } 128 129 private Artifact resolveVersion(Collection parentConfigurations, Artifact working) { 130 Artifact deAliased = (Artifact) explicitResolution.get(working); 132 if (deAliased != null) { 133 working = deAliased; 134 } 135 SortedSet existingArtifacts; 136 if (artifactManager != null) { 137 existingArtifacts = artifactManager.getLoadedArtifacts(working); 138 } else { 139 existingArtifacts = new TreeSet (); 140 } 141 142 if (existingArtifacts.size() == 1) { 144 return (Artifact) existingArtifacts.first(); 145 } 146 147 148 if (existingArtifacts.size() == 0) { 150 SortedSet list = new TreeSet (); 151 for (Iterator iterator = repositories.iterator(); iterator.hasNext();) { 152 ListableRepository repository = (ListableRepository) iterator.next(); 153 list.addAll(repository.list(working)); 154 } 155 156 if (list.isEmpty()) { 157 return null; 158 } 159 return (Artifact) list.last(); 160 } 161 162 164 Artifact artifact = searchParents(parentConfigurations, working); 166 if (artifact != null) { 167 return artifact; 168 } 169 170 return (Artifact) existingArtifacts.last(); 172 } 173 174 private Artifact searchParents(Collection parentConfigurations, Artifact working) { 175 for (Iterator iterator = parentConfigurations.iterator(); iterator.hasNext();) { 176 Configuration configuration = (Configuration) iterator.next(); 177 178 if (matches(configuration.getId(), working)) { 180 return configuration.getId(); 181 } 182 183 Environment environment = configuration.getEnvironment(); 184 if (environment.isInverseClassLoading()) { 185 Artifact artifact = getArtifactVersion(configuration.getDependencies(), working); 187 if (artifact != null) { 188 return artifact; 189 } 190 191 artifact = searchParents(configuration.getClassParents(), working); 193 if (artifact != null) { 194 return artifact; 195 } 196 197 } else { 198 Artifact artifact = searchParents(configuration.getClassParents(), working); 200 if (artifact != null) { 201 return artifact; 202 } 203 204 artifact = getArtifactVersion(configuration.getDependencies(), working); 206 if (artifact != null) { 207 return artifact; 208 } 209 } 210 } 211 return null; 212 } 213 214 private Artifact getArtifactVersion(Collection artifacts, Artifact query) { 215 for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) { 216 Artifact artifact = (Artifact) iterator.next(); 217 if (matches(artifact, query)) { 218 return artifact; 219 } 220 } 221 return null; 222 } 223 224 private boolean matches(Artifact candidate, Artifact query) { 225 return query.matches(candidate); 226 } 227 228 public static final GBeanInfo GBEAN_INFO; 229 230 static { 231 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(DefaultArtifactResolver.class, "ArtifactResolver"); 232 infoFactory.addAttribute("explicitResolution", Map .class, true, true); 233 infoFactory.addReference("ArtifactManager", ArtifactManager.class, "ArtifactManager"); 234 infoFactory.addReference("Repositories", ListableRepository.class, "Repository"); 235 infoFactory.addInterface(ArtifactResolver.class); 236 237 infoFactory.setConstructor(new String []{ 238 "ArtifactManager", 239 "Repositories", 240 "explicitResolution" 241 }); 242 243 244 GBEAN_INFO = infoFactory.getBeanInfo(); 245 } 246 247 public static GBeanInfo getGBeanInfo() { 248 return GBEAN_INFO; 249 } 250 } | Popular Tags |