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>
* 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 baseDir;
public HomeBase()
public BaseHome()
{
String userDir = System.getProperty("user.dir");
this.homeDir = new File(System.getProperty("jetty.home",userDir));
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.baseDir = baseDir==null?homeDir:baseDir;
@ -258,13 +258,8 @@ public class HomeBase
{
return path;
}
String value = homeDir.getAbsolutePath();
if (path.startsWith(value))
{
return "${jetty.home}" + path.substring(value.length());
}
String value;
if (isBaseDifferent())
{
@ -274,6 +269,14 @@ public class HomeBase
return "${jetty.base}" + path.substring(value.length());
}
}
value = homeDir.getAbsolutePath();
if (path.startsWith(value))
{
return "${jetty.home}" + path.substring(value.length());
}
return path;
}
}

View File

@ -49,8 +49,8 @@ import java.util.TreeSet;
* </p>
*
* <p>
* 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 START system property. The format of each line in this file is:
* 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
* START system property. The format of each line in this file is:
* </p>
*
* <p>
@ -101,18 +101,15 @@ import java.util.TreeSet;
* <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>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>,
* <code>"=="</code>,<code>"!="</code></li>
* <li>OPERATOR := one of <code>"&lt;"</code>,<code>"&gt;"</code>,<code>"&lt;="</code>,<code>"&gt;="</code>, <code>"=="</code>,<code>"!="</code></li>
* </ul>
*
* <p>
* 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.
* 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.
* </p>
*
* <p>
* 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.
* 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.
* </p>
*
* <p>
@ -120,13 +117,13 @@ import java.util.TreeSet;
* </p>
*
* <p>
* Note: a special discovered section identifier <code>[=path_to_directory/*]</code> is allowed to auto-create section
* IDs, based on directory names found in the path specified in the "path_to_directory/" part of the identifier.
* Note: a special discovered section identifier <code>[=path_to_directory/*]</code> is allowed to auto-create section IDs, based on directory names found in
* the path specified in the "path_to_directory/" part of the identifier.
* </p>
*
* <p>
* 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 OPTIONS property may be used to pass in a list of tags, eg. :
* 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
* OPTIONS property may be used to pass in a list of tags, eg. :
* </p>
*
* <pre>
@ -146,13 +143,12 @@ public class Config
public static final String DEFAULT_SECTION = "";
static
{
String ver = System.getProperty("jetty.version", null);
if(ver == null) {
String ver = System.getProperty("jetty.version",null);
if (ver == null)
{
Package pkg = Config.class.getPackage();
if (pkg != null &&
"Eclipse.org - Jetty".equals(pkg.getImplementationVendor()) &&
(pkg.getImplementationVersion() != null))
if (pkg != null && "Eclipse.org - Jetty".equals(pkg.getImplementationVendor()) && (pkg.getImplementationVersion() != null))
{
ver = pkg.getImplementationVersion();
}
@ -173,15 +169,15 @@ public class Config
private static final String __version;
private static boolean DEBUG = false;
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, Classpath> _classpaths = new HashMap<String, Classpath>();
private final List<String> _xml = new ArrayList<String>();
private String _classname = null;
private int argCount = 0;
private final Set<String> _options = new TreeSet<String>(new Comparator<String>()
{
// Make sure "*" is always at the end of the list
@ -198,16 +194,16 @@ public class Config
return o1.compareTo(o2);
}
});
public Config()
{
__instance=this;
_homebase = new HomeBase();
__instance = this;
_homebase = new BaseHome();
setProperty("jetty.home",_homebase.getHome());
setProperty("jetty.base",_homebase.getBase());
}
public HomeBase getHomeBase()
public BaseHome getBaseHome()
{
return _homebase;
}
@ -222,7 +218,7 @@ public class Config
}
return cp;
}
private boolean addClasspathComponent(List<String> sections, String component)
{
for (String section : sections)
@ -300,12 +296,12 @@ public class Config
System.err.println(msg);
}
}
public static void debug(String format, Object ... args)
public static void debug(String format, Object... args)
{
if (DEBUG)
{
System.err.printf(format+"%n",args);
System.err.printf(format + "%n",args);
}
}
@ -380,8 +376,7 @@ public class Config
*
* @param optionIds
* the list of section ids to fetch
* @return the {@link Classpath} representing combination all of the selected sectionIds, combined with the default
* section id, and '*' special id.
* @return the {@link Classpath} representing combination all of the selected sectionIds, combined with the default section id, and '*' special id.
*/
public Classpath getCombinedClasspath(Collection<String> optionIds)
{
@ -410,31 +405,35 @@ public class Config
{
_properties.clear();
}
/* This method is static so it can be accessed by XmlConfiguration */
/* This method is static so it can be accessed by XmlConfiguration */
public static Properties getProperties()
{
Properties properties = new Properties();
// Add System Properties First
Enumeration<?> ensysprop = System.getProperties().propertyNames();
while(ensysprop.hasMoreElements()) {
while (ensysprop.hasMoreElements())
{
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)
for (String key : __instance._properties.keySet()) {
for (String key : __instance._properties.keySet())
{
properties.put(key,__instance._properties.get(key));
}
return properties;
}
public String getProperty(String name)
{
if ("version".equalsIgnoreCase(name)) {
if ("version".equalsIgnoreCase(name))
{
return __version;
}
// Search Config Properties First
if (_properties.containsKey(name)) {
if (_properties.containsKey(name))
{
return _properties.get(name);
}
// Return what exists in System.Properties otherwise.
@ -447,7 +446,7 @@ public class Config
if (_properties.containsKey(name))
return _properties.get(name);
// Return what exists in System.Properties otherwise.
return System.getProperty(name, defaultValue);
return System.getProperty(name,defaultValue);
}
/**
@ -543,7 +542,8 @@ public class Config
/**
* Parse the configuration
*
* @param stream the stream to read from
* @param stream
* the stream to read from
* @throws IOException
*/
public void parse(InputStream stream) throws IOException
@ -558,7 +558,7 @@ public class Config
*/
public void parse(Reader reader) throws IOException
{
try(BufferedReader buf = new BufferedReader(reader))
try (BufferedReader buf = new BufferedReader(reader))
{
List<String> options = new ArrayList<String>();
options.add(DEFAULT_SECTION);
@ -583,8 +583,8 @@ public class Config
// Normal case: section identifier (possibly separated by commas)
options = Arrays.asList(identifier.split(","));
List<String> option_ids=new ArrayList<String>();
List<String> option_ids = new ArrayList<String>();
// Ensure section classpaths exist
for (String optionId : options)
{
@ -593,23 +593,22 @@ public class Config
if (!_classpaths.containsKey(optionId))
_classpaths.put(optionId,new Classpath());
if (!option_ids.contains(optionId))
option_ids.add(optionId);
}
// Process Dynamic
for (String optionId : options)
{
if (optionId.charAt(0) != '=')
continue;
option_ids = processDynamicSectionIdentifier(optionId.substring(1),option_ids);
}
options = option_ids;
continue;
}
@ -685,18 +684,18 @@ public class Config
String version = st.nextToken();
ver.parse(version);
eval = (operator.equals("<") && java_version.compare(ver) < 0) || (operator.equals(">") && java_version.compare(ver) > 0)
|| (operator.equals("<=") && java_version.compare(ver) <= 0) || (operator.equals("=<") && java_version.compare(ver) <= 0)
|| (operator.equals("=>") && java_version.compare(ver) >= 0) || (operator.equals(">=") && java_version.compare(ver) >= 0)
|| (operator.equals("==") && java_version.compare(ver) == 0) || (operator.equals("!=") && java_version.compare(ver) != 0);
|| (operator.equals("<=") && java_version.compare(ver) <= 0) || (operator.equals("=<") && java_version.compare(ver) <= 0)
|| (operator.equals("=>") && java_version.compare(ver) >= 0) || (operator.equals(">=") && java_version.compare(ver) >= 0)
|| (operator.equals("==") && java_version.compare(ver) == 0) || (operator.equals("!=") && java_version.compare(ver) != 0);
}
else if (condition.equals("nargs"))
{
String operator = st.nextToken();
int number = Integer.parseInt(st.nextToken());
eval = (operator.equals("<") && argCount < number) || (operator.equals(">") && argCount > number)
|| (operator.equals("<=") && argCount <= number) || (operator.equals("=<") && argCount <= number)
|| (operator.equals("=>") && argCount >= number) || (operator.equals(">=") && argCount >= number)
|| (operator.equals("==") && argCount == number) || (operator.equals("!=") && argCount != number);
|| (operator.equals("<=") && argCount <= number) || (operator.equals("=<") && argCount <= number)
|| (operator.equals("=>") && argCount >= number) || (operator.equals(">=") && argCount >= number)
|| (operator.equals("==") && argCount == number) || (operator.equals("!=") && argCount != number);
}
else
{
@ -758,7 +757,7 @@ public class Config
// Recursively add all unconsidered JAR and ZIP files to classpath
if (subject.endsWith("/**"))
{
//directory hierarchy of jar files - recursively add all jars and zips in the hierarchy
// directory hierarchy of jar files - recursively add all jars and zips in the hierarchy
File dir = new File(fixPath(file.substring(0,file.length() - 2)));
addJars(options,dir,true);
continue;
@ -834,27 +833,27 @@ public class Config
}
}
private List<String> processDynamicSectionIdentifier(String dynamicPathId,List<String> sections) throws IOException
private List<String> processDynamicSectionIdentifier(String dynamicPathId, List<String> sections) throws IOException
{
String rawPath;
boolean deep;
if (dynamicPathId.endsWith("/*"))
{
deep=false;
deep = false;
rawPath = fixPath(dynamicPathId.substring(0,dynamicPathId.length() - 1));
}
else if (dynamicPathId.endsWith("/**"))
{
deep=true;
deep = true;
rawPath = fixPath(dynamicPathId.substring(0,dynamicPathId.length() - 2));
}
else
else
{
String msg = "Illegal dynamic path [" + dynamicPathId + "]";
throw new IOException(msg);
}
File parentDir = new File(expand(rawPath));
if (!parentDir.exists())
return sections;
@ -870,24 +869,24 @@ public class Config
List<String> dyn_sections = new ArrayList<String>();
List<String> super_sections = new ArrayList<String>();
if (sections!=null)
if (sections != null)
super_sections.addAll(sections);
for (File dir : dirs)
{
String id = dir.getName();
if (!_classpaths.keySet().contains(id))
_classpaths.put(id, new Classpath());
_classpaths.put(id,new Classpath());
dyn_sections.clear();
if (sections!=null)
if (sections != null)
dyn_sections.addAll(sections);
dyn_sections.add(id);
super_sections.add(id);
debug("dynamic: " + dyn_sections);
addJars(dyn_sections,dir,deep);
}
return super_sections;
}
@ -938,12 +937,12 @@ public class Config
}
if (name.equals("jetty.base"))
{
File base=new File(value);
File base = new File(value);
try
{
value=base.getCanonicalPath();
value = base.getCanonicalPath();
}
catch(Exception e)
catch (Exception e)
{
e.printStackTrace();
}
@ -953,10 +952,10 @@ public class Config
public void addOption(String option)
{
_options.add(option);
_options.add(option);
_properties.put("OPTIONS",join(_options,","));
}
public Set<String> getKnownOptions()
{
return _classpaths.keySet();
@ -972,7 +971,7 @@ public class Config
_options.remove(option);
_properties.put("OPTIONS",join(_options,","));
}
private String join(Collection<?> coll, String delim)
{
StringBuffer buf = new StringBuffer();

View File

@ -121,21 +121,26 @@ public class Main implements IncludeListener
{
String source = "";
// Handle default ini args
ArrayList<String> arguments = new ArrayList<>(Arrays.asList(args));
boolean ini = false;
for (String arg : arguments)
{
if (arg.startsWith("--ini=") || arg.equals("--ini"))
{
ini = true;
}
}
if (!ini)
{
// Ensure ini is declared
if(findArgumentPrefix(arguments, "--ini=") == null) {
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
List<String> xmls = new ArrayList<String>();
@ -216,11 +221,10 @@ public class Main implements IncludeListener
if (arg.startsWith("--ini=") || arg.equals("--ini"))
{
ini = true;
if (arg.length() > 6)
{
String name = arg.substring(6);
File file = _config.getHomeBase().getFile(name);
File file = _config.getBaseHome().getFile(name);
StartIni startini = new StartIni(file,this);
_iniFiles.add(file);
arguments.addAll(i + 1,startini.getLines());
@ -374,16 +378,28 @@ public class Main implements IncludeListener
}
xmls.add(arg);
}
return xmls;
}
private String findArgumentPrefix(ArrayList<String> arguments, String prefix)
{
for (String arg : arguments)
{
if (arg.startsWith(prefix))
{
return arg;
}
}
return null;
}
@Override
public List<StartIni> onIniInclude(String path) throws IOException
{
List<StartIni> included = new ArrayList<>();
HomeBase hb = _config.getHomeBase();
BaseHome hb = _config.getBaseHome();
// Allow --enable and --disable to work
_iniIncludePaths.add(path);
@ -498,7 +514,7 @@ public class Main implements IncludeListener
};
// list etc
List<File> configFiles = _config.getHomeBase().listFiles("etc",filter);
List<File> configFiles = _config.getBaseHome().listFiles("etc",filter);
for (File configFile : configFiles)
{
@ -509,7 +525,7 @@ public class Main implements IncludeListener
{
for (File file : _iniFiles)
{
String path = _config.getHomeBase().toShortForm(file);
String path = _config.getBaseHome().toShortForm(file);
System.out.printf("%s%s%n",indent,path);
if (Config.isDebug())
@ -542,12 +558,12 @@ public class Main implements IncludeListener
private String path(String path)
{
return _config.getHomeBase().toShortForm(path);
return _config.getBaseHome().toShortForm(path);
}
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,
@ -622,7 +638,7 @@ public class Main implements IncludeListener
System.err.println("java.class.path=" + classpath);
System.err.println("classloader=" + cl);
System.err.println("classloader.parent=" + cl.getParent());
System.err.println("properties=" + _config.getProperties());
System.err.println("properties=" + Config.getProperties());
}
for (String m : _enable)
@ -753,14 +769,14 @@ public class Main implements IncludeListener
}
// Try normal locations
File xml = _config.getHomeBase().getFile(xmlFilename);
File xml = _config.getBaseHome().getFile(xmlFilename);
if (FS.isFile(xml))
{
return xml.getAbsolutePath();
}
// Try again, but prefixed with "etc/"
xml = _config.getHomeBase().getFile("etc/" + xmlFilename);
xml = _config.getBaseHome().getFile("etc/" + xmlFilename);
if (FS.isFile(xml))
{
return xml.getAbsolutePath();
@ -777,8 +793,8 @@ public class Main implements IncludeListener
{
cmd.addArg(x);
}
cmd.addRawArg("-Djetty.home=" + _config.getHomeBase().getHome());
cmd.addRawArg("-Djetty.base=" + _config.getHomeBase().getBase());
cmd.addRawArg("-Djetty.home=" + _config.getBaseHome().getHome());
cmd.addRawArg("-Djetty.base=" + _config.getBaseHome().getBase());
// Special Stop/Shutdown properties
ensureSystemPropertySet("STOP.PORT");
@ -796,7 +812,7 @@ public class Main implements IncludeListener
cmd.addRawArg(_config.getMainClassname());
// Check if we need to pass properties as a file
Properties properties = _config.getProperties();
Properties properties = Config.getProperties();
if (properties.size() > 0)
{
File prop_file = File.createTempFile("start",".properties");
@ -826,7 +842,7 @@ public class Main implements IncludeListener
return; // done
}
Properties props = _config.getProperties();
Properties props = Config.getProperties();
if (props.containsKey(key))
{
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') "
+ "then overlapping entries will not be repeated in the eventual classpath.");
System.out.println();
System.out.printf("${jetty.home} = %s%n",_config.getHomeBase().getHome());
System.out.printf("${jetty.base} = %s%n",_config.getHomeBase().getBase());
System.out.printf("${jetty.home} = %s%n",_config.getBaseHome().getHome());
System.out.printf("${jetty.base} = %s%n",_config.getBaseHome().getBase());
System.out.println();
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)?");
HomeBase hb = _config.getHomeBase();
BaseHome hb = _config.getBaseHome();
// walk all ini include paths that were used
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)?");
HomeBase hb = _config.getHomeBase();
BaseHome hb = _config.getBaseHome();
// walk all ini include paths that were used
boolean found = false;

View File

@ -90,7 +90,7 @@ public class StartIni implements Iterable<String>
}
else
{
// Collect HomeBase resolved included StartIni's
// Collect BaseHome resolved included StartIni's
for (StartIni included : listener.onIniInclude(line))
{
// 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.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<>();
for (File file : files)
@ -48,7 +48,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
File baseDir = null;
HomeBase hb = new HomeBase(homeDir,baseDir);
BaseHome hb = new BaseHome(homeDir,baseDir);
File startIni = hb.getFile("/start.ini");
String ref = hb.toShortForm(startIni);
@ -64,7 +64,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
File baseDir = null;
HomeBase hb = new HomeBase(homeDir,baseDir);
BaseHome hb = new BaseHome(homeDir,baseDir);
List<File> files = hb.listFiles("/start.d");
List<String> expected = new ArrayList<>();
@ -83,7 +83,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
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<String> expected = new ArrayList<>();
@ -102,7 +102,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
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<String> expected = new ArrayList<>();
@ -122,7 +122,7 @@ public class HomeBaseTest
File homeDir = MavenTestingUtils.getTestResourceDir("hb.1/home");
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");
String ref = hb.toShortForm(startIni);