1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.felix.obrplugin;
20  
21  
22  import java.io.File;
23  import java.net.URI;
24  import java.net.URL;
25  import java.util.Arrays;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.artifact.manager.WagonManager;
33  import org.apache.maven.artifact.repository.ArtifactRepository;
34  import org.apache.maven.plugin.AbstractMojo;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.logging.Log;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.settings.Settings;
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  public final class ObrDeploy extends AbstractMojo
51  {
52      
53  
54  
55  
56  
57      private boolean ignoreLock;
58  
59      
60  
61  
62  
63  
64      private String prefixUrl;
65  
66      
67  
68  
69  
70  
71      private String bundleUrl;
72  
73      
74  
75  
76  
77  
78      private String remoteOBR;
79  
80      
81  
82  
83  
84  
85      private String obrRepository;
86  
87      
88  
89  
90  
91  
92      private List supportedProjectTypes = Arrays.asList( new String[]
93          { "jar", "bundle" } );
94  
95      
96  
97  
98  
99      private ArtifactRepository deploymentRepository;
100 
101     
102 
103 
104 
105 
106     private String altDeploymentRepository;
107 
108     
109 
110 
111 
112 
113     private String obrDeploymentRepository;
114 
115     
116 
117 
118 
119 
120 
121 
122     private ArtifactRepository localRepository;
123 
124     
125 
126 
127 
128 
129 
130 
131     private MavenProject project;
132 
133     
134 
135 
136 
137 
138     private List attachedArtifacts;
139 
140     
141 
142 
143 
144 
145 
146 
147     private Settings settings;
148 
149     
150 
151 
152 
153 
154     private WagonManager m_wagonManager;
155 
156     
157 
158 
159     private Artifact m_sourceArtifact;
160 
161     
162 
163 
164     private Artifact m_docArtifact;
165 
166 
167     public void execute() throws MojoExecutionException
168     {
169         String projectType = project.getPackaging();
170 
171         
172         if ( !supportedProjectTypes.contains( projectType ) )
173         {
174             getLog().warn(
175                 "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
176             return;
177         }
178         else if ( "NONE".equalsIgnoreCase( remoteOBR ) || "false".equalsIgnoreCase( remoteOBR ) )
179         {
180             getLog().info( "Remote OBR update disabled (enable with -DremoteOBR)" );
181             return;
182         }
183 
184         
185         for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
186         {
187             Artifact artifact = ( Artifact ) i.next();
188             if ( "sources".equals( artifact.getClassifier() ) )
189             {
190                 m_sourceArtifact = artifact;
191             }
192             else if ( "javadoc".equals( artifact.getClassifier() ) )
193             {
194                 m_docArtifact = artifact;
195             }
196         }
197 
198         
199         if ( null == remoteOBR || remoteOBR.trim().length() == 0 || "true".equalsIgnoreCase( remoteOBR ) )
200         {
201             remoteOBR = obrRepository;
202         }
203 
204         URI tempURI = ObrUtils.findRepositoryXml( "", remoteOBR );
205         String repositoryName = new File( tempURI.getSchemeSpecificPart() ).getName();
206 
207         Log log = getLog();
208         ObrUpdate update;
209 
210         RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
211         openRepositoryConnection( remoteFile );
212 
213         
214         log.info( "LOCK " + remoteFile + '/' + repositoryName );
215         remoteFile.lockFile( repositoryName, ignoreLock );
216         File downloadedRepositoryXml = null;
217 
218         try
219         {
220             
221             log.info( "Downloading " + repositoryName );
222             downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" );
223 
224             String mavenRepository = localRepository.getBasedir();
225 
226             URI repositoryXml = downloadedRepositoryXml.toURI();
227             URI obrXmlFile = ObrUtils.findObrXml( project );
228 
229             Config userConfig = new Config();
230             userConfig.setRemoteFile( true );
231 
232             if ( bundleUrl != null )
233             {
234                 
235                 URI uri = URI.create( bundleUrl );
236                 log.info( "Computed bundle uri: " + uri );
237                 userConfig.setRemoteBundle( uri );
238             }
239             else if ( prefixUrl != null )
240             {
241                 
242                 URI bundleJar = ObrUtils.getArtifactURI( localRepository, project.getArtifact() );
243                 String relative = ObrUtils.getRelativeURI( ObrUtils.toFileURI( mavenRepository ), bundleJar )
244                     .toASCIIString();
245                 URL resourceURL = new URL( new URL( prefixUrl + '/' ), relative );
246                 URI uri = URI.create( resourceURL.toString() );
247                 log.info( "Computed bundle uri: " + uri );
248                 userConfig.setRemoteBundle( uri );
249             }
250 
251             update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
252             update.parseRepositoryXml();
253 
254             updateRemoteBundleMetadata( project.getArtifact(), update );
255             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
256             {
257                 updateRemoteBundleMetadata( ( Artifact ) i.next(), update );
258             }
259 
260             update.writeRepositoryXml();
261 
262             if ( downloadedRepositoryXml.exists() )
263             {
264                 
265                 log.info( "Uploading " + repositoryName );
266                 remoteFile.put( downloadedRepositoryXml, repositoryName );
267             }
268         }
269         catch ( Exception e )
270         {
271             log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e );
272         }
273         finally
274         {
275             
276             log.info( "UNLOCK " + remoteFile + '/' + repositoryName );
277             remoteFile.unlockFile( repositoryName );
278             remoteFile.disconnect();
279 
280             if ( null != downloadedRepositoryXml )
281             {
282                 downloadedRepositoryXml.delete();
283             }
284         }
285     }
286 
287     private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" );
288 
289 
290     private void openRepositoryConnection( RemoteFileManager remoteFile ) throws MojoExecutionException
291     {
292         
293         if ( obrDeploymentRepository != null )
294         {
295             altDeploymentRepository = obrDeploymentRepository;
296         }
297 
298         if ( deploymentRepository == null && altDeploymentRepository == null )
299         {
300             String msg = "Deployment failed: repository element was not specified in the pom inside"
301                 + " distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter";
302 
303             throw new MojoExecutionException( msg );
304         }
305 
306         if ( altDeploymentRepository != null )
307         {
308             getLog().info( "Using alternate deployment repository " + altDeploymentRepository );
309 
310             Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository );
311             if ( !matcher.matches() )
312             {
313                 throw new MojoExecutionException( "Invalid syntax for alternative repository \""
314                     + altDeploymentRepository + "\". Use \"id::layout::url\"." );
315             }
316 
317             remoteFile.connect( matcher.group( 1 ).trim(), matcher.group( 3 ).trim() );
318         }
319         else
320         {
321             remoteFile.connect( deploymentRepository.getId(), deploymentRepository.getUrl() );
322         }
323     }
324 
325 
326     private void updateRemoteBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
327     {
328         if ( !supportedProjectTypes.contains( artifact.getType() ) )
329         {
330             return;
331         }
332         else if ( null == artifact.getFile() || artifact.getFile().isDirectory() )
333         {
334             getLog().error( "No artifact found, try \"mvn install bundle:deploy\"" );
335             return;
336         }
337 
338         URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
339 
340         URI sourceJar = null;
341         if ( null != m_sourceArtifact )
342         {
343             sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
344         }
345 
346         URI docJar = null;
347         if ( null != m_docArtifact )
348         {
349             docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact );
350         }
351 
352         update.updateRepository( bundleJar, sourceJar, docJar );
353     }
354 }