All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lumbermill.internal.StringTemplate Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016 Sony Mobile Communications, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package lumbermill.internal;

import lumbermill.api.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * StringTemplate is used instead of a reguar string in most configuration
 * options to provide the possibility to use variables from the event
 * instead of hard coded strings.
 *
 * StringTemplate.compile("{elb_status_code} == 500"}
 * StringTemplate.compile("'{host}' == '127.0.0.1'"}
 * StringTemplate.compile("application.status"} // Fields are not required
 *
 */
public class StringTemplate {

    private final Logger LOGGER = LoggerFactory.getLogger(StringTemplate.class);

    private final String pattern;
    private final List fields = new ArrayList<>();

    public StringTemplate(String pattern) {
        this.pattern = pattern;
        initializeFields(pattern, fields);
    }

    public String original() {
        return pattern;
    }

    public static StringTemplate compile(String pattern) {
        if (!hasFields(pattern)) {
            return new NoFieldsTemplate(pattern);
        }
        return new StringTemplate(pattern);
    }

    private static boolean hasFields(String pattern) {
        return new StringTemplate(pattern).hasFields();
    }

    private boolean hasFields() {
        return fields.size() > 0;
    }


    /**
     * Formats the contents from the field according to the contents of the
     * pattern. This will return Optional.empty() if the pattern contains a field
     * that is not present in the event.
     */
    public Optional format(Event event) {

        if (fields.size() > 0) {

            String newExpression = pattern;
            boolean foundField = false;
            for( SimpleField field : fields) {
                if (event.has(field.name)) {
                    newExpression = newExpression.replace (
                            String.format("{%s}", field.name),
                            String.format("%s", event.valueAsString(field.name)));
                    foundField = true;
                    newExpression = field.valueOf(newExpression);
                } else {
                    String property = System.getProperty(field.name);
                    if (property != null) {
                        foundField = true;
                        newExpression = newExpression.replace (
                                String.format("{%s}", field.name),
                                String.format("%s", property));
                        if (LOGGER.isTraceEnabled()) {
                            LOGGER.trace("Value for field {} was found as system property", field.name);
                        }
                    } else {
                        String env = System.getenv(field.name);
                        if (env != null) {
                            foundField = true;
                            newExpression = newExpression.replace(
                                    String.format("{%s}", field.name),
                                    String.format("%s", env));
                            if (LOGGER.isTraceEnabled()) {
                                LOGGER.trace("Value for field {} was found as system environment variable", field.name);
                            }
                        }
                    }
                }
            }

            return foundField ?
                    Optional.of(newExpression) :
                    Optional.empty();
        } else {
            return Optional.of(pattern);
        }
    }


    /**
     * Format that will only check System.getProperty() and System.getenv()
     */
    public Optional format() {

        if (fields.size() == 0) {
            return Optional.of(pattern);
        }

        String newExpression = pattern;
        boolean foundField = false;
        for( SimpleField field : fields) {


            String property = System.getProperty(field.name);
            if (property != null) {
                foundField = true;
                newExpression = newExpression.replace(
                        String.format("{%s}", field.expression),
                        String.format("%s", property));
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Value for field {} was found as system property", field.name);
                }
                continue;
            }

            String env = System.getenv(field.name);

            if (env != null) {
                foundField = true;
                newExpression = newExpression.replace(
                        String.format("{%s}", field.expression),
                        String.format("%s", env));
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Value for field {} was found as system environment variable", field.name);
                }
                continue;
            }

            if (field.hasDefault()) {
                foundField = true;
                newExpression = newExpression.replace(
                        String.format("{%s}", field.expression),
                        String.format("%s", field.defaultValue));
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Value for field {} was found as system environment variable", field.name);
                }

            }



        }
        return foundField ?
                Optional.of(newExpression) :
                Optional.empty();

    }

    private void initializeFields(String expression, List fields) {

        int first = expression.indexOf("{");
        int next = expression.indexOf("}");

        if( first != -1 && next != -1) {
            String substring = expression.substring(++first, next);
            fields.add(SimpleField.of(substring));
            initializeFields(expression.substring(++next, expression.length()), fields);
        }
    }

    @Override
    public String toString() {
        return "StringTemplate{" +
                "pattern='" + pattern + '\'' +
                ", fields=" + Arrays.toString(fields.toArray()) +
                '}';
    }

    private static class SimpleField {

        public final String expression;
        public final String name;
        public final String defaultValue;

        static SimpleField of(String field) {
            return new SimpleField(field);
        }

        private SimpleField(String name) {
            this.expression = name;

            name = name.trim();
            if (name.contains("||")) {
                String[] split = name.split(Pattern.quote("||"));
                this.name = split[0].trim();
                this.defaultValue = split.length > 1 ? split[1].trim() : "";
            } else {
                this.name = name;
                this.defaultValue = null;
            }
        }

        public boolean hasDefault() {
            return defaultValue != null;
        }

        public  String  valueOf(T value) {
            return String.valueOf(value);
        }
    }

   private static class NoFieldsTemplate extends StringTemplate {

        private final Optional pattern;
        public NoFieldsTemplate(String pattern) {
            super(pattern);
            this.pattern = Optional.of(pattern);
        }

        @Override
        public Optional format(Event event) {
            return pattern;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy