use of loghub.RouteParser.PropertyContext in project LogHub by fbacchella.
the class Configuration method resolveProperties.
private Set<String> resolveProperties(RouteParser.ConfigurationContext tree, ConfigListener conflistener, Map<String, PropertyContext> propertiesContext) throws ConfigException {
Set<String> lockedProperties = new HashSet<>();
tree.property().forEach(pc -> propertiesContext.put(pc.propertyName().getText(), pc));
if (propertiesContext.containsKey("locale")) {
String localString = getStringLitteral(propertiesContext.remove("locale").beanValue());
if (localString != null) {
logger.debug("setting locale to {}", localString);
Locale l = Locale.forLanguageTag(localString);
Locale.setDefault(l);
lockedProperties.add("locale");
}
}
if (propertiesContext.containsKey("timezone")) {
String tz = getStringLitteral(propertiesContext.remove("timezone").beanValue());
if (tz != null) {
try {
logger.debug("setting time zone to {}", tz);
ZoneId id = ZoneId.of(tz);
TimeZone.setDefault(TimeZone.getTimeZone(id));
} catch (DateTimeException e) {
logger.error("Invalid timezone {}: {}", tz, e.getMessage());
}
lockedProperties.add("timezone");
}
}
// resolve the plugins property to define the class loader
if (propertiesContext.containsKey("plugins")) {
PropertyContext pc = propertiesContext.remove("plugins");
String[] path = getStringOrArrayLitteral(pc.beanValue());
if (path.length > 0) {
try {
logger.debug("Looking for plugins in {}", (Object[]) path);
classLoader = doClassLoader(path);
} catch (IOException ex) {
throw new ConfigException("can't load plugins: " + ex.getMessage(), conflistener.stream.getSourceName(), pc.start, ex);
}
}
}
lockedProperties.add("plugins");
// resolve some top level log4j properties
URI log4JUri = null;
PropertyContext pc = null;
if (propertiesContext.containsKey("log4j.configURL")) {
pc = propertiesContext.remove("log4j.configURL");
try {
log4JUri = getLitteral(pc.beanValue(), i -> i.stringLiteral().getText(), j -> {
try {
return new URL(j).toURI();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
});
logger.debug("Configured log4j URL to {}", log4JUri);
} catch (RuntimeException e) {
logger.error("Invalid log4j URL");
}
} else if (propertiesContext.containsKey("log4j.configFile")) {
pc = propertiesContext.remove("log4j.configFile");
log4JUri = getLitteral(pc.beanValue(), i -> i.stringLiteral().getText(), j -> {
return new File(j).toURI();
});
logger.debug("Configured log4j URL to {}", log4JUri);
}
if (log4JUri != null) {
// Try to read the log4j configuration, because log4j2 intercept possible exceptions and don't forward them
try (InputStream is = log4JUri.toURL().openStream()) {
int toread = 0;
while ((toread = is.available()) != 0) {
is.skip(toread);
}
} catch (IOException e) {
throw new ConfigException(e.getMessage(), conflistener.stream.getSourceName(), pc.start, e);
}
LoggerContext ctx = (LoggerContext) LogManager.getContext(classLoader, true);
// Possible exception are already catched (I hope)
ctx.setConfigLocation(log4JUri);
logger.debug("log4j reconfigured");
}
lockedProperties.add("log4j.configURL");
lockedProperties.add("log4j.configFile");
return lockedProperties;
}
use of loghub.RouteParser.PropertyContext in project LogHub by fbacchella.
the class Configuration method runparsing.
private Properties runparsing(CharStream cs) throws ConfigException {
try {
Map<String, PropertyContext> propertiesContext = new HashMap<>();
ConfigListener conflistener = new ConfigListener();
ParseTreeWalker walker = new ParseTreeWalker();
RouteParser.ConfigurationContext tree = getTree(cs, conflistener);
Set<String> lockedProperties = resolveProperties(tree, conflistener, propertiesContext);
walker.walk(conflistener, tree);
Consumer<Path> parseSubFile = filePath -> {
try {
if (Files.isHidden(filePath)) {
return;
}
logger.debug("parsing {}", filePath);
CharStream localcs = CharStreams.fromPath(filePath);
RouteParser.ConfigurationContext localtree = getTree(localcs, conflistener);
lockedProperties.forEach(i -> conflistener.lockProperty(i));
resolveProperties(localtree, conflistener, propertiesContext);
walker.walk(conflistener, localtree);
} catch (IOException e) {
throw new ConfigException(e.getMessage(), filePath.toString(), e);
}
};
if (propertiesContext.containsKey("includes")) {
Arrays.stream(getStringOrArrayLitteral(propertiesContext.remove("includes").beanValue())).forEach(sourceName -> {
Path sourcePath = Paths.get(sourceName);
if (Files.isRegularFile(sourcePath)) {
// A file is given
parseSubFile.accept(sourcePath);
} else if (Files.isDirectory(sourcePath)) {
// A directory is given
try {
Files.list(sourcePath).forEach(i -> parseSubFile.accept(i));
} catch (IOException e) {
throw new ConfigException(e.getMessage(), sourceName, e);
}
} else {
// It might be a directory/pattern
Path patternPath = sourcePath.getFileName();
Path parent = sourcePath.getParent();
Pattern mask = Helpers.convertGlobToRegex(patternPath.toString());
if (Files.isDirectory(parent)) {
try {
Files.list(parent).filter(i -> mask.matcher(i.getFileName().toString()).matches()).forEach(i -> parseSubFile.accept(i));
} catch (IOException e) {
throw new ConfigException(e.getMessage(), sourceName, e);
}
}
}
});
}
return analyze(conflistener);
} catch (RecognitionException e) {
if (e.getCtx() instanceof ParserRuleContext) {
ParserRuleContext ctx = (ParserRuleContext) e.getCtx();
throw new ConfigException(e.getMessage(), e.getInputStream().getSourceName(), ctx.start, e);
} else {
throw e;
}
}
}
Aggregations