View Javadoc

1   package org.apache.continuum.buildagent.manager;
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 java.io.File;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
28  import org.apache.continuum.buildagent.installation.BuildAgentInstallationService;
29  import org.apache.continuum.buildagent.utils.ContinuumBuildAgentUtil;
30  import org.apache.continuum.model.repository.LocalRepository;
31  import org.apache.continuum.release.config.ContinuumReleaseDescriptor;
32  import org.apache.maven.continuum.model.project.Project;
33  import org.apache.maven.continuum.model.project.ProjectGroup;
34  import org.apache.maven.continuum.release.ContinuumReleaseException;
35  import org.apache.maven.continuum.release.ContinuumReleaseManager;
36  import org.apache.maven.continuum.release.ContinuumReleaseManagerListener;
37  import org.apache.maven.continuum.release.DefaultReleaseManagerListener;
38  import org.apache.maven.shared.release.ReleaseResult;
39  import org.apache.maven.shared.release.config.ReleaseDescriptor;
40  import org.codehaus.plexus.util.StringUtils;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  /**
45   * @plexus.component role="org.apache.continuum.buildagent.manager.BuildAgentReleaseManager" role-hint="default"
46   */
47  public class DefaultBuildAgentReleaseManager
48      implements BuildAgentReleaseManager
49  {
50      private static final Logger log = LoggerFactory.getLogger( DefaultBuildAgentReleaseManager.class );
51  
52      /**
53       * @plexus.requirement
54       */
55      ContinuumReleaseManager releaseManager;
56  
57      /**
58       * @plexus.requirement
59       */
60      BuildAgentConfigurationService buildAgentConfigurationService;
61  
62      /**
63       * @plexus.requirement
64       */
65      BuildAgentInstallationService buildAgentInstallationService;
66  
67      public String releasePrepare( Map<String, Object> projectMap, Map<String, Object> properties,
68                                    Map<String, String> releaseVersion, Map<String, String> developmentVersion,
69                                    Map<String, String> environments )
70          throws ContinuumReleaseException
71      {
72          Project project = getProject( projectMap );
73  
74          Properties releaseProperties = getReleaseProperties( properties );
75  
76          ContinuumReleaseManagerListener listener = new DefaultReleaseManagerListener();
77  
78          String workingDirectory = buildAgentConfigurationService.getWorkingDirectory( project.getId() ).getPath();
79  
80          String executable = buildAgentInstallationService.getExecutorConfigurator(
81              BuildAgentInstallationService.MAVEN2_TYPE ).getExecutable();
82  
83          if ( environments != null )
84          {
85              String m2Home =
86                  environments.get( buildAgentInstallationService.getEnvVar( BuildAgentInstallationService.MAVEN2_TYPE ) )
87                  ;
88              if ( StringUtils.isNotEmpty( m2Home ) )
89              {
90                  executable = m2Home + File.separator + "bin" + File.separator + executable;
91              }
92          }
93  
94          try
95          {
96              return releaseManager.prepare( project, releaseProperties, releaseVersion, developmentVersion, listener,
97                                             workingDirectory, environments, executable );
98          }
99          catch ( ContinuumReleaseException e )
100         {
101             log.error( "Error while preparing release" );
102             throw e;
103         }
104     }
105 
106     public ReleaseResult getReleaseResult( String releaseId )
107     {
108         return (ReleaseResult) releaseManager.getReleaseResults().get( releaseId );
109     }
110 
111     public Map<String, Object> getListener( String releaseId )
112     {
113         ContinuumReleaseManagerListener listener =
114             (ContinuumReleaseManagerListener) releaseManager.getListeners().get( releaseId );
115 
116         Map<String, Object> map = new HashMap<String, Object>();
117 
118         if ( listener != null )
119         {
120             map.put( ContinuumBuildAgentUtil.KEY_RELEASE_STATE, listener.getState() );
121             if ( listener.getPhases() != null )
122             {
123                 map.put( ContinuumBuildAgentUtil.KEY_RELEASE_PHASES, listener.getPhases() );
124             }
125             if ( listener.getCompletedPhases() != null )
126             {
127                 map.put( ContinuumBuildAgentUtil.KEY_COMPLETED_RELEASE_PHASES, listener.getCompletedPhases() );
128             }
129             if ( listener.getInProgress() != null )
130             {
131                 map.put( ContinuumBuildAgentUtil.KEY_RELEASE_IN_PROGRESS, listener.getInProgress() );
132             }
133             if ( listener.getError() != null )
134             {
135                 map.put( ContinuumBuildAgentUtil.KEY_RELEASE_ERROR, listener.getError() );
136             }
137         }
138 
139         return map;
140     }
141 
142     public void removeListener( String releaseId )
143     {
144         releaseManager.getListeners().remove( releaseId );
145     }
146 
147     public String getPreparedReleaseName( String releaseId )
148     {
149         Map preparedReleases = releaseManager.getPreparedReleases();
150 
151         if ( preparedReleases.containsKey( releaseId ) )
152         {
153             ReleaseDescriptor descriptor = (ReleaseDescriptor) preparedReleases.get( releaseId );
154             return descriptor.getReleaseVersions().get( releaseId ).toString();
155         }
156 
157         return "";
158     }
159 
160     public void releasePerform( String releaseId, String goals, String arguments, boolean useReleaseProfile,
161                                 Map repository )
162         throws ContinuumReleaseException
163     {
164         ContinuumReleaseManagerListener listener = new DefaultReleaseManagerListener();
165 
166         LocalRepository repo = null;
167 
168         if ( !repository.isEmpty() )
169         {
170             repo = new LocalRepository();
171             repo.setLayout( ContinuumBuildAgentUtil.getLocalRepositoryLayout( repository ) );
172             repo.setName( ContinuumBuildAgentUtil.getLocalRepositoryName( repository ) );
173             repo.setLocation( ContinuumBuildAgentUtil.getLocalRepository( repository ) );
174         }
175 
176         File performDirectory =
177             new File( buildAgentConfigurationService.getWorkingDirectory(), "releases-" + System.currentTimeMillis() );
178         performDirectory.mkdirs();
179 
180         releaseManager.perform( releaseId, performDirectory, goals, arguments, useReleaseProfile, listener, repo );
181     }
182 
183     public String releasePerformFromScm( String goals, String arguments, boolean useReleaseProfile, Map repository,
184                                          String scmUrl, String scmUsername, String scmPassword, String scmTag,
185                                          String scmTagBase, Map<String, String> environments )
186         throws ContinuumReleaseException
187     {
188         ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
189         descriptor.setScmSourceUrl( scmUrl );
190         descriptor.setScmUsername( scmUsername );
191         descriptor.setScmPassword( scmPassword );
192         descriptor.setScmReleaseLabel( scmTag );
193         descriptor.setScmTagBase( scmTagBase );
194         descriptor.setEnvironments( environments );
195 
196         String releaseId = "";
197 
198         do
199         {
200             releaseId = String.valueOf( System.currentTimeMillis() );
201         }
202         while ( releaseManager.getPreparedReleases().containsKey( releaseId ) );
203 
204         releaseManager.getPreparedReleases().put( releaseId, descriptor );
205 
206         releasePerform( releaseId, goals, arguments, useReleaseProfile, repository );
207 
208         return releaseId;
209     }
210 
211     public String releaseCleanup( String releaseId )
212     {
213         releaseManager.getReleaseResults().remove( releaseId );
214 
215         ContinuumReleaseManagerListener listener =
216             (ContinuumReleaseManagerListener) releaseManager.getListeners().remove( releaseId );
217 
218         if ( listener != null )
219         {
220             return listener.getGoalName() + "Finished";
221         }
222         else
223         {
224             return "";
225         }
226     }
227 
228     public void releaseRollback( String releaseId, int projectId )
229         throws ContinuumReleaseException
230     {
231         ContinuumReleaseManagerListener listener = new DefaultReleaseManagerListener();
232 
233         releaseManager.rollback( releaseId, buildAgentConfigurationService.getWorkingDirectory( projectId ).getPath(),
234                                  listener );
235 
236         //recurse until rollback is finished
237         while ( listener.getState() != ContinuumReleaseManagerListener.FINISHED )
238         {
239             try
240             {
241                 Thread.sleep( 1000 );
242             }
243             catch ( InterruptedException e )
244             {
245                 //do nothing
246             }
247         }
248 
249         releaseManager.getPreparedReleases().remove( releaseId );
250     }
251 
252     private Project getProject( Map<String, Object> context )
253     {
254         Project project = new Project();
255 
256         project.setId( ContinuumBuildAgentUtil.getProjectId( context ) );
257         project.setGroupId( ContinuumBuildAgentUtil.getGroupId( context ) );
258         project.setArtifactId( ContinuumBuildAgentUtil.getArtifactId( context ) );
259         project.setScmUrl( ContinuumBuildAgentUtil.getScmUrl( context ) );
260 
261         ProjectGroup group = new ProjectGroup();
262 
263         String localRepo = ContinuumBuildAgentUtil.getLocalRepository( context );
264         if ( StringUtils.isBlank( localRepo ) )
265         {
266             group.setLocalRepository( null );
267         }
268         else
269         {
270             LocalRepository localRepository = new LocalRepository();
271             localRepository.setLocation( localRepo );
272             group.setLocalRepository( localRepository );
273         }
274 
275         project.setProjectGroup( group );
276 
277         return project;
278     }
279 
280     private Properties getReleaseProperties( Map<String, Object> context )
281     {
282         Properties props = new Properties();
283 
284         String prop = ContinuumBuildAgentUtil.getScmUsername( context );
285         if ( StringUtils.isNotBlank( prop ) )
286         {
287             props.put( "username", prop );
288         }
289 
290         prop = ContinuumBuildAgentUtil.getScmPassword( context );
291         if ( StringUtils.isNotBlank( prop ) )
292         {
293             props.put( "password", prop );
294         }
295 
296         prop = ContinuumBuildAgentUtil.getScmTagBase( context );
297         if ( StringUtils.isNotBlank( prop ) )
298         {
299             props.put( "tagBase", prop );
300         }
301 
302         prop = ContinuumBuildAgentUtil.getScmCommentPrefix( context );
303         if ( StringUtils.isNotBlank( prop ) )
304         {
305             props.put( "commentPrefix", prop );
306         }
307 
308         prop = ContinuumBuildAgentUtil.getScmTag( context );
309         if ( StringUtils.isNotBlank( prop ) )
310         {
311             props.put( "tag", prop );
312         }
313 
314         prop = ContinuumBuildAgentUtil.getPrepareGoals( context );
315         if ( StringUtils.isNotBlank( prop ) )
316         {
317             props.put( "prepareGoals", prop );
318         }
319 
320         prop = ContinuumBuildAgentUtil.getArguments( context );
321         if ( StringUtils.isNotBlank( prop ) )
322         {
323             props.put( "arguments", prop );
324         }
325 
326         prop = ContinuumBuildAgentUtil.getUseEditMode( context );
327         if ( StringUtils.isNotBlank( prop ) )
328         {
329             props.put( "useEditMode", prop );
330         }
331 
332         prop = ContinuumBuildAgentUtil.getAddSchema( context );
333         if ( StringUtils.isNotBlank( prop ) )
334         {
335             props.put( "addSchema", prop );
336         }
337 
338         prop = ContinuumBuildAgentUtil.getAutoVersionSubmodules( context );
339         if ( StringUtils.isNotBlank( prop ) )
340         {
341             props.put( "autoVersionSubmodules", prop );
342         }
343         return props;
344     }
345 
346 
347 }