View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm;
20  
21  import java.io.File;
22  import java.io.Serializable;
23  import java.util.Date;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
29   * @author Olivier Lamy
30   *
31   */
32  public class CommandParameters implements Serializable {
33      private static final long serialVersionUID = -7346070735958137283L;
34  
35      private Map<String, Object> parameters = new HashMap<String, Object>();
36  
37      // ----------------------------------------------------------------------
38      // String
39      // ----------------------------------------------------------------------
40  
41      /**
42       * Return the parameter value as String.
43       *
44       * @param parameter The parameter
45       * @return The parameter value as a String
46       * @throws ScmException if the parameter doesn't exist
47       */
48      public String getString(CommandParameter parameter) throws ScmException {
49          Object object = getObject(String.class, parameter);
50  
51          return object.toString();
52      }
53  
54      /**
55       * Return the parameter value or the default value if it doesn't exist.
56       *
57       * @param parameter    The parameter
58       * @param defaultValue The default value
59       * @return The parameter value as a String
60       * @throws ScmException if the value is in the wrong type
61       */
62      public String getString(CommandParameter parameter, String defaultValue) throws ScmException {
63          Object object = getObject(String.class, parameter, null);
64  
65          if (object == null) {
66              return defaultValue;
67          }
68  
69          return object.toString();
70      }
71  
72      /**
73       * Set a parameter value.
74       *
75       * @param parameter The parameter name
76       * @param value     The value of the parameter
77       * @throws ScmException if the parameter already exist
78       */
79      public void setString(CommandParameter parameter, String value) throws ScmException {
80          setObject(parameter, value);
81      }
82  
83      // ----------------------------------------------------------------------
84      // Int
85      // ----------------------------------------------------------------------
86  
87      /**
88       * Return the parameter value as int.
89       *
90       * @param parameter The parameter
91       * @return The parameter value as a String
92       * @throws ScmException if the parameter doesn't exist
93       */
94      public int getInt(CommandParameter parameter) throws ScmException {
95          return ((Integer) getObject(Integer.class, parameter)).intValue();
96      }
97  
98      /**
99       * Return the parameter value as int or the default value if it doesn't exist.
100      *
101      * @param parameter    The parameter
102      * @param defaultValue The defaultValue
103      * @return The parameter value as a int
104      * @throws ScmException if the value is in the wrong type
105      */
106     public int getInt(CommandParameter parameter, int defaultValue) throws ScmException {
107         Integer value = ((Integer) getObject(Integer.class, parameter, null));
108 
109         if (value == null) {
110             return defaultValue;
111         }
112 
113         return value.intValue();
114     }
115 
116     /**
117      * Set a parameter value.
118      *
119      * @param parameter The parameter name
120      * @param value     The value of the parameter
121      * @throws ScmException if the parameter already exist
122      */
123     public void setInt(CommandParameter parameter, int value) throws ScmException {
124         setObject(parameter, Integer.valueOf(value));
125     }
126 
127     // ----------------------------------------------------------------------
128     // Date
129     // ----------------------------------------------------------------------
130 
131     /**
132      * Return the parameter value as Date.
133      *
134      * @param parameter The parameter
135      * @return The parameter value as a Date
136      * @throws ScmException if the parameter doesn't exist
137      */
138     public Date getDate(CommandParameter parameter) throws ScmException {
139         return (Date) getObject(Date.class, parameter);
140     }
141 
142     /**
143      * Return the parameter value as String or the default value if it doesn't exist.
144      *
145      * @param parameter    The parameter
146      * @param defaultValue The defaultValue
147      * @return The parameter value as a Date
148      * @throws ScmException if the value is in the wrong type
149      */
150     public Date getDate(CommandParameter parameter, Date defaultValue) throws ScmException {
151         return (Date) getObject(Date.class, parameter, defaultValue);
152     }
153 
154     /**
155      * Set a parameter value.
156      *
157      * @param parameter The parameter name
158      * @param date      The value of the parameter
159      * @throws ScmException if the parameter already exist
160      */
161     public void setDate(CommandParameter parameter, Date date) throws ScmException {
162         setObject(parameter, date);
163     }
164 
165     // ----------------------------------------------------------------------
166     // Boolean
167     // ----------------------------------------------------------------------
168 
169     /**
170      * Return the parameter value as boolean.
171      *
172      * @param parameter The parameter
173      * @return The parameter value as a boolean
174      * @throws ScmException if the parameter doesn't exist
175      */
176     public boolean getBoolean(CommandParameter parameter) throws ScmException {
177         return Boolean.valueOf(getString(parameter)).booleanValue();
178     }
179 
180     /**
181      * Return the parameter value as boolean.
182      *
183      * @since 1.7
184      * @param parameter    The parameter
185      * @param defaultValue default value if parameter not exists
186      * @return The parameter value as a boolean
187      * @throws ScmException if the parameter doesn't exist
188      */
189     public boolean getBoolean(CommandParameter parameter, boolean defaultValue) throws ScmException {
190         return Boolean.valueOf(getString(parameter, Boolean.toString(defaultValue)))
191                 .booleanValue();
192     }
193 
194     // ----------------------------------------------------------------------
195     // ScmVersion
196     // ----------------------------------------------------------------------
197 
198     /**
199      * Return the parameter value as ScmVersion.
200      *
201      * @param parameter The parameter
202      * @return The parameter value as a ScmVersion
203      * @throws ScmException if the parameter doesn't exist
204      */
205     public ScmVersion getScmVersion(CommandParameter parameter) throws ScmException {
206         return (ScmVersion) getObject(ScmVersion.class, parameter);
207     }
208 
209     /**
210      * Return the parameter value as ScmVersion or the default value.
211      *
212      * @param parameter    The parameter
213      * @param defaultValue The default value
214      * @return The parameter value as a ScmVersion
215      * @throws ScmException if the parameter doesn't exist
216      */
217     public ScmVersion getScmVersion(CommandParameter parameter, ScmVersion defaultValue) throws ScmException {
218         return (ScmVersion) getObject(ScmVersion.class, parameter, defaultValue);
219     }
220 
221     /**
222      * Set a parameter value.
223      *
224      * @param parameter  The parameter name
225      * @param scmVersion The tbranch/tag/revision
226      * @throws ScmException if the parameter already exist
227      */
228     public void setScmVersion(CommandParameter parameter, ScmVersion scmVersion) throws ScmException {
229         setObject(parameter, scmVersion);
230     }
231 
232     // ----------------------------------------------------------------------
233     // File[]
234     // ----------------------------------------------------------------------
235 
236     /**
237      * @param parameter not null
238      * @return an array of files
239      * @throws ScmException if any
240      */
241     public File[] getFileArray(CommandParameter parameter) throws ScmException {
242         return (File[]) getObject(File[].class, parameter);
243     }
244 
245     /**
246      * @param parameter    not null
247      * @param defaultValue could be null
248      * @return an array of files
249      * @throws ScmException if any
250      */
251     public File[] getFileArray(CommandParameter parameter, File[] defaultValue) throws ScmException {
252         return (File[]) getObject(File[].class, parameter, defaultValue);
253     }
254 
255     public ScmTagParameters getScmTagParameters(CommandParameter parameter) throws ScmException {
256         return (ScmTagParameters) getObject(ScmTagParameters.class, parameter, new ScmTagParameters());
257     }
258 
259     public void setScmTagParameters(CommandParameter parameter, ScmTagParameters scmTagParameters) throws ScmException {
260         setObject(parameter, scmTagParameters);
261     }
262 
263     public void setScmBranchParameters(CommandParameter parameter, ScmBranchParameters scmBranchParameters)
264             throws ScmException {
265         setObject(parameter, scmBranchParameters);
266     }
267 
268     public ScmBranchParameters getScmBranchParameters(CommandParameter parameter) throws ScmException {
269         return (ScmBranchParameters) getObject(ScmBranchParameters.class, parameter, new ScmBranchParameters());
270     }
271 
272     // ----------------------------------------------------------------------
273     //
274     // ----------------------------------------------------------------------
275 
276     /**
277      * Return the value object.
278      *
279      * @param clazz     The type of the parameter value
280      * @param parameter The parameter
281      * @return The parameter value
282      * @throws ScmException if the parameter doesn't exist
283      */
284     private Object getObject(Class<?> clazz, CommandParameter parameter) throws ScmException {
285         Object object = getObject(clazz, parameter, null);
286 
287         if (object == null) {
288             throw new ScmException("Missing parameter: '" + parameter.getName() + "'.");
289         }
290 
291         return object;
292     }
293 
294     /**
295      * Return the value object or the default value if it doesn't exist.
296      *
297      * @param clazz        The type of the parameter value
298      * @param parameter    The parameter
299      * @param defaultValue The defaultValue
300      * @return The parameter value
301      * @throws ScmException if the defaultValue is in the wrong type
302      */
303     private Object getObject(Class<?> clazz, CommandParameter parameter, Object defaultValue) throws ScmException {
304         Object object = parameters.get(parameter.getName());
305 
306         if (object == null) {
307             return defaultValue;
308         }
309 
310         if (clazz != null && !clazz.isAssignableFrom(object.getClass())) {
311             throw new ScmException("Wrong parameter type for '" + parameter.getName() + ". " + "Expected: "
312                     + clazz.getName() + ", got: " + object.getClass().getName());
313         }
314 
315         return object;
316     }
317 
318     /**
319      * Set the parameter value.
320      *
321      * @param parameter The parameter
322      * @param value     The parameter value
323      * @throws ScmException if the parameter already exist
324      */
325     private void setObject(CommandParameter parameter, Object value) throws ScmException {
326         Object object = getObject(null, parameter, null);
327 
328         if (object != null) {
329             throw new ScmException("The parameter is already set: " + parameter.getName());
330         }
331 
332         parameters.put(parameter.getName(), value);
333     }
334 
335     /**
336      * Removes a parameter, silent if it didn't exist.
337      *
338      * @param parameter to remove
339      */
340     public void remove(CommandParameter parameter) {
341         parameters.remove(parameter.getName());
342     }
343 }