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.core.config.builder.impl;
18  
19  import java.util.ArrayList;
20  import java.util.LinkedHashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.logging.log4j.Level;
25  import org.apache.logging.log4j.core.config.Configuration;
26  import org.apache.logging.log4j.core.config.builder.api.Component;
27  import org.apache.logging.log4j.core.config.builder.api.ComponentBuilder;
28  import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
29  
30  /**
31   * Generic component that captures attributes and Components in preparation for assembling the Appender's
32   * Component.
33   *
34   * @since 2.4
35   */
36  class DefaultComponentBuilder<T extends ComponentBuilder<T>, CB extends ConfigurationBuilder<? extends Configuration>>
37          implements ComponentBuilder<T> {
38  
39      private final CB builder;
40      private final String type;
41      private final Map<String, String> attributes = new LinkedHashMap<>();
42      private final List<Component> components = new ArrayList<>();
43      private final String name;
44      private final String value;
45  
46      public DefaultComponentBuilder(final CB builder, final String type) {
47          this(builder, null, type, null);
48      }
49  
50      public DefaultComponentBuilder(final CB builder, final String name, final String type) {
51          this(builder, name, type, null);
52      }
53  
54      public DefaultComponentBuilder(final CB builder, final String name, final String type,
55              final String value) {
56          this.type = type;
57          this.builder = builder;
58          this.name = name;
59          this.value = value;
60      }
61  
62      @Override
63      public T addAttribute(final String key, final boolean value) {
64          return put(key, Boolean.toString(value));
65      }
66  
67      @Override
68      public T addAttribute(final String key, final Enum<?> value) {
69          return put(key, value.name());
70      }
71  
72      @Override
73      public T addAttribute(final String key, final int value) {
74          return put(key, Integer.toString(value));
75      }
76  
77  
78      @Override
79      public T addAttribute(final String key, final Level level) {
80          return put(key, level.toString());
81      }
82  
83      @Override
84      public T addAttribute(final String key, final Object value) {
85          return put(key, value.toString());
86      }
87  
88  
89      @Override
90      public T addAttribute(final String key, final String value) {
91          return put(key, value);
92      }
93  
94      @Override
95      @SuppressWarnings("unchecked")
96      public T addComponent(final ComponentBuilder<?> builder) {
97          components.add(builder.build());
98          return (T) this;
99      }
100 
101     @Override
102     public Component build() {
103         final Component component = new Component(type, name, value);
104         component.getAttributes().putAll(attributes);
105         component.getComponents().addAll(components);
106         return component;
107     }
108 
109     @Override
110     public CB getBuilder() {
111         return builder;
112     }
113 
114     @Override
115     public String getName() {
116         return name;
117     }
118 
119     @SuppressWarnings("unchecked")
120     protected T put(final String key, final String value) {
121         attributes.put(key, value);
122         return (T) this;
123     }
124 }