- 浏览: 2551624 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Sillycat Framework(II)Enhancement of BeanFilter
1. filter the bean properties
2. consider the Map=null
3. consider the property is POJO, and POJO is null
4. consider the properties file loading and configuration policy
1. BeanFilter interface in Java
BeanFilter.java:
package com.sillycat.easymarket.filter;
public interface BeanFilter {
public void filter(Object obj, String rule);
}
2. BeanFilterImpl implemetation class in Groovy
BeanFilterImpl.groovy:
package com.sillycat.easymarket.filter;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
import com.sillycat.easymarket.utils.SystemConfigurationAdapter;
import org.springframework.stereotype.Component;
public class BeanFilterImpl implements BeanFilter {
private static final String DEFAULT_RULE = "rule1";
private static final String DOT = ".";
private final Log log = LogFactory.getLog(this.getClass());
@Autowired
SystemConfigurationAdapter filterConfiguration;
public void filter(Object obj, String rule) {
if (null == obj) {
log.info("null Object do not need filter!");
return;
}
if (null == rule || "".equals(rule.trim())) {
rule = DEFAULT_RULE;
}
log.info("filter rule...." + rule);
List<String> filters = filterConfiguration.getStringList(rule);
if (filters != null && !filters.isEmpty()) {
for (int i = 0; i < filters.size(); i++) {
String filter = filters.get(i);
try {
if (!needIgnore(obj, filter)) {
log.info("doing filter rule...." + filter);
PropertyUtils.setProperty(obj, filter, null);
}
} catch (IllegalAccessException e) {
log.error(e);
} catch (InvocationTargetException e) {
log.error(e);
} catch (NoSuchMethodException e) {
log.error(e);
}
}
}
}
private boolean needIgnore(Object obj, String filter)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
boolean flag = false;
if (null == filter && "".equals(filter.trim())) {
flag = true;
return flag;
}
if (isNullValueInPath(obj, filter, 0)) {
flag = true;
return flag;
}
return flag;
}
private boolean isNullValueInPath(Object obj, String filter, int start)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
boolean flag = false;
int location = 0;
if ((location = filter.indexOf(DOT, start)) > 0) {
String name = filter.substring(start, location);
Object obj_tmp = BeanUtils.getProperty(obj, name);
if (obj_tmp == null) {
flag = true;
return flag;
}
isNullValueInPath(obj, filter, location + 1);
}
return flag;
}
}
3. the filter.properties control the filter processor
#filter rule 1
rule1=personPassword
rule1=address.city
rule1=others.other2
#filter rule 2
rule2=personPassword
rule2=address.country
rule2=others.other1
rule2=others.other2
rule2=address.email
4. TestCase of this groovy implementation:
package com.sillycat.easymarket.filter;
import java.util.HashMap;
import java.util.Map;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
public class BeanFilterTest extends BaseTestCase {
private BeanFilter beanFilter;
public void setUp() throws Exception {
super.setUp();
beanFilter = (BeanFilter) ctx.getBean("beanFilter");
}
public void tearDown() throws Exception {
super.tearDown();
}
public void testDumy() {
assertTrue(true);
}
public void testFilter() {
Person p = new Person();
Address a = new Address();
a.setCity("Chengdu");
a.setCountry("China");
a.setEmail("hua.luo@chengdu.digby.com");
a.setId(Integer.valueOf(1));
//p.setAddress(a);
p.setAge(Integer.valueOf(29));
p.setGender("man");
p.setPersonName("sillycat");
p.setPersonPassword("test");
p.setId(Integer.valueOf(1));
Map<String, Object> others = new HashMap<String, Object>();
others.put("other1", "good");
others.put("other2", "other requirement");
//p.setOthers(others);
System.out.println(p);
System.out.println(p.getOthers());
beanFilter.filter(p, null);
System.out.println(p);
System.out.println(p.getOthers());
}
}
5. properties file loading policy in spring
<bean id="filterConfiguration" class="com.sillycat.easymarket.utils.SystemConfigurationAdapter">
<property name="configfile" value="filter.properties"/>
</bean>
package com.sillycat.easymarket.utils;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SystemConfigurationAdapter {
private final Log log = LogFactory.getLog(this.getClass());
private CompositeConfiguration config;
private PropertiesConfiguration propertiesConfig;
private String configfile = "easygroovy.properties";
public void init() {
config = new CompositeConfiguration();
if (propertiesConfig == null) {
try {
propertiesConfig = new PropertiesConfiguration(configfile);
log.info("configuration file loading..." + configfile);
propertiesConfig
.setReloadingStrategy(new FileChangedReloadingStrategy());
config.addConfiguration(propertiesConfig);
} catch (ConfigurationException e) {
log.error(e);
}
}
}
public String getString(String propertyKey) {
if (config == null) {
init();
}
return config.getString(propertyKey);
}
public String getString(String propertyKey, String defaultValue) {
if (config == null) {
init();
}
return config.getString(propertyKey, defaultValue);
}
public int getInt(String propertyKey) {
if (config == null) {
init();
}
return config.getInt(propertyKey);
}
public int getInt(String key, int defaultValue) {
if (config == null) {
init();
}
return config.getInt(key, defaultValue);
}
public float getFloat(String propertyKey) {
if (config == null) {
init();
}
return config.getFloat(propertyKey);
}
public float getFloat(String propertyKey, float defaultValue) {
if (config == null) {
init();
}
return config.getFloat(propertyKey, defaultValue);
}
public boolean getBoolean(String propertyKey) {
if (config == null) {
init();
}
return config.getBoolean(propertyKey);
}
public boolean getBoolean(String propertyKey, boolean defualtValue) {
return config.getBoolean(propertyKey, defualtValue);
}
public String[] getStringArray(String propertyKey) {
if (config == null) {
init();
}
return config.getStringArray(propertyKey);
}
public List<String> getStringList(String propertyKey) {
List<String> list = new ArrayList<String>();
String[] strArr = getStringArray(propertyKey);
for (String value : strArr) {
list.add(value);
}
return list;
}
@SuppressWarnings("rawtypes")
public List getList(String propertyKey) {
if (config == null) {
init();
}
return config.getList(propertyKey);
}
public void setConfigfile(String configfile) {
this.configfile = configfile;
}
}
1. filter the bean properties
2. consider the Map=null
3. consider the property is POJO, and POJO is null
4. consider the properties file loading and configuration policy
1. BeanFilter interface in Java
BeanFilter.java:
package com.sillycat.easymarket.filter;
public interface BeanFilter {
public void filter(Object obj, String rule);
}
2. BeanFilterImpl implemetation class in Groovy
BeanFilterImpl.groovy:
package com.sillycat.easymarket.filter;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
import com.sillycat.easymarket.utils.SystemConfigurationAdapter;
import org.springframework.stereotype.Component;
public class BeanFilterImpl implements BeanFilter {
private static final String DEFAULT_RULE = "rule1";
private static final String DOT = ".";
private final Log log = LogFactory.getLog(this.getClass());
@Autowired
SystemConfigurationAdapter filterConfiguration;
public void filter(Object obj, String rule) {
if (null == obj) {
log.info("null Object do not need filter!");
return;
}
if (null == rule || "".equals(rule.trim())) {
rule = DEFAULT_RULE;
}
log.info("filter rule...." + rule);
List<String> filters = filterConfiguration.getStringList(rule);
if (filters != null && !filters.isEmpty()) {
for (int i = 0; i < filters.size(); i++) {
String filter = filters.get(i);
try {
if (!needIgnore(obj, filter)) {
log.info("doing filter rule...." + filter);
PropertyUtils.setProperty(obj, filter, null);
}
} catch (IllegalAccessException e) {
log.error(e);
} catch (InvocationTargetException e) {
log.error(e);
} catch (NoSuchMethodException e) {
log.error(e);
}
}
}
}
private boolean needIgnore(Object obj, String filter)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
boolean flag = false;
if (null == filter && "".equals(filter.trim())) {
flag = true;
return flag;
}
if (isNullValueInPath(obj, filter, 0)) {
flag = true;
return flag;
}
return flag;
}
private boolean isNullValueInPath(Object obj, String filter, int start)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
boolean flag = false;
int location = 0;
if ((location = filter.indexOf(DOT, start)) > 0) {
String name = filter.substring(start, location);
Object obj_tmp = BeanUtils.getProperty(obj, name);
if (obj_tmp == null) {
flag = true;
return flag;
}
isNullValueInPath(obj, filter, location + 1);
}
return flag;
}
}
3. the filter.properties control the filter processor
#filter rule 1
rule1=personPassword
rule1=address.city
rule1=others.other2
#filter rule 2
rule2=personPassword
rule2=address.country
rule2=others.other1
rule2=others.other2
rule2=address.email
4. TestCase of this groovy implementation:
package com.sillycat.easymarket.filter;
import java.util.HashMap;
import java.util.Map;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
public class BeanFilterTest extends BaseTestCase {
private BeanFilter beanFilter;
public void setUp() throws Exception {
super.setUp();
beanFilter = (BeanFilter) ctx.getBean("beanFilter");
}
public void tearDown() throws Exception {
super.tearDown();
}
public void testDumy() {
assertTrue(true);
}
public void testFilter() {
Person p = new Person();
Address a = new Address();
a.setCity("Chengdu");
a.setCountry("China");
a.setEmail("hua.luo@chengdu.digby.com");
a.setId(Integer.valueOf(1));
//p.setAddress(a);
p.setAge(Integer.valueOf(29));
p.setGender("man");
p.setPersonName("sillycat");
p.setPersonPassword("test");
p.setId(Integer.valueOf(1));
Map<String, Object> others = new HashMap<String, Object>();
others.put("other1", "good");
others.put("other2", "other requirement");
//p.setOthers(others);
System.out.println(p);
System.out.println(p.getOthers());
beanFilter.filter(p, null);
System.out.println(p);
System.out.println(p.getOthers());
}
}
5. properties file loading policy in spring
<bean id="filterConfiguration" class="com.sillycat.easymarket.utils.SystemConfigurationAdapter">
<property name="configfile" value="filter.properties"/>
</bean>
package com.sillycat.easymarket.utils;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SystemConfigurationAdapter {
private final Log log = LogFactory.getLog(this.getClass());
private CompositeConfiguration config;
private PropertiesConfiguration propertiesConfig;
private String configfile = "easygroovy.properties";
public void init() {
config = new CompositeConfiguration();
if (propertiesConfig == null) {
try {
propertiesConfig = new PropertiesConfiguration(configfile);
log.info("configuration file loading..." + configfile);
propertiesConfig
.setReloadingStrategy(new FileChangedReloadingStrategy());
config.addConfiguration(propertiesConfig);
} catch (ConfigurationException e) {
log.error(e);
}
}
}
public String getString(String propertyKey) {
if (config == null) {
init();
}
return config.getString(propertyKey);
}
public String getString(String propertyKey, String defaultValue) {
if (config == null) {
init();
}
return config.getString(propertyKey, defaultValue);
}
public int getInt(String propertyKey) {
if (config == null) {
init();
}
return config.getInt(propertyKey);
}
public int getInt(String key, int defaultValue) {
if (config == null) {
init();
}
return config.getInt(key, defaultValue);
}
public float getFloat(String propertyKey) {
if (config == null) {
init();
}
return config.getFloat(propertyKey);
}
public float getFloat(String propertyKey, float defaultValue) {
if (config == null) {
init();
}
return config.getFloat(propertyKey, defaultValue);
}
public boolean getBoolean(String propertyKey) {
if (config == null) {
init();
}
return config.getBoolean(propertyKey);
}
public boolean getBoolean(String propertyKey, boolean defualtValue) {
return config.getBoolean(propertyKey, defualtValue);
}
public String[] getStringArray(String propertyKey) {
if (config == null) {
init();
}
return config.getStringArray(propertyKey);
}
public List<String> getStringList(String propertyKey) {
List<String> list = new ArrayList<String>();
String[] strArr = getStringArray(propertyKey);
for (String value : strArr) {
list.add(value);
}
return list;
}
@SuppressWarnings("rawtypes")
public List getList(String propertyKey) {
if (config == null) {
init();
}
return config.getList(propertyKey);
}
public void setConfigfile(String configfile) {
this.configfile = configfile;
}
}
发表评论
-
Stop Update Here
2020-04-28 09:00 316I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 475NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 368Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 369Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 336Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 430Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 435Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 373Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 455VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 384Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 423Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 247GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 450GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 317Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 293Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ...
相关推荐
使用 Playframework 2.x 构建基于 JAVA8 的 RESTful 服务。 使用原生 SQL 构建基于 DBUtils 的 DAO 层。 使用 flyway 进行数据库迁移。 Flyway支持sbt、maven等,支持java/scala等API。 (DAO测试基于localhost/...
此外,考虑到现代Web应用对性能的要求,"sillycat-ui"可能也进行了优化,比如懒加载策略,只在需要时加载组件,减少首屏加载时间;或者使用Tree Shaking技术,去除未使用的代码,减小打包后的体积。 最后,文档是...
傻猫 发展 入门 需要执行以下步骤 安装所需的软件包 $ sudo apt install scons gcc-avr avr-libc avrdude 安装可选软件包 $ sudo apt install astyle valgrind 安装 $ sudo apt install cmake ...
这个名为 "sillycat-camel" 的项目,可能是某位开发者为了学习或者实践Apache Camel而创建的一个示例项目。从标题和描述来看,这可能是一个教程或者指导,旨在帮助用户了解如何使用Apache Camel进行自我学习。 ...
【sillycat2.github.io:水水水】项目是一个基于GitHub Pages构建的个人博客网站,由用户"傻猫2"创建。GitHub Pages是GitHub提供的一项免费服务,允许用户托管静态网页,通常用于个人简历、博客或者项目展示。在这个...
傻猫代理 代理到不同位置的代理。
傻猫火花 去做 添加单元测试 支持Spark SQL 支持火花流 ...bin / spark-submit-class com.sillycat.spark.ExecutorApp /Users/carl/work/sillycat/sillycat-spark/target/scala-2.10/sillycat-spark-assembly