1 package org.apache.maven.archetype.source;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.util.Iterator;
26
27 import org.apache.maven.archetype.catalog.Archetype;
28 import org.apache.maven.archetype.catalog.ArchetypeCatalog;
29 import org.apache.maven.project.ProjectBuildingRequest;
30 import org.apache.maven.shared.repository.RepositoryManager;
31
32 import org.codehaus.plexus.component.annotations.Component;
33 import org.codehaus.plexus.component.annotations.Requirement;
34 import org.codehaus.plexus.util.ReaderFactory;
35
36 @Component( role = ArchetypeDataSource.class, hint = "catalog" )
37 public class LocalCatalogArchetypeDataSource
38 extends CatalogArchetypeDataSource
39 {
40 @Requirement
41 private RepositoryManager repositoryManager;
42
43 public void updateCatalog( ProjectBuildingRequest buildingRequest, Archetype archetype )
44 throws ArchetypeDataSourceException
45 {
46 File localRepo = repositoryManager.getLocalRepositoryBasedir( buildingRequest );
47
48 File catalogFile = new File( localRepo, ARCHETYPE_CATALOG_FILENAME );
49
50 getLogger().debug( "Using catalog " + catalogFile.getAbsolutePath() );
51
52 ArchetypeCatalog catalog;
53 if ( catalogFile.exists() )
54 {
55 try
56 {
57 getLogger().debug( "Reading the catalog " + catalogFile );
58 catalog = readCatalog( ReaderFactory.newXmlReader( catalogFile ) );
59 }
60 catch ( FileNotFoundException ex )
61 {
62 getLogger().debug( "Catalog file don't exist" );
63 catalog = new ArchetypeCatalog();
64 }
65 catch ( IOException e )
66 {
67 throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
68 }
69 }
70 else
71 {
72 getLogger().debug( "Catalog file don't exist" );
73 catalog = new ArchetypeCatalog();
74 }
75
76 Iterator<Archetype> archetypes = catalog.getArchetypes().iterator();
77 boolean found = false;
78 Archetype newArchetype = archetype;
79 while ( !found && archetypes.hasNext() )
80 {
81 Archetype a = archetypes.next();
82 if ( a.getGroupId().equals( archetype.getGroupId() )
83 && a.getArtifactId().equals( archetype.getArtifactId() ) )
84 {
85 newArchetype = a;
86 found = true;
87 }
88 }
89 if ( !found )
90 {
91 catalog.addArchetype( newArchetype );
92 }
93
94 newArchetype.setVersion( archetype.getVersion() );
95 newArchetype.setRepository( archetype.getRepository() );
96 newArchetype.setDescription( archetype.getDescription() );
97 newArchetype.setProperties( archetype.getProperties() );
98 newArchetype.setGoals( archetype.getGoals() );
99
100 writeLocalCatalog( catalog, catalogFile );
101 }
102
103 public ArchetypeCatalog getArchetypeCatalog( ProjectBuildingRequest buildingRequest )
104 throws ArchetypeDataSourceException
105 {
106 File localRepo = repositoryManager.getLocalRepositoryBasedir( buildingRequest );
107
108 File catalogFile = new File( localRepo, ARCHETYPE_CATALOG_FILENAME );
109
110 if ( catalogFile.exists() && catalogFile.isDirectory() )
111 {
112 catalogFile = new File( catalogFile, ARCHETYPE_CATALOG_FILENAME );
113 }
114 getLogger().debug( "Using catalog " + catalogFile );
115
116 if ( catalogFile.exists() )
117 {
118 try
119 {
120 return readCatalog( ReaderFactory.newXmlReader( catalogFile ) );
121 }
122 catch ( FileNotFoundException e )
123 {
124 throw new ArchetypeDataSourceException( "The specific archetype catalog does not exist.", e );
125 }
126 catch ( IOException e )
127 {
128 throw new ArchetypeDataSourceException( "Error reading archetype catalog.", e );
129 }
130 }
131 else
132 {
133 return new ArchetypeCatalog();
134 }
135 }
136 }