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.archiva.common.ArchivaException;
26 import org.apache.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 );
53 private DependencyTreeBuilder dependencyTreeBuilder;
56 private UserRepositories userRepositories;
58 public static class TreeEntry
60 private String pre = "";
62 private String post = "";
64 private Artifact artifact;
66 public void setArtifact( Artifact artifact )
68 this.artifact = artifact;
71 public Artifact getArtifact()
76 public String getPost()
81 public String getPre()
86 public void appendPre( String string )
91 public void appendPost( String string )
97 public List<TreeEntry> gatherTreeList( String groupId, String artifactId, String modelVersion )
98 throws ArchivaException
100 if ( StringUtils.isBlank( groupId ) )
102 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
103 + "]: groupId is blank.";
105 throw new ArchivaException( emsg );
108 if ( StringUtils.isBlank( artifactId ) )
110 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
111 + "]: artifactId is blank.";
113 throw new ArchivaException( emsg );
116 if ( StringUtils.isBlank( modelVersion ) )
118 String emsg = "Error generating dependency tree [" + Keys.toKey( groupId, artifactId, modelVersion )
119 + "]: version is blank.";
121 throw new ArchivaException( emsg );
124 // TODO Cache the results to disk, in XML format, in the same place as the artifact is located.
126 TreeListVisitor visitor = new TreeListVisitor();
129 dependencyTreeBuilder.buildDependencyTree( userRepositories.getObservableRepositoryIds( getPrincipal() ),
130 groupId, artifactId, modelVersion, visitor );
132 catch ( DependencyTreeBuilderException e )
134 throw new ArchivaException( "Unable to build dependency tree: " + e.getMessage(), e );
137 return visitor.getList();
140 private String getPrincipal()
142 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
145 private static class TreeListVisitor
146 implements DependencyNodeVisitor
148 private List<TreeEntry> list;
150 private TreeEntry currentEntry;
152 boolean firstChild = true;
154 private DependencyNode firstNode;
156 public TreeListVisitor()
158 this.list = new ArrayList<TreeEntry>();
161 public List<TreeEntry> getList()
166 public boolean visit( DependencyNode node )
168 if ( firstNode == null )
173 currentEntry = new TreeEntry();
177 currentEntry.appendPre( "<ul>" );
180 currentEntry.appendPre( "<li>" );
181 currentEntry.setArtifact( node.getArtifact() );
182 currentEntry.appendPost( "</li>" );
183 this.list.add( currentEntry );
185 if ( !node.getChildren().isEmpty() )
193 public boolean endVisit( DependencyNode node )
197 if ( !node.getChildren().isEmpty() )
199 currentEntry.appendPost( "</ul>" );
202 if ( node == firstNode )
204 currentEntry.appendPost( "</ul>" );