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