1 package org.apache.archiva.web.tags;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.ActionContext;
23 import org.apache.archiva.dependency.tree.maven2.DependencyTreeBuilder;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.common.ArchivaException;
26 import org.apache.maven.archiva.model.Keys;
27 import org.apache.archiva.security.ArchivaXworkUser;
28 import org.apache.archiva.security.UserRepositories;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.shared.dependency.tree.DependencyNode;
31 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
32 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
38 import java.util.ArrayList;
39 import java.util.List;
46 @Service( "dependencyTree" )
47 public class DependencyTree
49 private Logger log = LoggerFactory.getLogger( DependencyTree.class );
52 * plexus.requirement role-hint="maven2"
55 private DependencyTreeBuilder dependencyTreeBuilder;
61 private UserRepositories userRepositories;
63 public static class TreeEntry
65 private String pre = "";
67 private String post = "";
69 private Artifact artifact;
71 public void setArtifact( Artifact artifact )
73 this.artifact = artifact;
76 public Artifact getArtifact()
81 public String getPost()
86 public String getPre()
91 public void appendPre( String string )
96 public void appendPost( String string )
102 public List<TreeEntry> gatherTreeList( String groupId, String artifactId, String modelVersion )
103 throws ArchivaException
105 if ( StringUtils.isBlank( groupId ) )
107 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
108 + "]: groupId is blank.";
110 throw new ArchivaException( emsg );
113 if ( StringUtils.isBlank( artifactId ) )
115 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
116 + "]: artifactId is blank.";
118 throw new ArchivaException( emsg );
121 if ( StringUtils.isBlank( modelVersion ) )
123 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
124 + "]: version is blank.";
126 throw new ArchivaException( emsg );
129 // TODO Cache the results to disk, in XML format, in the same place as the artifact is located.
131 TreeListVisitor visitor = new TreeListVisitor();
134 dependencyTreeBuilder.buildDependencyTree( userRepositories.getObservableRepositoryIds( getPrincipal() ),
135 groupId, artifactId, modelVersion, visitor );
137 catch ( DependencyTreeBuilderException e )
139 throw new ArchivaException( "Unable to build dependency tree: " + e.getMessage(), e );
142 return visitor.getList();
145 private String getPrincipal()
147 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
150 private static class TreeListVisitor
151 implements DependencyNodeVisitor
153 private List<TreeEntry> list;
155 private TreeEntry currentEntry;
157 boolean firstChild = true;
159 private DependencyNode firstNode;
161 public TreeListVisitor()
163 this.list = new ArrayList<TreeEntry>();
166 public List<TreeEntry> getList()
171 public boolean visit( DependencyNode node )
173 if ( firstNode == null )
178 currentEntry = new TreeEntry();
182 currentEntry.appendPre( "<ul>" );
185 currentEntry.appendPre( "<li>" );
186 currentEntry.setArtifact( node.getArtifact() );
187 currentEntry.appendPost( "</li>" );
188 this.list.add( currentEntry );
190 if ( !node.getChildren().isEmpty() )
198 public boolean endVisit( DependencyNode node )
202 if ( !node.getChildren().isEmpty() )
204 currentEntry.appendPost( "</ul>" );
207 if ( node == firstNode )
209 currentEntry.appendPost( "</ul>" );