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.common.ArchivaException;
24 import org.apache.archiva.dependency.tree.maven2.DependencyTreeBuilder;
25 import org.apache.archiva.model.Keys;
26 import org.apache.archiva.security.ArchivaXworkUser;
27 import org.apache.archiva.security.UserRepositories;
28 import org.apache.commons.lang.StringUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.sonatype.aether.artifact.Artifact;
32 import org.sonatype.aether.graph.DependencyNode;
33 import org.sonatype.aether.graph.DependencyVisitor;
34 import org.springframework.stereotype.Service;
36 import javax.inject.Inject;
37 import java.util.ArrayList;
38 import java.util.List;
43 @Service( "dependencyTree" )
44 public class DependencyTree
46 private Logger log = LoggerFactory.getLogger( DependencyTree.class );
50 private DependencyTreeBuilder dependencyTreeBuilder;
53 private UserRepositories userRepositories;
55 public static class TreeEntry
57 private String pre = "";
59 private String post = "";
61 private Artifact artifact;
63 public void setArtifact( Artifact artifact )
65 this.artifact = artifact;
68 public Artifact getArtifact()
73 public String getPost()
78 public String getPre()
83 public void appendPre( String string )
88 public void appendPost( String string )
94 public List<TreeEntry> gatherTreeList( String groupId, String artifactId, String modelVersion )
95 throws ArchivaException
97 if ( StringUtils.isBlank( groupId ) )
99 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
100 + "]: groupId is blank.";
102 throw new ArchivaException( emsg );
105 if ( StringUtils.isBlank( artifactId ) )
107 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
108 + "]: artifactId is blank.";
110 throw new ArchivaException( emsg );
113 if ( StringUtils.isBlank( modelVersion ) )
115 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
116 + "]: version is blank.";
118 throw new ArchivaException( emsg );
121 // TODO Cache the results to disk, in XML format, in the same place as the artifact is located.
123 TreeListVisitor visitor = new TreeListVisitor();
126 dependencyTreeBuilder.buildDependencyTree( userRepositories.getObservableRepositoryIds( getPrincipal() ),
127 groupId, artifactId, modelVersion, visitor );
129 catch ( Exception e )
131 throw new ArchivaException( "Unable to build dependency tree: " + e.getMessage(), e );
134 return visitor.getList();
137 private String getPrincipal()
139 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
142 private static class TreeListVisitor
143 implements DependencyVisitor
145 private List<TreeEntry> list;
147 private TreeEntry currentEntry;
149 boolean firstChild = true;
151 private DependencyNode firstNode;
153 public TreeListVisitor()
155 this.list = new ArrayList<TreeEntry>();
158 public List<TreeEntry> getList()
163 public boolean visitEnter( DependencyNode node )
165 if ( firstNode == null )
170 currentEntry = new TreeEntry();
174 currentEntry.appendPre( "<ul>" );
177 currentEntry.appendPre( "<li>" );
178 currentEntry.setArtifact( node.getDependency().getArtifact() );
179 currentEntry.appendPost( "</li>" );
180 this.list.add( currentEntry );
182 if ( !node.getChildren().isEmpty() )
190 public boolean visitLeave( org.sonatype.aether.graph.DependencyNode node )
194 if ( !node.getChildren().isEmpty() )
196 currentEntry.appendPost( "</ul>" );
199 if ( node == firstNode )
201 currentEntry.appendPost( "</ul>" );