View Javadoc
1   package org.codehaus.mojo.keytool;
2   
3   /*
4    * Copyright 2005-2013 The Codehaus
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License" );
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.maven.plugin.AbstractMojo;
20  import org.apache.maven.plugins.annotations.Parameter;
21  
22  import java.text.MessageFormat;
23  import java.util.ResourceBundle;
24  
25  /**
26   * Abstract class that contains fields/methods common to KeyTool Mojo classes.
27   *
28   * @author Sharmarke Aden (<a href="mailto:saden1@gmail.com">saden</a>)
29   * @author $Author$
30   * @version $Revision$
31   */
32  public abstract class AbstractKeyToolMojo
33      extends AbstractMojo
34  {
35  
36      /**
37       * Set to {@code true} to disable the plugin.
38       *
39       * @since 1.1
40       */
41      @Parameter( defaultValue = "false" )
42      private boolean skip;
43  
44      /**
45       * Enable verbose mode (in mojo and in keytool command).
46       * <p/>
47       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
48       */
49      @Parameter( defaultValue = "false" )
50      private boolean verbose;
51  
52      /**
53       * @return value of the {@link #skip} flag
54       */
55      public final boolean isSkip()
56      {
57          return skip;
58      }
59  
60      /**
61       * @param skip the skip flag value to set.
62       */
63      public final void setSkip( boolean skip )
64      {
65          this.skip = skip;
66      }
67  
68      /**
69       * @return value of the {@link #verbose} flag
70       */
71      public final boolean isVerbose()
72      {
73          return verbose;
74      }
75  
76      /**
77       * @param verbose the verbose flag value to set.
78       */
79      public final void setVerbose( boolean verbose )
80      {
81          this.verbose = verbose;
82      }
83  
84      /**
85       * Gets a message for a given key from the resource bundle backing the implementation.
86       *
87       * @param key  The key of the message to return (cano not be null).
88       * @param args Arguments to format the message with or {@code null}.
89       * @return The message with key {@code key} from the resource bundle backing the implementation.
90       */
91      private String getMessage( final String key, final Object[] args )
92      {
93          if ( key == null )
94          {
95              throw new NullPointerException( "key" );
96          }
97  
98          return new MessageFormat( ResourceBundle.getBundle( "keytool" ).getString( key ) ).format( args );
99      }
100 
101     /**
102      * Gets a message for a given key from the resource bundle backing the implementation.
103      *
104      * @param key The key of the message to return.
105      * @return The message with key {@code key} from the resource bundle backing the implementation.
106      */
107     protected String getMessage( final String key )
108     {
109         return getMessage( key, new Object[]{ } );
110     }
111 
112     /**
113      * Gets a message for a given key and the given parameter from the resource bundle backing the implementation.
114      *
115      * @param key The key of the message to return.
116      * @param arg argument of the sentence to translate
117      * @return The message with key {@code key} from the resource bundle backing the implementation.
118      */
119     protected String getMessage( final String key, final Object arg )
120     {
121         return getMessage( key, new Object[]{ arg } );
122     }
123 
124     /**
125      * Gets a message for a given key and the given parameters from the resource bundle backing the implementation.
126      *
127      * @param key  The key of the message to return.
128      * @param arg1 first argument of the sentence to translate
129      * @param arg2 second argument of the sentence to translate
130      * @return The message with key {@code key} from the resource bundle backing the implementation.
131      */
132     protected String getMessage( final String key, final Object arg1, final Object arg2 )
133     {
134         return getMessage( key, new Object[]{ arg1, arg2 } );
135     }
136 }