KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > container > configuration > ObjectParam


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.container.configuration;
6
7 import java.util.* ;
8 import org.apache.commons.beanutils.PropertyUtils ;
9 /**
10  * Jul 19, 2004
11  * @author: Tuan Nguyen
12  * @email: tuan08@users.sourceforge.net
13  * @version: $Id: ObjectParam.java,v 1.1 2004/10/28 15:35:23 tuan08 Exp $
14  */

15 public class ObjectParam extends Parameter {
16   private String JavaDoc type_ ;
17   private String JavaDoc package_ ;
18     private Object JavaDoc object_ ;
19   private ArrayList properties_ = new ArrayList() ;
20   
21   public String JavaDoc getType() { return type_ ;}
22   public void setType(String JavaDoc 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 JavaDoc getObject() {
32     if(object_ == null) {
33       populateBean() ;
34     }
35     return object_ ;
36   }
37   
38   public void addProperty(String JavaDoc name , String JavaDoc value) {
39     properties_.add(new Property(name, value)) ;
40   }
41   
42   private void populateBean() {
43     Property prop = null ;
44     try {
45       Class JavaDoc 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           //arrary or list
51
populateBeanInArray(object_ , prop.name_ , prop.value_) ;
52         } else {
53           Object JavaDoc valueBean = getValue(prop.value_) ;
54           PropertyUtils.setProperty(object_, prop.name_, valueBean) ;
55         }
56       }
57     } catch(Throwable JavaDoc ex) {
58       if(prop != null) {
59         //System.out.println("Exception when try setting the prop.name " + prop.name_ +
60
// ", value prop.value " + prop.value_) ;
61
}
62       ex.printStackTrace() ;
63     }
64   }
65   
66   private void populateBeanInArray(Object JavaDoc bean, String JavaDoc name , String JavaDoc value) throws Exception JavaDoc {
67     int idx = name.lastIndexOf("[") ;
68     String JavaDoc arrayBeanName = name.substring(0, idx) ;
69     int index = Integer.parseInt(name.substring(idx + 1, name.length() - 1)) ;
70     Object JavaDoc arrayBean = PropertyUtils.getProperty(bean, arrayBeanName) ;
71     if(arrayBean instanceof List) {
72       List list = (List) arrayBean ;
73       Object JavaDoc 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 JavaDoc valueBean = getValue(value) ;
82       c.add(valueBean) ;
83     } else {
84       Object JavaDoc[] array = (Object JavaDoc[]) arrayBean ;
85       array[index] = getValue(value) ;
86     }
87   }
88   
89   private Object JavaDoc getValue(String JavaDoc value) throws Exception JavaDoc {
90     if(value.startsWith("#new")) {
91       String JavaDoc[] temp = value.split(" ") ;
92       String JavaDoc className = temp[1] ;
93       if(className.indexOf(".") < 0) {
94         className = package_ + "." + className ;
95         Class JavaDoc clazz = Class.forName(className) ;
96         return clazz.newInstance() ;
97       }
98     } else if(value.startsWith("#int")) {
99       String JavaDoc[] temp = value.split(" ") ;
100       value = temp[1].trim() ;
101       return new Integer JavaDoc(value) ;
102     } else if(value.startsWith("#boolean")) {
103       String JavaDoc[] temp = value.split(" ") ;
104       value = temp[1].trim() ;
105       return new Boolean JavaDoc("true".equals(value)) ;
106     }
107     return value ;
108   }
109   
110   static class Property {
111     String JavaDoc name_ ;
112     String JavaDoc value_ ;
113     
114     public Property(String JavaDoc name, String JavaDoc value) {
115       name_ = name;
116       value_ = value ;
117     }
118   }
119 }
Popular Tags