View Javadoc

1   /*
2    VersionOptionExecution.java
3    Creation date : 9/06/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.commoncli.option.impl.version;
26  
27  import static java.util.logging.Level.INFO;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.LEFT_PARENTHESIS_CHAR;
30  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.LINE_CHAR;
31  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.RIGHT_PARENTHESIS_CHAR;
32  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.SPACE_CHAR;
33  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
34  import static net.sourceforge.plantumldependency.common.utils.system.SystemUtils.getSystemPropertiesSetToDisplay;
35  import static net.sourceforge.plantumldependency.commoncli.constants.CommandLineConstants.IMPORTANT_SYSTEM_PROPERTIES;
36  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.JAVA_PROGRAM_NULL_ERROR;
37  
38  import java.util.Set;
39  import java.util.logging.Logger;
40  
41  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
42  import net.sourceforge.plantumldependency.commoncli.option.execution.AbstractOptionExecution;
43  import net.sourceforge.plantumldependency.commoncli.program.JavaProgram;
44  
45  /**
46   * The default option execution associated to the "-version" option, displaying the program usage
47   * information.
48   *
49   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
50   * @since 1.3.0
51   * @version 1.3.0
52   */
53  public class VersionOptionExecution extends AbstractOptionExecution {
54  
55      /** Serial version UID. */
56      private static final long serialVersionUID = 5663514744085994950L;
57  
58      /** The class logger. */
59      private static final transient Logger LOGGER = getLogger(VersionOptionExecution.class.getName());
60  
61      /** The java program to display authors information. */
62      private JavaProgram javaProgram;
63  
64      /** The verbose mode which can affect the version to display, if necessary. */
65      private boolean verboseMode;
66  
67      /**
68       * Full constructor.
69       *
70       * @param program
71       *            the java program to display help information, mustn't be <code>null</code>.
72       * @param verboseModeActive
73       *            the verbose mode which can affect the version to display.
74       * @param optionPriority
75       *            the option priority as an integer.
76       * @since 1.3.0
77       */
78      public VersionOptionExecution(final JavaProgram program, final boolean verboseModeActive, final int optionPriority) {
79          super(optionPriority);
80          setJavaProgram(program);
81          setVerboseMode(verboseModeActive);
82      }
83  
84      /**
85       * Default constructor.
86       *
87       * @param program
88       *            the java program to display help information, mustn't be <code>null</code>.
89       * @param optionPriority
90       *            the option priority as an integer. <i>Note : the priority must be unique amongst
91       *            all options executions</i>.
92       * @since 1.3.0
93       */
94      public VersionOptionExecution(final JavaProgram program, final int optionPriority) {
95          this(program, true, optionPriority);
96      }
97  
98      /**
99       * {@inheritDoc}
100      *
101      * @since 1.3.0
102      */
103     @Override
104     public VersionOptionExecution deepClone() {
105         final VersionOptionExecution a = (VersionOptionExecution) super.deepClone();
106         a.javaProgram = getJavaProgram().deepClone();
107         return a;
108     }
109 
110     /**
111      * {@inheritDoc}
112      *
113      * @since 1.3.0
114      */
115     @Override
116     public void execute() throws CommandLineException {
117         final StringBuilder buffer = new StringBuilder(getJavaProgram().getName());
118         buffer.append(" version ");
119         buffer.append(getJavaProgram().getVersion().getFullVersionNumber());
120         buffer.append(SPACE_CHAR);
121         buffer.append(LEFT_PARENTHESIS_CHAR);
122         buffer.append(getJavaProgram().getVersion().getCompilationTime());
123         buffer.append(RIGHT_PARENTHESIS_CHAR);
124         buffer.append(SPACE_CHAR);
125 
126         Set < String > systemPropertiesSetToDisplay = null;
127         if (isVerboseMode()) {
128             systemPropertiesSetToDisplay = getSystemPropertiesSetToDisplay();
129         } else {
130             systemPropertiesSetToDisplay = getSystemPropertiesSetToDisplay(IMPORTANT_SYSTEM_PROPERTIES);
131         }
132 
133         for (final String propertyToDisplay : systemPropertiesSetToDisplay) {
134             buffer.append(LINE_CHAR);
135             buffer.append(propertyToDisplay);
136         }
137 
138         final String bufferStr = buffer.toString();
139         System.out.println(bufferStr);
140         LOGGER.log(INFO, bufferStr);
141     }
142 
143     /**
144      * Gets the value of <code>javaProgram</code>.
145      *
146      * @return the value of <code>javaProgram</code>.
147      * @see #setJavaProgram(JavaProgram)
148      * @since 1.3.0
149      */
150     private JavaProgram getJavaProgram() {
151         return javaProgram;
152     }
153 
154     /**
155      * Gets the value of <code>verboseMode</code>.
156      *
157      * @return the value of <code>verboseMode</code>.
158      * @see #setVerboseMode(boolean)
159      * @since 1.3.0
160      */
161     private boolean isVerboseMode() {
162         return verboseMode;
163     }
164 
165     /**
166      * Sets the value of <code>javaProgram</code>.
167      *
168      * @param value
169      *            the <code>javaProgram</code> to set, can be <code>null</code>.
170      * @see #getJavaProgram()
171      * @since 1.3.0
172      */
173     private void setJavaProgram(final JavaProgram value) {
174         checkNull(value, JAVA_PROGRAM_NULL_ERROR);
175 
176         javaProgram = value;
177     }
178 
179     /**
180      * Sets the value of <code>verboseMode</code>.
181      *
182      * @param value
183      *            the <code>verboseMode</code> to set, can be <code>null</code>.
184      * @see #isVerboseMode()
185      * @since 1.3.0
186      */
187     private void setVerboseMode(final boolean value) {
188         verboseMode = value;
189     }
190 
191     /**
192      * {@inheritDoc}
193      *
194      * @since 1.3.0
195      */
196     @Override
197     public String toString() {
198         return getClass().getSimpleName() + " [" + super.toString() + ", javaProgram=" + javaProgram + ", verboseMode="
199                 + verboseMode + "]";
200     }
201 }