Skip to content

Commit

Permalink
Merge d5113f0 into c2385f5
Browse files Browse the repository at this point in the history
  • Loading branch information
sunbufu committed Jul 14, 2019
2 parents c2385f5 + d5113f0 commit e0d1cf6
Show file tree
Hide file tree
Showing 28 changed files with 489 additions and 35 deletions.
Expand Up @@ -43,7 +43,7 @@ public EncryptRule(final EncryptRuleConfiguration encryptRuleConfiguration) {
this.encryptRuleConfig = encryptRuleConfiguration;
encryptorEngine = new ShardingEncryptorEngine(encryptRuleConfiguration);
}

/**
* Get encrypt table names.
*
Expand Down
@@ -0,0 +1,127 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package org.apache.shardingsphere.core.util;

import java.util.Properties;

import org.apache.shardingsphere.api.config.RuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.core.rule.Authentication;
import org.apache.shardingsphere.core.yaml.engine.YamlEngine;
import org.apache.shardingsphere.core.yaml.swapper.impl.AuthenticationYamlSwapper;
import org.apache.shardingsphere.core.yaml.swapper.impl.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.core.yaml.swapper.impl.MasterSlaveRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.core.yaml.swapper.impl.ShardingRuleConfigurationYamlSwapper;

import lombok.extern.slf4j.Slf4j;

/**
* Configuration printer class.
*
* @author sunbufu
*/
@Slf4j
public final class ConfigurationLogger {

private ConfigurationLogger() { }

/**
* log properties configuration.
*
* @param propsConfiguration properties configuration
*/
public static void log(final Properties propsConfiguration) {
if (null != propsConfiguration) {
log(propsConfiguration.getClass().getSimpleName(), YamlEngine.marshal(propsConfiguration));
}
}

/**
* log EncryptRuleConfiguration.
*
* @param encryptRuleConfiguration encryptRule configuration
*/
public static void log(final EncryptRuleConfiguration encryptRuleConfiguration) {
if (null != encryptRuleConfiguration) {
log(encryptRuleConfiguration.getClass().getSimpleName(),
YamlEngine.marshal(new EncryptRuleConfigurationYamlSwapper().swap(encryptRuleConfiguration)));
}
}

/**
* log ruleConfiguration.
*
* @param ruleConfiguration ruleConfiguration
*/
public static void log(final RuleConfiguration ruleConfiguration) {
if (null != ruleConfiguration) {
if (ruleConfiguration instanceof ShardingRuleConfiguration) {
ConfigurationLogger.log((ShardingRuleConfiguration) ruleConfiguration);
} else if (ruleConfiguration instanceof MasterSlaveRuleConfiguration) {
ConfigurationLogger.log((MasterSlaveRuleConfiguration) ruleConfiguration);
} else if (ruleConfiguration instanceof EncryptRuleConfiguration) {
ConfigurationLogger.log((EncryptRuleConfiguration) ruleConfiguration);
}
}
}

/**
* log ShardingRuleConfiguration.
*
* @param shardingRuleConfiguration shardingRule configuration
*/
public static void log(final ShardingRuleConfiguration shardingRuleConfiguration) {
if (null != shardingRuleConfiguration) {
log(shardingRuleConfiguration.getClass().getSimpleName(),
YamlEngine.marshal(new ShardingRuleConfigurationYamlSwapper().swap(shardingRuleConfiguration)));
}
}

/**
* log MasterSlaveRuleConfiguration.
*
* @param masterSlaveRuleConfiguration masterSlaveRule configuration
*/
public static void log(final MasterSlaveRuleConfiguration masterSlaveRuleConfiguration) {
if (null != masterSlaveRuleConfiguration) {
log(masterSlaveRuleConfiguration.getClass().getSimpleName(),
YamlEngine.marshal(new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRuleConfiguration)));
}
}

/**
* log AuthenticationConfiguration.
*
* @param authenticationConfiguration authentication configuration
*/
public static void log(final Authentication authenticationConfiguration) {
if (null != authenticationConfiguration) {
log(authenticationConfiguration.getClass().getSimpleName(),
YamlEngine.marshal(new AuthenticationYamlSwapper().swap(authenticationConfiguration)));
}
}

/**
* log configuration log.
*
* @param base base node name
* @param yamlStr yaml string
*/
public static void log(final String base, final String yamlStr) {
log.info("{}\n{}", base, yamlStr);
}

}
@@ -0,0 +1,196 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package org.apache.shardingsphere.core.util;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

