俊星的BLOG

我的DWR之DefaultContainer

DWR中采用DefaultContainer来加载默认的配置信息,下面是我的实现:
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class CantainerTest {
    
public static void main(String[] args) {
        Cantainer cant 
= new Cantainer();
        
try {
            cant.addParam(
"name""can_name");
            cant.addParam(
"bool""true");
            cant.addParam(
"age""111");
            cant.addParam(
"beanOne"new BeanOne());
            cant.addParam(BeanTwo.
class.getName(), "BeanTwo");
            cant.finishConfig();

            System.out.println(cant.getBean(
"name"));
            System.out.println(cant.getBean(
"bool"));
            System.out.println(cant.getBean(
"age"));
            System.out.println(cant.getBean(
"beanOne"));
            System.out.println(cant.getBean(BeanTwo.
class.getName()));

        }
 catch (Exception e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}


class Cantainer {
    
private Map<String, Object> beans = new HashMap<String, Object>();

    
public void addParam(String key, Object value) throws Exception {
        
if (value instanceof String) {
            
try {
                Class impl 
= Class.forName((String) value);
                value 
= impl.newInstance();
            }
 catch (ClassNotFoundException e) {
            }

        }

        beans.put(key, value);
    }


    
public void finishConfig() throws Exception {
        Collection
<Object> col = beans.values();
        
for (Object obj : col) {
            
if (!(obj instanceof String)) {
                
// 开始自动匹配
                BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
                
for (PropertyDescriptor desc : bi.getPropertyDescriptors()) {
                    Object value 
= beans.get(desc.getName());
                    
if (value != null{
                        Method setter 
= desc.getWriteMethod();
                        Class propType 
= setter.getParameterTypes()[0];
                        
boolean invokeFlg = false;
                        
if (value.getClass().isAssignableFrom(propType)) {
                            invokeFlg 
= true;
                        }
 else if (propType == Boolean.TYPE && value.getClass() == String.class{
                            value 
= Boolean.valueOf((String) value);
                            invokeFlg 
= true;
                        }
 else if (propType == Integer.TYPE && value.getClass() == String.class{
                            value 
= Integer.valueOf((String) value);
                            invokeFlg 
= true;
                        }
 else if (propType == Boolean.TYPE && value.getClass() == String.class{
                            value 
= Boolean.valueOf((String) value);
                            invokeFlg 
= true;
                        }

                        
if (invokeFlg) {
                            setter.invoke(obj, 
new Object[] { value });
                        }

                    }

                }

            }

        }

    }


    
public Object getBean(String key) {
        
return beans.get(key);
    }

}


class BeanOne {
    
public BeanOne() {
        
super();
        
this.name = "name";
    }


    
private String name;

    
public String getName() {
        
return name;
    }


    
public void setName(String name) {
        
this.name = name;
    }


    
public String toString() {
        
return "BeanOne name:" + this.name;
    }

}


class BeanTwo {
    
private BeanOne beanOne;
    
private boolean bool;
    
private int age;

    
public BeanTwo() {
        
this.beanOne = new BeanOne();
        
this.bool = false;
        
this.age = 123;
    }


    
public BeanOne getBeanOne() {
        
return beanOne;
    }


    
public void setBeanOne(BeanOne beanOne) {
        
this.beanOne = beanOne;
    }


    
public boolean isBool() {
        
return bool;
    }


    
public void setBool(boolean bool) {
        
this.bool = bool;
    }


    
public int getAge() {
        
return age;
    }


    
public void setAge(int age) {
        
this.age = age;
    }


    
public String toString() {
        
return "BeanTwo beanOne:<" + this.beanOne + "> bool:" + this.bool + " age:" + this.age;
    }

}

输出如下:
can_name
true
111
BeanOne name:can_name
BeanTwo beanOne:<BeanOne name:can_name> bool:true age:111

posted on 2009-05-20 22:45 俊星 阅读(226) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: