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 return object.toString(); 057 } 058 059 /** 060 * Return the parameter value or the default value if it doesn't exist. 061 * 062 * @param parameter The parameter 063 * @param defaultValue The default value 064 * @return The parameter value as a String 065 * @throws ScmException if the value is in the wrong type 066 */ 067 public String getString( CommandParameter parameter, String defaultValue ) 068 throws ScmException 069 { 070 Object object = getObject( String.class, parameter, null ); 071 072 if ( object == null ) 073 { 074 return defaultValue; 075 } 076 077 return object.toString(); 078 } 079 080 /** 081 * Set a parameter value. 082 * 083 * @param parameter The parameter name 084 * @param value The value of the parameter 085 * @throws ScmException if the parameter already exist 086 */ 087 public void setString( CommandParameter parameter, String value ) 088 throws ScmException 089 { 090 setObject( parameter, value ); 091 } 092 093 // ---------------------------------------------------------------------- 094 // Int 095 // ---------------------------------------------------------------------- 096 097 /** 098 * Return the parameter value as int. 099 * 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}