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 org.apache.maven.archetype.catalog.Archetype; |
23 | |
import org.apache.maven.archetype.catalog.ArchetypeCatalog; |
24 | |
import org.apache.maven.artifact.manager.WagonManager; |
25 | |
import org.apache.maven.wagon.Wagon; |
26 | |
import org.apache.maven.wagon.WagonException; |
27 | |
import org.apache.maven.wagon.authentication.AuthenticationInfo; |
28 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
29 | |
import org.apache.maven.wagon.repository.Repository; |
30 | |
import org.codehaus.plexus.component.annotations.Component; |
31 | |
import org.codehaus.plexus.component.annotations.Requirement; |
32 | |
import org.codehaus.plexus.util.ReaderFactory; |
33 | |
|
34 | |
import java.io.File; |
35 | |
import java.io.IOException; |
36 | |
import java.util.Properties; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
@Component( role = ArchetypeDataSource.class, hint = "remote-catalog" ) |
42 | 12 | public class RemoteCatalogArchetypeDataSource |
43 | |
extends CatalogArchetypeDataSource |
44 | |
{ |
45 | |
@Requirement |
46 | |
private WagonManager wagonManager; |
47 | |
|
48 | |
public static final String REPOSITORY_PROPERTY = "repository"; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static final String REPOSITORY_ID = "archetype"; |
55 | |
|
56 | |
public ArchetypeCatalog getArchetypeCatalog( Properties properties ) |
57 | |
throws ArchetypeDataSourceException |
58 | |
{ |
59 | 2 | String repository = properties.getProperty( REPOSITORY_PROPERTY ); |
60 | |
|
61 | 2 | if ( repository == null ) |
62 | |
{ |
63 | 0 | throw new ArchetypeDataSourceException( "To use the remote catalog you must specify the 'repository'" |
64 | |
+ " property with an URL." ); |
65 | |
} |
66 | |
|
67 | 2 | if ( repository.endsWith( "/" ) ) |
68 | |
{ |
69 | 2 | repository = repository.substring( 0, repository.length() - 1 ); |
70 | |
} |
71 | |
|
72 | |
try |
73 | |
{ |
74 | 2 | return downloadCatalog( repository, ARCHETYPE_CATALOG_FILENAME ); |
75 | |
} |
76 | 0 | catch ( ArchetypeDataSourceException e ) |
77 | |
{ |
78 | 0 | throw e; |
79 | |
} |
80 | 0 | catch ( Exception e ) |
81 | |
{ |
82 | 0 | String repositoryPath = repository.substring( 0, repository.lastIndexOf( "/" ) ); |
83 | 0 | String filename = repository.substring( repository.lastIndexOf( "/" ) + 1 ); |
84 | |
|
85 | |
try |
86 | |
{ |
87 | 0 | return downloadCatalog( repositoryPath, filename ); |
88 | |
} |
89 | 0 | catch ( Exception ex ) |
90 | |
{ |
91 | 0 | getLogger().warn( "Error reading archetype catalog " + repository, ex ); |
92 | 0 | return new ArchetypeCatalog(); |
93 | |
} |
94 | |
} |
95 | |
} |
96 | |
|
97 | |
public void updateCatalog( Properties properties, Archetype archetype ) |
98 | |
throws ArchetypeDataSourceException |
99 | |
{ |
100 | 0 | throw new ArchetypeDataSourceException( "Not supported yet." ); |
101 | |
} |
102 | |
|
103 | |
private ArchetypeCatalog downloadCatalog( String repositoryPath, String filename ) |
104 | |
throws WagonException, IOException, ArchetypeDataSourceException |
105 | |
{ |
106 | 2 | getLogger().debug( "Searching for remote catalog: " + repositoryPath + "/" + filename ); |
107 | |
|
108 | |
|
109 | 2 | Repository wagonRepository = new Repository( REPOSITORY_ID, repositoryPath ); |
110 | 2 | AuthenticationInfo authInfo = wagonManager.getAuthenticationInfo( wagonRepository.getId() ); |
111 | 2 | ProxyInfo proxyInfo = wagonManager.getProxy( wagonRepository.getProtocol() ); |
112 | |
|
113 | 2 | Wagon wagon = wagonManager.getWagon( wagonRepository ); |
114 | |
|
115 | 2 | File catalog = File.createTempFile( "archetype-catalog", ".xml" ); |
116 | |
try |
117 | |
{ |
118 | 2 | wagon.connect( wagonRepository, authInfo, proxyInfo ); |
119 | 2 | wagon.get( filename, catalog ); |
120 | |
|
121 | 2 | return readCatalog( ReaderFactory.newXmlReader( catalog ) ); |
122 | |
} |
123 | |
finally |
124 | |
{ |
125 | 2 | disconnectWagon( wagon ); |
126 | 2 | catalog.delete(); |
127 | |
} |
128 | |
} |
129 | |
|
130 | |
private void disconnectWagon( Wagon wagon ) |
131 | |
{ |
132 | |
try |
133 | |
{ |
134 | 2 | wagon.disconnect(); |
135 | |
} |
136 | 0 | catch ( Exception e ) |
137 | |
{ |
138 | 0 | getLogger().warn( "Problem disconnecting from wagon - ignoring: " + e.getMessage() ); |
139 | 2 | } |
140 | 2 | } |
141 | |
|
142 | |
} |