424390 - Allow enabling modules via regex

This commit is contained in:
Joakim Erdfelt 2013-12-27 10:52:03 -07:00
parent f8e44a1ae7
commit 966c4e0130
3 changed files with 47 additions and 5 deletions

View File

@ -31,6 +31,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.regex.Pattern;
/**
* Access for all modules declared, as well as what is enabled.
@ -207,13 +208,33 @@ public class Modules implements Iterable<Module>
public void enable(String name, List<String> sources)
{
Module module = modules.get(name);
if (module == null)
if (name.contains("*"))
{
System.err.printf("WARNING: Cannot enable requested module [%s]: not a valid module name.%n",name);
return;
// A regex!
Pattern pat = Pattern.compile(name);
for (Map.Entry<String, Module> entry : modules.entrySet())
{
if (pat.matcher(entry.getKey()).matches())
{
enableModule(entry.getValue(),sources);
}
}
}
StartLog.debug("Enabling module: %s (via %s)",name,Main.join(sources,", "));
else
{
Module module = modules.get(name);
if (module == null)
{
System.err.printf("WARNING: Cannot enable requested module [%s]: not a valid module name.%n",name);
return;
}
enableModule(module,sources);
}
}
private void enableModule(Module module, List<String> sources)
{
StartLog.debug("Enabling module: %s (via %s)",module.getName(),Main.join(sources,", "));
module.setEnabled(true);
if (sources != null)
{

View File

@ -146,4 +146,9 @@ public class StartLog
System.out.println("Establishing " + logfile + " on " + new Date());
}
}
public static void enableDebug()
{
getInstance().debug = true;
}
}

View File

@ -46,6 +46,22 @@ public class ModulesTest
modules.registerAll(basehome, DEFAULT_ARGS);
Assert.assertThat("Module count",modules.count(),is(30));
}
@Test
public void testEnableRegexSimple() throws IOException
{
StartLog.enableDebug();
File homeDir = MavenTestingUtils.getTestResourceDir("usecases/home");
BaseHome basehome = new BaseHome(homeDir,homeDir);
Modules modules = new Modules();
modules.registerAll(basehome, DEFAULT_ARGS);
modules.enable("[sj]{1}.*",TEST_SOURCE);
String expected[] = { "jmx", "stats", "spdy", "security", "jndi", "jsp", "servlet", "jaas", "server" };
Assert.assertThat("Enabled Module count",modules.resolveEnabled().size(),is(expected.length));
}
@Test
public void testResolve_ServerHttp() throws IOException