Line | Hits | Source |
---|---|---|
1 | /******************************************************************************* | |
2 | * Demetrix process modelling system | |
3 | * | |
4 | * Copyright (c) 2003, 2004 Dimitri A. Pissarenko | |
5 | * | |
6 | * This file is part of Demetrix. | |
7 | * | |
8 | * Demetrix is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2.1 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * Demetrix is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with Demetrix; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * For further information you may | |
23 | * | |
24 | * - send an e-mail in Russian, German or English to dimitri.pissarenko@gmx.net | |
25 | * - look at http://sourceforge.net/projects/demetrix/ | |
26 | * - look at http://demetrix.sourceforge.net/ | |
27 | * - look at http://members.inode.at/d.pissarenko/ | |
28 | * | |
29 | *****************************************************************************/ | |
30 | /* | |
31 | * Created on 15.01.2004 | |
32 | */ | |
33 | package net.sourceforge.demetrix.properties; | |
34 | ||
35 | import java.beans.BeanInfo; | |
36 | import java.beans.IntrospectionException; | |
37 | import java.beans.Introspector; | |
38 | import java.beans.PropertyDescriptor; | |
39 | import java.io.File; | |
40 | import java.lang.reflect.InvocationTargetException; | |
41 | import java.util.Enumeration; | |
42 | import java.util.Hashtable; | |
43 | import java.util.Iterator; | |
44 | import java.util.LinkedHashSet; | |
45 | import java.util.Set; | |
46 | ||
47 | import org.apache.log4j.Logger; | |
48 | ||
49 | /** | |
50 | * @author Dimitri Pissarenko | |
51 | * | |
52 | */ | |
53 | public class JavaBeanBasedDemetrixPropertiesHolder | |
54 | implements DemetrixPropertiesHolder { | |
55 | private Logger logger=Logger.getLogger(getClass()); | |
56 | private DemetrixPropertiesStorage storage; | |
57 | private BeanInfo beanInfo; | |
58 | private Hashtable typeMappingTable; | |
59 | private Object bean; | |
60 | private Set propertyNamesToIgnore; | |
61 | public JavaBeanBasedDemetrixPropertiesHolder(Object bean) | |
62 | 14 | { |
63 | 14 | this.propertyNamesToIgnore=new LinkedHashSet(); |
64 | 14 | this.init(bean); |
65 | 14 | } |
66 | public JavaBeanBasedDemetrixPropertiesHolder(Object bean, Set propertyNamesToIgnore) | |
67 | 1 | { |
68 | 1 | this.propertyNamesToIgnore=propertyNamesToIgnore; |
69 | 1 | this.init(bean); |
70 | 1 | } |
71 | ||
72 | private void init(Object bean) | |
73 | { | |
74 | 15 | this.bean=bean; |
75 | try | |
76 | { | |
77 | 15 | beanInfo=Introspector.getBeanInfo(this.bean.getClass()); |
78 | ||
79 | } | |
80 | 0 | catch (IntrospectionException exception) |
81 | { | |
82 | this.logger.error("", exception); | |
83 | 15 | } |
84 | 15 | this.typeMappingTable=this.createTypeMappingTable(); |
85 | 15 | this.storage=this.createDemetrixPropertiesStorage(this.bean); |
86 | ||
87 | 15 | } |
88 | protected Hashtable createTypeMappingTable() | |
89 | { | |
90 | 15 | Hashtable typeMappingTable=null; |
91 | 15 | typeMappingTable=new Hashtable(); |
92 | 59 | typeMappingTable.put(Double.class.getName(), DoubleProperty.class); |
93 | 15 | typeMappingTable.put(Double.TYPE.getName(), DoubleProperty.class); |
94 | 15 | typeMappingTable.put(Boolean.class.getName(), BooleanProperty.class); |
95 | 15 | typeMappingTable.put(Boolean.TYPE.getName(), BooleanProperty.class); |
96 | 15 | typeMappingTable.put(Integer.class.getName(), IntegerProperty.class); |
97 | 15 | typeMappingTable.put(Integer.TYPE.getName(), IntegerProperty.class); |
98 | ||
99 | 15 | typeMappingTable.put(String.class.getName(), StringProperty.class); |
100 | 15 | typeMappingTable.put(File.class.getName(), FileProperty.class); |
101 | ||
102 | 15 | return typeMappingTable; |
103 | } | |
104 | protected DemetrixPropertiesStorage createDemetrixPropertiesStorage(Object bean) | |
105 | { | |
106 | 15 | DemetrixPropertiesStorage storage=null; |
107 | 15 | PropertyDescriptor[] propertyDescriptors=null; |
108 | 15 | DemetrixProperty demetrixProperty=null; |
109 | 15 | Object value=null; |
110 | ||
111 | 15 | storage=new DemetrixPropertiesStorage(); |
112 | 15 | propertyDescriptors=this.beanInfo.getPropertyDescriptors(); |
113 | 87 | for (int i=0; i < propertyDescriptors.length; i++) |
114 | { | |
115 | ||
116 | 72 | if (!this.propertyNamesToIgnore.contains(propertyDescriptors[i].getName())) |
117 | { | |
118 | 71 | demetrixProperty=this.createDemetrixProperty(propertyDescriptors[i], bean); |
119 | 71 | if (demetrixProperty!=null) |
120 | { | |
121 | 55 | storage.addProperty(demetrixProperty); |
122 | } | |
123 | ||
124 | } | |
125 | } | |
126 | 15 | return storage; |
127 | } | |
128 | protected DemetrixProperty createCorrespondingProperty(Class valueClass) | |
129 | { | |
130 | 62 | DemetrixProperty demetrixProperty=null; |
131 | 62 | Class propertyClass=null; |
132 | ||
133 | 62 | propertyClass=(Class)this.typeMappingTable.get(valueClass.getName()); |
134 | 62 | if (propertyClass!=null) |
135 | { | |
136 | try | |
137 | { | |
138 | 61 | demetrixProperty=(DemetrixProperty)propertyClass.newInstance(); |
139 | } | |
140 | 0 | catch (InstantiationException exception) |
141 | { | |
142 | this.logger.error("", exception); | |
143 | } | |
144 | 0 | catch (IllegalAccessException exception) |
145 | { | |
146 | this.logger.error("", exception); | |
147 | 61 | } |
148 | } | |
149 | else | |
150 | { | |
151 | this.logger.debug("could not find a suitable DemetrixProperty class for the property type \"" + valueClass +"\""); | |
152 | } | |
153 | 62 | return demetrixProperty; |
154 | } | |
155 | protected DemetrixProperty createDemetrixProperty(PropertyDescriptor propertyDescriptor, Object propertyOwner) | |
156 | { | |
157 | 71 | DemetrixProperty demetrixProperty=null; |
158 | 71 | Object value=null; |
159 | 71 | Class propertyClass=null; |
160 | ||
161 | 71 | propertyClass=propertyDescriptor.getPropertyType(); |
162 | 71 | if (!propertyClass.equals(Class.class)) |
163 | { | |
164 | 56 | demetrixProperty=this.createCorrespondingProperty(propertyDescriptor.getPropertyType()); |
165 | 56 | if (demetrixProperty!=null) |
166 | { | |
167 | try | |
168 | { | |
169 | 55 | value=propertyDescriptor.getReadMethod().invoke(propertyOwner, new Object[0]); |
170 | } | |
171 | 0 | catch (InvocationTargetException exception) |
172 | { | |
173 | this.logger.error("", exception); | |
174 | } | |
175 | 0 | catch (IllegalAccessException exception) |
176 | { | |
177 | this.logger.error("", exception); | |
178 | 55 | } |
179 | 55 | demetrixProperty.setValue(value); |
180 | 55 | demetrixProperty.setName(propertyDescriptor.getName()); |
181 | ||
182 | } | |
183 | } | |
184 | ||
185 | 71 | return demetrixProperty; |
186 | } | |
187 | /* (non-Javadoc) | |
188 | * @see net.sourceforge.demetrix.properties.DemetrixPropertiesHolder#addProperty(net.sourceforge.demetrix.properties.DemetrixProperty) | |
189 | */ | |
190 | public void addProperty(DemetrixProperty property) { | |
191 | 1 | this.storage.addProperty(property); |
192 | ||
193 | 1 | } |
194 | ||
195 | /* (non-Javadoc) | |
196 | * @see net.sourceforge.demetrix.properties.DemetrixPropertiesHolder#getAllProperties() | |
197 | */ | |
198 | public Iterator getAllProperties() { | |
199 | 8 | return this.storage.getAllProperties(); |
200 | } | |
201 | ||
202 | /* (non-Javadoc) | |
203 | * @see net.sourceforge.demetrix.properties.DemetrixPropertiesHolder#getAllPropertyNames() | |
204 | */ | |
205 | public Enumeration getAllPropertyNames() { | |
206 | 1 | return this.storage.getAllPropertyNames(); |
207 | } | |
208 | ||
209 | /* (non-Javadoc) | |
210 | * @see net.sourceforge.demetrix.properties.DemetrixPropertiesHolder#getPropertiesStorage() | |
211 | */ | |
212 | public DemetrixPropertiesHolder getPropertiesStorage() { | |
213 | 8 | return this.storage; |
214 | } | |
215 | ||
216 | /* (non-Javadoc) | |
217 | * @see net.sourceforge.demetrix.properties.DemetrixPropertiesHolder#getProperty(java.lang.String) | |
218 | */ | |
219 | public DemetrixProperty getProperty(String name) { | |
220 | 21 | return this.storage.getProperty(name); |
221 | } | |
222 | /** | |
223 | * Note: this method was contributed by Gerhard Görlich | |
224 | */ | |
225 | public void updateUnderlyingBean () | |
226 | { | |
227 | 5 | Iterator properties = null; |
228 | 5 | DemetrixProperty demetrixProperty=null; |
229 | 5 | PropertyDescriptor[] propertyDescriptors=null; |
230 | PropertyDescriptor g; | |
231 | 5 | Object[] arg = null; |
232 | ||
233 | 5 | propertyDescriptors=this.beanInfo.getPropertyDescriptors(); |
234 | 30 | for (int i=0; i < propertyDescriptors.length; i++) |
235 | { | |
236 | 25 | demetrixProperty = storage.getProperty(propertyDescriptors[i].getName()); |
237 | 25 | if (demetrixProperty != null) |
238 | { | |
239 | 20 | arg = new Object [1]; |
240 | 20 | arg[0] = demetrixProperty.getValue(); |
241 | try{ | |
242 | 20 | propertyDescriptors[i].getWriteMethod().invoke(this.bean,arg); |
243 | } | |
244 | 0 | catch (IllegalAccessException exception) |
245 | { | |
246 | this.logger.error("", exception); | |
247 | } | |
248 | 0 | catch (InvocationTargetException exception) |
249 | { | |
250 | this.logger.error("", exception); | |
251 | 20 | } |
252 | } | |
253 | } | |
254 | ||
255 | 5 | } |
256 | ||
257 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |