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 org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.IteratorUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.archiva.common.ArchivaException;
26 import org.apache.archiva.web.tags.DependencyTree.TreeEntry;
27 import org.apache.struts2.views.annotations.StrutsTag;
28 import org.apache.struts2.views.annotations.StrutsTagAttribute;
29 import org.springframework.beans.BeansException;
30 import org.springframework.web.context.WebApplicationContext;
31 import org.springframework.web.context.support.WebApplicationContextUtils;
33 import java.io.IOException;
34 import java.io.PrintWriter;
35 import java.util.Iterator;
36 import java.util.List;
38 import javax.servlet.jsp.JspException;
39 import javax.servlet.jsp.PageContext;
40 import javax.servlet.jsp.tagext.IterationTag;
41 import javax.servlet.jsp.tagext.TagSupport;
42 import javax.servlet.jsp.tagext.TryCatchFinally;
45 * DependencyTreeTag - just here to output the dependency tree to the browser.
46 * It was easier to do it this way, vs accessing the dependency graph via a JSP.
49 * <archiva:dependency-tree groupId="org.apache.archiva"
50 * artifactId="archiva-common"
53 * <b>${node.groupId}</b>:<b>${node.artifactId}</b>:<b>${node.version}</b> (${node.scope})
54 * </archiva:dependency-tree>
59 @StrutsTag(name = "dependency-tree", tldBodyContent = "JSP", tldTagClass = "org.apache.archiva.web.tags.DependencyTreeTag", description = "Render a dependency tree for the provided project.")
60 public class DependencyTreeTag
62 implements IterationTag, TryCatchFinally
64 private String groupId;
66 private String artifactId;
68 @SuppressWarnings("unused")
69 private String version;
71 private String nodevar;
73 private Iterator<TreeEntry> treeIterator;
75 private List<TreeEntry> tree;
77 private TreeEntry currentTreeEntry;
79 private String modelVersion;
81 public int doAfterBody()
84 if ( currentTreeEntry != null )
86 out( currentTreeEntry.getPost() );
89 if ( treeIterator.hasNext() )
91 currentTreeEntry = treeIterator.next();
92 out( currentTreeEntry.getPre() );
94 return EVAL_BODY_AGAIN;
97 out( "\n</div><!-- end of dependency-graph -->" );
102 public void doCatch( Throwable t )
108 public void doFinally()
113 @SuppressWarnings("unchecked")
114 public int doStartTag()
117 DependencyTree deptree;
120 WebApplicationContext webApplicationContext =
121 WebApplicationContextUtils.getRequiredWebApplicationContext( pageContext.getServletContext() );
123 deptree = webApplicationContext.getBean( "dependencyTree", DependencyTree.class );
125 catch ( BeansException e )
127 throw new JspException( "Unable to lookup DependencyTree: " + e.getMessage(), e );
130 if ( deptree == null )
132 throw new JspException( "Unable to process dependency tree. Component not found." );
135 if ( StringUtils.isBlank( nodevar ) )
140 out( "<div class=\"dependency-graph\">" );
143 this.tree = deptree.gatherTreeList( groupId, artifactId, modelVersion );
145 if ( CollectionUtils.isEmpty( this.tree ) )
150 treeIterator = tree.iterator();
152 currentTreeEntry = treeIterator.next();
153 out( currentTreeEntry.getPre() );
156 catch ( ArchivaException e )
158 treeIterator = IteratorUtils.EMPTY_LIST_ITERATOR;
161 e.printStackTrace( new PrintWriter( pageContext.getOut() ) );
165 return EVAL_BODY_INCLUDE;
168 public void release()
179 @StrutsTagAttribute(description = "The artifactId", type = "String", defaultValue = "", required = true, rtexprvalue = true)
180 public void setArtifactId( String artifactId )
182 this.artifactId = artifactId;
185 @StrutsTagAttribute(description = "The groupId", type = "String", defaultValue = "", required = true, rtexprvalue = true)
186 public void setGroupId( String groupId )
188 this.groupId = groupId;
191 @StrutsTagAttribute(description = "The variable name for the node.", type = "String", defaultValue = "", required = false, rtexprvalue = true)
192 public void setNodevar( String nodevar )
194 this.nodevar = nodevar;
197 @StrutsTagAttribute(description = "The version", type = "String", defaultValue = "", required = true, rtexprvalue = true)
198 public void setVersion( String version )
200 this.version = version;
203 @StrutsTagAttribute(description = "The version of the project model. Used to verify the dependency graph for generic snapshots not yet in the repo.", type = "String", defaultValue = "", required = false, rtexprvalue = true)
204 public void setModelVersion( String modelVersion )
206 this.modelVersion = modelVersion;
209 private void exposeVariables()
212 if ( currentTreeEntry == null )
214 pageContext.removeAttribute( nodevar, PageContext.PAGE_SCOPE );
218 pageContext.setAttribute( nodevar, currentTreeEntry.getArtifact() );
222 private void out( String msg )
227 pageContext.getOut().print( msg );
229 catch ( IOException e )
231 throw new JspException( "Unable to output to jsp page context." );
235 private void unExposeVariables()
237 pageContext.removeAttribute( nodevar, PageContext.PAGE_SCOPE );