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  
18  package org.apache.shardingsphere.logging.yaml.swapper;
19  
20  import org.apache.shardingsphere.logging.config.LoggingRuleConfiguration;
21  import org.apache.shardingsphere.logging.constant.LoggingOrder;
22  import org.apache.shardingsphere.logging.rule.builder.DefaultLoggingRuleConfigurationBuilder;
23  import org.apache.shardingsphere.logging.yaml.config.YamlAppendersConfigurationConverter;
24  import org.apache.shardingsphere.logging.yaml.config.YamlLoggersConfigurationConverter;
25  import org.apache.shardingsphere.logging.yaml.config.YamlLoggingRuleConfiguration;
26  import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
27  
28  /**
29   * YAML logging rule configuration swapper.
30   */
31  public final class YamlLoggingRuleConfigurationSwapper implements YamlRuleConfigurationSwapper<YamlLoggingRuleConfiguration, LoggingRuleConfiguration> {
32      
33      @Override
34      public YamlLoggingRuleConfiguration swapToYamlConfiguration(final LoggingRuleConfiguration data) {
35          YamlLoggingRuleConfiguration result = new YamlLoggingRuleConfiguration();
36          result.setLoggers(YamlLoggersConfigurationConverter.convertYamlLoggerConfigurations(data.getLoggers()));
37          result.setAppenders(YamlAppendersConfigurationConverter.convertYamlAppenderConfigurations(data.getAppenders()));
38          return result;
39      }
40      
41      @Override
42      public LoggingRuleConfiguration swapToObject(final YamlLoggingRuleConfiguration yamlConfig) {
43          LoggingRuleConfiguration result = new LoggingRuleConfiguration(YamlLoggersConfigurationConverter.convertShardingSphereLogger(yamlConfig.getLoggers()),
44                  YamlAppendersConfigurationConverter.convertShardingSphereAppender(yamlConfig.getAppenders()));
45          if (null == result.getLoggers()) {
46              result = getDefaultLoggingRuleConfiguration();
47          }
48          return result;
49      }
50      
51      private LoggingRuleConfiguration getDefaultLoggingRuleConfiguration() {
52          return new DefaultLoggingRuleConfigurationBuilder().build();
53      }
54      
55      @Override
56      public Class<LoggingRuleConfiguration> getTypeClass() {
57          return LoggingRuleConfiguration.class;
58      }
59      
60      @Override
61      public String getRuleTagName() {
62          return "LOGGING";
63      }
64      
65      @Override
66      public int getOrder() {
67          return LoggingOrder.ORDER;
68      }
69  }