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