310094 Improved start.jar options handling and configs

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1682 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-05-03 23:01:42 +00:00
parent fb55975e42
commit cc6b89040f
2 changed files with 103 additions and 36 deletions

View File

@ -49,13 +49,14 @@ import org.eclipse.jetty.start.log.RedirectedStreamLogger;
*
* <p>
* The behaviour of Main is controlled by the parsing of the {@link Config} "org/eclipse/start/start.config" file
* obtained as a resource or file. This can be overridden with the START system property.
* obtained as a resource or file.
* </p>
*/
public class Main
{
private boolean _showUsage = false;
private boolean _dumpVersions = false;
private boolean _listConfig = false;
private boolean _listOptions = false;
private boolean _dryRun = false;
private boolean _exec = false;
@ -64,6 +65,7 @@ public class Main
private final Config _config = new Config();
private Set<String> _sysProps = new HashSet<String>();
private List<String> _jvmArgs = new ArrayList<String>();
private String _startConfig = null;
private String _jettyHome;
@ -86,27 +88,31 @@ public class Main
{
List<String> arguments = new ArrayList<String>();
// do we have any non option args
boolean has_args=false;
for (String arg : args)
has_args|=!arg.startsWith("-");
// if no non-option args, add the start.ini
if (!has_args)
arguments.addAll(loadStartIni()); // Add Arguments from start.ini (if it exists)
// add the command line args
// add the command line args and look for start.ini args
boolean ini=false;
for (String arg : args)
{
if ("%start.ini".equals(arg))
if (arg.startsWith("--ini="))
{
arguments.addAll(loadStartIni());
continue;
ini=true;
if (arg.length()>6)
{
arguments.addAll(loadStartIni(arg.substring(6)));
continue;
}
}
arguments.add(arg);
else if (arg.startsWith("--config="))
{
_startConfig=arg.substring(9);
}
else
arguments.add(arg);
}
// if no non-option inis, add the start.ini
if (!ini)
arguments.addAll(0,loadStartIni(null));
// The XML Configuration Files to initialize with
List<String> xmls = new ArrayList<String>();
@ -140,6 +146,12 @@ public class Main
continue;
}
if ("--list-config".equals(arg))
{
_listConfig=true;
continue;
}
if ("--exec-print".equals(arg)||"--dry-run".equals(arg))
{
_dryRun = true;
@ -235,12 +247,14 @@ public class Main
/**
* If a start.ini is present in the CWD, then load it into the argument list.
*/
private List<String> loadStartIni()
private List<String> loadStartIni(String ini)
{
String jettyHome=System.getProperty("jetty.home");
File startIniFile = (jettyHome!=null)? new File(jettyHome,"start.ini"):new File("start.ini");
File startIniFile = ini==null?((jettyHome!=null)? new File(jettyHome,"start.ini"):new File("start.ini")):new File(ini);
if (!startIniFile.exists() || !startIniFile.canRead())
{
if (ini!=null)
System.err.println("Warning - can't find ini file: "+ini);
// No start.ini found, skip load.
return Collections.emptyList();
}
@ -352,7 +366,7 @@ public class Main
}
else if (info.equals("@STARTINI"))
{
List<String> ini = loadStartIni();
List<String> ini = loadStartIni(null);
if (ini!=null && ini.size()>0)
{
for (String a : ini)
@ -535,6 +549,12 @@ public class Main
showAllOptionsWithVersions(classpath);
return;
}
if (_listConfig)
{
listConfig();
return;
}
// Show Command Line to execute Jetty
if (_dryRun)
@ -920,6 +940,34 @@ public class Main
return ret;
}
private void listConfig()
{
InputStream cfgstream = null;
try
{
cfgstream=getConfigStream();
byte[] buf=new byte[4096];
int len=0;
while (len>=0)
{
len=cfgstream.read(buf);
if (len>0)
System.out.write(buf,0,len);
}
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
finally
{
close(cfgstream);
}
}
/**
* Load Configuration.
*
@ -937,18 +985,9 @@ public class Main
{
// Pass in xmls.size into Config so that conditions based on "nargs" work.
_config.setArgCount(xmls.size());
// What start.config should we use?
String cfgName = System.getProperty("START","org/eclipse/jetty/start/start.config");
Config.debug("config=" + cfgName);
// Look up config as resource first.
cfgstream = getClass().getClassLoader().getResourceAsStream(cfgName);
// resource not found, try filesystem next
if (cfgstream == null)
cfgstream = new FileInputStream(cfgName);
cfgstream=getConfigStream();
// parse the config
_config.parse(cfgstream);
@ -985,6 +1024,24 @@ public class Main
}
}
private InputStream getConfigStream() throws FileNotFoundException
{
String config=_startConfig;
if (config==null || config.length()==0)
config=System.getProperty("START","org/eclipse/jetty/start/start.config");
Config.debug("config=" + config);
// Look up config as resource first.
InputStream cfgstream = getClass().getClassLoader().getResourceAsStream(config);
// resource not found, try filesystem next
if (cfgstream == null)
cfgstream = new FileInputStream(config);
return cfgstream;
}
private void startMonitor()
{
int port = Integer.parseInt(System.getProperty("STOP.PORT","-1"));

View File

@ -12,6 +12,8 @@ Command Line Options:
dependent jars, then exit.
--list-options List the details of each classpath OPTION
--list-config List the start.config file.
--dry-run Print the command line that the start.jar generates,
then exit. This may be used to generate command lines
@ -21,7 +23,6 @@ Command Line Options:
a sub processes. This can be used when start.ini
contains -X or -D arguments, but creates an extra
JVM instance.
--secure Enable Security:
* JVM Security Manager
@ -30,7 +31,17 @@ Command Line Options:
* Audit Logging
--stop Stop the running Jetty instance.
--config=<file> Specify an alternate start.config file.
The default is the start.config file inside
the start.jar. The default can also be specified
with the START system property.
--ini=<file> Load command line arguments from a file. If
no --ini options are specified, then the
start.ini file will be read if it exists.
A --ini option with no file indicates that
start.ini should not be read.
System Properties:
These are set with a command line like "java -Dname=value ..." and are
@ -101,9 +112,8 @@ Available Configurations:
Defaults:
A start.ini file may be used to specify default arguments to start.jar,
which are used if no command line arguments are provided and override
the defaults in the start.config file. If arguments are provided on the
command line, the special "%start.ini" argument may be used to include
the start.ini arguments and merge any OPTIONS. The current start.ini
the defaults in the start.config file. If --ini options are provided on
the command line, then start.ini will no be read. The current start.ini
arguments are:
@STARTINI@