1 package org.apache.maven.scm;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.Serializable;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.Map;
27
28
29
30
31
32
33 public class CommandParameters
34 implements Serializable
35 {
36 private static final long serialVersionUID = -7346070735958137283L;
37
38 private Map<String, Object> parameters = new HashMap<String, Object>();
39
40
41
42
43
44
45
46
47
48
49
50
51 public String getString( CommandParameter parameter )
52 throws ScmException
53 {
54 Object object = getObject( String.class, parameter );
55
56 if ( object == null )
57 {
58 throw new ScmException( "Missing parameter: '" + parameter.getName() + "'." );
59 }
60
61 return object.toString();
62 }
63
64
65
66
67
68
69
70
71
72 public String getString( CommandParameter parameter, String defaultValue )
73 throws ScmException
74 {
75 Object object = getObject( String.class, parameter, null );
76
77 if ( object == null )
78 {
79 return defaultValue;
80 }
81
82 return object.toString();
83 }
84
85
86
87
88
89
90
91
92 public void setString( CommandParameter parameter, String value )
93 throws ScmException
94 {
95 setObject( parameter, value );
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109 public int getInt( CommandParameter parameter )
110 throws ScmException
111 {
112 return ( (Integer) getObject( Integer.class, parameter ) ).intValue();
113 }
114
115
116
117
118
119
120
121
122
123 public int getInt( CommandParameter parameter, int defaultValue )
124 throws ScmException
125 {
126 Integer value = ( (Integer) getObject( Integer.class, parameter, null ) );
127
128 if ( value == null )
129 {
130 return defaultValue;
131 }
132
133 return value.intValue();
134 }
135
136
137
138
139
140
141
142
143 public void setInt( CommandParameter parameter, int value )
144 throws ScmException
145 {
146 setObject( parameter, Integer.valueOf( value ) );
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160 public Date getDate( CommandParameter parameter )
161 throws ScmException
162 {
163 return (Date) getObject( Date.class, parameter );
164 }
165
166
167
168
169
170
171
172
173
174 public Date getDate( CommandParameter parameter, Date defaultValue )
175 throws ScmException
176 {
177 return (Date) getObject( Date.class, parameter, defaultValue );
178 }
179
180
181
182
183
184
185
186
187 public void setDate( CommandParameter parameter, Date date )
188 throws ScmException
189 {
190 setObject( parameter, date );
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204 public boolean getBoolean( CommandParameter parameter )
205 throws ScmException
206 {
207 return Boolean.valueOf( getString( parameter ) ).booleanValue();
208 }
209
210
211
212
213
214
215
216
217
218 public boolean getBoolean( CommandParameter parameter, boolean defaultValue )
219 throws ScmException
220 {
221 return Boolean.valueOf( getString( parameter, Boolean.toString( defaultValue ) ) ).booleanValue();
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235 public ScmVersion getScmVersion( CommandParameter parameter )
236 throws ScmException
237 {
238 return (ScmVersion) getObject( ScmVersion.class, parameter );
239 }
240
241
242
243
244
245
246
247
248
249 public ScmVersion getScmVersion( CommandParameter parameter, ScmVersion defaultValue )
250 throws ScmException
251 {
252 return (ScmVersion) getObject( ScmVersion.class, parameter, defaultValue );
253 }
254
255
256
257
258
259
260
261
262 public void setScmVersion( CommandParameter parameter, ScmVersion scmVersion )
263 throws ScmException
264 {
265 setObject( parameter, scmVersion );
266 }
267
268
269
270
271
272
273
274
275
276
277 public File[] getFileArray( CommandParameter parameter )
278 throws ScmException
279 {
280 return (File[]) getObject( File[].class, parameter );
281 }
282
283
284
285
286
287
288
289 public File[] getFileArray( CommandParameter parameter, File[] defaultValue )
290 throws ScmException
291 {
292 return (File[]) getObject( File[].class, parameter, defaultValue );
293 }
294
295
296 public ScmTagParameters getScmTagParameters( CommandParameter parameter )
297 throws ScmException
298 {
299 return (ScmTagParameters) getObject( ScmTagParameters.class, parameter, new ScmTagParameters() );
300 }
301
302 public void setScmTagParameters( CommandParameter parameter, ScmTagParameters scmTagParameters )
303 throws ScmException
304 {
305 setObject( parameter, scmTagParameters );
306 }
307
308 public void setScmBranchParameters( CommandParameter parameter, ScmBranchParameters scmBranchParameters )
309 throws ScmException
310 {
311 setObject( parameter, scmBranchParameters );
312 }
313
314 public ScmBranchParameters getScmBranchParameters( CommandParameter parameter )
315 throws ScmException
316 {
317 return (ScmBranchParameters) getObject( ScmBranchParameters.class, parameter, new ScmBranchParameters() );
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332 private Object getObject( Class<?> clazz, CommandParameter parameter )
333 throws ScmException
334 {
335 Object object = getObject( clazz, parameter, null );
336
337 if ( object == null )
338 {
339 throw new ScmException( "Missing parameter: '" + parameter.getName() + "'." );
340 }
341
342 return object;
343 }
344
345
346
347
348
349
350
351
352
353
354 private Object getObject( Class<?> clazz, CommandParameter parameter, Object defaultValue )
355 throws ScmException
356 {
357 Object object = parameters.get( parameter.getName() );
358
359 if ( object == null )
360 {
361 return defaultValue;
362 }
363
364 if ( clazz != null && !clazz.isAssignableFrom( object.getClass() ) )
365 {
366 throw new ScmException(
367 "Wrong parameter type for '" + parameter.getName() + ". " + "Expected: " + clazz.getName() + ", got: "
368 + object.getClass().getName() );
369 }
370
371 return object;
372 }
373
374
375
376
377
378
379
380
381 private void setObject( CommandParameter parameter, Object value )
382 throws ScmException
383 {
384 Object object = getObject( null, parameter, null );
385
386 if ( object != null )
387 {
388 throw new ScmException( "The parameter is already set: " + parameter.getName() );
389 }
390
391 parameters.put( parameter.getName(), value );
392 }
393
394
395
396
397
398
399 public void remove( CommandParameter parameter )
400 {
401 parameters.remove( parameter );
402 }
403 }