001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.spring.boot;
018
019import org.apache.logging.log4j.core.config.Configuration;
020import org.apache.logging.log4j.core.config.Node;
021import org.apache.logging.log4j.core.config.arbiters.Arbiter;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
024import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
025import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
026import org.springframework.core.env.Environment;
027import org.springframework.core.env.Profiles;
028import org.springframework.util.StringUtils;
029
030/**
031 * An Aribter that uses the active Spring profile to determine if configuration should be included.
032 */
033@Plugin(name = "SpringProfile", category = Node.CATEGORY, elementType = Arbiter.ELEMENT_TYPE,
034        deferChildren = true, printObject = true)
035public class SpringProfileArbiter extends SpringEnvironmentHolder implements Arbiter {
036
037    private final String[] profileNames;
038
039    private SpringProfileArbiter(final String[] profiles) {
040        this.profileNames = profiles;
041
042    }
043
044    @Override
045    public boolean isCondition() {
046        Environment environment = getEnvironment();
047        if (environment == null) {
048            return false;
049        }
050
051        if (profileNames.length == 0) {
052            return false;
053        }
054        return environment.acceptsProfiles(Profiles.of(profileNames));
055    }
056
057    @PluginBuilderFactory
058    public static Builder newBuilder() {
059        return new Builder();
060    }
061
062    public static class Builder implements org.apache.logging.log4j.core.util.Builder<SpringProfileArbiter> {
063
064        public static final String ATTR_NAME = "name";
065
066        @PluginBuilderAttribute(ATTR_NAME)
067        private String name;
068
069        @PluginConfiguration
070        private Configuration configuration;;
071
072        /**
073         * Sets the Profile Name or Names.
074         * @param name the profile name(s).
075         * @return this
076         */
077        public Builder setName(final String name) {
078            this.name = name;
079            return asBuilder();
080        }
081
082        public Builder setConfiguration(final Configuration configuration) {
083            this.configuration = configuration;
084            return asBuilder();
085        }
086
087        private SpringProfileArbiter.Builder asBuilder() {
088            return this;
089        }
090
091        public SpringProfileArbiter build() {
092            String[] profileNames = StringUtils.trimArrayElements(
093                    StringUtils.commaDelimitedListToStringArray(configuration.getStrSubstitutor().replace(name)));
094            return new SpringProfileArbiter(profileNames);
095        }
096    }
097}