Coverage details for net.sourceforge.demetrix.properties.JavaBeanBasedDemetrixPropertiesHolder

LineHitsSource
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)
6214    {
6314        this.propertyNamesToIgnore=new LinkedHashSet();
6414        this.init(bean);
6514    }
66     public JavaBeanBasedDemetrixPropertiesHolder(Object bean, Set propertyNamesToIgnore)
671    {
681        this.propertyNamesToIgnore=propertyNamesToIgnore;
691        this.init(bean);
701    }
71  
72     private void init(Object bean)
73     {
7415        this.bean=bean;
75         try
76         {
7715            beanInfo=Introspector.getBeanInfo(this.bean.getClass());
78             
79         }
800        catch (IntrospectionException exception)
81         {
82             this.logger.error("", exception);
8315        }
8415        this.typeMappingTable=this.createTypeMappingTable();
8515        this.storage=this.createDemetrixPropertiesStorage(this.bean);
86  
8715    }
88     protected Hashtable createTypeMappingTable()
89     {
9015        Hashtable typeMappingTable=null;
9115        typeMappingTable=new Hashtable();
9259        typeMappingTable.put(Double.class.getName(), DoubleProperty.class);
9315        typeMappingTable.put(Double.TYPE.getName(), DoubleProperty.class);
9415        typeMappingTable.put(Boolean.class.getName(), BooleanProperty.class);
9515        typeMappingTable.put(Boolean.TYPE.getName(), BooleanProperty.class);
9615        typeMappingTable.put(Integer.class.getName(), IntegerProperty.class);
9715        typeMappingTable.put(Integer.TYPE.getName(), IntegerProperty.class);
98         
9915        typeMappingTable.put(String.class.getName(), StringProperty.class);
10015        typeMappingTable.put(File.class.getName(), FileProperty.class);
101         
10215        return typeMappingTable;
103     }
104     protected DemetrixPropertiesStorage createDemetrixPropertiesStorage(Object bean)
105     {
10615        DemetrixPropertiesStorage storage=null;
10715        PropertyDescriptor[] propertyDescriptors=null;
10815        DemetrixProperty demetrixProperty=null;
10915        Object value=null;
110         
11115        storage=new DemetrixPropertiesStorage();
11215        propertyDescriptors=this.beanInfo.getPropertyDescriptors();
11387        for (int i=0; i < propertyDescriptors.length; i++)
114         {
115         
11672            if (!this.propertyNamesToIgnore.contains(propertyDescriptors[i].getName()))
117             {
11871                demetrixProperty=this.createDemetrixProperty(propertyDescriptors[i], bean);
11971                if (demetrixProperty!=null)
120                 {
12155                    storage.addProperty(demetrixProperty);
122                 }
123                 
124             }
125         }
12615        return storage;
127     }
128     protected DemetrixProperty createCorrespondingProperty(Class valueClass)
129     {
13062        DemetrixProperty demetrixProperty=null;
13162        Class propertyClass=null;
132         
13362        propertyClass=(Class)this.typeMappingTable.get(valueClass.getName());
13462        if (propertyClass!=null)
135         {
136             try
137             {
13861                demetrixProperty=(DemetrixProperty)propertyClass.newInstance();
139             }
1400            catch (InstantiationException exception)
141             {
142                 this.logger.error("", exception);
143             }
1440            catch (IllegalAccessException exception)
145             {
146                 this.logger.error("", exception);
14761            }
148         }
149         else
150         {
151             this.logger.debug("could not find a suitable DemetrixProperty class for the property type \"" + valueClass +"\"");
152         }
15362        return demetrixProperty;
154     }
155     protected DemetrixProperty createDemetrixProperty(PropertyDescriptor propertyDescriptor, Object propertyOwner)
156     {
15771        DemetrixProperty demetrixProperty=null;
15871        Object value=null;
15971        Class propertyClass=null;
160         
16171        propertyClass=propertyDescriptor.getPropertyType();
16271        if (!propertyClass.equals(Class.class))
163         {
16456            demetrixProperty=this.createCorrespondingProperty(propertyDescriptor.getPropertyType());
16556            if (demetrixProperty!=null)
166             {
167                 try
168                 {
16955                    value=propertyDescriptor.getReadMethod().invoke(propertyOwner, new Object[0]);
170                 }
1710                catch (InvocationTargetException exception)
172                 {
173                     this.logger.error("", exception);
174                 }
1750                catch (IllegalAccessException exception)
176                 {
177                     this.logger.error("", exception);
17855                }
17955                demetrixProperty.setValue(value);
18055                demetrixProperty.setName(propertyDescriptor.getName());
181                 
182             }
183         }
184         
18571        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) {
1911        this.storage.addProperty(property);
192  
1931    }
194  
195     /* (non-Javadoc)
196      * @see net.sourceforge.demetrix.properties.DemetrixPropertiesHolder#getAllProperties()
197      */
198     public Iterator getAllProperties() {
1998        return this.storage.getAllProperties();
200     }
201  
202     /* (non-Javadoc)
203      * @see net.sourceforge.demetrix.properties.DemetrixPropertiesHolder#getAllPropertyNames()
204      */
205     public Enumeration getAllPropertyNames() {
2061        return this.storage.getAllPropertyNames();
207     }
208  
209     /* (non-Javadoc)
210      * @see net.sourceforge.demetrix.properties.DemetrixPropertiesHolder#getPropertiesStorage()
211      */
212     public DemetrixPropertiesHolder getPropertiesStorage() {
2138        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) {
22021        return this.storage.getProperty(name);
221     }
222     /**
223      * Note: this method was contributed by Gerhard Görlich
224      */
225     public void updateUnderlyingBean ()
226     {
2275        Iterator properties = null;
2285        DemetrixProperty demetrixProperty=null;
2295        PropertyDescriptor[] propertyDescriptors=null;
230         PropertyDescriptor g;
2315        Object[] arg = null;
232         
2335        propertyDescriptors=this.beanInfo.getPropertyDescriptors();
23430        for (int i=0; i < propertyDescriptors.length; i++)
235         {
23625            demetrixProperty = storage.getProperty(propertyDescriptors[i].getName());
23725            if (demetrixProperty != null)
238             {
23920                arg = new Object [1];
24020                arg[0] = demetrixProperty.getValue();
241                 try{
24220                    propertyDescriptors[i].getWriteMethod().invoke(this.bean,arg);
243                 }
2440                catch (IllegalAccessException exception)
245                 {
246                     this.logger.error("", exception);
247                 }
2480                catch (InvocationTargetException exception)
249                 {
250                     this.logger.error("", exception);
25120                }
252             }
253         }
254                 
2555    }
256     
257 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.