Search in sources :

Example 1 with PropertyContext

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;
}
Also used : Locale(java.util.Locale) PropertyContext(loghub.RouteParser.PropertyContext) Arrays(java.util.Arrays) ArrayContext(loghub.RouteParser.ArrayContext) Input(loghub.configuration.ConfigListener.Input) URL(java.net.URL) LoggerContext(org.apache.logging.log4j.core.LoggerContext) URISyntaxException(java.net.URISyntaxException) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) URLClassLoader(java.net.URLClassLoader) CharStreams(org.antlr.v4.runtime.CharStreams) Helpers(loghub.Helpers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) ObjectReference(loghub.configuration.ConfigListener.ObjectReference) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) DateTimeException(java.time.DateTimeException) Receiver(loghub.Receiver) TimeZone(java.util.TimeZone) Collection(java.util.Collection) LiteralContext(loghub.RouteParser.LiteralContext) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Reader(java.io.Reader) RouteLexer(loghub.RouteLexer) ZoneId(java.time.ZoneId) RouteParser(loghub.RouteParser) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Processor(loghub.Processor) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ThrowingFunction(loghub.Helpers.ThrowingFunction) ThrowingConsumer(loghub.Helpers.ThrowingConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CharStream(org.antlr.v4.runtime.CharStream) Sender(loghub.Sender) ThrowingPredicate(loghub.Helpers.ThrowingPredicate) Output(loghub.configuration.ConfigListener.Output) BeanValueContext(loghub.RouteParser.BeanValueContext) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) Pipeline(loghub.Pipeline) IOException(java.io.IOException) Source(loghub.Source) File(java.io.File) Consumer(java.util.function.Consumer) NamedSubPipeline(loghub.processors.NamedSubPipeline) RecognitionException(org.antlr.v4.runtime.RecognitionException) Paths(java.nio.file.Paths) AnonymousSubPipeline(loghub.processors.AnonymousSubPipeline) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Event(loghub.Event) InputStream(java.io.InputStream) ZoneId(java.time.ZoneId) InputStream(java.io.InputStream) IOException(java.io.IOException) URI(java.net.URI) LoggerContext(org.apache.logging.log4j.core.LoggerContext) URL(java.net.URL) DateTimeException(java.time.DateTimeException) PropertyContext(loghub.RouteParser.PropertyContext) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with PropertyContext

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;
        }
    }
}
Also used : Path(java.nio.file.Path) PropertyContext(loghub.RouteParser.PropertyContext) Arrays(java.util.Arrays) ArrayContext(loghub.RouteParser.ArrayContext) Input(loghub.configuration.ConfigListener.Input) URL(java.net.URL) LoggerContext(org.apache.logging.log4j.core.LoggerContext) URISyntaxException(java.net.URISyntaxException) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) URLClassLoader(java.net.URLClassLoader) CharStreams(org.antlr.v4.runtime.CharStreams) Helpers(loghub.Helpers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) ObjectReference(loghub.configuration.ConfigListener.ObjectReference) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) DateTimeException(java.time.DateTimeException) Receiver(loghub.Receiver) TimeZone(java.util.TimeZone) Collection(java.util.Collection) LiteralContext(loghub.RouteParser.LiteralContext) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Reader(java.io.Reader) RouteLexer(loghub.RouteLexer) ZoneId(java.time.ZoneId) RouteParser(loghub.RouteParser) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Processor(loghub.Processor) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ThrowingFunction(loghub.Helpers.ThrowingFunction) ThrowingConsumer(loghub.Helpers.ThrowingConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CharStream(org.antlr.v4.runtime.CharStream) Sender(loghub.Sender) ThrowingPredicate(loghub.Helpers.ThrowingPredicate) Output(loghub.configuration.ConfigListener.Output) BeanValueContext(loghub.RouteParser.BeanValueContext) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) Pipeline(loghub.Pipeline) IOException(java.io.IOException) Source(loghub.Source) File(java.io.File) Consumer(java.util.function.Consumer) NamedSubPipeline(loghub.processors.NamedSubPipeline) RecognitionException(org.antlr.v4.runtime.RecognitionException) Paths(java.nio.file.Paths) AnonymousSubPipeline(loghub.processors.AnonymousSubPipeline) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Event(loghub.Event) InputStream(java.io.InputStream) Pattern(java.util.regex.Pattern) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) HashMap(java.util.HashMap) RouteParser(loghub.RouteParser) IOException(java.io.IOException) CharStream(org.antlr.v4.runtime.CharStream) PropertyContext(loghub.RouteParser.PropertyContext) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 DateTimeException (java.time.DateTimeException)2 ZoneId (java.time.ZoneId)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2