1 5 package org.exoplatform.container.configuration; 6 7 import java.util.* ; 8 import org.apache.commons.beanutils.PropertyUtils ; 9 15 public class ObjectParam extends Parameter { 16 private String type_ ; 17 private String package_ ; 18 private Object object_ ; 19 private ArrayList properties_ = new ArrayList() ; 20 21 public String getType() { return type_ ;} 22 public void setType(String s) { 23 type_ = s; 24 int idx = type_.lastIndexOf(".") ; 25 if(idx > 0 ) { 26 package_ = type_.substring(0, idx) ; 27 } 28 } 29 30 31 public Object getObject() { 32 if(object_ == null) { 33 populateBean() ; 34 } 35 return object_ ; 36 } 37 38 public void addProperty(String name , String value) { 39 properties_.add(new Property(name, value)) ; 40 } 41 42 private void populateBean() { 43 Property prop = null ; 44 try { 45 Class clazz = Class.forName(type_) ; 46 object_ = clazz.newInstance() ; 47 for(int i = 0 ; i < properties_.size() ; i++) { 48 prop = (Property) properties_.get(i) ; 49 if(prop.name_.endsWith("]")) { 50 populateBeanInArray(object_ , prop.name_ , prop.value_) ; 52 } else { 53 Object valueBean = getValue(prop.value_) ; 54 PropertyUtils.setProperty(object_, prop.name_, valueBean) ; 55 } 56 } 57 } catch(Throwable ex) { 58 if(prop != null) { 59 } 62 ex.printStackTrace() ; 63 } 64 } 65 66 private void populateBeanInArray(Object bean, String name , String value) throws Exception { 67 int idx = name.lastIndexOf("[") ; 68 String arrayBeanName = name.substring(0, idx) ; 69 int index = Integer.parseInt(name.substring(idx + 1, name.length() - 1)) ; 70 Object arrayBean = PropertyUtils.getProperty(bean, arrayBeanName) ; 71 if(arrayBean instanceof List) { 72 List list = (List) arrayBean ; 73 Object valueBean = getValue(value) ; 74 if(list.size() == index) { 75 list.add(valueBean) ; 76 } else { 77 list.set(index, valueBean) ; 78 } 79 } else if(arrayBean instanceof Collection) { 80 Collection c = (Collection) arrayBean ; 81 Object valueBean = getValue(value) ; 82 c.add(valueBean) ; 83 } else { 84 Object [] array = (Object []) arrayBean ; 85 array[index] = getValue(value) ; 86 } 87 } 88 89 private Object getValue(String value) throws Exception { 90 if(value.startsWith("#new")) { 91 String [] temp = value.split(" ") ; 92 String className = temp[1] ; 93 if(className.indexOf(".") < 0) { 94 className = package_ + "." + className ; 95 Class clazz = Class.forName(className) ; 96 return clazz.newInstance() ; 97 } 98 } else if(value.startsWith("#int")) { 99 String [] temp = value.split(" ") ; 100 value = temp[1].trim() ; 101 return new Integer (value) ; 102 } else if(value.startsWith("#boolean")) { 103 String [] temp = value.split(" ") ; 104 value = temp[1].trim() ; 105 return new Boolean ("true".equals(value)) ; 106 } 107 return value ; 108 } 109 110 static class Property { 111 String name_ ; 112 String value_ ; 113 114 public Property(String name, String value) { 115 name_ = name; 116 value_ = value ; 117 } 118 } 119 } | Popular Tags |