View Javadoc

1   package org.apache.maven.continuum.core.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.dao.BuildDefinitionDao;
23  import org.apache.continuum.dao.ScheduleDao;
24  import org.apache.maven.continuum.ContinuumException;
25  import org.apache.maven.continuum.configuration.ConfigurationService;
26  import org.apache.maven.continuum.model.project.BuildDefinition;
27  import org.apache.maven.continuum.model.project.Project;
28  import org.apache.maven.continuum.model.project.ProjectGroup;
29  import org.apache.maven.continuum.model.project.Schedule;
30  import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
31  import org.apache.maven.continuum.store.ContinuumStoreException;
32  
33  import java.util.List;
34  
35  /**
36   * AbstractBuildDefinitionContinuumAction:
37   *
38   * @author Jesse McConnell <jmcconnell@apache.org>
39   * @version $Id: AbstractBuildDefinitionContinuumAction.java 1372260 2012-08-13 04:29:09Z brett $
40   */
41  public abstract class AbstractBuildDefinitionContinuumAction
42      extends AbstractContinuumAction
43  {
44      /**
45       * @plexus.requirement
46       */
47      private BuildDefinitionDao buildDefinitionDao;
48  
49      /**
50       * @plexus.requirement
51       */
52      private ScheduleDao scheduleDao;
53  
54      protected void resolveDefaultBuildDefinitionsForProject( BuildDefinition buildDefinition, Project project )
55          throws ContinuumException
56      {
57          try
58          {
59              // if buildDefinition passed in is not default then we are done
60              if ( buildDefinition.isDefaultForProject() )
61              {
62                  BuildDefinition storedDefinition = buildDefinitionDao.getDefaultBuildDefinitionForProject(
63                      project.getId() );
64  
65                  if ( storedDefinition != null )
66                  {
67                      storedDefinition.setDefaultForProject( false );
68  
69                      buildDefinitionDao.storeBuildDefinition( storedDefinition );
70                  }
71              }
72          }
73          catch ( ContinuumObjectNotFoundException nfe )
74          {
75              getLogger().debug( getClass().getName() +
76                                     ": safely ignoring the resetting of old build definition becuase it didn't exist" );
77          }
78          catch ( ContinuumStoreException cse )
79          {
80              throw new ContinuumException( "error updating old default build definition", cse );
81          }
82      }
83  
84      /**
85       * resolves build definition defaults between project groups and projects
86       * <p/>
87       * 1) project groups have default build definitions
88       * 2) if project has default build definition, that overrides project group definition
89       * 3) changing parent default build definition does not effect project if it has a default declared
90       * 4) project groups must have a default build definition
91       *
92       * @param buildDefinition
93       * @param projectGroup
94       * @throws ContinuumException
95       */
96      protected void resolveDefaultBuildDefinitionsForProjectGroup( BuildDefinition buildDefinition,
97                                                                    ProjectGroup projectGroup )
98          throws ContinuumException
99      {
100         try
101         {
102             List<BuildDefinition> storedDefinitions = buildDefinitionDao.getDefaultBuildDefinitionsForProjectGroup(
103                 projectGroup.getId() );
104 
105             for ( BuildDefinition storedDefinition : storedDefinitions )
106             {
107                 // if buildDefinition passed in is not default then we are done
108                 if ( buildDefinition.isDefaultForProject() )
109                 {
110                     if ( storedDefinition != null && storedDefinition.getId() != buildDefinition.getId() )
111                     {
112                         if ( buildDefinition.getType() != null && buildDefinition.getType().equals(
113                             storedDefinition.getType() ) )
114                         {
115                             //Required to get build def from store because storedDefinition is readonly
116                             BuildDefinition def = buildDefinitionDao.getBuildDefinition( storedDefinition.getId() );
117                             def.setDefaultForProject( false );
118 
119                             buildDefinitionDao.storeBuildDefinition( def );
120                         }
121                     }
122                 }
123                 else
124                 {
125                     //make sure we are not wacking out default build definition, that would be bad
126                     if ( buildDefinition.getId() == storedDefinition.getId() )
127                     {
128                         getLogger().info(
129                             "processing this build definition would result in no default build definition for project group" );
130                         throw new ContinuumException(
131                             "processing this build definition would result in no default build definition for project group" );
132                     }
133                 }
134             }
135         }
136         catch ( ContinuumStoreException cse )
137         {
138             getLogger().info( "error updating old default build definition", cse );
139             throw new ContinuumException( "error updating old default build definition", cse );
140         }
141     }
142 
143     /**
144      * attempts to walk through the list of build definitions and upon finding a match update it with the
145      * information in the BuildDefinition object passed in.
146      *
147      * @param buildDefinitions
148      * @param buildDefinition
149      * @throws ContinuumException
150      */
151     protected void updateBuildDefinitionInList( List<BuildDefinition> buildDefinitions,
152                                                 BuildDefinition buildDefinition )
153         throws ContinuumException
154     {
155         try
156         {
157             BuildDefinition storedDefinition = null;
158 
159             for ( BuildDefinition bd : buildDefinitions )
160             {
161                 if ( bd.getId() == buildDefinition.getId() )
162                 {
163                     storedDefinition = bd;
164                 }
165             }
166 
167             if ( storedDefinition != null )
168             {
169                 storedDefinition.setGoals( buildDefinition.getGoals() );
170                 storedDefinition.setArguments( buildDefinition.getArguments() );
171                 storedDefinition.setBuildFile( buildDefinition.getBuildFile() );
172                 storedDefinition.setBuildFresh( buildDefinition.isBuildFresh() );
173                 storedDefinition.setUpdatePolicy( buildDefinition.getUpdatePolicy() );
174 
175                 // special case of this is resolved in the resolveDefaultBuildDefinitionsForProjectGroup method
176                 storedDefinition.setDefaultForProject( buildDefinition.isDefaultForProject() );
177 
178                 Schedule schedule;
179                 if ( buildDefinition.getSchedule() == null )
180                 {
181                     try
182                     {
183                         schedule = scheduleDao.getScheduleByName( ConfigurationService.DEFAULT_SCHEDULE_NAME );
184                     }
185                     catch ( ContinuumStoreException e )
186                     {
187                         throw new ContinuumException( "Can't get default schedule.", e );
188                     }
189                 }
190                 else
191                 {
192                     schedule = scheduleDao.getSchedule( buildDefinition.getSchedule().getId() );
193                 }
194 
195                 storedDefinition.setSchedule( schedule );
196 
197                 storedDefinition.setProfile( buildDefinition.getProfile() );
198 
199                 storedDefinition.setDescription( buildDefinition.getDescription() );
200 
201                 storedDefinition.setType( buildDefinition.getType() );
202 
203                 storedDefinition.setAlwaysBuild( buildDefinition.isAlwaysBuild() );
204 
205                 buildDefinitionDao.storeBuildDefinition( storedDefinition );
206             }
207             else
208             {
209                 throw new ContinuumException( "failed update, build definition didn't exist in project group" );
210             }
211         }
212         catch ( ContinuumStoreException cse )
213         {
214             throw new ContinuumException( "error in accessing or storing build definition" );
215         }
216     }
217 }