`
sillycat
  • 浏览: 2551624 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Sillycat Framework(II)Enhancement of BeanFilter

阅读更多
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;
}
}


分享到:
评论

相关推荐

    sillycat-playrest:使用 Playframework 2.x 构建基于 JAVA8 的 RESTful 服务

    使用 Playframework 2.x 构建基于 JAVA8 的 RESTful 服务。 使用原生 SQL 构建基于 DBUtils 的 DAO 层。 使用 flyway 进行数据库迁移。 Flyway支持sbt、maven等,支持java/scala等API。 (DAO测试基于localhost/...

    sillycat-ui:所有与UI相关的功能和插件

    此外,考虑到现代Web应用对性能的要求,"sillycat-ui"可能也进行了优化,比如懒加载策略,只在需要时加载组件,减少首屏加载时间;或者使用Tree Shaking技术,去除未使用的代码,减小打包后的体积。 最后,文档是...

    sillycat:SillyCat是一种无线传感器系统

    傻猫 发展 入门 需要执行以下步骤 安装所需的软件包 $ sudo apt install scons gcc-avr avr-libc avrdude 安装可选软件包 $ sudo apt install astyle valgrind 安装 $ sudo apt install cmake ...

    sillycat-camel:告诉我自己如何使用骆驼

    这个名为 "sillycat-camel" 的项目,可能是某位开发者为了学习或者实践Apache Camel而创建的一个示例项目。从标题和描述来看,这可能是一个教程或者指导,旨在帮助用户了解如何使用Apache Camel进行自我学习。 ...

    sillycat2.github.io:水水水

    【sillycat2.github.io:水水水】项目是一个基于GitHub Pages构建的个人博客网站,由用户"傻猫2"创建。GitHub Pages是GitHub提供的一项免费服务,允许用户托管静态网页,通常用于个人简历、博客或者项目展示。在这个...

    sillycat-proxy:代理到不同位置的代理

    傻猫代理 代理到不同位置的代理。

    傻猫火花

    傻猫火花 去做 添加单元测试 支持Spark SQL 支持火花流 ...bin / spark-submit-class com.sillycat.spark.ExecutorApp /Users/carl/work/sillycat/sillycat-spark/target/scala-2.10/sillycat-spark-assembly

Global site tag (gtag.js) - Google Analytics