1 package org.apache.maven.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.maven.archiva.common.ArchivaException;
26 import org.apache.maven.archiva.web.tags.DependencyTree.TreeEntry;
27 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.util.Iterator;
32 import java.util.List;
34 import javax.servlet.jsp.JspException;
35 import javax.servlet.jsp.PageContext;
36 import javax.servlet.jsp.tagext.IterationTag;
37 import javax.servlet.jsp.tagext.TagSupport;
38 import javax.servlet.jsp.tagext.TryCatchFinally;
41 * DependencyTreeTag - just here to output the dependency tree to the browser.
42 * It was easier to do it this way, vs accessing the dependency graph via a JSP.
45 * <archiva:dependency-tree groupId="org.apache.maven.archiva"
46 * artifactId="archiva-common"
49 * <b>${node.groupId}</b>:<b>${node.artifactId}</b>:<b>${node.version}</b> (${node.scope})
50 * </archiva:dependency-tree>
55 public class DependencyTreeTag
57 implements IterationTag, TryCatchFinally
59 private String groupId;
61 private String artifactId;
63 private String version;
65 private String nodevar;
67 private Iterator treeIterator;
69 private List<TreeEntry> tree;
71 private TreeEntry currentTreeEntry;
73 private String modelVersion;
75 public int doAfterBody()
78 if ( currentTreeEntry != null )
80 out( currentTreeEntry.getPost() );
83 if ( treeIterator.hasNext() )
85 currentTreeEntry = (TreeEntry) treeIterator.next();
86 out( currentTreeEntry.getPre() );
88 return EVAL_BODY_AGAIN;
91 out( "\n</div><!-- end of dependency-graph -->" );
96 public void doCatch( Throwable t )
102 public void doFinally()
107 public int doStartTag()
110 DependencyTree deptree;
113 deptree = (DependencyTree) PlexusTagUtil.lookup( pageContext, DependencyTree.class.getName() );
115 catch ( ComponentLookupException e )
117 throw new JspException( "Unable to lookup DependencyTree: " + e.getMessage(), e );
120 if ( deptree == null )
122 throw new JspException( "Unable to process dependency tree. Component not found." );
125 if ( StringUtils.isBlank( nodevar ) )
130 out( "<div class=\"dependency-graph\">" );
133 this.tree = deptree.gatherTreeList( groupId, artifactId, modelVersion, nodevar, pageContext );
135 if ( CollectionUtils.isEmpty( this.tree ) )
140 treeIterator = tree.iterator();
142 currentTreeEntry = (TreeEntry) treeIterator.next();
143 out( currentTreeEntry.getPre() );
146 catch ( ArchivaException e )
148 treeIterator = IteratorUtils.EMPTY_LIST_ITERATOR;
151 e.printStackTrace( new PrintWriter( pageContext.getOut() ) );
155 return EVAL_BODY_INCLUDE;
158 public void release()
169 public void setArtifactId( String artifactId )
171 this.artifactId = artifactId;
174 public void setGroupId( String groupId )
176 this.groupId = groupId;
179 public void setNodevar( String nodevar )
181 this.nodevar = nodevar;
184 public void setVersion( String version )
186 this.version = version;
189 public void setModelVersion( String modelVersion )
191 this.modelVersion = modelVersion;
194 private void exposeVariables()
197 if ( currentTreeEntry == null )
199 pageContext.removeAttribute( nodevar, PageContext.PAGE_SCOPE );
203 pageContext.setAttribute( nodevar, currentTreeEntry.getArtifact() );
207 private void out( String msg )
212 pageContext.getOut().print( msg );
214 catch ( IOException e )
216 throw new JspException( "Unable to output to jsp page context." );
220 private void unExposeVariables()
222 pageContext.removeAttribute( nodevar, PageContext.PAGE_SCOPE );