jetty-start / DownloadArg to its own class + Main cleanup

This commit is contained in:
Joakim Erdfelt 2013-08-29 08:59:20 -07:00
parent de4febf1b9
commit 78dfd287e3
3 changed files with 183 additions and 256 deletions

View File

@ -45,8 +45,6 @@ import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jetty.start.StartArgs.DownloadArg;
/**
* Main start class.
* <p>
@ -332,6 +330,166 @@ public class Main
modules.dumpEnabledTree();
}
private void moduleIni(StartArgs args, String name, boolean topLevel, boolean appendStartIni) throws IOException
{
// Find the start.d relative to the base directory only.
File start_d = baseHome.getBaseFile("start.d");
// Is this a module?
Modules modules = args.getAllModules();
Module module = modules.get(name);
if (module == null)
{
StartLog.warn("ERROR: No known module for %s",name);
return;
}
// Find any named ini file and check it follows the convention
File start_ini = baseHome.getBaseFile("start.ini");
String short_start_ini = baseHome.toShortForm(start_ini);
File ini = new File(start_d,name + ".ini");
String short_ini = baseHome.toShortForm(ini);
StartIni module_ini = null;
if (ini.exists())
{
module_ini = new StartIni(ini);
if (module_ini.getLineMatches(Pattern.compile("--module=(.*, *)*" + name)).size() == 0)
{
StartLog.warn("ERROR: %s is not enabled in %s!",name,short_ini);
return;
}
}
boolean transitive = module.isEnabled() && (module.getSources().size() == 0);
boolean has_ini_lines = module.getInitialise().size() > 0;
// If it is not enabled or is transitive with ini template lines or toplevel and doesn't exist
if (!module.isEnabled() || (transitive && has_ini_lines) || (topLevel && !ini.exists() && !appendStartIni))
{
String source = null;
PrintWriter out = null;
try
{
if (appendStartIni)
{
if ((!start_ini.exists() && !start_ini.createNewFile()) || !start_ini.canWrite())
{
StartLog.warn("ERROR: Bad %s! ",start_ini);
return;
}
source = short_start_ini;
StartLog.warn("%-15s initialised in %s (appended)",name,source);
out = new PrintWriter(new FileWriter(start_ini,true));
}
else
{
// Create the directory if needed
if (!start_d.exists())
{
start_d.mkdirs();
}
if (!start_d.isDirectory() || !start_d.canWrite())
{
StartLog.warn("ERROR: Bad start.d %s! ",start_d);
return;
}
// Create a new ini file for it
if (!ini.createNewFile())
{
StartLog.warn("ERROR: %s cannot be initialised in %s! ",name,short_ini);
return;
}
source = short_ini;
StartLog.warn("%-15s initialised in %s (created)",name,source);
out = new PrintWriter(ini);
}
if (appendStartIni)
{
out.println();
}
out.println("#");
out.println("# Initialize module " + name);
out.println("#");
Pattern p = Pattern.compile("--module=([^,]+)(,([^,]+))*");
out.println("--module=" + name);
args.parse("--module=" + name,source);
modules.enable(name,Collections.singletonList(source));
for (String line : module.getInitialise())
{
out.println(line);
args.parse(line,source);
Matcher m = p.matcher(line);
if (m.matches())
{
for (int i = 1; i <= m.groupCount(); i++)
{
String n = m.group(i);
if (n == null)
{
continue;
}
n = n.trim();
if ((n.length() == 0) || n.startsWith(","))
{
continue;
}
modules.enable(n,Collections.singletonList(source));
}
}
}
}
finally
{
if (out != null)
{
out.close();
}
}
}
else if (ini.exists())
{
StartLog.info("%-15s initialised in %s",name,short_ini);
}
// Also list other places this module is enabled
for (String source : module.getSources())
{
if (!short_ini.equals(source))
{
StartLog.warn("%-15s enabled in %s",name,baseHome.toShortForm(source));
}
}
// Do downloads now
for (String download : module.getDownloads())
{
download(new DownloadArg(download));
}
// Process dependencies from top level only
if (topLevel)
{
List<Module> parents = new ArrayList<>();
for (String parent : modules.resolveParentModulesOf(name))
{
if (!name.equals(parent))
{
Module m = modules.get(parent);
m.setEnabled(true);
parents.add(m);
}
}
Collections.sort(parents,Collections.reverseOrder(new Module.DepthComparator()));
for (Module m : parents)
{
moduleIni(args,m.getName(),false,appendStartIni);
}
}
}
/**
* Convenience for <code>processCommandLine(cmdLine.toArray(new String[cmdLine.size()]))</code>
*/
@ -478,19 +636,18 @@ public class Main
}
}
// Initialize
for (String module : args.getModuleStartIni())
{
moduleIni(args,module,true,true);
}
// Initialize
for (String module : args.getModuleIni())
{
moduleIni(args,module,true,false);
}
// Informational command line, don't run jetty
if (!args.isRun())
{
@ -611,152 +768,6 @@ public class Main
}
}
private void moduleIni(StartArgs args, String name, boolean topLevel,boolean appendStartIni) throws IOException
{
// Find the start.d relative to the base directory only.
File start_d=baseHome.getBaseFile("start.d");
// Is this a module?
Modules modules=args.getAllModules();
Module module=modules.get(name);
if (module==null)
{
StartLog.warn("ERROR: No known module for %s",name);
return;
}
// Find any named ini file and check it follows the convention
File start_ini=baseHome.getBaseFile("start.ini");
String short_start_ini = baseHome.toShortForm(start_ini);
File ini=new File(start_d,name+".ini");
String short_ini = baseHome.toShortForm(ini);
StartIni module_ini=null;
if (ini.exists())
{
module_ini=new StartIni(ini);
if (module_ini.getLineMatches(Pattern.compile("--module=(.*, *)*"+name)).size()==0)
{
StartLog.warn("ERROR: %s is not enabled in %s!",name,short_ini);
return;
}
}
boolean transitive=module.isEnabled() && module.getSources().size()==0;
boolean has_ini_lines = module.getInitialise().size()>0;
// If it is not enabled or is transitive with ini template lines or toplevel and doesn't exist
if (!module.isEnabled() || (transitive && has_ini_lines) || (topLevel && !ini.exists() && !appendStartIni))
{
String source=null;
PrintWriter out=null;
try
{
if (appendStartIni)
{
if (!start_ini.exists() && !start_ini.createNewFile() || !start_ini.canWrite())
{
StartLog.warn("ERROR: Bad %s! ",start_ini);
return;
}
source = short_start_ini;
StartLog.warn("%-15s initialised in %s (appended)",name,source);
out = new PrintWriter(new FileWriter(start_ini,true));
}
else
{
// Create the directory if needed
if (!start_d.exists())
start_d.mkdirs();
if (!start_d.isDirectory() || !start_d.canWrite())
{
StartLog.warn("ERROR: Bad start.d %s! ",start_d);
return;
}
// Create a new ini file for it
if (!ini.createNewFile())
{
StartLog.warn("ERROR: %s cannot be initialised in %s! ",name,short_ini);
return;
}
source=short_ini;
StartLog.warn("%-15s initialised in %s (created)",name,source);
out = new PrintWriter(ini);
}
if (appendStartIni)
out.println();
out.println("#");
out.println("# Initialize module "+name);
out.println("#");
Pattern p = Pattern.compile("--module=([^,]+)(,([^,]+))*");
out.println("--module="+name);
args.parse("--module="+name,source);
modules.enable(name,Collections.singletonList(source));
for (String line : module.getInitialise())
{
out.println(line);
args.parse(line,source);
Matcher m=p.matcher(line);
if (m.matches())
{
for (int i=1;i<=m.groupCount();i++)
{
String n=m.group(i);
if (n==null)
continue;
n=n.trim();
if (n.length()==0||n.startsWith(","))
continue;
modules.enable(n,Collections.singletonList(source));
}
}
}
}
finally
{
if (out!=null)
out.close();
}
}
else if (ini.exists())
{
StartLog.info("%-15s initialised in %s",name,short_ini);
}
// Also list other places this module is enabled
for(String source:module.getSources())
{
if (!short_ini.equals(source))
StartLog.warn("%-15s enabled in %s",name,baseHome.toShortForm(source));
}
// Do downloads now
for (String download : module.getDownloads())
download(StartArgs.toDownloadArg(download));
// Process dependencies from top level only
if (topLevel)
{
List<Module> parents = new ArrayList<>();
for (String parent:modules.resolveParentModulesOf(name))
{
if (!name.equals(parent))
{
Module m=modules.get(parent);
m.setEnabled(true);
parents.add(m);
}
}
Collections.sort(parents,Collections.reverseOrder(new Module.DepthComparator()));
for (Module m : parents)
{
moduleIni(args,m.getName(),false,appendStartIni);
}
}
}
public void usage(boolean exit)
{
String usageResource = "org/eclipse/jetty/start/usage.txt";

View File

@ -39,76 +39,7 @@ import java.util.Set;
*/
public class StartArgs
{
public static class DownloadArg
{
public String uri;
public String location;
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
DownloadArg other = (DownloadArg)obj;
if (uri == null)
{
if (other.uri != null)
{
return false;
}
}
else if (!uri.equals(other.uri))
{
return false;
}
if (location == null)
{
if (other.location != null)
{
return false;
}
}
else if (!location.equals(other.location))
{
return false;
}
return true;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result) + ((uri == null)?0:uri.hashCode());
result = (prime * result) + ((location == null)?0:location.hashCode());
return result;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("DownloadArg [uri=");
builder.append(uri);
builder.append(", location=");
builder.append(location);
builder.append("]");
return builder.toString();
}
}
public static final String CMD_LINE_SOURCE = "<cmd-line>";
public static final String VERSION;
static
@ -135,7 +66,7 @@ public class StartArgs
// TODO: might make sense to declare this in modules/base.mod
private static final String SERVER_MAIN = "org.eclipse.jetty.xml.XmlConfiguration";
private List<String> commandLine = new ArrayList<>();
private Set<String> modules = new HashSet<>();
private Map<String, List<String>> sources = new HashMap<>();
@ -148,8 +79,8 @@ public class StartArgs
private List<String> jvmArgs = new ArrayList<>();
private List<String> moduleIni = new ArrayList<>();
private List<String> moduleStartIni = new ArrayList<>();
private Modules allModules;
private Modules allModules;
// Should the server be run?
private boolean run = true;
private boolean help = false;
@ -159,6 +90,7 @@ public class StartArgs
private boolean listConfig = false;
private boolean version = false;
private boolean dryRun = false;
private boolean exec = false;
public StartArgs(String[] commandLineArgs)
@ -167,30 +99,9 @@ public class StartArgs
classpath = new Classpath();
}
static DownloadArg toDownloadArg(String uriLocation)
{
String parts[] = uriLocation.split(":",3);
if (parts.length != 3)
{
throw new IllegalArgumentException("Not <http uri>:<location>");
}
if (!"http".equalsIgnoreCase(parts[0]))
{
throw new IllegalArgumentException("Download only supports http protocol");
}
if (!parts[1].startsWith("//"))
{
throw new IllegalArgumentException("Download URI invalid: " + uriLocation);
}
DownloadArg arg = new DownloadArg();
arg.uri = String.format("%s:%s",parts[0],parts[1]);
arg.location = parts[2];
return arg;
}
private void addDownload(String uriLocation)
{
DownloadArg arg=toDownloadArg(uriLocation);
DownloadArg arg = new DownloadArg(uriLocation);
if (!downloads.contains(arg))
{
downloads.add(arg);
@ -450,16 +361,6 @@ public class StartArgs
return this.commandLine;
}
public List<String> getModuleIni()
{
return moduleIni;
}
public List<String> getModuleStartIni()
{
return moduleStartIni;
}
public List<DownloadArg> getDownloads()
{
@ -538,6 +439,16 @@ public class StartArgs
return System.getProperty("main.class",mainclass);
}
public List<String> getModuleIni()
{
return moduleIni;
}
public List<String> getModuleStartIni()
{
return moduleStartIni;
}
public Properties getProperties()
{
return properties;
@ -671,8 +582,10 @@ public class StartArgs
public void parse(String arg, String source)
{
if (arg.trim().startsWith("#"))
{
return;
}
if ("--help".equals(arg) || "-?".equals(arg))
{
if (!CMD_LINE_SOURCE.equals(source))
@ -751,7 +664,9 @@ public class StartArgs
if (arg.startsWith("--module-ini="))
{
if (!CMD_LINE_SOURCE.equals(source))
{
throw new UsageException(ERR_BAD_ARG,"%s not allowed in %s",arg,source);
}
moduleIni.addAll(getValues(arg));
run = false;
return;
@ -760,7 +675,9 @@ public class StartArgs
if (arg.startsWith("--module-start-ini="))
{
if (!CMD_LINE_SOURCE.equals(source))
{
throw new UsageException(ERR_BAD_ARG,"%s not allowed in %s",arg,source);
}
moduleStartIni.addAll(getValues(arg));
run = false;
return;

View File

@ -31,7 +31,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jetty.start.StartArgs.DownloadArg;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.Assert;