1 package org.apache.continuum.distributed.commons.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Map;
23
24
25
26
27 public class ContinuumDistributedUtil
28 {
29 public static final String KEY_PROJECT_ID = "project-id";
30
31 public static final String KEY_PROJECT_GROUP_ID = "project-group-id";
32
33 public static final String KEY_PROJECT_NAME = "project-name";
34
35 public static final String KEY_ARTIFACT_ID = "artifact-id";
36
37 public static int getProjectId( Map<String, Object> context )
38 {
39 return getInteger( context, KEY_PROJECT_ID );
40 }
41
42 public static int getProjectGroupId( Map<String, Object> context )
43 {
44 return getInteger( context, KEY_PROJECT_GROUP_ID );
45 }
46
47 public static String getArtifactId( Map<String, Object> context )
48 {
49 return getString( context, KEY_ARTIFACT_ID );
50 }
51
52 public static String getProjectName( Map<String, Object> context )
53 {
54 return getString( context, KEY_PROJECT_NAME );
55 }
56
57 public static String getProjectNameAndId( Map<String, Object> context )
58 {
59 StringBuilder result = new StringBuilder();
60
61 if ( getProjectName( context ) != null )
62 {
63 result.append( getProjectName( context ) ).append( " " );
64 }
65 else if ( getArtifactId( context ) != null )
66 {
67 result.append( getArtifactId( context ) ).append( " " );
68 }
69
70 if ( context.containsKey( KEY_PROJECT_ID ) )
71 {
72 result.append( "(projectId=" ).append( getProjectId( context ) ).append( ")" );
73 }
74 else
75 {
76 result.append( "(projectGroupId=" ).append( getProjectGroupId( context ) ).append( ")" );
77 }
78
79 return result.toString();
80 }
81
82
83
84
85
86 private static String getString( Map<String, Object> context, String key )
87 {
88 Object obj = getObject( context, key, null );
89
90 if ( obj == null )
91 {
92 return null;
93 }
94 else
95 {
96 return (String) obj;
97 }
98 }
99
100 private static int getInteger( Map<String, Object> context, String key )
101 {
102 Object obj = getObject( context, key, null );
103
104 if ( obj == null )
105 {
106 return 0;
107 }
108 else
109 {
110 return (Integer) obj;
111 }
112 }
113
114 private static Object getObject( Map<String, Object> context, String key, Object defaultValue )
115 {
116 Object value = context.get( key );
117
118 if ( value == null )
119 {
120 return defaultValue;
121 }
122
123 return value;
124 }
125 }
126