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