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