View Javadoc

1   package org.apache.maven.continuum.web.action;
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.continuum.configuration.BuildAgentConfigurationException;
23  import org.apache.continuum.model.repository.LocalRepository;
24  import org.apache.continuum.release.config.ContinuumReleaseDescriptor;
25  import org.apache.continuum.release.distributed.DistributedReleaseUtil;
26  import org.apache.continuum.release.distributed.manager.DistributedReleaseManager;
27  import org.apache.continuum.web.action.AbstractReleaseAction;
28  import org.apache.continuum.web.util.AuditLog;
29  import org.apache.continuum.web.util.AuditLogConstants;
30  import org.apache.maven.continuum.ContinuumException;
31  import org.apache.maven.continuum.model.project.Project;
32  import org.apache.maven.continuum.model.system.Profile;
33  import org.apache.maven.continuum.release.ContinuumReleaseManager;
34  import org.apache.maven.continuum.release.ContinuumReleaseManagerListener;
35  import org.apache.maven.continuum.release.DefaultReleaseManagerListener;
36  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
37  import org.apache.maven.model.Model;
38  import org.apache.maven.model.Plugin;
39  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
40  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
41  import org.apache.maven.shared.release.ReleaseResult;
42  import org.codehaus.plexus.util.xml.Xpp3Dom;
43  
44  import java.io.File;
45  import java.io.FileReader;
46  import java.util.ArrayList;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Map;
50  
51  /**
52   * @author Edwin Punzalan
53   * @version $Id: ReleasePerformAction.java 781924 2009-06-05 06:42:54Z ctan $
54   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="releasePerform"
55   */
56  public class ReleasePerformAction
57      extends AbstractReleaseAction
58  {
59      private int projectId;
60  
61      private String releaseId;
62  
63      private String scmUrl;
64  
65      private String scmUsername;
66  
67      private String scmPassword;
68  
69      private String scmTag;
70  
71      private String scmTagBase;
72  
73      private String goals = "clean deploy";
74  
75      private String arguments;
76  
77      private boolean useReleaseProfile = true;
78  
79      private ContinuumReleaseManagerListener listener;
80  
81      private ReleaseResult result;
82  
83      private String projectGroupName = "";
84  
85      private List<Profile> profiles;
86  
87      private int profileId;
88  
89      private void init()
90          throws Exception
91      {
92          if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
93          {
94              DistributedReleaseManager distributedReleaseManager = getContinuum().getDistributedReleaseManager();
95  
96              getReleasePluginParameters( distributedReleaseManager.getReleasePluginParameters( projectId, "pom.xml" ) );
97          }
98          else
99          {
100             Project project = getContinuum().getProject( projectId );
101     
102             String workingDirectory = getContinuum().getWorkingDirectory( project.getId() ).getPath();
103     
104             getReleasePluginParameters( workingDirectory, "pom.xml" );
105         }
106     }
107 
108     public String inputFromScm()
109         throws Exception
110     {
111         try
112         {
113             checkBuildProjectInGroupAuthorization( getProjectGroupName() );
114         }
115         catch ( AuthorizationRequiredException e )
116         {
117             return REQUIRES_AUTHORIZATION;
118         }
119 
120         try
121         {
122             init();
123         }
124         catch ( BuildAgentConfigurationException e )
125         {
126             List<String> args = new ArrayList<String>();
127             args.add( e.getMessage() );
128 
129             addActionError( getText( "distributedBuild.releasePerform.input.error", args ) ) ;
130             return ERROR;
131         }
132 
133         populateFromProject();
134 
135         releaseId = "";
136 
137         profiles = this.getContinuum().getProfileService().getAllProfiles();
138 
139         return SUCCESS;
140     }
141 
142     public String input()
143         throws Exception
144     {
145         try
146         {
147             checkBuildProjectInGroupAuthorization( getProjectGroupName() );
148         }
149         catch ( AuthorizationRequiredException e )
150         {
151             return REQUIRES_AUTHORIZATION;
152         }
153 
154         try
155         {
156             init();
157         }
158         catch ( BuildAgentConfigurationException e )
159         {
160             List<String> args = new ArrayList<String>();
161             args.add( e.getMessage() );
162 
163             addActionError( getText( "distributedBuild.releasePerform.input.error", args ) ) ;
164             return ERROR;
165         }
166 
167 
168         return SUCCESS;
169     }
170 
171     private void getReleasePluginParameters( String workingDirectory, String pomFilename )
172         throws Exception
173     {
174         //TODO: Use the model reader so we'll can get the plugin configuration from parent too
175         MavenXpp3Reader pomReader = new MavenXpp3Reader();
176         Model model = pomReader.read( new FileReader( new File( workingDirectory, pomFilename ) ) );
177 
178         if ( model.getBuild() != null && model.getBuild().getPlugins() != null )
179         {
180             for ( Plugin plugin : (List<Plugin>) model.getBuild().getPlugins() )
181             {
182                 if ( plugin.getGroupId() != null && plugin.getGroupId().equals( "org.apache.maven.plugins" ) &&
183                     plugin.getArtifactId() != null && plugin.getArtifactId().equals( "maven-release-plugin" ) )
184                 {
185                     Xpp3Dom dom = (Xpp3Dom) plugin.getConfiguration();
186 
187                     if ( dom != null )
188                     {
189                         Xpp3Dom configuration = dom.getChild( "useReleaseProfile" );
190                         if ( configuration != null )
191                         {
192                             useReleaseProfile = Boolean.valueOf( configuration.getValue() );
193                         }
194 
195                         configuration = dom.getChild( "goals" );
196                         if ( configuration != null )
197                         {
198                             goals = configuration.getValue();
199                             if ( model.getDistributionManagement() != null &&
200                                 model.getDistributionManagement().getSite() != null )
201                             {
202                                 goals += " site-deploy";
203                             }
204                         }
205 
206                         configuration = dom.getChild( "arguments" );
207                         if ( configuration != null )
208                         {
209                             arguments = configuration.getValue();
210                         }
211 
212                     }
213                 }
214             }
215         }
216     }
217 
218     public String execute()
219         throws Exception
220     {
221         try
222         {
223             checkBuildProjectInGroupAuthorization( getProjectGroupName() );
224         }
225         catch ( AuthorizationRequiredException e )
226         {
227             return REQUIRES_AUTHORIZATION;
228         }
229 
230         Project project = getContinuum().getProject( projectId );
231 
232         LocalRepository repository = project.getProjectGroup().getLocalRepository();
233 
234         if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
235         {
236             DistributedReleaseManager releaseManager = getContinuum().getDistributedReleaseManager();
237 
238             try
239             {
240                 releaseManager.releasePerform( projectId, releaseId, goals, arguments, useReleaseProfile, repository );
241             }
242             catch ( BuildAgentConfigurationException e )
243             {
244                 List<String> args = new ArrayList<String>();
245                 args.add( e.getMessage() );
246 
247                 addActionError( getText( "distributedBuild.releasePerform.release.error", args ) );
248                 return ERROR;
249             }
250         }
251         else
252         {
253             listener = new DefaultReleaseManagerListener();
254     
255             ContinuumReleaseManager releaseManager = getContinuum().getReleaseManager();
256 
257             //todo should be configurable
258             File performDirectory = new File( getContinuum().getConfiguration().getWorkingDirectory(),
259                                               "releases-" + System.currentTimeMillis() );
260             performDirectory.mkdirs();
261 
262             releaseManager.perform( releaseId, performDirectory, goals, arguments, useReleaseProfile, listener,
263                                     repository );
264         }
265 
266         AuditLog event = new AuditLog( "ReleaseId=" + releaseId, AuditLogConstants.PERFORM_RELEASE );
267         event.setCategory( AuditLogConstants.PROJECT );
268         event.setCurrentUser( getPrincipal() );
269         event.log();
270 
271         return SUCCESS;
272     }
273 
274     public String executeFromScm()
275         throws Exception
276     {
277         if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
278         {
279             Project project = getContinuum().getProject( projectId );
280 
281             LocalRepository repository = project.getProjectGroup().getLocalRepository();
282 
283             DistributedReleaseManager releaseManager = getContinuum().getDistributedReleaseManager();
284             Map<String, String> environments = new HashMap<String, String>();
285             
286             if ( profileId != -1 )
287             {
288                 Profile profile = getContinuum().getProfileService().getProfile( profileId );
289                 environments = getEnvironments( profile );
290             }
291 
292             try
293             {
294                 releaseManager.releasePerformFromScm( projectId, goals, arguments, useReleaseProfile, repository, scmUrl, 
295                                                       scmUsername, scmPassword, scmTag, scmTagBase, environments );
296             }
297             catch ( BuildAgentConfigurationException e )
298             {
299                 List<String> args = new ArrayList<String>();
300                 args.add( e.getMessage() );
301 
302                 addActionError( getText( "distributedBuild.releasePerform.release.error", args ) );
303                 return ERROR;
304             }
305 
306             return SUCCESS;
307         }
308         else
309         {
310             ContinuumReleaseManager releaseManager = getContinuum().getReleaseManager();
311     
312             ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
313             descriptor.setScmSourceUrl( scmUrl );
314             descriptor.setScmUsername( scmUsername );
315             descriptor.setScmPassword( scmPassword );
316             descriptor.setScmReleaseLabel( scmTag );
317             descriptor.setScmTagBase( scmTagBase );
318     
319             if ( profileId != -1 )
320             {
321                 Profile profile = getContinuum().getProfileService().getProfile( profileId );
322                 descriptor.setEnvironments( getEnvironments( profile ) );
323             }
324     
325             do
326             {
327                 releaseId = String.valueOf( System.currentTimeMillis() );
328             }
329             while ( releaseManager.getPreparedReleases().containsKey( releaseId ) );
330     
331             releaseManager.getPreparedReleases().put( releaseId, descriptor );
332     
333             return execute();
334         }
335     }
336 
337     private void populateFromProject()
338         throws Exception
339     {
340         Project project = getContinuum().getProjectWithAllDetails( projectId );
341 
342         scmUrl = project.getScmUrl();
343         scmUsername = project.getScmUsername();
344         scmPassword = project.getScmPassword();
345 
346         if ( scmUrl.startsWith( "scm:svn:" ) )
347         {
348             scmTagBase = new SvnScmProviderRepository( scmUrl, scmUsername, scmPassword ).getTagBase();
349         }
350         else
351         {
352             scmTagBase = "";
353         }
354 
355         releaseId = "";
356     }
357 
358     private void getReleasePluginParameters( Map context )
359     {
360         useReleaseProfile = DistributedReleaseUtil.getUseReleaseProfile( context, useReleaseProfile );
361 
362         goals = DistributedReleaseUtil.getGoals( context, goals );
363 
364         arguments = DistributedReleaseUtil.getArguments( context, "" );
365     }
366 
367     public String getReleaseId()
368     {
369         return releaseId;
370     }
371 
372     public void setReleaseId( String releaseId )
373     {
374         this.releaseId = releaseId;
375     }
376 
377     public String getScmUrl()
378     {
379         return scmUrl;
380     }
381 
382     public void setScmUrl( String scmUrl )
383     {
384         this.scmUrl = scmUrl;
385     }
386 
387     public String getScmUsername()
388     {
389         return scmUsername;
390     }
391 
392     public void setScmUsername( String scmUsername )
393     {
394         this.scmUsername = scmUsername;
395     }
396 
397     public String getScmPassword()
398     {
399         return scmPassword;
400     }
401 
402     public void setScmPassword( String scmPassword )
403     {
404         this.scmPassword = scmPassword;
405     }
406 
407     public String getScmTag()
408     {
409         return scmTag;
410     }
411 
412     public void setScmTag( String scmTag )
413     {
414         this.scmTag = scmTag;
415     }
416 
417     public String getScmTagBase()
418     {
419         return scmTagBase;
420     }
421 
422     public void setScmTagBase( String scmTagBase )
423     {
424         this.scmTagBase = scmTagBase;
425     }
426 
427     public String getGoals()
428     {
429         return goals;
430     }
431 
432     public void setGoals( String goals )
433     {
434         this.goals = goals;
435     }
436 
437     public boolean isUseReleaseProfile()
438     {
439         return useReleaseProfile;
440     }
441 
442     public void setUseReleaseProfile( boolean useReleaseProfile )
443     {
444         this.useReleaseProfile = useReleaseProfile;
445     }
446 
447     public ContinuumReleaseManagerListener getListener()
448     {
449         return listener;
450     }
451 
452     public void setListener( ContinuumReleaseManagerListener listener )
453     {
454         this.listener = listener;
455     }
456 
457     public ReleaseResult getResult()
458     {
459         return result;
460     }
461 
462     public void setResult( ReleaseResult result )
463     {
464         this.result = result;
465     }
466 
467     public int getProjectId()
468     {
469         return projectId;
470     }
471 
472     public void setProjectId( int projectId )
473     {
474         this.projectId = projectId;
475     }
476 
477     public String getProjectGroupName()
478         throws ContinuumException
479     {
480         if ( projectGroupName == null || "".equals( projectGroupName ) )
481         {
482             projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
483         }
484 
485         return projectGroupName;
486     }
487 
488     public List<Profile> getProfiles()
489     {
490         return profiles;
491     }
492 
493     public void setProfiles( List<Profile> profiles )
494     {
495         this.profiles = profiles;
496     }
497 
498     public int getProfileId()
499     {
500         return profileId;
501     }
502 
503     public void setProfileId( int profileId )
504     {
505         this.profileId = profileId;
506     }
507 
508     public String getArguments()
509     {
510         return arguments;
511     }
512 
513     public void setArguments( String arguments )
514     {
515         this.arguments = arguments;
516     }
517 }