View Javadoc
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&oslash;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      */
213     public boolean getBoolean( CommandParameter parameter, boolean defaultValue )
214         throws ScmException
215     {
216         return Boolean.valueOf( getString( parameter, Boolean.toString( defaultValue ) ) ).booleanValue();
217     }
218 
219     // ----------------------------------------------------------------------
220     // ScmVersion
221     // ----------------------------------------------------------------------
222 
223     /**
224      * Return the parameter value as ScmVersion.
225      *
226      * @param parameter The parameter
227      * @return The parameter value as a ScmVersion
228      * @throws ScmException if the parameter doesn't exist
229      */
230     public ScmVersion getScmVersion( CommandParameter parameter )
231         throws ScmException
232     {
233         return (ScmVersion) getObject( ScmVersion.class, parameter );
234     }
235 
236     /**
237      * Return the parameter value as ScmVersion or the default value.
238      *
239      * @param parameter    The parameter
240      * @param defaultValue The default value
241      * @return The parameter value as a ScmVersion
242      * @throws ScmException if the parameter doesn't exist
243      */
244     public ScmVersion getScmVersion( CommandParameter parameter, ScmVersion defaultValue )
245         throws ScmException
246     {
247         return (ScmVersion) getObject( ScmVersion.class, parameter, defaultValue );
248     }
249 
250     /**
251      * Set a parameter value.
252      *
253      * @param parameter  The parameter name
254      * @param scmVersion The tbranch/tag/revision
255      * @throws ScmException if the parameter already exist
256      */
257     public void setScmVersion( CommandParameter parameter, ScmVersion scmVersion )
258         throws ScmException
259     {
260         setObject( parameter, scmVersion );
261     }
262 
263     // ----------------------------------------------------------------------
264     // File[]
265     // ----------------------------------------------------------------------
266 
267     /**
268      * @param parameter not null
269      * @return an array of files
270      * @throws ScmException if any
271      */
272     public File[] getFileArray( CommandParameter parameter )
273         throws ScmException
274     {
275         return (File[]) getObject( File[].class, parameter );
276     }
277 
278     /**
279      * @param parameter    not null
280      * @param defaultValue could be null
281      * @return an array of files
282      * @throws ScmException if any
283      */
284     public File[] getFileArray( CommandParameter parameter, File[] defaultValue )
285         throws ScmException
286     {
287         return (File[]) getObject( File[].class, parameter, defaultValue );
288     }
289 
290 
291     public ScmTagParameters getScmTagParameters( CommandParameter parameter )
292         throws ScmException
293     {
294         return (ScmTagParameters) getObject( ScmTagParameters.class, parameter, new ScmTagParameters() );
295     }
296 
297     public void setScmTagParameters( CommandParameter parameter, ScmTagParameters scmTagParameters )
298         throws ScmException
299     {
300         setObject( parameter, scmTagParameters );
301     }
302 
303     public void setScmBranchParameters( CommandParameter parameter, ScmBranchParameters scmBranchParameters )
304         throws ScmException
305     {
306         setObject( parameter, scmBranchParameters );
307     }
308 
309     public ScmBranchParameters getScmBranchParameters( CommandParameter parameter )
310         throws ScmException
311     {
312         return (ScmBranchParameters) getObject( ScmBranchParameters.class, parameter, new ScmBranchParameters() );
313     }
314 
315     // ----------------------------------------------------------------------
316     //
317     // ----------------------------------------------------------------------
318 
319     /**
320      * Return the value object.
321      *
322      * @param clazz     The type of the parameter value
323      * @param parameter The parameter
324      * @return The parameter value
325      * @throws ScmException if the parameter doesn't exist
326      */
327     private Object getObject( Class<?> clazz, CommandParameter parameter )
328         throws ScmException
329     {
330         Object object = getObject( clazz, parameter, null );
331 
332         if ( object == null )
333         {
334             throw new ScmException( "Missing parameter: '" + parameter.getName() + "'." );
335         }
336 
337         return object;
338     }
339 
340     /**
341      * Return the value object or the default value if it doesn't exist.
342      *
343      * @param clazz        The type of the parameter value
344      * @param parameter    The parameter
345      * @param defaultValue The defaultValue
346      * @return The parameter value
347      * @throws ScmException if the defaultValue is in the wrong type
348      */
349     private Object getObject( Class<?> clazz, CommandParameter parameter, Object defaultValue )
350         throws ScmException
351     {
352         Object object = parameters.get( parameter.getName() );
353 
354         if ( object == null )
355         {
356             return defaultValue;
357         }
358 
359         if ( clazz != null && !clazz.isAssignableFrom( object.getClass() ) )
360         {
361             throw new ScmException(
362                 "Wrong parameter type for '" + parameter.getName() + ". " + "Expected: " + clazz.getName() + ", got: "
363                     + object.getClass().getName() );
364         }
365 
366         return object;
367     }
368 
369     /**
370      * Set the parameter value.
371      *
372      * @param parameter The parameter
373      * @param value     The parameter value
374      * @throws ScmException if the parameter already exist
375      */
376     private void setObject( CommandParameter parameter, Object value )
377         throws ScmException
378     {
379         Object object = getObject( null, parameter, null );
380 
381         if ( object != null )
382         {
383             throw new ScmException( "The parameter is already set: " + parameter.getName() );
384         }
385 
386         parameters.put( parameter.getName(), value );
387     }
388 
389     /**
390      * Removes a parameter, silent if it didn't exist.
391      *
392      * @param parameter to remove
393      */
394     public void remove( CommandParameter parameter )
395     {
396         parameters.remove( parameter.getName() );
397     }
398 }