001package org.apache.maven.scm; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.io.Serializable; 024import java.util.Date; 025import java.util.HashMap; 026import java.util.Map; 027 028/** 029 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> 030 * @author Olivier Lamy 031 * 032 */ 033public class CommandParameters 034 implements Serializable 035{ 036 private static final long serialVersionUID = -7346070735958137283L; 037 038 private Map<String, Object> parameters = new HashMap<String, Object>(); 039 040 // ---------------------------------------------------------------------- 041 // String 042 // ---------------------------------------------------------------------- 043 044 /** 045 * Return the parameter value as String. 046 * 047 * @param parameter The parameter 048 * @return The parameter value as a String 049 * @throws ScmException if the parameter doesn't exist 050 */ 051 public String getString( CommandParameter parameter ) 052 throws ScmException 053 { 054 Object object = getObject( String.class, parameter ); 055 056 if ( object == null ) 057 { 058 throw new ScmException( "Missing parameter: '" + parameter.getName() + "'." ); 059 } 060 061 return object.toString(); 062 } 063 064 /** 065 * Return the parameter value or the default value if it doesn't exist. 066 * 067 * @param parameter The parameter 068 * @param defaultValue The default value 069 * @return The parameter value as a String 070 * @throws ScmException if the value is in the wrong type 071 */ 072 public String getString( CommandParameter parameter, String defaultValue ) 073 throws ScmException 074 { 075 Object object = getObject( String.class, parameter, null ); 076 077 if ( object == null ) 078 { 079 return defaultValue; 080 } 081 082 return object.toString(); 083 } 084 085 /** 086 * Set a parameter value. 087 * 088 * @param parameter The parameter name 089 * @param value The value of the parameter 090 * @throws ScmException if the parameter already exist 091 */ 092 public void setString( CommandParameter parameter, String value ) 093 throws ScmException 094 { 095 setObject( parameter, value ); 096 } 097 098 // ---------------------------------------------------------------------- 099 // Int 100 // ---------------------------------------------------------------------- 101 102 /** 103 * Return the parameter value as int. 104 * 105 * @param parameter The parameter 106 * @return The parameter value as a String 107 * @throws ScmException if the parameter doesn't exist 108 */ 109 public int getInt( CommandParameter parameter ) 110 throws ScmException 111 { 112 return ( (Integer) getObject( Integer.class, parameter ) ).intValue(); 113 } 114 115 /** 116 * Return the parameter value as int or the default value if it doesn't exist. 117 * 118 * @param parameter The parameter 119 * @param defaultValue The defaultValue 120 * @return The parameter value as a int 121 * @throws ScmException if the value is in the wrong type 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 * Set a parameter value. 138 * 139 * @param parameter The parameter name 140 * @param value The value of the parameter 141 * @throws ScmException if the parameter already exist 142 */ 143 public void setInt( CommandParameter parameter, int value ) 144 throws ScmException 145 { 146 setObject( parameter, Integer.valueOf( value ) ); 147 } 148 149 // ---------------------------------------------------------------------- 150 // Date 151 // ---------------------------------------------------------------------- 152 153 /** 154 * Return the parameter value as Date. 155 * 156 * @param parameter The parameter 157 * @return The parameter value as a Date 158 * @throws ScmException if the parameter doesn't exist 159 */ 160 public Date getDate( CommandParameter parameter ) 161 throws ScmException 162 { 163 return (Date) getObject( Date.class, parameter ); 164 } 165 166 /** 167 * Return the parameter value as String or the default value if it doesn't exist. 168 * 169 * @param parameter The parameter 170 * @param defaultValue The defaultValue 171 * @return The parameter value as a Date 172 * @throws ScmException if the value is in the wrong type 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 * Set a parameter value. 182 * 183 * @param parameter The parameter name 184 * @param date The value of the parameter 185 * @throws ScmException if the parameter already exist 186 */ 187 public void setDate( CommandParameter parameter, Date date ) 188 throws ScmException 189 { 190 setObject( parameter, date ); 191 } 192 193 // ---------------------------------------------------------------------- 194 // Boolean 195 // ---------------------------------------------------------------------- 196 197 /** 198 * Return the parameter value as boolean. 199 * 200 * @param parameter The parameter 201 * @return The parameter value as a boolean 202 * @throws ScmException if the parameter doesn't exist 203 */ 204 public boolean getBoolean( CommandParameter parameter ) 205 throws ScmException 206 { 207 return Boolean.valueOf( getString( parameter ) ).booleanValue(); 208 } 209 210 /** 211 * Return the parameter value as boolean. 212 * 213 * @since 1.7 214 * @param parameter The parameter 215 * @param defaultValue default value if parameter not exists 216 * @return The parameter value as a boolean 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 // ScmVersion 226 // ---------------------------------------------------------------------- 227 228 /** 229 * Return the parameter value as ScmVersion. 230 * 231 * @param parameter The parameter 232 * @return The parameter value as a ScmVersion 233 * @throws ScmException if the parameter doesn't exist 234 */ 235 public ScmVersion getScmVersion( CommandParameter parameter ) 236 throws ScmException 237 { 238 return (ScmVersion) getObject( ScmVersion.class, parameter ); 239 } 240 241 /** 242 * Return the parameter value as ScmVersion or the default value. 243 * 244 * @param parameter The parameter 245 * @param defaultValue The default value 246 * @return The parameter value as a ScmVersion 247 * @throws ScmException if the parameter doesn't exist 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 * Set a parameter value. 257 * 258 * @param parameter The parameter name 259 * @param scmVersion The tbranch/tag/revision 260 * @throws ScmException if the parameter already exist 261 */ 262 public void setScmVersion( CommandParameter parameter, ScmVersion scmVersion ) 263 throws ScmException 264 { 265 setObject( parameter, scmVersion ); 266 } 267 268 // ---------------------------------------------------------------------- 269 // File[] 270 // ---------------------------------------------------------------------- 271 272 /** 273 * @param parameter not null 274 * @return an array of files 275 * @throws ScmException if any 276 */ 277 public File[] getFileArray( CommandParameter parameter ) 278 throws ScmException 279 { 280 return (File[]) getObject( File[].class, parameter ); 281 } 282 283 /** 284 * @param parameter not null 285 * @param defaultValue could be null 286 * @return an array of files 287 * @throws ScmException if any 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 * Return the value object. 326 * 327 * @param clazz The type of the parameter value 328 * @param parameter The parameter 329 * @return The parameter value 330 * @throws ScmException if the parameter doesn't exist 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 * Return the value object or the default value if it doesn't exist. 347 * 348 * @param clazz The type of the parameter value 349 * @param parameter The parameter 350 * @param defaultValue The defaultValue 351 * @return The parameter value 352 * @throws ScmException if the defaultValue is in the wrong type 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 * Set the parameter value. 376 * 377 * @param parameter The parameter 378 * @param value The parameter value 379 * @throws ScmException if the parameter already exist 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 * Removes a parameter, silent if it didn't exist. 396 * 397 * @param parameter name of the parameter to remove 398 */ 399 public void remove( CommandParameter parameter ) 400 { 401 parameters.remove( parameter ); 402 } 403}