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("
[email protected]");
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;
}
}