View Javadoc

1   package org.apache.maven.index.archetype;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
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
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Properties;
25  
26  import org.apache.lucene.search.Query;
27  import org.apache.maven.archetype.catalog.Archetype;
28  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
29  import org.apache.maven.archetype.source.ArchetypeDataSource;
30  import org.apache.maven.archetype.source.ArchetypeDataSourceException;
31  import org.apache.maven.index.ArtifactInfo;
32  import org.apache.maven.index.FlatSearchRequest;
33  import org.apache.maven.index.FlatSearchResponse;
34  import org.apache.maven.index.MAVEN;
35  import org.apache.maven.index.NexusIndexer;
36  import org.apache.maven.index.context.IndexingContext;
37  import org.apache.maven.index.expr.SourcedSearchExpression;
38  import org.codehaus.plexus.component.annotations.Component;
39  import org.codehaus.plexus.component.annotations.Requirement;
40  import org.codehaus.plexus.logging.AbstractLogEnabled;
41  
42  /**
43   * @author Eugene Kuleshov
44   */
45  @Component( role = ArchetypeDataSource.class, hint = "nexus" )
46  public class NexusArchetypeDataSource
47      extends AbstractLogEnabled
48      implements ArchetypeDataSource
49  {
50      @Requirement
51      private NexusIndexer indexer;
52  
53      public ArchetypeCatalog getArchetypeCatalog( Properties properties )
54          throws ArchetypeDataSourceException
55      {
56          ArchetypeCatalog catalog = new ArchetypeCatalog();
57  
58          try
59          {
60              Map<String, String> repositories = getRepositoryMap();
61  
62              Query pq = indexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression( "maven-archetype" ) );
63  
64              FlatSearchRequest searchRequest = new FlatSearchRequest( pq );
65  
66              FlatSearchResponse searchResponse = indexer.searchFlat( searchRequest );
67  
68              for ( ArtifactInfo info : searchResponse.getResults() )
69              {
70                  Archetype archetype = new Archetype();
71                  archetype.setGroupId( info.groupId );
72                  archetype.setArtifactId( info.artifactId );
73                  archetype.setVersion( info.version );
74                  archetype.setDescription( info.description );
75                  archetype.setRepository( repositories.get( info.repository ) );
76  
77                  catalog.addArchetype( archetype );
78              }
79          }
80          catch ( Exception ex )
81          {
82              getLogger().error( "Unable to retrieve archetypes", ex );
83          }
84  
85          return catalog;
86      }
87  
88      private Map<String, String> getRepositoryMap()
89      {
90          // can't cache this because indexes can be changed
91          Map<String, String> repositories = new HashMap<String, String>();
92  
93          for ( IndexingContext context : indexer.getIndexingContexts().values() )
94          {
95              String repositoryUrl = context.getRepositoryUrl();
96              if ( repositoryUrl != null )
97              {
98                  repositories.put( context.getId(), repositoryUrl );
99              }
100         }
101 
102         return repositories;
103     }
104 
105     public void updateCatalog( Properties properties, Archetype archetype )
106     {
107         // TODO maybe update index
108     }
109 }