Coverage Report - org.apache.maven.archetype.ui.DefaultArchetypeSelector
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeSelector
55 %
58/106
48 %
29/60
0
 
 1  
 package org.apache.maven.archetype.ui;
 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 org.apache.maven.archetype.ArchetypeGenerationRequest;
 23  
 import org.apache.maven.archetype.ArchetypeManager;
 24  
 import org.apache.maven.archetype.catalog.Archetype;
 25  
 import org.apache.maven.archetype.exception.ArchetypeNotDefined;
 26  
 import org.apache.maven.archetype.exception.ArchetypeSelectionFailure;
 27  
 import org.apache.maven.archetype.exception.UnknownArchetype;
 28  
 import org.apache.maven.archetype.exception.UnknownGroup;
 29  
 import org.codehaus.plexus.components.interactivity.PrompterException;
 30  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 31  
 import org.codehaus.plexus.util.StringUtils;
 32  
 
 33  
 import java.io.IOException;
 34  
 import java.util.HashMap;
 35  
 import java.util.LinkedHashMap;
 36  
 import java.util.List;
 37  
 import java.util.Map;
 38  
 
 39  
 /** @plexus.component */
 40  5
 public class DefaultArchetypeSelector
 41  
     extends AbstractLogEnabled
 42  
     implements ArchetypeSelector
 43  
 {
 44  
     static final String DEFAULT_ARCHETYPE_GROUPID = "org.apache.maven.archetypes";
 45  
 
 46  
     static final String DEFAULT_ARCHETYPE_VERSION = "1.0";
 47  
 
 48  
     static final String DEFAULT_ARCHETYPE_ARTIFACTID = "maven-archetype-quickstart";
 49  
 
 50  
     /** @plexus.requirement */
 51  
     private ArchetypeSelectionQueryer archetypeSelectionQueryer;
 52  
 
 53  
     /** @plexus.requirement */
 54  
     private ArchetypeManager archetypeManager;
 55  
 
 56  
     public void selectArchetype( ArchetypeGenerationRequest request, Boolean interactiveMode, String catalogs )
 57  
         throws ArchetypeNotDefined, UnknownArchetype, UnknownGroup, IOException, PrompterException,
 58  
         ArchetypeSelectionFailure
 59  
     {
 60  5
         ArchetypeDefinition definition = new ArchetypeDefinition( request );
 61  
 
 62  5
         if ( definition.isDefined() && StringUtils.isNotEmpty( request.getArchetypeRepository() ) )
 63  
         {
 64  0
             getLogger().info( "Archetype defined by properties" );
 65  0
             return;
 66  
         }
 67  
 
 68  5
         Map<String, List<Archetype>> archetypes = getArchetypesByCatalog( catalogs );
 69  
 
 70  5
         if ( definition.isDefined() )
 71  
         {
 72  1
             Map.Entry<String, Archetype> found =
 73  
                 findArchetype( archetypes, request.getArchetypeGroupId(), request.getArchetypeArtifactId() );
 74  
 
 75  1
             if ( found != null )
 76  
             {
 77  0
                 String catalogKey = found.getKey();
 78  0
                 Archetype archetype = found.getValue();
 79  
 
 80  0
                 updateDefinition( definition, archetype, catalogKey );
 81  
 
 82  0
                 getLogger().info( "Archetype repository missing. Using the one from " + archetype
 83  
                                       + " found in catalog " + catalogKey );
 84  0
             }
 85  
             else
 86  
             {
 87  1
                 getLogger().warn( "Archetype not found in any catalog. Falling back to central repository (http://repo1.maven.org/maven2)." );
 88  1
                 getLogger().warn( "Use -DarchetypeRepository=<your repository> if archetype's repository is elsewhere." );
 89  
 
 90  1
                 definition.setRepository( "http://repo1.maven.org/maven2" );
 91  
             }
 92  1
         }
 93  4
         else if ( definition.isPartiallyDefined() )
 94  
         {
 95  0
             Map.Entry<String, Archetype> found =
 96  
                 findArchetype( archetypes, request.getArchetypeGroupId(), request.getArchetypeArtifactId() );
 97  
 
 98  0
             if ( found != null )
 99  
             {
 100  0
                 String catalogKey = found.getKey();
 101  0
                 Archetype archetype = found.getValue();
 102  
 
 103  0
                 updateDefinition( definition, archetype, catalogKey );
 104  
 
 105  0
                 getLogger().info( "Archetype " + archetype + " found in catalog " + catalogKey );
 106  0
             }
 107  
             else
 108  
             {
 109  0
                 getLogger().warn( "Specified archetype not found." );
 110  0
                 if ( interactiveMode.booleanValue() )
 111  
                 {
 112  0
                     definition.setVersion( null );
 113  0
                     definition.setGroupId( null );
 114  0
                     definition.setArtifactId( null );
 115  
                 }
 116  
             }
 117  
         }
 118  
 
 119  
         // set the defaults - only group and version can be auto-defaulted
 120  5
         if ( definition.getGroupId() == null )
 121  
         {
 122  4
             definition.setGroupId( DEFAULT_ARCHETYPE_GROUPID );
 123  
         }
 124  5
         if ( definition.getVersion() == null )
 125  
         {
 126  4
             definition.setVersion( DEFAULT_ARCHETYPE_VERSION );
 127  
         }
 128  
 
 129  5
         if ( !definition.isPartiallyDefined() )
 130  
         {
 131  
             // if artifact ID is set to its default, we still prompt to confirm
 132  3
             if ( definition.getArtifactId() == null )
 133  
             {
 134  3
                 getLogger().info( "No archetype defined. Using " + DEFAULT_ARCHETYPE_ARTIFACTID + " ("
 135  
                     + definition.getGroupId() + ":" + DEFAULT_ARCHETYPE_ARTIFACTID + ":" + definition.getVersion()
 136  
                     + ")" );
 137  3
                 definition.setArtifactId( DEFAULT_ARCHETYPE_ARTIFACTID );
 138  
             }
 139  
 
 140  3
             if ( interactiveMode.booleanValue() && ( archetypes.size() > 0 ) )
 141  
             {
 142  2
                 Archetype selectedArchetype = archetypeSelectionQueryer.selectArchetype( archetypes, definition );
 143  
 
 144  2
                 String catalogKey = getCatalogKey( archetypes, selectedArchetype );
 145  
 
 146  2
                 updateDefinition( definition, selectedArchetype, catalogKey );
 147  
             }
 148  
 
 149  
             // Make sure the groupId and artifactId are valid, the version may just default to
 150  
             // the latest release.
 151  3
             if ( !definition.isPartiallyDefined() )
 152  
             {
 153  0
                 throw new ArchetypeSelectionFailure( "No valid archetypes could be found to choose." );
 154  
             }
 155  
         }
 156  
 
 157  
         // finally update the request with gathered information
 158  5
         definition.updateRequest( request );
 159  5
     }
 160  
 
 161  
     private Map<String, List<Archetype>> getArchetypesByCatalog( String catalogs )
 162  
     {
 163  5
         if ( catalogs == null )
 164  
         {
 165  0
             throw new NullPointerException( "catalogs cannot be null" );
 166  
         }
 167  
 
 168  5
         Map<String, List<Archetype>> archetypes = new LinkedHashMap<String, List<Archetype>>();
 169  
 
 170  5
         for ( String catalog : StringUtils.split( catalogs, "," ) )
 171  
         {
 172  0
             if ( "internal".equalsIgnoreCase( catalog ) )
 173  
             {
 174  0
                 archetypes.put( "internal", archetypeManager.getInternalCatalog().getArchetypes() );
 175  
             }
 176  0
             else if ( "local".equalsIgnoreCase( catalog ) )
 177  
             {
 178  0
                 archetypes.put( "local", archetypeManager.getDefaultLocalCatalog().getArchetypes() );
 179  
             }
 180  0
             else if ( "remote".equalsIgnoreCase( catalog ) )
 181  
             {
 182  0
                 List<Archetype> archetypesFromRemote = archetypeManager.getRemoteCatalog().getArchetypes();
 183  0
                 if ( archetypesFromRemote.size() > 0 )
 184  
                 {
 185  0
                     archetypes.put( "remote", archetypesFromRemote );
 186  
                 }
 187  
                 else
 188  
                 {
 189  0
                     getLogger().warn( "No archetype found in remote catalog. Defaulting to internal catalog" );
 190  0
                     archetypes.put( "internal", archetypeManager.getInternalCatalog().getArchetypes() );
 191  
                 }
 192  0
             }
 193  0
             else if ( catalog.startsWith( "file://" ) )
 194  
             {
 195  0
                 String path = catalog.substring( 7 );
 196  0
                 archetypes.put( catalog, archetypeManager.getLocalCatalog( path ).getArchetypes() );
 197  0
             }
 198  0
             else if ( catalog.startsWith( "http://" ) )
 199  
             {
 200  0
                 archetypes.put( catalog, archetypeManager.getRemoteCatalog( catalog ).getArchetypes() );
 201  
             }
 202  
         }
 203  
 
 204  5
         if ( archetypes.size() == 0 )
 205  
         {
 206  5
             getLogger().info( "No catalog defined. Using internal catalog" );
 207  
 
 208  5
             archetypes.put( "internal", archetypeManager.getInternalCatalog().getArchetypes() );
 209  
         }
 210  5
         return archetypes;
 211  
     }
 212  
 
 213  
     private void updateDefinition( ArchetypeDefinition definition, Archetype archetype, String catalogKey )
 214  
     {
 215  2
         definition.setGroupId( archetype.getGroupId() );
 216  2
         definition.setArtifactId( archetype.getArtifactId() );
 217  2
         definition.setVersion( archetype.getVersion() );
 218  2
         definition.setName( archetype.getArtifactId() );
 219  
 
 220  2
         String repository = archetype.getRepository();
 221  2
         if ( StringUtils.isNotEmpty( repository ) )
 222  
         {
 223  0
             definition.setRepository( repository );
 224  
         }
 225  2
         else if ( catalogKey.indexOf( ':' ) > 1 )
 226  
         {
 227  
             // file: or http:
 228  0
             int lastIndex = catalogKey.lastIndexOf( '/' );
 229  0
             String catalogBase = catalogKey.substring( 0, ( lastIndex > 7 ? lastIndex : catalogKey.length() ) );
 230  0
             definition.setRepository( catalogBase );
 231  
         }
 232  
 
 233  2
         definition.setGoals( StringUtils.join( archetype.getGoals().iterator(), "," ) );
 234  2
     }
 235  
 
 236  
     public void setArchetypeSelectionQueryer( ArchetypeSelectionQueryer archetypeSelectionQueryer )
 237  
     {
 238  5
         this.archetypeSelectionQueryer = archetypeSelectionQueryer;
 239  5
     }
 240  
 
 241  
     private String getCatalogKey( Map<String, List<Archetype>> archetypes, Archetype selectedArchetype )
 242  
     {
 243  2
         for ( Map.Entry<String, List<Archetype>> entry : archetypes.entrySet() )
 244  
         {
 245  2
             List<Archetype> catalog = entry.getValue();
 246  
 
 247  2
             if ( catalog.contains( selectedArchetype ) )
 248  
             {
 249  0
                 return entry.getKey();
 250  
             }
 251  2
         }
 252  2
         return "";
 253  
     }
 254  
 
 255  
     private Map.Entry<String, Archetype> findArchetype( Map<String, List<Archetype>> archetypes, String groupId,
 256  
                                                         String artifactId )
 257  
     {
 258  1
         Archetype example = new Archetype();
 259  1
         example.setGroupId( groupId );
 260  1
         example.setArtifactId( artifactId );
 261  
 
 262  1
         for ( Map.Entry<String, List<Archetype>> entry : archetypes.entrySet() )
 263  
         {
 264  1
             List<Archetype> catalog = entry.getValue();
 265  
 
 266  1
             if ( catalog.contains( example ) )
 267  
             {
 268  0
                 Archetype archetype = catalog.get( catalog.indexOf( example ) );
 269  
 
 270  0
                 return newMapEntry( entry.getKey(), archetype );
 271  
             }
 272  1
         }
 273  
 
 274  1
         return null;
 275  
     }
 276  
     
 277  
     private static <K, V> Map.Entry<K, V> newMapEntry( K key, V value )
 278  
     {
 279  0
         Map<K, V> map = new HashMap<K, V>( 1 );
 280  0
         map.put( key, value );
 281  
 
 282  0
         return map.entrySet().iterator().next();
 283  
     }
 284  
 }