jetty-start / HomeBase -> BaseHome and fix to ini expansion order

This commit is contained in:
Joakim Erdfelt 2013-08-19 16:17:32 -07:00
parent 8e66da396e
commit f08aa88fbd
5 changed files with 142 additions and 124 deletions

View File

@ -36,19 +36,19 @@ import java.util.Objects;
* <p> * <p>
* The <code>${jetty.base}</code> directory is where the execution specific configuration and webapps are obtained from. * The <code>${jetty.base}</code> directory is where the execution specific configuration and webapps are obtained from.
*/ */
public class HomeBase public class BaseHome
{ {
private File homeDir; private File homeDir;
private File baseDir; private File baseDir;
public HomeBase() public BaseHome()
{ {
String userDir = System.getProperty("user.dir"); String userDir = System.getProperty("user.dir");
this.homeDir = new File(System.getProperty("jetty.home",userDir)); this.homeDir = new File(System.getProperty("jetty.home",userDir));
this.baseDir = new File(System.getProperty("jetty.base",homeDir.getAbsolutePath())); this.baseDir = new File(System.getProperty("jetty.base",homeDir.getAbsolutePath()));
} }
public HomeBase(File homeDir, File baseDir) public BaseHome(File homeDir, File baseDir)
{ {
this.homeDir = homeDir; this.homeDir = homeDir;
this.baseDir = baseDir==null?homeDir:baseDir; this.baseDir = baseDir==null?homeDir:baseDir;
@ -259,12 +259,7 @@ public class HomeBase
return path; return path;
} }
String value = homeDir.getAbsolutePath(); String value;
if (path.startsWith(value))
{
return "${jetty.home}" + path.substring(value.length());
}
if (isBaseDifferent()) if (isBaseDifferent())
{ {
@ -274,6 +269,14 @@ public class HomeBase
return "${jetty.base}" + path.substring(value.length()); return "${jetty.base}" + path.substring(value.length());
} }
} }
value = homeDir.getAbsolutePath();
if (path.startsWith(value))
{
return "${jetty.home}" + path.substring(value.length());
}
return path; return path;
} }
} }

View File

