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 java.util.List;
23  
24  import org.apache.continuum.dao.BuildDefinitionDao;
25  import org.apache.continuum.dao.ScheduleDao;
26  import org.apache.maven.continuum.ContinuumException;
27  import org.apache.maven.continuum.configuration.ConfigurationService;
28  import org.apache.maven.continuum.model.project.BuildDefinition;
29  import org.apache.maven.continuum.model.project.Project;
30  import org.apache.maven.continuum.model.project.ProjectGroup;
31  import org.apache.maven.continuum.model.project.Schedule;
32  import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
33  import org.apache.maven.continuum.store.ContinuumStoreException;
34  
35  /**
36   * AbstractBuildDefinitionContinuumAction:
37   *
38   * @author Jesse McConnell <jmcconnell@apache.org>
39   * @version $Id: AbstractBuildDefinitionContinuumAction.java 764863 2009-04-14 16:28:12Z evenisse $
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 =
63                      buildDefinitionDao.getDefaultBuildDefinitionForProject( 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 =
103                 buildDefinitionDao.getDefaultBuildDefinitionsForProjectGroup( 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 &&
113                             buildDefinition.getType().equals( 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 
174                 // special case of this is resolved in the resolveDefaultBuildDefinitionsForProjectGroup method
175                 storedDefinition.setDefaultForProject( buildDefinition.isDefaultForProject() );
176 
177                 Schedule schedule;
178                 if ( buildDefinition.getSchedule() == null )
179                 {
180                     try
181                     {
182                         schedule = scheduleDao.getScheduleByName( ConfigurationService.DEFAULT_SCHEDULE_NAME );
183                     }
184                     catch ( ContinuumStoreException e )
185                     {
186                         throw new ContinuumException( "Can't get default schedule.", e );
187                     }
188                 }
189                 else
190                 {
191                     schedule = scheduleDao.getSchedule( buildDefinition.getSchedule().getId() );
192                 }
193 
194                 storedDefinition.setSchedule( schedule );
195 
196                 storedDefinition.setProfile( buildDefinition.getProfile() );
197 
198                 storedDefinition.setDescription( buildDefinition.getDescription() );
199 
200                 storedDefinition.setType( buildDefinition.getType() );
201 
202                 storedDefinition.setAlwaysBuild( buildDefinition.isAlwaysBuild() );
203 
204                 buildDefinitionDao.storeBuildDefinition( storedDefinition );
205             }
206             else
207             {
208                 throw new ContinuumException( "failed update, build definition didn't exist in project group" );
209             }
210         }
211         catch ( ContinuumStoreException cse )
212         {
213             throw new ContinuumException( "error in accessing or storing build definition" );
214         }
215     }
216 }