View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.genesis.dependency;
21  
22  import java.util.Map;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  
27  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
28  import org.apache.maven.artifact.resolver.ArtifactCollector;
29  import org.apache.maven.artifact.resolver.ArtifactResolver;
30  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
33  import org.apache.maven.artifact.factory.ArtifactFactory;
34  import org.apache.maven.artifact.versioning.VersionRange;
35  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
36  import org.apache.maven.artifact.Artifact;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.project.ProjectBuildingException;
39  import org.apache.maven.project.artifact.InvalidDependencyVersionException;
40  import org.apache.maven.model.DependencyManagement;
41  import org.apache.maven.model.Dependency;
42  
43  import org.codehaus.plexus.PlexusContainer;
44  import org.codehaus.plexus.PlexusConstants;
45  import org.codehaus.plexus.context.Context;
46  import org.codehaus.plexus.context.ContextException;
47  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
48  
49  /**
50   * ???
51   *
52   * @version $Rev: 454266 $ $Date: 2006-10-08 20:20:03 -0700 (Sun, 08 Oct 2006) $
53   */
54  public class DependencyHelper
55      implements Contextualizable
56  {
57      private ArtifactRepositoryFactory artifactRepositoryFactory = null;
58  
59      private ArtifactMetadataSource artifactMetadataSource = null;
60  
61      private ArtifactCollector artifactCollector = null;
62  
63      private ArtifactFactory artifactFactory = null;
64  
65      private ArtifactResolver artifactResolver = null;
66  
67      private PlexusContainer container;
68  
69      private ArtifactRepository repository;
70  
71      //
72      // TODO: Figure out how to get ${localRepository} injected so we don't need it passed in.
73      //
74  
75      public void setArtifactRepository(final ArtifactRepository repository) {
76          this.repository = repository;
77      }
78  
79      private ArtifactRepository getArtifactRepository() {
80          if (repository == null) {
81              throw new IllegalStateException("Not initialized; missing ArtifactRepository");
82          }
83          return repository;
84      }
85  
86      public DependencyTree getDependencies(final MavenProject project)
87          throws ProjectBuildingException, InvalidDependencyVersionException, ArtifactResolutionException
88      {
89          assert project != null;
90  
91          Map managedVersions = getManagedVersionMap(project, artifactFactory);
92          DependencyResolutionListener listener = new DependencyResolutionListener();
93  
94          if (project.getDependencyArtifacts() == null) {
95              project.setDependencyArtifacts(project.createArtifacts(artifactFactory, null, null));
96          }
97  
98          artifactCollector.collect(
99                  project.getDependencyArtifacts(),
100                 project.getArtifact(),
101                 managedVersions,
102                 getArtifactRepository(),
103                 project.getRemoteArtifactRepositories(),
104                 artifactMetadataSource,
105                 null,
106                 Collections.singletonList(listener));
107         
108         return listener.getDependencyTree();
109     }
110 
111     public static Map getManagedVersionMap(final MavenProject project, final ArtifactFactory factory) throws ProjectBuildingException {
112         assert project != null;
113         assert factory != null;
114 
115         DependencyManagement dependencyManagement = project.getDependencyManagement();
116         Map managedVersionMap;
117 
118         if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
119             managedVersionMap = new HashMap();
120             Iterator iter = dependencyManagement.getDependencies().iterator();
121 
122             while (iter.hasNext()) {
123                 Dependency d = (Dependency) iter.next();
124 
125                 try {
126                     VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion());
127                     Artifact artifact = factory.createDependencyArtifact(
128                             d.getGroupId(),
129                             d.getArtifactId(),
130                             versionRange,
131                             d.getType(),
132                             d.getClassifier(),
133                             d.getScope());
134                     managedVersionMap.put(d.getManagementKey(), artifact);
135                 }
136                 catch (InvalidVersionSpecificationException e) {
137                     throw new ProjectBuildingException(project.getId(),
138                             "Unable to parse version '" + d.getVersion() +
139                             "' for dependency '" + d.getManagementKey() + "': " + e.getMessage(), e);
140                 }
141             }
142         }
143         else {
144             managedVersionMap = Collections.EMPTY_MAP;
145         }
146 
147         return managedVersionMap;
148     }
149 
150     //
151     // Contextualizable
152     //
153 
154     public void contextualize(final Context context) throws ContextException {
155         container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY);
156     }
157 
158     //
159     // Component Access
160     //
161     
162     public ArtifactResolver getArtifactResolver() {
163         return artifactResolver;
164     }
165 
166     public ArtifactRepositoryFactory getArtifactRepositoryFactory() {
167         return artifactRepositoryFactory;
168     }
169 
170     public ArtifactMetadataSource getArtifactMetadataSource() {
171         return artifactMetadataSource;
172     }
173 
174     public ArtifactCollector getArtifactCollector() {
175         return artifactCollector;
176     }
177 
178     public ArtifactFactory getArtifactFactory() {
179         return artifactFactory;
180     }
181 }