import org.apache.shardingsphere.api.config.RuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptorRuleConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.core.rule.Authentication;
import org.apache.shardingsphere.core.rule.ProxyUser;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class ConfigurationLoggerTest {

@Mock
private Logger log;

@Before
public void setLog() throws NoSuchFieldException, IllegalAccessException {
Field field = ConfigurationLogger.class.getDeclaredField("log");
setFinalStatic(field, log);
}

private static void setFinalStatic(final Field field, final Object newValue)
throws NoSuchFieldException, IllegalAccessException {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}

public void assertEqualsWithLogInfo(final String base, final String yamlStr) {
doAnswer(new Answer() {
@Override
public Void answer(InvocationOnMock invocationOnMock) {
Assert.assertEquals(base, invocationOnMock.getArgument(1));
Assert.assertEquals(yamlStr, invocationOnMock.getArgument(2));
return null;
}
}).when(log).info(anyString(), anyString(), anyString());
}

@Test
public void logPropsConfiguration() {
Properties properties = new Properties();
properties.put("masterDataSourceName", "master_ds");
properties.put("slaveDataSourceNames", Arrays.asList("slave_ds_0", "slave_ds_1"));

String base = "Properties";
String yamlStr =
"slaveDataSourceNames:\n" + "- slave_ds_0\n" + "- slave_ds_1\n" + "masterDataSourceName: master_ds\n";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log(properties);
}

@Test
public void logEncryptRuleConfiguration() {
EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration();
Properties properties = new Properties();
properties.put("aes.key.value", "123456abc");
EncryptorRuleConfiguration encryptorRuleConfiguration =
new EncryptorRuleConfiguration("aes", "user.user_name", properties);
encryptRuleConfiguration.getEncryptorRuleConfigs().put("encryptor_aes", encryptorRuleConfiguration);

String base = "EncryptRuleConfiguration";
String yamlStr = "encryptors:\n" + " encryptor_aes:\n" + " assistedQueryColumns: ''\n" + " props:\n"
+ " aes.key.value: 123456abc\n" + " qualifiedColumns: user.user_name\n" + " type: aes\n";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log(encryptRuleConfiguration);
}

@Test
public void logShardingRuleConfiguration() {
ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("user", "ds_${0}.user_${0..1}");
shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration);

String base = "ShardingRuleConfiguration";
String yamlStr =
"tables:\n" + " user:\n" + " actualDataNodes: ds_${0}.user_${0..1}\n" + " logicTable: user\n";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log(shardingRuleConfiguration);
}

@Test
public void logMasterSlaveRuleConfiguration() {
MasterSlaveRuleConfiguration masterSlaveRuleConfiguration =
new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1"));

String base = "MasterSlaveRuleConfiguration";
String yamlStr = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n"
+ "- slave_ds_0\n" + "- slave_ds_1\n";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log(masterSlaveRuleConfiguration);
}

@Test
public void logRuleConfiguration(){
String base, yamlStr;
// EncryptRuleConfiguration
EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration();
Properties properties = new Properties();
properties.put("aes.key.value", "123456abc");
EncryptorRuleConfiguration encryptorRuleConfiguration =
new EncryptorRuleConfiguration("aes", "user.user_name", properties);
encryptRuleConfiguration.getEncryptorRuleConfigs().put("encryptor_aes", encryptorRuleConfiguration);

base = "EncryptRuleConfiguration";
yamlStr = "encryptors:\n" + " encryptor_aes:\n" + " assistedQueryColumns: ''\n" + " props:\n"
+ " aes.key.value: 123456abc\n" + " qualifiedColumns: user.user_name\n" + " type: aes\n";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log((RuleConfiguration) encryptRuleConfiguration);

// ShardingRuleConfiguration
ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("user", "ds_${0}.user_${0..1}");
shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration);

base = "ShardingRuleConfiguration";
yamlStr =
"tables:\n" + " user:\n" + " actualDataNodes: ds_${0}.user_${0..1}\n" + " logicTable: user\n";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log((RuleConfiguration) shardingRuleConfiguration);

// MasterSlaveRuleConfiguration
MasterSlaveRuleConfiguration masterSlaveRuleConfiguration =
new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1"));

base = "MasterSlaveRuleConfiguration";
yamlStr = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n"
+ "- slave_ds_0\n" + "- slave_ds_1\n";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log((RuleConfiguration) masterSlaveRuleConfiguration);
}

@Test
public void logAuthenticationConfiguration() {
Authentication authentication = new Authentication();
authentication.getUsers().put("root", new ProxyUser("123456", Collections.singletonList("sharding_db")));

String base = "Authentication";
String yamlStr = "users:\n" + " root:\n" + " authorizedSchemas: sharding_db\n" + " password: '123456'\n";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log(authentication);
}

@Test
public void log() {
String base = "base";
String yamlStr = "yamlStr";
assertEqualsWithLogInfo(base, yamlStr);

ConfigurationLogger.log(base, yamlStr);
}

}
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.apache.shardingsphere" level="info" additivity="false">
<appender-ref ref="console"/>
</logger>
<logger name="org.apache.shardingsphere.core.util.ConfigurationLogger" level="off" />

<root>
<level value="error" />
<appender-ref ref="console" />
</root>
</configuration>
Expand Up @@ -26,7 +26,8 @@
<appender-ref ref="console"/>
</logger>
<logger name="org.apache.shardingsphere.core.execute.sql.execute.threadlocal.ExecutorExceptionHandler" level="off" />

<logger name="org.apache.shardingsphere.core.util.ConfigurationLogger" level="off" />

<root>
<level value="error" />
<appender-ref ref="console" />
Expand Down
Expand Up @@ -26,7 +26,8 @@
<appender-ref ref="console"/>
</logger>
<logger name="org.apache.shardingsphere.core.execute.sql.execute.threadlocal.ExecutorExceptionHandler" level="off" />

<logger name="org.apache.shardingsphere.core.util.ConfigurationLogger" level="off" />

<root>
<level value="error" />
<appender-ref ref="console" />
Expand Down
Expand Up @@ -26,7 +26,8 @@
<appender-ref ref="console"/>
</logger>
<logger name="org.apache.shardingsphere.core.execute.sql.execute.threadlocal.ExecutorExceptionHandler" level="off" />

<logger name="org.apache.shardingsphere.core.util.ConfigurationLogger" level="off" />

<root>
<level value="error" />
<appender-ref ref="console" />
Expand Down

0 comments on commit e0d1cf6

Please sign in to comment.