View Javadoc

1   package org.apache.continuum.builder.utils;
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.utils.build.BuildTrigger;
23  import org.apache.maven.continuum.model.project.BuildResult;
24  
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Date;
28  import java.util.List;
29  import java.util.Map;
30  
31  public class ContinuumBuildConstant
32  {
33      public static final String KEY_PROJECT_ID = "project-id";
34  
35      public static final String KEY_PROJECT_VERSION = "project-version";
36  
37      public static final String KEY_PROJECT_BUILD_NUMBER = "build-number";
38  
39      public static final String KEY_BUILD_DEFINITION_ID = "builddefinition-id";
40  
41      public static final String KEY_BUILD_DEFINITION_LABEL = "builddefinition-label";
42  
43      public static final String KEY_TRIGGER = "trigger";
44  
45      public static final String KEY_USERNAME = "username";
46  
47      public static final String KEY_BUILD_TRIGGER = "buildTrigger";
48  
49      public static final String KEY_EXECUTOR_ID = "executor-id";
50  
51      public static final String KEY_SCM_URL = "scm-url";
52  
53      public static final String KEY_SCM_USERNAME = "scm-username";
54  
55      public static final String KEY_SCM_PASSWORD = "scm-password";
56  
57      public static final String KEY_BUILD_FILE = "build-file";
58  
59      public static final String KEY_GOALS = "goals";
60  
61      public static final String KEY_ARGUMENTS = "arguments";
62  
63      public static final String KEY_BUILD_FRESH = "build-fresh";
64  
65      public static final String KEY_ALWAYS_BUILD = "always-build";
66  
67      public static final String KEY_START_TIME = "start-time";
68  
69      public static final String KEY_END_TIME = "end-time";
70  
71      public static final String KEY_BUILD_ERROR = "build-error";
72  
73      public static final String KEY_BUILD_EXIT_CODE = "build-exit-code";
74  
75      public static final String KEY_BUILD_STATE = "build-state";
76  
77      public static final String KEY_SCM_COMMAND_OUTPUT = "scm-command-output";
78  
79      public static final String KEY_SCM_COMMAND_LINE = "scm-command-line";
80  
81      public static final String KEY_SCM_PROVIDER_MESSAGE = "scm-provider-message";
82  
83      public static final String KEY_SCM_EXCEPTION = "scm-exception";
84  
85      public static final String KEY_SCM_SUCCESS = "scm-success";
86  
87      public static final String KEY_PROJECT_GROUP_ID = "project-group-id";
88  
89      public static final String KEY_PROJECT_GROUP_NAME = "project-group-name";
90  
91      public static final String KEY_SCM_ROOT_ADDRESS = "scm-root-address";
92  
93      public static final String KEY_SCM_ROOT_ID = "scm-root-id";
94  
95      public static final String KEY_SCM_ERROR = "scm-error";
96  
97      public static final String KEY_PROJECT_NAME = "project-name";
98  
99      public static final String KEY_BUILD_OUTPUT = "build-output";
100 
101     public static final String KEY_BUILD_RESULT = "build-result";
102 
103     public static final String KEY_PROJECT_STATE = "project-state";
104 
105     public static final String KEY_INSTALLATION_NAME = "installation-name";
106 
107     public static final String KEY_INSTALLATION_TYPE = "installation-type";
108 
109     public static final String KEY_INSTALLATION_VAR_NAME = "installation-var-name";
110 
111     public static final String KEY_INSTALLATION_VAR_VALUE = "installation-var-value";
112 
113     public static final String KEY_LOCAL_REPOSITORY = "local-repository";
114 
115     public static final String KEY_SCM_CHANGES = "scm-changes";
116 
117     public static final String KEY_CHANGESET_ID = "changeset-id";
118 
119     public static final String KEY_CHANGESET_AUTHOR = "changeset-author";
120 
121     public static final String KEY_CHANGESET_COMMENT = "changeset-comment";
122 
123     public static final String KEY_CHANGESET_DATE = "changeset-date";
124 
125     public static final String KEY_CHANGESET_FILES = "changeset-files";
126 
127     public static final String KEY_CHANGEFILE_NAME = "changefile-name";
128 
129     public static final String KEY_CHANGEFILE_REVISION = "changefile-revision";
130 
131     public static final String KEY_CHANGEFILE_STATUS = "changefile-status";
132 
133     public static final String KEY_OLD_SCM_CHANGES = "old-scm-changes";
134 
135     public static final String KEY_PROJECT_DESCRIPTION = "project-description";
136 
137     public static final String KEY_GROUP_ID = "group-id";
138 
139     public static final String KEY_ARTIFACT_ID = "artifact-id";
140 
141     public static final String KEY_PROJECT_DEVELOPERS = "project-developers";
142 
143     public static final String KEY_PROJECT_DEPENDENCIES = "project-dependencies";
144 
145     public static final String KEY_PROJECT_NOTIFIERS = "project-notifiers";
146 
147     public static final String KEY_PROJECT_URL = "project-url";
148 
149     public static final String KEY_SCM_TAG = "scm-tag";
150 
151     public static final String KEY_SCM_RESULT = "scm-result";
152 
153     public static final String KEY_PROJECT_PARENT = "project-parent";
154 
155     public static final String KEY_NOTIFIER_TYPE = "notifier-type";
156 
157     public static final String KEY_NOTIFIER_CONFIGURATION = "notifier-configuration";
158 
159     public static final String KEY_NOTIFIER_FROM = "notifier-from";
160 
161     public static final String KEY_NOTIFIER_RECIPIENT_TYPE = "notifier-recipient-type";
162 
163     public static final String KEY_NOTIFIER_ENABLED = "notifier-enabled";
164 
165     public static final String KEY_NOTIFIER_SEND_ON_SUCCESS = "notifier-send-on-success";
166 
167     public static final String KEY_NOTIFIER_SEND_ON_FAILURE = "notifier-send-on-failure";
168 
169     public static final String KEY_NOTIFIER_SEND_ON_ERROR = "notifier-send-on-error";
170 
171     public static final String KEY_NOTIFIER_SEND_ON_SCMFAILURE = "notifier-send-on-scmfailure";
172 
173     public static final String KEY_NOTIFIER_SEND_ON_WARNING = "notifier-send-on-warning";
174 
175     public static final String KEY_PROJECT_DEVELOPER_NAME = "developer-name";
176 
177     public static final String KEY_PROJECT_DEVELOPER_EMAIL = "developer-email";
178 
179     public static final String KEY_PROJECT_DEVELOPER_SCMID = "developer-scmid";
180 
181     public static final String KEY_MAVEN_PROJECT = "maven-project";
182 
183     public static final String KEY_PROJECT_MODULES = "project-modules";
184 
185     public static final String KEY_LATEST_UPDATE_DATE = "latest-update-date";
186 
187     public static final String KEY_BUILD_AGENT_URL = "build-agent-url";
188 
189     public static final String KEY_MAX_JOB_EXEC_TIME = "max-job-exec-time";
190 
191     public static int getProjectId( Map<String, Object> context )
192     {
193         return getInteger( context, KEY_PROJECT_ID );
194     }
195 
196     public static int getBuildDefinitionId( Map<String, Object> context )
197     {
198         return getInteger( context, KEY_BUILD_DEFINITION_ID );
199     }
200 
201     public static String getBuildError( Map<String, Object> context )
202     {
203         return getString( context, KEY_BUILD_ERROR );
204     }
205 
206     public static int getTrigger( Map<String, Object> context )
207     {
208         return getInteger( context, KEY_TRIGGER );
209     }
210 
211     public static String getUsername( Map<String, Object> context )
212     {
213         return getString( context, KEY_USERNAME, "" );
214     }
215 
216     public static BuildTrigger getBuildTrigger( Map<String, Object> context )
217     {
218         BuildTrigger defaultValue = new BuildTrigger( 0, "" );
219         return (BuildTrigger) getObject( context, KEY_BUILD_TRIGGER, defaultValue );
220     }
221 
222     public static long getStartTime( Map<String, Object> context )
223     {
224         return new Long( getString( context, KEY_START_TIME ) );
225     }
226 
227     public static long getEndTime( Map<String, Object> context )
228     {
229         return new Long( getString( context, KEY_END_TIME ) );
230     }
231 
232     public static int getBuildExitCode( Map<String, Object> context )
233     {
234         return getInteger( context, KEY_BUILD_EXIT_CODE );
235     }
236 
237     public static int getBuildState( Map<String, Object> context )
238     {
239         return getInteger( context, KEY_BUILD_STATE );
240     }
241 
242     public static String getScmCommandLine( Map<String, Object> context )
243     {
244         return getString( context, KEY_SCM_COMMAND_LINE );
245     }
246 
247     public static String getScmCommandOutput( Map<String, Object> context )
248     {
249         return getString( context, KEY_SCM_COMMAND_OUTPUT );
250     }
251 
252     public static String getScmException( Map<String, Object> context )
253     {
254         return getString( context, KEY_SCM_EXCEPTION );
255     }
256 
257     public static String getScmProviderMessage( Map<String, Object> context )
258     {
259         return getString( context, KEY_SCM_PROVIDER_MESSAGE );
260     }
261 
262     public static boolean isScmSuccess( Map<String, Object> context )
263     {
264         return getBoolean( context, KEY_SCM_SUCCESS );
265     }
266 
267     public static int getProjectGroupId( Map<String, Object> context )
268     {
269         return getInteger( context, KEY_PROJECT_GROUP_ID );
270     }
271 
272     public static String getScmRootAddress( Map<String, Object> context )
273     {
274         return getString( context, KEY_SCM_ROOT_ADDRESS );
275     }
276 
277     public static String getScmError( Map<String, Object> context )
278     {
279         return getString( context, KEY_SCM_ERROR );
280     }
281 
282     public static String getBuildOutput( Map<String, Object> context )
283     {
284         return getString( context, KEY_BUILD_OUTPUT );
285     }
286 
287     public static BuildResult getBuildResult( Map<String, Object> context, Object defaultValue )
288     {
289         return (BuildResult) getObject( context, KEY_BUILD_RESULT, defaultValue );
290     }
291 
292     public static String getInstallationName( Map<String, Object> context )
293     {
294         return getString( context, KEY_INSTALLATION_NAME );
295     }
296 
297     public static String getInstallationType( Map<String, Object> context )
298     {
299         return getString( context, KEY_INSTALLATION_TYPE );
300     }
301 
302     public static String getInstallationVarValue( Map<String, Object> context )
303     {
304         return getString( context, KEY_INSTALLATION_VAR_VALUE );
305     }
306 
307     public static String getInstallationVarName( Map<String, Object> context )
308     {
309         return getString( context, KEY_INSTALLATION_VAR_NAME );
310     }
311 
312     public static List<Map<String, Object>> getScmChanges( Map<String, Object> context )
313     {
314         return getList( context, KEY_SCM_CHANGES );
315     }
316 
317     public static String getChangeSetAuthor( Map<String, Object> context )
318     {
319         return getString( context, KEY_CHANGESET_AUTHOR );
320     }
321 
322     public static String getChangeSetComment( Map<String, Object> context )
323     {
324         return getString( context, KEY_CHANGESET_COMMENT );
325     }
326 
327     public static long getChangeSetDate( Map<String, Object> context )
328     {
329         Date date = getDate( context, KEY_CHANGESET_DATE );
330 
331         if ( date == null )
332         {
333             return 0;
334         }
335         else
336         {
337             return date.getTime();
338         }
339     }
340 
341     public static List<Map<String, Object>> getChangeSetFiles( Map<String, Object> context )
342     {
343         return getList( context, KEY_CHANGESET_FILES );
344     }
345 
346     public static String getChangeFileName( Map<String, Object> context )
347     {
348         return getString( context, KEY_CHANGEFILE_NAME );
349     }
350 
351     public static String getChangeFileRevision( Map<String, Object> context )
352     {
353         return getString( context, KEY_CHANGEFILE_REVISION );
354     }
355 
356     public static String getChangeFileStatus( Map<String, Object> context )
357     {
358         return getString( context, KEY_CHANGEFILE_STATUS );
359     }
360 
361     public static String getGroupId( Map<String, Object> context )
362     {
363         return getString( context, KEY_GROUP_ID );
364     }
365 
366     public static String getArtifactId( Map<String, Object> context )
367     {
368         return getString( context, KEY_ARTIFACT_ID );
369     }
370 
371     public static String getVersion( Map<String, Object> context )
372     {
373         return getString( context, KEY_PROJECT_VERSION );
374 
375     }
376 
377     public static String getProjectName( Map<String, Object> context )
378     {
379         return getString( context, KEY_PROJECT_NAME );
380     }
381 
382     public static String getProjectDescription( Map<String, Object> context )
383     {
384         return getString( context, KEY_PROJECT_DESCRIPTION );
385     }
386 
387     public static String getProjectUrl( Map<String, Object> context )
388     {
389         return getString( context, KEY_PROJECT_URL );
390     }
391 
392     public static String getScmUrl( Map<String, Object> context )
393     {
394         return getString( context, KEY_SCM_URL );
395     }
396 
397     public static String getScmTag( Map<String, Object> context )
398     {
399         return getString( context, KEY_SCM_TAG );
400     }
401 
402     public static Map<String, Object> getProjectParent( Map<String, Object> context )
403     {
404         return getMap( context, KEY_PROJECT_PARENT );
405     }
406 
407     public static List<Map<String, Object>> getProjectDevelopers( Map<String, Object> context )
408     {
409         return getList( context, KEY_PROJECT_DEVELOPERS );
410     }
411 
412     public static String getDeveloperName( Map<String, Object> context )
413     {
414         return getString( context, KEY_PROJECT_DEVELOPER_NAME );
415     }
416 
417     public static String getDeveloperEmail( Map<String, Object> context )
418     {
419         return getString( context, KEY_PROJECT_DEVELOPER_EMAIL );
420     }
421 
422     public static String getDeveloperScmId( Map<String, Object> context )
423     {
424         return getString( context, KEY_PROJECT_DEVELOPER_SCMID );
425     }
426 
427     public static List<Map<String, Object>> getProjectDependencies( Map<String, Object> context )
428     {
429         return getList( context, KEY_PROJECT_DEPENDENCIES );
430     }
431 
432     public static List<Map<String, Object>> getProjectNotifiers( Map<String, Object> context )
433     {
434         return getList( context, KEY_PROJECT_NOTIFIERS );
435     }
436 
437     public static Map getNotifierConfiguration( Map<String, Object> context )
438     {
439         return getMap( context, KEY_NOTIFIER_CONFIGURATION );
440     }
441 
442     public static int getNotifierFrom( Map<String, Object> context )
443     {
444         return getInteger( context, KEY_NOTIFIER_FROM );
445     }
446 
447     public static int getNotifierRecipientType( Map<String, Object> context )
448     {
449         return getInteger( context, KEY_NOTIFIER_RECIPIENT_TYPE );
450     }
451 
452     public static String getNotifierType( Map<String, Object> context )
453     {
454         return getString( context, KEY_NOTIFIER_TYPE );
455     }
456 
457     public static boolean isNotifierEnabled( Map<String, Object> context )
458     {
459         return getBoolean( context, KEY_NOTIFIER_ENABLED );
460     }
461 
462     public static boolean isNotifierSendOnError( Map<String, Object> context )
463     {
464         return getBoolean( context, KEY_NOTIFIER_SEND_ON_ERROR );
465     }
466 
467     public static boolean isNotifierSendOnFailure( Map<String, Object> context )
468     {
469         return getBoolean( context, KEY_NOTIFIER_SEND_ON_FAILURE );
470     }
471 
472     public static boolean isNotifierSendOnScmFailure( Map<String, Object> context )
473     {
474         return getBoolean( context, KEY_NOTIFIER_SEND_ON_SCMFAILURE );
475     }
476 
477     public static boolean isNotifierSendOnSuccess( Map<String, Object> context )
478     {
479         return getBoolean( context, KEY_NOTIFIER_SEND_ON_SUCCESS );
480     }
481 
482     public static boolean isNotifierSendOnWarning( Map<String, Object> context )
483     {
484         return getBoolean( context, KEY_NOTIFIER_SEND_ON_WARNING );
485     }
486 
487     public static Map<String, Object> getScmResult( Map<String, Object> context )
488     {
489         return getMap( context, KEY_SCM_RESULT );
490     }
491 
492     public static Map<String, Object> getMavenProject( Map<String, Object> context )
493     {
494         return getMap( context, KEY_MAVEN_PROJECT );
495     }
496 
497     public static List<String> getProjectModules( Map<String, Object> context )
498     {
499         return getList( context, KEY_PROJECT_MODULES );
500     }
501 
502     public static Date getLatestUpdateDate( Map<String, Object> context )
503     {
504         return getDate( context, KEY_LATEST_UPDATE_DATE );
505     }
506 
507     public static String getBuildAgentUrl( Map<String, Object> context )
508     {
509         return getString( context, KEY_BUILD_AGENT_URL );
510     }
511 
512     public static int getScmRootId( Map<String, Object> context )
513     {
514         return getInteger( context, KEY_SCM_ROOT_ID );
515     }
516 
517     public static String getBuildDefinitionLabel( Map<String, Object> context )
518     {
519         return getString( context, KEY_BUILD_DEFINITION_LABEL, "" );
520     }
521 
522     // ----------------------------------------------------------------------
523     //
524     // ----------------------------------------------------------------------
525 
526     protected static String getString( Map<String, Object> context, String key )
527     {
528         Object obj = getObject( context, key, null );
529 
530         if ( obj == null )
531         {
532             return null;
533         }
534         else
535         {
536             return (String) obj;
537         }
538     }
539 
540     protected static String getString( Map<String, Object> context, String key, String defaultValue )
541     {
542         return (String) getObject( context, key, defaultValue );
543     }
544 
545     protected static boolean getBoolean( Map<String, Object> context, String key )
546     {
547         Object obj = getObject( context, key, null );
548 
549         return obj != null && (Boolean) obj;
550     }
551 
552     protected static boolean getBoolean( Map<String, Object> context, String key, boolean defaultValue )
553     {
554         return (Boolean) getObject( context, key, defaultValue );
555     }
556 
557     protected static int getInteger( Map<String, Object> context, String key )
558     {
559         Object obj = getObject( context, key, null );
560 
561         if ( obj == null )
562         {
563             return 0;
564         }
565         else
566         {
567             return (Integer) obj;
568         }
569     }
570 
571     protected static Date getDate( Map<String, Object> context, String key )
572     {
573         Object obj = getObject( context, key, null );
574 
575         if ( obj == null )
576         {
577             return null;
578         }
579         else
580         {
581             return (Date) obj;
582         }
583     }
584 
585     protected static List getList( Map<String, Object> context, String key )
586     {
587         Object obj = getObject( context, key, null );
588 
589         if ( obj == null )
590         {
591             return null;
592         }
593         else
594         {
595             List<Object> list = new ArrayList<Object>();
596             Object[] objA = (Object[]) obj;
597 
598             list.addAll( Arrays.asList( objA ) );
599 
600             return list;
601         }
602     }
603 
604     protected static Map getMap( Map<String, Object> context, String key )
605     {
606         Object obj = getObject( context, key, null );
607 
608         if ( obj == null )
609         {
610             return null;
611         }
612         else
613         {
614             return (Map) obj;
615         }
616     }
617 
618     protected static Object getObject( Map<String, Object> context, String key )
619     {
620         if ( !context.containsKey( key ) )
621         {
622             throw new RuntimeException( "Missing key '" + key + "'." );
623         }
624 
625         Object value = context.get( key );
626 
627         if ( value == null )
628         {
629             throw new RuntimeException( "Missing value for key '" + key + "'." );
630         }
631 
632         return value;
633     }
634 
635     protected static Object getObject( Map<String, Object> context, String key, Object defaultValue )
636     {
637         Object value = context.get( key );
638 
639         if ( value == null )
640         {
641             return defaultValue;
642         }
643 
644         return value;
645     }
646 }