Improve logging modules and listing #984

Trim provider list and accept as default if only 1 available
This commit is contained in:
Greg Wilkins 2016-10-20 10:05:09 +11:00
parent 4dfd3fad73
commit 6bc8717053
6 changed files with 59 additions and 32 deletions

View File

@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -308,7 +309,7 @@ public class Modules implements Iterable<Module>
if (p.isTransitive() && !transitive) if (p.isTransitive() && !transitive)
p.clearTransitiveEnable(); p.clearTransitiveEnable();
else else
throw new UsageException("%s provides %s, which is already provided by %s enabled in %s",module.getName(),name,p.getName(),p.getEnableSources()); throw new UsageException("Module %s provides %s, which is already provided by %s enabled in %s",module.getName(),name,p.getName(),p.getEnableSources());
} }
}; };
} }
@ -337,11 +338,12 @@ public class Modules implements Iterable<Module>
for(String dependsOn:module.getDepends()) for(String dependsOn:module.getDepends())
{ {
// Look for modules that provide that dependency // Look for modules that provide that dependency
Set<Module> providers = _provided.get(dependsOn); Set<Module> providers = getAvailableProviders(dependsOn);
StartLog.debug("%s depends on %s provided by ",module,dependsOn,providers);
StartLog.debug("Module %s depends on %s provided by ",module,dependsOn,providers);
// If there are no known providers of the module // If there are no known providers of the module
if ((providers==null||providers.isEmpty())) if (providers.isEmpty())
{ {
// look for a dynamic module // look for a dynamic module
if (dependsOn.contains("/")) if (dependsOn.contains("/"))
@ -359,13 +361,15 @@ public class Modules implements Iterable<Module>
} }
// If a provider is already enabled, then add a transitive enable // If a provider is already enabled, then add a transitive enable
long enabled=providers.stream().filter(Module::isEnabled).count(); if (providers.stream().filter(Module::isEnabled).count()!=0)
if (enabled>0)
providers.stream().filter(m->m.isEnabled()&&m!=module).forEach(m->enable(newlyEnabled,m,"transitive provider of "+dependsOn+" for "+module.getName(),true)); providers.stream().filter(m->m.isEnabled()&&m!=module).forEach(m->enable(newlyEnabled,m,"transitive provider of "+dependsOn+" for "+module.getName(),true));
else else
{ {
// Is there an obvious default? // Is there an obvious default?
Optional<Module> dftProvider = providers.stream().filter(m->m.getName().equals(dependsOn)).findFirst(); Optional<Module> dftProvider = (providers.size()==1)
?providers.stream().findFirst()
:providers.stream().filter(m->m.getName().equals(dependsOn)).findFirst();
if (dftProvider.isPresent()) if (dftProvider.isPresent())
enable(newlyEnabled,dftProvider.get(),"transitive provider of "+dependsOn+" for "+module.getName(),true); enable(newlyEnabled,dftProvider.get(),"transitive provider of "+dependsOn+" for "+module.getName(),true);
else if (StartLog.isDebugEnabled()) else if (StartLog.isDebugEnabled())
@ -374,6 +378,47 @@ public class Modules implements Iterable<Module>
} }
} }
private Set<Module> getAvailableProviders(String name)
{
// Get all available providers
Set<Module> providers = _provided.get(name);
if (providers==null || providers.isEmpty())
return Collections.emptySet();
providers = new HashSet<>(providers);
// find all currently provided names by other modules
Set<String> provided = new HashSet<>();
for (Module m : _modules)
{
if (m.isEnabled())
{
provided.add(m.getName());
provided.addAll(m.getProvides());
}
}
// Remove any that cannot be selected
for (Iterator<Module> i = providers.iterator(); i.hasNext();)
{
Module provider = i.next();
if (!provider.isEnabled())
{
for (String p : provider.getProvides())
{
if (provided.contains(p))
{
i.remove();
break;
}
}
}
}
return providers;
}
public Module get(String name) public Module get(String name)
{ {
Module module = _names.get(name); Module module = _names.get(name);
@ -381,7 +426,7 @@ public class Modules implements Iterable<Module>
{ {
String reason = _deprecated.getProperty(name); String reason = _deprecated.getProperty(name);
if (reason!=null) if (reason!=null)
StartLog.warn("Module '%s' is no longer available: %s",name,reason); StartLog.warn("Module %s is no longer available: %s",name,reason);
} }
return module; return module;
} }
@ -405,13 +450,13 @@ public class Modules implements Iterable<Module>
// Check dependencies // Check dependencies
m.getDepends().forEach(d-> m.getDepends().forEach(d->
{ {
Set<Module> providers =_provided.get(d); Set<Module> providers = getAvailableProviders(d);
if (providers.stream().filter(Module::isEnabled).count()==0) if (providers.stream().filter(Module::isEnabled).count()==0)
{ {
if (unsatisfied.length()>0) if (unsatisfied.length()>0)
unsatisfied.append(','); unsatisfied.append(',');
unsatisfied.append(m.getName()); unsatisfied.append(m.getName());
StartLog.error("Module %s requires a `%s` module from one of %s%n",m.getName(),d,providers); StartLog.error("Module %s requires a module providing %s from one of %s%n",m.getName(),d,providers);
} }
}); });
}); });

View File

@ -14,6 +14,7 @@ slf4j-impl
[provides] [provides]
jcl-api jcl-api
jcl-impl jcl-impl
slf4j+jcl
[files] [files]
maven://org.slf4j/jcl-over-slf4j/${slf4j.version}|lib/slf4j/jcl-over-slf4j-${slf4j.version}.jar maven://org.slf4j/jcl-over-slf4j/${slf4j.version}|lib/slf4j/jcl-over-slf4j-${slf4j.version}.jar

View File

@ -12,6 +12,7 @@ slf4j-impl
[provides] [provides]
jul-impl jul-impl
slf4j+jul
[files] [files]
maven://org.slf4j/jul-to-slf4j/${slf4j.version}|lib/slf4j/jul-to-slf4j-${slf4j.version}.jar maven://org.slf4j/jul-to-slf4j/${slf4j.version}|lib/slf4j/jul-to-slf4j-${slf4j.version}.jar

View File

@ -1,22 +0,0 @@
[description]
Provides a Log4j v1.2 binding to Log4j v2 logging.
[tags]
logging
log4j2
log4j
internal
[depends]
log4j2-api
log4j2-impl
[provides]
log4j-api
log4j-impl
[files]
maven://org.apache.logging.log4j/log4j-1.2-api/${log4j2.version}|lib/log4j/log4j-1.2-api-${log4j2.version}.jar
[lib]
lib/log4j/log4j-1.2-api-${log4j2.version}.jar

View File

@ -13,6 +13,7 @@ jcl-impl
[provides] [provides]
slf4j-impl slf4j-impl
slf4j+jcl
[files] [files]
maven://org.slf4j/slf4j-jcl/${slf4j.version}|lib/slf4j/slf4j-jcl-${slf4j.version}.jar maven://org.slf4j/slf4j-jcl/${slf4j.version}|lib/slf4j/slf4j-jcl-${slf4j.version}.jar

View File

@ -11,6 +11,7 @@ slf4j-api
[provides] [provides]
slf4j-impl slf4j-impl
slf4j+jul
[files] [files]
maven://org.slf4j/slf4j-jdk14/${slf4j.version}|lib/slf4j/slf4j-jdk14-${slf4j.version}.jar maven://org.slf4j/slf4j-jdk14/${slf4j.version}|lib/slf4j/slf4j-jdk14-${slf4j.version}.jar