[DS-3158] Move the command method somewhere less deprecated.

This code no longer has any connection with the deprecated class
Configurationmanager, so move it now before we lose it by accident.
This commit is contained in:
Mark H. Wood
2016-04-26 14:39:38 -04:00
parent b644471da0
commit be0d578ee7
3 changed files with 111 additions and 96 deletions

View File

@@ -0,0 +1,110 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.util;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
/**
* Configuration tools.
*
* @author mwood
*/
public class Configuration
{
/**
* Command-line interface for running configuration tasks. Possible
* arguments:
* <ul>
* <li>{@code --module name} the name of the configuration "module" for this property.</li>
* <li>{@code --property name} prints the value of the DSpace configuration
* property {@code name} to the standard output.</li>
* <li>{@code --raw} suppresses parameter substitution in the output.</li>
* <li>{@code --help} describes these options.</li>
* </ul>
* If the property does not exist, nothing is written.
*
* @param argv
* command-line arguments
*/
public static void main(String[] argv)
{
// Build a description of the command line
Options options = new Options();
options.addOption("p", "property", true, "name of the desired property");
options.addOption("m", "module", true,
"optional name of the module in which 'property' exists");
options.addOption("r", "raw", false,
"do not do property substitution on the value");
options.addOption("?", "Get help");
options.addOption("h", "help", false, "Get help");
// Analyze the command line
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try
{
cmd = parser.parse(options, argv);
} catch (ParseException ex)
{
System.err.println(ex.getMessage());
System.exit(1);
}
// Give help if asked
if (cmd.hasOption('?') || cmd.hasOption('h'))
{
new HelpFormatter().printHelp("dsprop [options]",
"Display the value of a DSpace configuration property",
options,
"If --module is omitted, then --property gives the entire" +
" name of the property. Otherwise the name is" +
" composed of module.property.");
System.exit(0);
}
// Check for missing required values
if (!cmd.hasOption('p'))
{
System.err.println("Error: -p is required");
System.exit(1);
}
// Figure out the property's full name
StringBuilder propNameBuilder = new StringBuilder(1024);
propNameBuilder.append(cmd.getOptionValue('p'));
if (cmd.hasOption('m'))
{
propNameBuilder.append('.').append(cmd.getOptionValue('m'));
}
String propName = propNameBuilder.toString();
// Print the property's value, if it exists
ConfigurationService cfg = DSpaceServicesFactory.getInstance().getConfigurationService();
if (!cfg.hasProperty(propName))
{
System.out.println();
}
else
{
String val;
if (cmd.hasOption('r'))
{
val = cfg.getPropertyValue(propName).toString();
}
else
{
val = cfg.getProperty(propName);
}
System.out.println(val);
}
System.exit(0);
}
}

View File

@@ -9,17 +9,10 @@ package org.dspace.core;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationConverter;
import org.apache.log4j.Logger;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
/**
@@ -419,92 +412,4 @@ public class ConfigurationManager
// Get property keys beginning with this prefix, and convert into an Enumeration
return java.util.Collections.enumeration(DSpaceServicesFactory.getInstance().getConfigurationService().getPropertyKeys(module));
}
/**
* Command-line interface for running configuration tasks. Possible
* arguments:
* <ul>
* <li>{@code -module name} the name of the configuration "module" for this property.</li>
* <li>{@code -property name} prints the value of the DSpace configuration
* property {@code name} to the standard output.</li>
* </ul>
* If the property does not exist, nothing is written.
*
* @param argv
* command-line arguments
*/
public static void main(String[] argv)
{
// Build a description of the command line
Options options = new Options();
options.addOption("p", "property", true,
"name of the desired property");
options.addOption("m", "module", true,
"optional name of the module in which 'property' exists");
options.addOption("r", "raw", false,
"do not do property substitution on the value");
options.addOption("?", "Get help");
options.addOption("h", "help", false, "Get help");
// Analyze the command line
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, argv);
} catch (ParseException ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
// Give help if asked
if (cmd.hasOption('?') || cmd.hasOption('h'))
{
new HelpFormatter().printHelp(
"dsprop [options]",
"Display the value of a DSpace configuration property",
options,
"If --module is omitted, then --property gives the entire"
+ " name of the property. Otherwise the name is"
+ " composed of module.property.");
System.exit(0);
}
// Check for missing required values
if (!cmd.hasOption('p'))
{
System.err.println("Error: -p is required");
System.exit(1);
}
// Figure out the property's full name
StringBuilder propNameBuilder = new StringBuilder(1024);
propNameBuilder.append(cmd.getOptionValue('p'));
if (cmd.hasOption('m'))
propNameBuilder.append('.')
.append(cmd.getOptionValue('m'));
String propName = propNameBuilder.toString();
// Print the property's value, if it exists
ConfigurationService cfg
= DSpaceServicesFactory.getInstance().getConfigurationService();
if (!cfg.hasProperty(propName))
{
System.out.println();
}
else
{
String val;
if (cmd.hasOption('r'))
val = cfg.getPropertyValue(propName).toString();
else
val = cfg.getProperty(propName);
System.out.println(val);
}
System.exit(0);
}
}

View File

@@ -79,7 +79,7 @@
<name>dsprop</name>
<description>View a DSpace property from dspace.cfg</description>
<step>
<class>org.dspace.core.ConfigurationManager</class>
<class>org.dspace.app.util.Configuration</class>
</step>
</command>
<command>