AbstractCommand.java
// SECTION-START[License Header]
// <editor-fold defaultstate="collapsed" desc=" Generated License ">
/*
* Java Object Management and Configuration
* Copyright (C) Christian Schulte <cs@schulte.it>, 2005-206
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $JOMC: AbstractCommand.java 5061 2015-05-31 13:20:40Z schulte $
*
*/
// </editor-fold>
// SECTION-END
package org.jomc.cli.commands;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import org.apache.commons.cli.CommandLine;
// SECTION-START[Documentation]
// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
/**
* JOMC ⁑ CLI ⁑ command implementation.
*
* <dl>
* <dt><b>Identifier:</b></dt><dd>JOMC ⁑ CLI ⁑ Command</dd>
* <dt><b>Name:</b></dt><dd>JOMC ⁑ CLI ⁑ Command</dd>
* <dt><b>Specifications:</b></dt>
* <dd>JOMC ⁑ CLI ⁑ Command @ 1.0</dd>
* <dt><b>Abstract:</b></dt><dd>Yes</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* <dt><b>Stateless:</b></dt><dd>No</dd>
* </dl>
*
* @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
* @version 1.9
*/
// </editor-fold>
// SECTION-END
// SECTION-START[Annotations]
// <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
// </editor-fold>
// SECTION-END
public abstract class AbstractCommand
implements
org.jomc.cli.Command
{
// SECTION-START[Command]
/**
* The listeners of the instance.
*/
private List<Listener> listeners;
/**
* Log level of the instance.
*/
private Level logLevel;
/**
* Gets the list of registered listeners.
* <p>
* This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you make
* to the returned list will be present inside the object. This is why there is no {@code set} method for the
* listeners property.
* </p>
*
* @return The list of registered listeners.
*
* @see #log(java.util.logging.Level, java.lang.String, java.lang.Throwable)
*/
public final List<Listener> getListeners()
{
if ( this.listeners == null )
{
this.listeners = new LinkedList<Listener>();
}
return this.listeners;
}
/**
* Gets the log level of the instance.
*
* @return The log level of the instance.
*
* @see #getDefaultLogLevel()
* @see #setLogLevel(java.util.logging.Level)
* @see #isLoggable(java.util.logging.Level)
*/
public final Level getLogLevel()
{
if ( this.logLevel == null )
{
this.logLevel = getDefaultLogLevel();
if ( this.isLoggable( Level.CONFIG ) )
{
this.log( Level.CONFIG,
this.getDefaultLogLevelInfo( this.getLocale(), this.logLevel.getLocalizedName() ), null );
}
}
return this.logLevel;
}
/**
* Sets the log level of the instance.
*
* @param value The new log level of the instance or {@code null}.
*
* @see #getLogLevel()
* @see #isLoggable(java.util.logging.Level)
*/
public final void setLogLevel( final Level value )
{
this.logLevel = value;
}
public final String getName()
{
return this.getCommandName();
}
public final String getAbbreviatedName()
{
return this.getAbbreviatedCommandName();
}
public final String getShortDescription( final Locale locale )
{
return this.getShortDescriptionMessage( locale );
}
public final String getLongDescription( final Locale locale )
{
return this.getLongDescriptionMessage( locale );
}
public final int execute( final CommandLine commandLine )
{
if ( commandLine == null )
{
throw new NullPointerException( "commandLine" );
}
int status;
try
{
if ( this.isLoggable( Level.INFO ) )
{
this.log( Level.INFO, this.getSeparator( this.getLocale() ), null );
this.log( Level.INFO, this.getApplicationTitle( this.getLocale() ), null );
this.log( Level.INFO, this.getSeparator( this.getLocale() ), null );
this.log( Level.INFO, this.getCommandInfoMessage( this.getLocale(), this.getCommandName() ), null );
}
this.preExecuteCommand( commandLine );
this.executeCommand( commandLine );
status = STATUS_SUCCESS;
}
catch ( final Throwable t )
{
this.log( Level.SEVERE, null, t );
status = STATUS_FAILURE;
}
finally
{
try
{
this.postExecuteCommand( commandLine );
}
catch ( final Throwable t )
{
this.log( Level.SEVERE, null, t );
status = STATUS_FAILURE;
}
}
if ( this.isLoggable( Level.INFO ) )
{
if ( status == STATUS_SUCCESS )
{
this.log( Level.INFO, this.getCommandSuccessMessage( this.getLocale(), this.getCommandName() ), null );
}
else if ( status == STATUS_FAILURE )
{
this.log( Level.INFO, this.getCommandFailureMessage( this.getLocale(), this.getCommandName() ), null );
}
this.log( Level.INFO, this.getSeparator( this.getLocale() ), null );
}
return status;
}
// SECTION-END
// SECTION-START[AbstractCommand]
/**
* Default log level.
*/
private static volatile Level defaultLogLevel;
/**
* Gets the default log level events are logged at.
* <p>
* The default log level is controlled by system property
* {@code org.jomc.cli.commands.AbstractCommand.defaultLogLevel} holding the log level to log events at by
* default. If that property is not set, the {@code WARNING} default is returned.
* </p>
*
* @return The log level events are logged at by default.
*
* @see #getLogLevel()
* @see Level#parse(java.lang.String)
*/
public static Level getDefaultLogLevel()
{
if ( defaultLogLevel == null )
{
defaultLogLevel = Level.parse(
System.getProperty( "org.jomc.cli.command.AbstractJomcCommand.defaultLogLevel",
System.getProperty( "org.jomc.cli.commands.AbstractCommand.defaultLogLevel",
Level.WARNING.getName() ) ) );
}
return defaultLogLevel;
}
/**
* Sets the default log level events are logged at.
*
* @param value The new default level events are logged at or {@code null}.
*
* @see #getDefaultLogLevel()
*/
public static void setDefaultLogLevel( final Level value )
{
defaultLogLevel = value;
}
/**
* Checks if a message at a given level is provided to the listeners of the instance.
*
* @param level The level to test.
*
* @return {@code true}, if messages at {@code level} are provided to the listeners of the instance;
* {@code false}, if messages at {@code level} are not provided to the listeners of the instance.
*
* @throws NullPointerException if {@code level} is {@code null}.
*
* @see #getLogLevel()
* @see #setLogLevel(java.util.logging.Level)
*/
protected boolean isLoggable( final Level level )
{
if ( level == null )
{
throw new NullPointerException( "level" );
}
return level.intValue() >= this.getLogLevel().intValue();
}
/**
* Notifies registered listeners.
*
* @param level The level of the event.
* @param message The message of the event or {@code null}.
* @param throwable The throwable of the event {@code null}.
*
* @throws NullPointerException if {@code level} is {@code null}.
*
* @see #getListeners()
* @see #isLoggable(java.util.logging.Level)
*/
protected void log( final Level level, final String message, final Throwable throwable )
{
if ( level == null )
{
throw new NullPointerException( "level" );
}
if ( this.isLoggable( level ) )
{
for ( final Listener l : this.getListeners() )
{
l.onLog( level, message, throwable );
}
}
}
/**
* Called by the {@code execute} method prior to the {@code executeCommand} method.
*
* @param commandLine The command line to execute.
*
* @throws NullPointerException if {@code commandLine} is {@code null}.
* @throws CommandExecutionException if executing the command fails.
*
* @see #execute(org.apache.commons.cli.CommandLine)
*/
protected void preExecuteCommand( final CommandLine commandLine ) throws CommandExecutionException
{
if ( commandLine == null )
{
throw new NullPointerException( "commandLine" );
}
}
/**
* Called by the {@code execute} method prior to the {@code postExecuteCommand} method.
*
* @param commandLine The command line to execute.
*
* @throws CommandExecutionException if executing the command fails.
*
* @see #execute(org.apache.commons.cli.CommandLine)
*/
protected abstract void executeCommand( final CommandLine commandLine ) throws CommandExecutionException;
/**
* Called by the {@code execute} method after the {@code preExecuteCommand}/{@code executeCommand} methods even if
* those methods threw an exception.
*
* @param commandLine The command line to execute.
*
* @throws NullPointerException if {@code commandLine} is {@code null}.
* @throws CommandExecutionException if executing the command fails.
*
* @see #execute(org.apache.commons.cli.CommandLine)
*/
protected void postExecuteCommand( final CommandLine commandLine ) throws CommandExecutionException
{
if ( commandLine == null )
{
throw new NullPointerException( "commandLine" );
}
}
/**
* Gets a message of a given {@code Throwable} recursively.
*
* @param t The {@code Throwable} to get the message of or {@code null}.
*
* @return The message associated with {@code t} or {@code null}.
*/
protected static String getExceptionMessage( final Throwable t )
{
return t != null
? t.getMessage() != null && t.getMessage().trim().length() > 0
? t.getMessage()
: getExceptionMessage( t.getCause() )
: null;
}
// SECTION-END
// SECTION-START[Constructors]
// <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
/** Creates a new {@code AbstractCommand} instance. */
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
public AbstractCommand()
{
// SECTION-START[Default Constructor]
super();
// SECTION-END
}
// </editor-fold>
// SECTION-END
// SECTION-START[Dependencies]
// <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
/**
* Gets the {@code <Locale>} dependency.
* <p>
* This method returns the {@code <default>} object of the {@code <java.util.Locale>} specification at specification level 1.1.
* That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
* </p>
* <dl>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl>
* @return The {@code <Locale>} dependency.
* @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private java.util.Locale getLocale()
{
final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
assert _d != null : "'Locale' dependency not found.";
return _d;
}
// </editor-fold>
// SECTION-END
// SECTION-START[Properties]
// <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
/**
* Gets the value of the {@code <Abbreviated Command Name>} property.
* <p><dl>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @return Abbreviated name of the command.
* @throws org.jomc.ObjectManagementException if getting the property instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private java.lang.String getAbbreviatedCommandName()
{
final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "Abbreviated Command Name" );
assert _p != null : "'Abbreviated Command Name' property not found.";
return _p;
}
/**
* Gets the value of the {@code <Command Name>} property.
* <p><dl>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @return Name of the command.
* @throws org.jomc.ObjectManagementException if getting the property instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private java.lang.String getCommandName()
{
final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "Command Name" );
assert _p != null : "'Command Name' property not found.";
return _p;
}
// </editor-fold>
// SECTION-END
// SECTION-START[Messages]
// <editor-fold defaultstate="collapsed" desc=" Generated Messages ">
/**
* Gets the text of the {@code <Application Title>} message.
* <p><dl>
* <dt><b>Languages:</b></dt>
* <dd>English (default)</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @param locale The locale of the message to return.
* @return The text of the {@code <Application Title>} message for {@code locale}.
* @throws org.jomc.ObjectManagementException if getting the message instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private String getApplicationTitle( final java.util.Locale locale )
{
final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Application Title", locale );
assert _m != null : "'Application Title' message not found.";
return _m;
}
/**
* Gets the text of the {@code <Command Failure Message>} message.
* <p><dl>
* <dt><b>Languages:</b></dt>
* <dd>English (default)</dd>
* <dd>Deutsch</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @param locale The locale of the message to return.
* @param toolName Format argument.
* @return The text of the {@code <Command Failure Message>} message for {@code locale}.
* @throws org.jomc.ObjectManagementException if getting the message instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private String getCommandFailureMessage( final java.util.Locale locale, final java.lang.String toolName )
{
final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Command Failure Message", locale, toolName );
assert _m != null : "'Command Failure Message' message not found.";
return _m;
}
/**
* Gets the text of the {@code <Command Info Message>} message.
* <p><dl>
* <dt><b>Languages:</b></dt>
* <dd>English (default)</dd>
* <dd>Deutsch</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @param locale The locale of the message to return.
* @param toolName Format argument.
* @return The text of the {@code <Command Info Message>} message for {@code locale}.
* @throws org.jomc.ObjectManagementException if getting the message instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private String getCommandInfoMessage( final java.util.Locale locale, final java.lang.String toolName )
{
final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Command Info Message", locale, toolName );
assert _m != null : "'Command Info Message' message not found.";
return _m;
}
/**
* Gets the text of the {@code <Command Success Message>} message.
* <p><dl>
* <dt><b>Languages:</b></dt>
* <dd>English (default)</dd>
* <dd>Deutsch</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @param locale The locale of the message to return.
* @param toolName Format argument.
* @return The text of the {@code <Command Success Message>} message for {@code locale}.
* @throws org.jomc.ObjectManagementException if getting the message instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private String getCommandSuccessMessage( final java.util.Locale locale, final java.lang.String toolName )
{
final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Command Success Message", locale, toolName );
assert _m != null : "'Command Success Message' message not found.";
return _m;
}
/**
* Gets the text of the {@code <Default Log Level Info>} message.
* <p><dl>
* <dt><b>Languages:</b></dt>
* <dd>English (default)</dd>
* <dd>Deutsch</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @param locale The locale of the message to return.
* @param defaultLogLevel Format argument.
* @return The text of the {@code <Default Log Level Info>} message for {@code locale}.
* @throws org.jomc.ObjectManagementException if getting the message instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private String getDefaultLogLevelInfo( final java.util.Locale locale, final java.lang.String defaultLogLevel )
{
final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Default Log Level Info", locale, defaultLogLevel );
assert _m != null : "'Default Log Level Info' message not found.";
return _m;
}
/**
* Gets the text of the {@code <Long Description Message>} message.
* <p><dl>
* <dt><b>Languages:</b></dt>
* <dd>English (default)</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @param locale The locale of the message to return.
* @return The text of the {@code <Long Description Message>} message for {@code locale}.
* @throws org.jomc.ObjectManagementException if getting the message instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private String getLongDescriptionMessage( final java.util.Locale locale )
{
final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Long Description Message", locale );
assert _m != null : "'Long Description Message' message not found.";
return _m;
}
/**
* Gets the text of the {@code <Separator>} message.
* <p><dl>
* <dt><b>Languages:</b></dt>
* <dd>English (default)</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @param locale The locale of the message to return.
* @return The text of the {@code <Separator>} message for {@code locale}.
* @throws org.jomc.ObjectManagementException if getting the message instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private String getSeparator( final java.util.Locale locale )
{
final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Separator", locale );
assert _m != null : "'Separator' message not found.";
return _m;
}
/**
* Gets the text of the {@code <Short Description Message>} message.
* <p><dl>
* <dt><b>Languages:</b></dt>
* <dd>English (default)</dd>
* <dt><b>Final:</b></dt><dd>No</dd>
* </dl></p>
* @param locale The locale of the message to return.
* @return The text of the {@code <Short Description Message>} message for {@code locale}.
* @throws org.jomc.ObjectManagementException if getting the message instance fails.
*/
@SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
private String getShortDescriptionMessage( final java.util.Locale locale )
{
final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Short Description Message", locale );
assert _m != null : "'Short Description Message' message not found.";
return _m;
}
// </editor-fold>
// SECTION-END
// SECTION-START[Generated Command]
// <editor-fold defaultstate="collapsed" desc=" Generated Options ">
/**
* Gets the options of the command.
* <p><strong>Options:</strong>
* <table border="1" width="100%" cellpadding="3" cellspacing="0">
* <tr class="TableSubHeadingColor">
* <th align="left" scope="col" nowrap><b>Specification</b></th>
* <th align="left" scope="col" nowrap><b>Implementation</b></th>
* </tr>
* </table>
* </p>
* @return The options of the command.
*/
@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
public org.apache.commons.cli.Options getOptions()
{
final org.apache.commons.cli.Options options = new org.apache.commons.cli.Options();
return options;
}
// </editor-fold>
// SECTION-END
}