@ -49,8 +49,8 @@ import java.util.TreeSet;
* </p> * </p>
* *
* <p> * <p>
* The behaviour of Main is controlled by the <code>"org/eclipse/start/start.config"</code> file obtained as a resource * The behaviour of Main is controlled by the <code>"org/eclipse/start/start.config"</code> file obtained as a resource or file. This can be overridden with the
* or file. This can be overridden with the START system property. The format of each line in this file is: * START system property. The format of each line in this file is:
* </p> * </p>
* *
* <p> * <p>
@ -101,18 +101,15 @@ import java.util.TreeSet;
* <li><code>exists file</code> - true if file/dir exists</li> * <li><code>exists file</code> - true if file/dir exists</li>
* <li><code>java OPERATOR version</code> - java version compared to literal</li> * <li><code>java OPERATOR version</code> - java version compared to literal</li>
* <li><code>nargs OPERATOR number</code> - number of command line args compared to literal</li> * <li><code>nargs OPERATOR number</code> - number of command line args compared to literal</li>
* <li>OPERATOR := one of <code>"&lt;"</code>,<code>"&gt;"</code>,<code>"&lt;="</code>,<code>"&gt;="</code>, * <li>OPERATOR := one of <code>"&lt;"</code>,<code>"&gt;"</code>,<code>"&lt;="</code>,<code>"&gt;="</code>, <code>"=="</code>,<code>"!="</code></li>
* <code>"=="</code>,<code>"!="</code></li>
* </ul> * </ul>
* *
* <p> * <p>
* CONDITIONS can be combined with <code>AND</code> <code>OR</code> or <code>!</code>, with <code>AND</code> being the * CONDITIONS can be combined with <code>AND</code> <code>OR</code> or <code>!</code>, with <code>AND</code> being the assume operator for a list of CONDITIONS.
* assume operator for a list of CONDITIONS.
* </p> * </p>
* *
* <p> * <p>
* Classpath operations are evaluated on the fly, so once a class or jar is added to the classpath, subsequent available * Classpath operations are evaluated on the fly, so once a class or jar is added to the classpath, subsequent available conditions will see that class.
* conditions will see that class.
* </p> * </p>
* *
* <p> * <p>
@ -120,13 +117,13 @@ import java.util.TreeSet;
* </p> * </p>
* *
* <p> * <p>
* Note: a special discovered section identifier <code>[=path_to_directory/*]</code> is allowed to auto-create section * Note: a special discovered section identifier <code>[=path_to_directory/*]</code> is allowed to auto-create section IDs, based on directory names found in
* IDs, based on directory names found in the path specified in the "path_to_directory/" part of the identifier. * the path specified in the "path_to_directory/" part of the identifier.
* </p> * </p>
* *
* <p> * <p>
* Clauses after a section header will only be included if they match one of the tags in the options property. By * Clauses after a section header will only be included if they match one of the tags in the options property. By default options are set to "default,*" or the
* default options are set to "default,*" or the OPTIONS property may be used to pass in a list of tags, eg. : * OPTIONS property may be used to pass in a list of tags, eg. :
* </p> * </p>
* *
* <pre> * <pre>
@ -148,11 +145,10 @@ public class Config
{ {
String ver = System.getProperty("jetty.version",null); String ver = System.getProperty("jetty.version",null);
if(ver == null) { if (ver == null)
{
Package pkg = Config.class.getPackage(); Package pkg = Config.class.getPackage();
if (pkg != null && if (pkg != null && "Eclipse.org - Jetty".equals(pkg.getImplementationVendor()) && (pkg.getImplementationVersion() != null))
"Eclipse.org - Jetty".equals(pkg.getImplementationVendor()) &&
(pkg.getImplementationVersion() != null))
{ {
ver = pkg.getImplementationVersion(); ver = pkg.getImplementationVersion();
} }
@ -174,7 +170,7 @@ public class Config
private static boolean DEBUG = false; private static boolean DEBUG = false;
private static Config __instance; private static Config __instance;
private final HomeBase _homebase; private final BaseHome _homebase;
private final Map<String, String> _properties = new HashMap<String, String>(); private final Map<String, String> _properties = new HashMap<String, String>();
private final Map<String, Classpath> _classpaths = new HashMap<String, Classpath>(); private final Map<String, Classpath> _classpaths = new HashMap<String, Classpath>();
private final List<String> _xml = new ArrayList<String>(); private final List<String> _xml = new ArrayList<String>();
@ -202,12 +198,12 @@ public class Config
public Config() public Config()
{ {
__instance = this; __instance = this;
_homebase = new HomeBase(); _homebase = new BaseHome();
setProperty("jetty.home",_homebase.getHome()); setProperty("jetty.home",_homebase.getHome());
setProperty("jetty.base",_homebase.getBase()); setProperty("jetty.base",_homebase.getBase());
} }
public HomeBase getHomeBase() public BaseHome getBaseHome()
{ {
return _homebase; return _homebase;
} }
@ -380,8 +376,7 @@ public class Config
* *
* @param optionIds * @param optionIds
* the list of section ids to fetch * the list of section ids to fetch
* @return the {@link Classpath} representing combination all of the selected sectionIds, combined with the default * @return the {@link Classpath} representing combination all of the selected sectionIds, combined with the default section id, and '*' special id.
* section id, and '*' special id.
*/ */
public Classpath getCombinedClasspath(Collection<String> optionIds) public Classpath getCombinedClasspath(Collection<String> optionIds)
{ {
@ -417,12 +412,14 @@ public class Config
Properties properties = new Properties(); Properties properties = new Properties();
// Add System Properties First // Add System Properties First
Enumeration<?> ensysprop = System.getProperties().propertyNames(); Enumeration<?> ensysprop = System.getProperties().propertyNames();
while(ensysprop.hasMoreElements()) { while (ensysprop.hasMoreElements())
{
String name = (String)ensysprop.nextElement(); String name = (String)ensysprop.nextElement();
properties.put(name,System.getProperty(name)); properties.put(name,System.getProperty(name));
} }
// Add Config Properties Next (overwriting any System Properties that exist) // Add Config Properties Next (overwriting any System Properties that exist)
for (String key : __instance._properties.keySet()) { for (String key : __instance._properties.keySet())
{
properties.put(key,__instance._properties.get(key)); properties.put(key,__instance._properties.get(key));
} }
return properties; return properties;
@ -430,11 +427,13 @@ public class Config
public String getProperty(String name) public String getProperty(String name)
{ {
if ("version".equalsIgnoreCase(name)) { if ("version".equalsIgnoreCase(name))
{
return __version; return __version;
} }
// Search Config Properties First // Search Config Properties First
if (_properties.containsKey(name)) { if (_properties.containsKey(name))
{
return _properties.get(name); return _properties.get(name);
} }
// Return what exists in System.Properties otherwise. // Return what exists in System.Properties otherwise.
@ -543,7 +542,8 @@ public class Config
/** /**
* Parse the configuration * Parse the configuration
* *
* @param stream the stream to read from * @param stream
* the stream to read from
* @throws IOException * @throws IOException
*/ */
public void parse(InputStream stream) throws IOException public void parse(InputStream stream) throws IOException
@ -598,7 +598,6 @@ public class Config
option_ids.add(optionId); option_ids.add(optionId);
} }
// Process Dynamic // Process Dynamic
for (String optionId : options) for (String optionId : options)
{ {

View File

@ -121,21 +121,26 @@ public class Main implements IncludeListener
{ {
String source = ""; String source = "";
// Handle default ini args
ArrayList<String> arguments = new ArrayList<>(Arrays.asList(args)); ArrayList<String> arguments = new ArrayList<>(Arrays.asList(args));
boolean ini = false;
for (String arg : arguments) // Ensure ini is declared
{ if(findArgumentPrefix(arguments, "--ini=") == null) {
if (arg.startsWith("--ini=") || arg.equals("--ini"))
{
ini = true;
}
}
if (!ini)
{
arguments.add(0,"--ini=start.ini"); arguments.add(0,"--ini=start.ini");
} }
// Set Home and Base at the start, as all other paths encountered
// will be based off of them.
String home = findArgumentPrefix(arguments, "-Djetty.home=");
if(home != null) {
String value = home.replaceFirst("^[^=]*=","");
_config.getBaseHome().setHomeDir(new File(value));
}
String base = findArgumentPrefix(arguments, "-Djetty.base=");
if(base != null) {
String value = base.replaceFirst("^[^=]*=","");
_config.getBaseHome().setBaseDir(new File(value));
}
// The XML Configuration Files to initialize with // The XML Configuration Files to initialize with
List<String> xmls = new ArrayList<String>(); List<String> xmls = new ArrayList<String>();
@ -216,11 +221,10 @@ public class Main implements IncludeListener
if (arg.startsWith("--ini=") || arg.equals("--ini")) if (arg.startsWith("--ini=") || arg.equals("--ini"))
{ {
ini = true;
if (arg.length() > 6) if (arg.length() > 6)
{ {
String name = arg.substring(6); String name = arg.substring(6);
File file = _config.getHomeBase().getFile(name); File file = _config.getBaseHome().getFile(name);
StartIni startini = new StartIni(file,this); StartIni startini = new StartIni(file,this);
_iniFiles.add(file); _iniFiles.add(file);
arguments.addAll(i + 1,startini.getLines()); arguments.addAll(i + 1,startini.getLines());
@ -378,12 +382,24 @@ public class Main implements IncludeListener
return xmls; return xmls;
} }
private String findArgumentPrefix(ArrayList<String> arguments, String prefix)
{
for (String arg : arguments)
{
if (arg.startsWith(prefix))
{
return arg;
}
}
return null;
}
@Override @Override
public List<StartIni> onIniInclude(String path) throws IOException public List<StartIni> onIniInclude(String path) throws IOException
{ {
List<StartIni> included = new ArrayList<>(); List<StartIni> included = new ArrayList<>();
HomeBase hb = _config.getHomeBase(); BaseHome hb = _config.getBaseHome();
// Allow --enable and --disable to work // Allow --enable and --disable to work
_iniIncludePaths.add(path); _iniIncludePaths.add(path);
@ -498,7 +514,7 @@ public class Main implements IncludeListener
}; };
// list etc // list etc
List<File> configFiles = _config.getHomeBase().listFiles("etc",filter); List<File> configFiles = _config.getBaseHome().listFiles("etc",filter);
for (File configFile : configFiles) for (File configFile : configFiles)
{ {
@ -509,7 +525,7 @@ public class Main implements IncludeListener
{ {
for (File file : _iniFiles) for (File file : _iniFiles)
{ {
String path = _config.getHomeBase().toShortForm(file); String path = _config.getBaseHome().toShortForm(file);
System.out.printf("%s%s%n",indent,path); System.out.printf("%s%s%n",indent,path);
if (Config.isDebug()) if (Config.isDebug())
@ -542,12 +558,12 @@ public class Main implements IncludeListener
private String path(String path) private String path(String path)
{ {
return _config.getHomeBase().toShortForm(path); return _config.getBaseHome().toShortForm(path);
} }
private String path(File file) private String path(File file)
{ {
return _config.getHomeBase().toShortForm(file); return _config.getBaseHome().toShortForm(file);
} }
public void invokeMain(ClassLoader classloader, String classname, List<String> args) throws IllegalAccessException, InvocationTargetException, public void invokeMain(ClassLoader classloader, String classname, List<String> args) throws IllegalAccessException, InvocationTargetException,
@ -622,7 +638,7 @@ public class Main implements IncludeListener
System.err.println("java.class.path=" + classpath); System.err.println("java.class.path=" + classpath);
System.err.println("classloader=" + cl); System.err.println("classloader=" + cl);
System.err.println("classloader.parent=" + cl.getParent()); System.err.println("classloader.parent=" + cl.getParent());
System.err.println("properties=" + _config.getProperties()); System.err.println("properties=" + Config.getProperties());
} }
for (String m : _enable) for (String m : _enable)
@ -753,14 +769,14 @@ public class Main implements IncludeListener
} }
// Try normal locations // Try normal locations
File xml = _config.getHomeBase().getFile(xmlFilename); File xml = _config.getBaseHome().getFile(xmlFilename);
if (FS.isFile(xml)) if (FS.isFile(xml))
{ {
return xml.getAbsolutePath(); return xml.getAbsolutePath();
} }
// Try again, but prefixed with "etc/" // Try again, but prefixed with "etc/"
xml = _config.getHomeBase().getFile("etc/" + xmlFilename); xml = _config.getBaseHome().getFile("etc/" + xmlFilename);
if (FS.isFile(xml)) if (FS.isFile(xml))
{ {
return xml.getAbsolutePath(); return xml.getAbsolutePath();
@ -777,8 +793,8 @@ public class Main implements IncludeListener
{ {
cmd.addArg(x); cmd.addArg(x);
} }
cmd.addRawArg("-Djetty.home=" + _config.getHomeBase().getHome()); cmd.addRawArg("-Djetty.home=" + _config.getBaseHome().getHome());
cmd.addRawArg("-Djetty.base=" + _config.getHomeBase().getBase()); cmd.addRawArg("-Djetty.base=" + _config.getBaseHome().getBase());
// Special Stop/Shutdown properties // Special Stop/Shutdown properties
ensureSystemPropertySet("STOP.PORT"); ensureSystemPropertySet("STOP.PORT");
@ -796,7 +812,7 @@ public class Main implements IncludeListener
cmd.addRawArg(_config.getMainClassname()); cmd.addRawArg(_config.getMainClassname());
// Check if we need to pass properties as a file // Check if we need to pass properties as a file
Properties properties = _config.getProperties(); Properties properties = Config.getProperties();
if (properties.size() > 0) if (properties.size() > 0)
{ {
File prop_file = File.createTempFile("start",".properties"); File prop_file = File.createTempFile("start",".properties");
@ -826,7 +842,7 @@ public class Main implements IncludeListener
return; // done return; // done
} }
Properties props = _config.getProperties(); Properties props = Config.getProperties();
if (props.containsKey(key)) if (props.containsKey(key))
{ {
String val = props.getProperty(key,null); String val = props.getProperty(key,null);
@ -900,8 +916,8 @@ public class Main implements IncludeListener
System.out.println("Note: If using multiple options (eg: 'Server,servlet,webapp,jms,jmx') " System.out.println("Note: If using multiple options (eg: 'Server,servlet,webapp,jms,jmx') "
+ "then overlapping entries will not be repeated in the eventual classpath."); + "then overlapping entries will not be repeated in the eventual classpath.");
System.out.println(); System.out.println();
System.out.printf("${jetty.home} = %s%n",_config.getHomeBase().getHome()); System.out.printf("${jetty.home} = %s%n",_config.getBaseHome().getHome());
System.out.printf("${jetty.base} = %s%n",_config.getHomeBase().getBase()); System.out.printf("${jetty.base} = %s%n",_config.getBaseHome().getBase());
System.out.println(); System.out.println();
for (String sectionId : sectionIds) for (String sectionId : sectionIds)
@ -1188,7 +1204,7 @@ public class Main implements IncludeListener
FileFilter disabledModuleFilter = new FS.FilenameRegexFilter("(\\d\\d\\d-)?"+Pattern.quote(module)+"\\.ini(\\.disabled)?"); FileFilter disabledModuleFilter = new FS.FilenameRegexFilter("(\\d\\d\\d-)?"+Pattern.quote(module)+"\\.ini(\\.disabled)?");
HomeBase hb = _config.getHomeBase(); BaseHome hb = _config.getBaseHome();
// walk all ini include paths that were used // walk all ini include paths that were used
boolean found = false; boolean found = false;
@ -1247,7 +1263,7 @@ public class Main implements IncludeListener
FileFilter disabledModuleFilter = new FS.FilenameRegexFilter("(\\d\\d\\d-)?"+Pattern.quote(module)+"\\.ini(\\.disabled)?"); FileFilter disabledModuleFilter = new FS.FilenameRegexFilter("(\\d\\d\\d-)?"+Pattern.quote(module)+"\\.ini(\\.disabled)?");
HomeBase hb = _config.getHomeBase(); BaseHome hb = _config.getBaseHome();
// walk all ini include paths that were used // walk all ini include paths that were used
boolean found = false; boolean found = false;

View File

@ -90,7 +90,7 @@ public class StartIni implements Iterable<String>
} }
else else
{ {
// Collect HomeBase resolved included StartIni's // Collect BaseHome resolved included StartIni's
for (StartIni included : listener.onIniInclude(line)) for (StartIni included : listener.onIniInclude(line))
{ {
// Merge each line with prior lines to prevent duplicates // Merge each line with prior lines to prevent duplicates

View File

@ -30,9 +30,9 @@ import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class HomeBaseTest public class BaseHomeTest
{ {
private void assertFileList(HomeBase hb, String message, List<String> expected, List<File> files) private void assertFileList(BaseHome hb, String message, List<String> expected, List<File> files)
{ {
List<String> actual = new ArrayList<>(); List<String> actual = new ArrayList<>();
for (File file : files) for (File file : files)
@ -48,7 +48,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home"); File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
File baseDir = null; File baseDir = null;
HomeBase hb = new HomeBase(homeDir,baseDir); BaseHome hb = new BaseHome(homeDir,baseDir);
File startIni = hb.getFile("/start.ini"); File startIni = hb.getFile("/start.ini");
String ref = hb.toShortForm(startIni); String ref = hb.toShortForm(startIni);
@ -64,7 +64,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home"); File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
File baseDir = null; File baseDir = null;
HomeBase hb = new HomeBase(homeDir,baseDir); BaseHome hb = new BaseHome(homeDir,baseDir);
List<File> files = hb.listFiles("/start.d"); List<File> files = hb.listFiles("/start.d");
List<String> expected = new ArrayList<>(); List<String> expected = new ArrayList<>();
@ -83,7 +83,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home"); File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
File baseDir = null; File baseDir = null;
HomeBase hb = new HomeBase(homeDir,baseDir); BaseHome hb = new BaseHome(homeDir,baseDir);
List<File> files = hb.listFiles("/start.d", new FS.IniFilter()); List<File> files = hb.listFiles("/start.d", new FS.IniFilter());
List<String> expected = new ArrayList<>(); List<String> expected = new ArrayList<>();
@ -102,7 +102,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home"); File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
File baseDir = MavenTestingUtils.getTestResourceDir("hb.1/base"); File baseDir = MavenTestingUtils.getTestResourceDir("hb.1/base");
HomeBase hb = new HomeBase(homeDir,baseDir); BaseHome hb = new BaseHome(homeDir,baseDir);
List<File> files = hb.listFiles("/start.d"); List<File> files = hb.listFiles("/start.d");
List<String> expected = new ArrayList<>(); List<String> expected = new ArrayList<>();
@ -122,7 +122,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home"); File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
File baseDir = MavenTestingUtils.getTestResourceDir("hb.1/base"); File baseDir = MavenTestingUtils.getTestResourceDir("hb.1/base");
HomeBase hb = new HomeBase(homeDir,baseDir); BaseHome hb = new BaseHome(homeDir,baseDir);
File startIni = hb.getFile("/start.ini"); File startIni = hb.getFile("/start.ini");
String ref = hb.toShortForm(startIni); String ref = hb.toShortForm(startIni);