View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.spring.boot;
18  
19  import org.apache.logging.log4j.core.config.Configuration;
20  import org.apache.logging.log4j.core.config.Node;
21  import org.apache.logging.log4j.core.config.arbiters.Arbiter;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
24  import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
25  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
26  import org.springframework.core.env.Environment;
27  import org.springframework.core.env.Profiles;
28  import org.springframework.util.StringUtils;
29  
30  /**
31   * An Aribter that uses the active Spring profile to determine if configuration should be included.
32   */
33  @Plugin(name = "SpringProfile", category = Node.CATEGORY, elementType = Arbiter.ELEMENT_TYPE,
34          deferChildren = true, printObject = true)
35  public class SpringProfileArbiter extends SpringEnvironmentHolder implements Arbiter {
36  
37      private final String[] profileNames;
38  
39      private SpringProfileArbiter(final String[] profiles) {
40          this.profileNames = profiles;
41  
42      }
43  
44      @Override
45      public boolean isCondition() {
46          Environment environment = getEnvironment();
47          if (environment == null) {
48              return false;
49          }
50  
51          if (profileNames.length == 0) {
52              return false;
53          }
54          return environment.acceptsProfiles(Profiles.of(profileNames));
55      }
56  
57      @PluginBuilderFactory
58      public static Builder newBuilder() {
59          return new Builder();
60      }
61  
62      public static class Builder implements org.apache.logging.log4j.core.util.Builder<SpringProfileArbiter> {
63  
64          public static final String ATTR_NAME = "name";
65  
66          @PluginBuilderAttribute(ATTR_NAME)
67          private String name;
68  
69          @PluginConfiguration
70          private Configuration configuration;;
71  
72          /**
73           * Sets the Profile Name or Names.
74           * @param name the profile name(s).
75           * @return this
76           */
77          public Builder setName(final String name) {
78              this.name = name;
79              return asBuilder();
80          }
81  
82          public Builder setConfiguration(final Configuration configuration) {
83              this.configuration = configuration;
84              return asBuilder();
85          }
86  
87          private SpringProfileArbiter.Builder asBuilder() {
88              return this;
89          }
90  
91          public SpringProfileArbiter build() {
92              String[] profileNames = StringUtils.trimArrayElements(
93                      StringUtils.commaDelimitedListToStringArray(configuration.getStrSubstitutor().replace(name)));
94              return new SpringProfileArbiter(profileNames);
95          }
96      }
97  }