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