KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > repository > DefaultArtifactResolver


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel.repository;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedHashSet JavaDoc;
23 import java.util.SortedSet JavaDoc;
24 import java.util.TreeSet JavaDoc;
25 import java.util.Map JavaDoc;
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 /**
32  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
33  */

34 public class DefaultArtifactResolver implements ArtifactResolver {
35     private final ArtifactManager artifactManager;
36     private final Collection JavaDoc repositories;
37     private final Map JavaDoc 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 JavaDoc repositories, Map JavaDoc 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 JavaDoc 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 JavaDoc groupId = source.getGroupId() == null ? Artifact.DEFAULT_GROUP_ID : source.getGroupId();
61         String JavaDoc artifactId = source.getArtifactId();
62         String JavaDoc 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 JavaDoc set = new LinkedHashSet JavaDoc();
78         for (Iterator JavaDoc 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 JavaDoc resolveInClassLoader(Collection JavaDoc artifacts) throws MissingDependencyException {
86         return resolveInClassLoader(artifacts, Collections.EMPTY_SET);
87     }
88
89     public LinkedHashSet JavaDoc resolveInClassLoader(Collection JavaDoc artifacts, Collection JavaDoc parentConfigurations) throws MissingDependencyException {
90         LinkedHashSet JavaDoc resolvedArtifacts = new LinkedHashSet JavaDoc();
91         for (Iterator JavaDoc 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 JavaDoc parentConfigurations) throws MissingDependencyException {
106         // Some tests break if we acntually try to search for fully-resolved artifacts
107
if(source.isResolved()) {
108             return source;
109         }
110 // if (artifact.getType() == null) {
111
// throw new IllegalArgumentException("Type not set " + artifact);
112
// }
113
//
114
// String groupId = source.getGroupId();
115
// if (groupId == null) {
116
// groupId = Artifact.DEFAULT_GROUP_ID;
117
// }
118

119 // Version version = source.getVersion();
120

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 JavaDoc parentConfigurations, Artifact working) {
130         //see if there is an explicit resolution for this artifact.
131
Artifact deAliased = (Artifact) explicitResolution.get(working);
132         if (deAliased != null) {
133             working = deAliased;
134         }
135         SortedSet JavaDoc existingArtifacts;
136         if (artifactManager != null) {
137             existingArtifacts = artifactManager.getLoadedArtifacts(working);
138         } else {
139             existingArtifacts = new TreeSet JavaDoc();
140         }
141
142         // if we have exactly one artifact loaded use its' version
143
if (existingArtifacts.size() == 1) {
144             return (Artifact) existingArtifacts.first();
145         }
146
147
148         // if we have no existing loaded artifacts grab the highest version from the repository
149
if (existingArtifacts.size() == 0) {
150             SortedSet JavaDoc list = new TreeSet JavaDoc();
151             for (Iterator JavaDoc 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         // more than one version of the artifact was loaded...
163

164         // if one of parents already loaded the artifact, use that version
165
Artifact artifact = searchParents(parentConfigurations, working);
166         if (artifact != null) {
167             return artifact;
168         }
169
170         // it wasn't declared by the parent so just use the highest verstion already loaded
171
return (Artifact) existingArtifacts.last();
172     }
173
174     private Artifact searchParents(Collection JavaDoc parentConfigurations, Artifact working) {
175         for (Iterator JavaDoc iterator = parentConfigurations.iterator(); iterator.hasNext();) {
176             Configuration configuration = (Configuration) iterator.next();
177
178             // check if this parent matches the groupId, artifactId, and type
179
if (matches(configuration.getId(), working)) {
180                 return configuration.getId();
181             }
182
183             Environment environment = configuration.getEnvironment();
184             if (environment.isInverseClassLoading()) {
185                 // Search dependencies of the configuration before searching the parents
186
Artifact artifact = getArtifactVersion(configuration.getDependencies(), working);
187                 if (artifact != null) {
188                     return artifact;
189                 }
190
191                 // wasn't declared in the dependencies, so search the parents of the configuration
192
artifact = searchParents(configuration.getClassParents(), working);
193                 if (artifact != null) {
194                     return artifact;
195                 }
196
197             } else {
198                 // Search the parents before the dependencies of the configuration
199
Artifact artifact = searchParents(configuration.getClassParents(), working);
200                 if (artifact != null) {
201                     return artifact;
202                 }
203
204                 // wasn't declared in a parent check the dependencies of the configuration
205
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 JavaDoc artifacts, Artifact query) {
215         for (Iterator JavaDoc 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 JavaDoc.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 JavaDoc[]{
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