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.model.project.ProjectScmRoot;
23  import org.apache.continuum.utils.build.BuildTrigger;
24  import org.apache.maven.continuum.model.project.BuildDefinition;
25  import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
26  import org.apache.maven.continuum.model.project.Project;
27  import org.apache.maven.continuum.model.project.ProjectDependency;
28  import org.apache.maven.continuum.model.project.ProjectGroup;
29  import org.apache.maven.continuum.model.scm.ScmResult;
30  import org.codehaus.plexus.action.AbstractAction;
31  
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
39   * @version $Id: AbstractContinuumAction.java 1372260 2012-08-13 04:29:09Z brett $
40   */
41  public abstract class AbstractContinuumAction
42      extends AbstractAction
43  {
44      // ----------------------------------------------------------------------
45      // Keys for the values that can be in the context
46      // ----------------------------------------------------------------------
47  
48      private static final String KEY_PROJECT_ID = "project-id";
49  
50      private static final String KEY_PROJECT = "project";
51  
52      private static final String KEY_PROJECTS = "projects";
53  
54      private static final String KEY_PROJECTS_BUILD_DEFINITIONS_MAP = "projects-build-definitions";
55  
56      private static final String KEY_BUILD_DEFINITION_TEMPLATE = "build-definition-template";
57  
58      private static final String KEY_BUILD_DEFINITION = "build-definition";
59  
60      private static final String KEY_BUILD_DEFINITION_ID = "build-definition-id";
61  
62      private static final String KEY_UNVALIDATED_PROJECT = "unvalidated-project";
63  
64      private static final String KEY_PROJECT_GROUP_ID = "project-group-id";
65  
66      private static final String KEY_UNVALIDATED_PROJECT_GROUP = "unvalidated-project-group";
67  
68      private static final String KEY_BUILD_ID = "build-id";
69  
70      private static final String KEY_WORKING_DIRECTORY = "working-directory";
71  
72      private static final String KEY_UPDATE_DEPENDENCIES = "update-dependencies";
73  
74      private static final String KEY_BUILD_TRIGGER = "buildTrigger";
75  
76      private static final String KEY_SCM_RESULT = "scmResult";
77  
78      private static final String KEY_OLD_SCM_RESULT = "old-scmResult";
79  
80      private static final String KEY_PROJECT_SCM_ROOT = "projectScmRoot";
81  
82      /**
83       * SCM root url. Used in these actions add-project-to-checkout-queue, checkout-project, clean-working-directory,
84       * create-projects-from-metadata, update-project-from-working-directory,
85       * update-working-directory-from-scm
86       */
87      private static final String KEY_PROJECT_SCM_ROOT_URL = "projectScmRootUrl";
88  
89      /**
90       * List of projects in a project group with a common scm root url.
91       */
92      private static final String KEY_PROJECTS_IN_GROUP_WITH_COMMON_SCM_ROOT = "projects-in-group-with-common-scm-root";
93  
94      private static final String KEY_OLD_BUILD_ID = "old-buildResult-id";
95  
96      private static final String KEY_SCM_RESULT_MAP = "scm-result-map";
97  
98      private static final String KEY_ROOT_DIRECTORY = "root-directory";
99  
100     // ----------------------------------------------------------------------
101     //
102     // ----------------------------------------------------------------------
103 
104     public static int getProjectId( Map<String, Object> context )
105     {
106         return getInteger( context, KEY_PROJECT_ID );
107     }
108 
109     public static void setProjectId( Map<String, Object> context, int projectId )
110     {
111         context.put( KEY_PROJECT_ID, projectId );
112     }
113 
114     public static Project getProject( Map<String, Object> context )
115     {
116         return (Project) getObject( context, KEY_PROJECT );
117     }
118 
119     public static Project getProject( Map<String, Object> context, Project defaultValue )
120     {
121         return (Project) getObject( context, KEY_PROJECT, defaultValue );
122     }
123 
124     public static void setProject( Map<String, Object> context, Project p )
125     {
126         context.put( KEY_PROJECT, p );
127     }
128 
129     public static int getProjectGroupId( Map<String, Object> context )
130     {
131         return getInteger( context, KEY_PROJECT_GROUP_ID );
132     }
133 
134     public static void setProjectGroupId( Map<String, Object> context, int projectGroupId )
135     {
136         context.put( KEY_PROJECT_GROUP_ID, projectGroupId );
137     }
138 
139     public static BuildDefinitionTemplate getBuildDefinitionTemplate( Map<String, Object> context )
140     {
141         return (BuildDefinitionTemplate) getObject( context, KEY_BUILD_DEFINITION_TEMPLATE, null );
142     }
143 
144     public static void setBuildDefinitionTemplate( Map<String, Object> context, BuildDefinitionTemplate bdt )
145     {
146         context.put( KEY_BUILD_DEFINITION_TEMPLATE, bdt );
147     }
148 
149     public static BuildDefinition getBuildDefinition( Map<String, Object> context )
150     {
151         return (BuildDefinition) getObject( context, KEY_BUILD_DEFINITION, null );
152     }
153 
154     public static void setBuildDefinition( Map<String, Object> context, BuildDefinition bd )
155     {
156         context.put( KEY_BUILD_DEFINITION, bd );
157     }
158 
159     public static int getBuildDefinitionId( Map<String, Object> context )
160     {
161         return getInteger( context, KEY_BUILD_DEFINITION_ID );
162     }
163 
164     public static void setBuildDefinitionId( Map<String, Object> context, int buildDefintionId )
165     {
166         context.put( KEY_BUILD_DEFINITION_ID, buildDefintionId );
167     }
168 
169     public static String getBuildId( Map<String, Object> context )
170     {
171         return getString( context, KEY_BUILD_ID );
172     }
173 
174     public static String getBuildId( Map<String, Object> context, String defaultValue )
175     {
176         return getString( context, KEY_BUILD_ID, defaultValue );
177     }
178 
179     public static void setBuildId( Map<String, Object> context, String buildId )
180     {
181         context.put( KEY_BUILD_ID, buildId );
182     }
183 
184     public static BuildTrigger getBuildTrigger( Map<String, Object> context )
185     {
186         BuildTrigger defaultValue = new BuildTrigger( 0, "" );
187         return (BuildTrigger) getObject( context, KEY_BUILD_TRIGGER, defaultValue );
188     }
189 
190     public static void setBuildTrigger( Map<String, Object> context, BuildTrigger buildTrigger )
191     {
192         context.put( KEY_BUILD_TRIGGER, buildTrigger );
193     }
194 
195     public static Project getUnvalidatedProject( Map<String, Object> context )
196     {
197         return (Project) getObject( context, KEY_UNVALIDATED_PROJECT );
198     }
199 
200     public static void setUnvalidatedProject( Map<String, Object> context, Project p )
201     {
202         context.put( KEY_UNVALIDATED_PROJECT, p );
203     }
204 
205     public static ProjectGroup getUnvalidatedProjectGroup( Map<String, Object> context )
206     {
207         return (ProjectGroup) getObject( context, KEY_UNVALIDATED_PROJECT_GROUP );
208     }
209 
210     public static void setUnvalidatedProjectGroup( Map<String, Object> context, ProjectGroup pg )
211     {
212         context.put( KEY_UNVALIDATED_PROJECT_GROUP, pg );
213     }
214 
215     public static File getWorkingDirectory( Map<String, Object> context )
216     {
217         return new File( getString( context, KEY_WORKING_DIRECTORY ) );
218     }
219 
220     public static void setWorkingDirectory( Map<String, Object> context, String workingDirectory )
221     {
222         context.put( KEY_WORKING_DIRECTORY, workingDirectory );
223     }
224 
225     public static List<ProjectDependency> getUpdatedDependencies( Map<String, Object> context )
226     {
227         return getUpdatedDependencies( context, null );
228     }
229 
230     public static List<ProjectDependency> getUpdatedDependencies( Map<String, Object> context,
231                                                                   List<ProjectDependency> defaultValue )
232     {
233         return (List<ProjectDependency>) getObject( context, KEY_UPDATE_DEPENDENCIES, defaultValue );
234     }
235 
236     public static void setUpdatedDependencies( Map<String, Object> context, List<ProjectDependency> dependencies )
237     {
238         context.put( KEY_UPDATE_DEPENDENCIES, dependencies );
239     }
240 
241     public static ScmResult getScmResult( Map<String, Object> context )
242     {
243         return getScmResult( context, null );
244     }
245 
246     public static ScmResult getScmResult( Map<String, Object> context, ScmResult defaultValue )
247     {
248         return (ScmResult) getObject( context, KEY_SCM_RESULT, defaultValue );
249     }
250 
251     public static void setScmResult( Map<String, Object> context, ScmResult scmResult )
252     {
253         context.put( KEY_SCM_RESULT, scmResult );
254     }
255 
256     public static ScmResult getOldScmResult( Map<String, Object> context )
257     {
258         return getOldScmResult( context, null );
259     }
260 
261     public static ScmResult getOldScmResult( Map<String, Object> context, ScmResult defaultValue )
262     {
263         return (ScmResult) getObject( context, KEY_OLD_SCM_RESULT, defaultValue );
264     }
265 
266     public static void setOldScmResult( Map<String, Object> context, ScmResult oldScmResult )
267     {
268         context.put( KEY_OLD_SCM_RESULT, oldScmResult );
269     }
270 
271     public static ProjectScmRoot getProjectScmRoot( Map<String, Object> context )
272     {
273         return (ProjectScmRoot) getObject( context, KEY_PROJECT_SCM_ROOT );
274     }
275 
276     public static void setProjectScmRoot( Map<String, Object> context, ProjectScmRoot projectScmRoot )
277     {
278         context.put( KEY_PROJECT_SCM_ROOT, projectScmRoot );
279     }
280 
281     public static int getOldBuildId( Map<String, Object> context )
282     {
283         return getInteger( context, KEY_OLD_BUILD_ID );
284     }
285 
286     public static void setOldBuildId( Map<String, Object> context, int oldBuildId )
287     {
288         context.put( KEY_OLD_BUILD_ID, oldBuildId );
289     }
290 
291     public static List<Project> getListOfProjects( Map<String, Object> context )
292     {
293         return (List<Project>) getObject( context, KEY_PROJECTS );
294     }
295 
296     public static void setListOfProjects( Map<String, Object> context, List<Project> projects )
297     {
298         context.put( KEY_PROJECTS, projects );
299     }
300 
301     public static List<Project> getListOfProjectsInGroupWithCommonScmRoot( Map<String, Object> context )
302     {
303         return (List<Project>) getObject( context, KEY_PROJECTS_IN_GROUP_WITH_COMMON_SCM_ROOT,
304                                           new ArrayList<Integer>() );
305     }
306 
307     public static void setListOfProjectsInGroupWithCommonScmRoot( Map<String, Object> context, List<Project> projects )
308     {
309         context.put( KEY_PROJECTS_IN_GROUP_WITH_COMMON_SCM_ROOT, projects );
310     }
311 
312     public static Map<Integer, BuildDefinition> getProjectsBuildDefinitionsMap( Map<String, Object> context )
313     {
314         return (Map<Integer, BuildDefinition>) getObject( context, KEY_PROJECTS_BUILD_DEFINITIONS_MAP );
315     }
316 
317     public static void setProjectsBuildDefinitionsMap( Map<String, Object> context,
318                                                        Map<Integer, BuildDefinition> projectsBuildDefinitionsMap )
319     {
320         context.put( KEY_PROJECTS_BUILD_DEFINITIONS_MAP, projectsBuildDefinitionsMap );
321     }
322 
323     public static Map<Integer, ScmResult> getScmResultMap( Map<String, Object> context )
324     {
325         return (Map<Integer, ScmResult>) getObject( context, KEY_SCM_RESULT_MAP );
326     }
327 
328     public static void setScmResultMap( Map<String, Object> context, Map<Integer, ScmResult> scmResultMap )
329     {
330         context.put( KEY_SCM_RESULT_MAP, scmResultMap );
331     }
332 
333     public static boolean isRootDirectory( Map<String, Object> context )
334     {
335         return getBoolean( context, KEY_ROOT_DIRECTORY, true );
336     }
337 
338     public static void setRootDirectory( Map<String, Object> context, boolean isRootDirectory )
339     {
340         context.put( KEY_ROOT_DIRECTORY, isRootDirectory );
341     }
342 
343     public static String getProjectScmRootUrl( Map<String, Object> context, String projectScmRootUrl )
344     {
345         return getString( context, KEY_PROJECT_SCM_ROOT_URL, projectScmRootUrl );
346     }
347 
348     public static void setProjectScmRootUrl( Map<String, Object> context, String projectScmRootUrl )
349     {
350         context.put( KEY_PROJECT_SCM_ROOT_URL, projectScmRootUrl );
351     }
352 
353     // ----------------------------------------------------------------------
354     //
355     // ----------------------------------------------------------------------
356 
357     public static String getString( Map<String, Object> context, String key )
358     {
359         return (String) getObject( context, key );
360     }
361 
362     public static String getString( Map<String, Object> context, String key, String defaultValue )
363     {
364         return (String) getObject( context, key, defaultValue );
365     }
366 
367     public static boolean getBoolean( Map<String, Object> context, String key )
368     {
369         return (Boolean) getObject( context, key );
370     }
371 
372     public static boolean getBoolean( Map<String, Object> context, String key, boolean defaultValue )
373     {
374         return (Boolean) getObject( context, key, defaultValue );
375     }
376 
377     protected static int getInteger( Map<String, Object> context, String key )
378     {
379         Object obj = getObject( context, key, null );
380 
381         if ( obj == null )
382         {
383             return 0;
384         }
385         else
386         {
387             return (Integer) obj;
388         }
389     }
390 
391     protected static Object getObject( Map<String, Object> context, String key )
392     {
393         if ( !context.containsKey( key ) )
394         {
395             throw new RuntimeException( "Missing key '" + key + "'." );
396         }
397 
398         Object value = context.get( key );
399 
400         if ( value == null )
401         {
402             throw new RuntimeException( "Missing value for key '" + key + "'." );
403         }
404 
405         return value;
406     }
407 
408     protected static Object getObject( Map<String, Object> context, String key, Object defaultValue )
409     {
410         Object value = context.get( key );
411 
412         if ( value == null )
413         {
414             return defaultValue;
415         }
416 
417         return value;
418     }
419 }