1 package org.apache.maven.repository.manager.web.action;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import com.opensymphony.xwork.ActionSupport;
20 import org.apache.lucene.index.Term;
21 import org.apache.lucene.search.BooleanClause;
22 import org.apache.lucene.search.BooleanQuery;
23 import org.apache.lucene.search.MatchAllDocsQuery;
24 import org.apache.lucene.search.TermQuery;
25 import org.apache.maven.repository.configuration.Configuration;
26 import org.apache.maven.repository.configuration.ConfigurationStore;
27 import org.apache.maven.repository.configuration.ConfigurationStoreException;
28 import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory;
29 import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
30 import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
31 import org.apache.maven.repository.indexing.RepositoryIndexException;
32 import org.apache.maven.repository.indexing.RepositoryIndexSearchException;
33 import org.apache.maven.repository.indexing.lucene.LuceneQuery;
34 import org.apache.maven.repository.indexing.record.StandardArtifactIndexRecord;
35 import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
36 import org.codehaus.plexus.util.StringUtils;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.List;
47 import java.util.StringTokenizer;
48 import java.util.TreeMap;
49 import java.util.TreeSet;
52 * Browse the repository.
54 * @todo the tree part probably belongs in a browsing component, and the indexer could optimize how it retrieves the terms rather than querying everything
55 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="browseAction"
57 public class BrowseAction
63 private RepositoryArtifactIndexFactory factory;
68 private ConfiguredRepositoryFactory repositoryFactory;
73 private ConfigurationStore configurationStore;
77 private String groupId;
79 private static final String GROUP_SEPARATOR = ".";
81 private List artifactIds;
83 private String artifactId;
85 private List versions;
87 public String browse()
88 throws ConfigurationStoreException, RepositoryIndexException, IOException, RepositoryIndexSearchException
90 RepositoryArtifactIndex index = getIndex();
92 if ( !index.exists() )
94 addActionError( "The repository is not yet indexed. Please wait, and then try again." );
98 GroupTreeNode rootNode = buildGroupTree( index );
100 this.groups = collateGroups( rootNode );
105 public String browseGroup()
106 throws ConfigurationStoreException, RepositoryIndexException, IOException, RepositoryIndexSearchException
108 RepositoryArtifactIndex index = getIndex();
110 if ( !index.exists() )
112 addActionError( "The repository is not yet indexed. Please wait, and then try again." );
116 GroupTreeNode rootNode = buildGroupTree( index );
118 if ( StringUtils.isEmpty( groupId ) )
121 addActionError( "You must specify a group ID to browse" );
125 StringTokenizer tok = new StringTokenizer( groupId, GROUP_SEPARATOR );
126 while ( tok.hasMoreTokens() )
128 String part = tok.nextToken();
130 if ( !rootNode.getChildren().containsKey( part ) )
133 addActionError( "The group specified was not found" );
138 rootNode = (GroupTreeNode) rootNode.getChildren().get( part );
142 this.groups = collateGroups( rootNode );
144 List records = index.search(
145 new LuceneQuery( new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, groupId ) ) ) );
147 Set artifactIds = new HashSet();
148 for ( Iterator i = records.iterator(); i.hasNext(); )
150 StandardArtifactIndexRecord record = (StandardArtifactIndexRecord) i.next();
151 artifactIds.add( record.getArtifactId() );
153 this.artifactIds = new ArrayList( artifactIds );
154 Collections.sort( this.artifactIds );
159 public String browseArtifact()
160 throws ConfigurationStoreException, RepositoryIndexException, IOException, RepositoryIndexSearchException
162 RepositoryArtifactIndex index = getIndex();
164 if ( StringUtils.isEmpty( groupId ) )
167 addActionError( "You must specify a group ID to browse" );
171 if ( StringUtils.isEmpty( artifactId ) )
174 addActionError( "You must specify a artifact ID to browse" );
178 BooleanQuery query = new BooleanQuery();
179 query.add( new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, groupId ) ),
180 BooleanClause.Occur.MUST );
181 query.add( new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, artifactId ) ),
182 BooleanClause.Occur.MUST );
184 List records = index.search( new LuceneQuery( query ) );
186 if ( records.isEmpty() )
189 addActionError( "Could not find any artifacts with the given group and artifact ID" );
193 Set versions = new HashSet();
194 for ( Iterator i = records.iterator(); i.hasNext(); )
196 StandardArtifactIndexRecord record = (StandardArtifactIndexRecord) i.next();
197 versions.add( record.getVersion() );
200 this.versions = new ArrayList( versions );
201 Collections.sort( this.versions );
206 private GroupTreeNode buildGroupTree( RepositoryArtifactIndex index )
207 throws IOException, RepositoryIndexSearchException
209 // TODO: give action message if indexing is in progress
211 // TODO: this will be inefficient over a very large number of artifacts, should be cached
213 List records = index.search( new LuceneQuery( new MatchAllDocsQuery() ) );
215 Set groups = new TreeSet();
216 for ( Iterator i = records.iterator(); i.hasNext(); )
218 StandardArtifactIndexRecord record = (StandardArtifactIndexRecord) i.next();
219 groups.add( record.getGroupId() );
222 GroupTreeNode rootNode = new GroupTreeNode();
224 // build a tree structure
225 for ( Iterator i = groups.iterator(); i.hasNext(); )
227 String groupId = (String) i.next();
229 StringTokenizer tok = new StringTokenizer( groupId, GROUP_SEPARATOR );
231 GroupTreeNode node = rootNode;
233 while ( tok.hasMoreTokens() )
235 String part = tok.nextToken();
237 if ( !node.getChildren().containsKey( part ) )
239 GroupTreeNode newNode = new GroupTreeNode( part, node );
240 node.addChild( newNode );
245 node = (GroupTreeNode) node.getChildren().get( part );
252 private List collateGroups( GroupTreeNode rootNode )
254 List groups = new ArrayList();
255 for ( Iterator i = rootNode.getChildren().values().iterator(); i.hasNext(); )
257 GroupTreeNode node = (GroupTreeNode) i.next();
259 while ( node.getChildren().size() == 1 )
261 node = (GroupTreeNode) node.getChildren().values().iterator().next();
264 groups.add( node.getFullName() );
269 private RepositoryArtifactIndex getIndex()
270 throws ConfigurationStoreException, RepositoryIndexException
272 Configuration configuration = configurationStore.getConfigurationFromStore();
273 File indexPath = new File( configuration.getIndexPath() );
275 return factory.createStandardIndex( indexPath );
278 public List getGroups()
283 public List getArtifactIds()
288 public String getGroupId()
293 public void setGroupId( String groupId )
295 this.groupId = groupId;
298 public String getArtifactId()
303 public void setArtifactId( String artifactId )
305 this.artifactId = artifactId;
308 public List getVersions()
313 private static class GroupTreeNode
315 private final String name;
317 private final String fullName;
319 private final Map children = new TreeMap();
327 GroupTreeNode( String name, GroupTreeNode parent )
330 this.fullName = parent.fullName != null ? parent.fullName + GROUP_SEPARATOR + name : name;
333 public String getName()
338 public String getFullName()
343 public Map getChildren()
348 public void addChild( GroupTreeNode newNode )
350 children.put( newNode.name, newNode );