ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Takacs Otto <takac...@freemail.hu>
Subject Re: iBATIS vs jakarta-beans
Date Thu, 24 Feb 2005 12:21:25 GMT
Hello

I have implemented a DynaProbe into iBATIS.

1. I did not want to break the existing library so i have re-factored 
the ProbeFactory part of the library
   I have moved all ProbeFactory functionality into ProbeFactoryBean and 
its subclasses.
   The ProbeFactory simply delegate the calls to the given factory bean.
   There are two implementation of ProbeFactoryBean. The (almost) 
copy-paste created DefaultProbeFactoryBean (created from the original 
ProbeFactory)
   and the TakacsotProbeFactoryBean (Of course the name can be anything 
else :) )
   The second factory bean always returns a DynaBeanProbe instance.

2. As you see the default implementations is the same as the original. 
So if you want to use DynaProbe
    call the following:
  ProbeFactory.setCurrentProbeFactory(new TakacsotProbeFactoryBean());

Here is the current implementation.
If you say that it is OK tell me how could i integrate it into the trunk 
(of course if you have the responsibility copy-paste the rest of the mail :)

      1	package com.ibatis.common.beans;
      2	/**
      3	 * An abstract factory for getting Probe implementations.
      4	 */
      5	public class ProbeFactory {
     15	    private static ProbeFactoryBean currentProbeFactory;
     16	
     17	    static{
     18	        currentProbeFactory = new DefaultProbeFactoryBean();
     19	    }
     20	    /**
     21	     * Factory method for getting a Probe object
     22	     *
     23	     * @return An implementation of the Probe interface
     24	     */
     25	    public static Probe getProbe() {
     26	        return currentProbeFactory.getProbe();
     28	    }
     29	    /**
     30	     * Factory method for getting a Probe object that is the 
best choice for the
     31	     * type of object supplied by the object parameter.
     32	     *
     33	     * @param object
     34	     *            The object to get a Probe for
     35	     * @return An implementation of the Probe interface
     36	     */
     37	    public static Probe getProbe(Object object) {
     38	        return currentProbeFactory.getProbe(object);
     51	    }
     52	    /**
     53	     * @return Returns the currentProbeFactory.
     54	     */
     55	    public synchronized static ProbeFactoryBean 
getCurrentProbeFactory() {
     56	        return currentProbeFactory;
     57	    }
     58	    /**
     59	     * @param currentProbeFactory
     60	     *            The currentProbeFactory to set.
     61	     */
     62	    public synchronized static void setCurrentProbeFactory(
     63	            ProbeFactoryBean currentProbeFactory) {
     64	        ProbeFactory.currentProbeFactory = currentProbeFactory;
     65	    }
     66	}

      6	package com.ibatis.common.beans;
      7	/**
      8	 * TODO Document the type!
      9	 *
     10	 * @author takacsot
     11	 *
     12	 */
     13	public abstract class ProbeFactoryBean {
     14	    public abstract Probe getProbe();
     15	    public abstract Probe getProbe(Object object);
     16	}

      6	package com.ibatis.common.beans;
      7	import java.util.Map;
      8	/**
      9	 * TODO Document the type!
     10	 *
     11	 * @author takacsot
     12	 *
     13	 */
     14	public class DefaultProbeFactoryBean extends ProbeFactoryBean {
     15	
     16	    private static final Probe DOM = new DomProbe();
     17	    private static final Probe MAP = new ComplexBeanProbe(); // 
Yes, use the complex one.
     18	    private static final Probe BEAN = new JavaBeanProbe();
     19	    private static final Probe GENERIC = new GenericProbe();
     20	    private static final Probe LEGACY = new ComplexBeanProbe();
     21	    /**
     22	     * Factory method for getting a Probe object
     23	     *
     24	     * @return An implementation of the Probe interface
     25	     */
     26	    public  Probe getProbe() {
     27	      return GENERIC;
     28	    }
     29	    /**
     30	     * Factory method for getting a Probe object that is
     31	     * the best choice for the type of object supplied
     32	     * by the object parameter.
     33	     *
     34	     * @param object The object to get a Probe for
     35	     * @return An implementation of the Probe interface
     36	     */
     37	    public  Probe getProbe(Object object) {
     38	      if (object instanceof Map) {
     39	        return MAP;
     40	      } else if (object instanceof org.w3c.dom.Document) {
     41	        return DOM;
     42	      } else if (object instanceof Class) {
     43	        return LEGACY;
     44	      } else {
     45	        return BEAN;
     46	      }
     47	    }
     48	}

      6	package com.ibatis.common.beans;
      7	import org.apache.commons.beanutils.DynaBean;
      8	/**
      9	 * TODO Document the type!
     10	 *
     11	 * @author takacsot
     12	 *
     13	 */
     14	public class TakacsotProbeFactoryBean extends 
DefaultProbeFactoryBean {
     15	
     16	    private static final Probe DYNA= new DynaBeanProbe();
     17	
     18	    /**
     19	     * @see 
com.ibatis.common.beans.ProbeFactoryBean#getProbe(java.lang.Object)
     20	     */
     21	    public Probe getProbe(Object object) {
     22	        //
     23	        if (object instanceof DynaBean) {
     24	            return DYNA;
     25	        }
     26	
     27	        return super.getProbe(object);
     28	    }
     29	    /**
     30	     * @see com.ibatis.common.beans.ProbeFactoryBean#getProbe()
     31	     */
     32	    public Probe getProbe() {
     33	        //
     34	        return DYNA;
     35	    }
     36	}

      1	/* $Id$
      2	 *
      3	 * Created on 2005.02.22.
      4	 *
      5	 */
      6	package com.ibatis.common.beans;
      7	import java.lang.reflect.InvocationTargetException;
      8	import org.apache.commons.beanutils.PropertyUtils;
      9	import org.apache.commons.logging.Log;
     10	import org.apache.commons.logging.LogFactory;
     11	/**
     12	 * DynaBeanProbe provide methods that allow access to DynaBeans.
     13	 * Of course it can be use with <emph>any</emph> kind of bean 
which follows
     14	 * the JavaBean conventions.
     15	 *
     16	 * @author takacsot
     17	 *
     18	 */
     19	public class DynaBeanProbe implements Probe {
     20	    private static Log log = 
LogFactory.getLog(DynaBeanProbe.class.getName());
     21	    /**
     22	     * @see 
com.ibatis.common.beans.Probe#getObject(java.lang.Object, java.lang.String)
     23	     */
     24	    public Object getObject(Object object, String name) {
     25	        //
     26	        try {
     27	            return PropertyUtils.getProperty(object, name);
     28	        }
     29	        catch (IllegalAccessException t) {
     30	            log.error(t,t);
     31	            throw new ProbeException(t);
     32	        }
     33	        catch (InvocationTargetException t) {
     34	            log.error(t,t);
     35	            throw new ProbeException(t);
     36	        }
     37	        catch (NoSuchMethodException t) {
     38	            log.error(t,t);
     39	            throw new ProbeException(t);
     40	        }
     41	        catch (RuntimeException e){
     42	            log.fatal(e,e);
     43	            throw new ProbeException(e);
     44	        }
     45	    }
     46	    /**
     47	     * @see 
com.ibatis.common.beans.Probe#setObject(java.lang.Object, 
java.lang.String, java.lang.Object)
     48	     */
     49	    public void setObject(Object object, String name, Object 
value) {
     50	        //
     51	        try {
     52	            PropertyUtils.setProperty(object, name, value);
     53	        }
     54	        catch (IllegalAccessException t) {
     55	            log.error(t,t);
     56	            throw new ProbeException(t);
     57	        }
     58	        catch (InvocationTargetException t) {
     59	            log.error(t,t);
     60	            throw new ProbeException(t);
     61	        }
     62	        catch (NoSuchMethodException t) {
     63	            log.error(t,t);
     64	            throw new ProbeException(t);
     65	        }
     66	        catch (RuntimeException e){
     67	            log.fatal(e,e);
     68	            throw new ProbeException(e);
     69	        }
     70	    }
     71	    /**
     72	     * @see 
com.ibatis.common.beans.Probe#getPropertyTypeForSetter(java.lang.Object, 
java.lang.String)
     73	     */
     74	    public Class getPropertyTypeForSetter(Object object, String 
name) {
     75	        //
     76	        try {
     77	            return PropertyUtils.getPropertyType(object, name);
     78	        }
     79	        catch (IllegalAccessException t) {
     80	            log.error(t,t);
     81	            throw new ProbeException(t);
     82	        }
     83	        catch (InvocationTargetException t) {
     84	            log.error(t,t);
     85	            throw new ProbeException(t);
     86	        }
     87	        catch (NoSuchMethodException t) {
     88	            log.error(t,t);
     89	            throw new ProbeException(t);
     90	        }
     91	        catch (RuntimeException e){
     92	            log.fatal(e,e);
     93	            throw new ProbeException(e);
     94	        }
     95	    }
     96	    /**
     97	     * @see 
com.ibatis.common.beans.Probe#getPropertyTypeForGetter(java.lang.Object, 
java.lang.String)
     98	     */
     99	    public Class getPropertyTypeForGetter(Object object, String 
name) {
    100	        //
    101	        try {
    102	            return PropertyUtils.getPropertyType(object, name);
    103	        }
    104	        catch (IllegalAccessException t) {
    105	            log.error(t,t);
    106	            throw new ProbeException(t);
    107	        }
    108	        catch (InvocationTargetException t) {
    109	            log.error(t,t);
    110	            throw new ProbeException(t);
    111	        }
    112	        catch (NoSuchMethodException t) {
    113	            log.error(t,t);
    114	            throw new ProbeException(t);
    115	        }
    116	        catch (RuntimeException e){
    117	            log.fatal(e,e);
    118	            throw new ProbeException(e);
    119	        }
    120	    }
    121	    /**
    122	     * @see 
com.ibatis.common.beans.Probe#hasWritableProperty(java.lang.Object, 
java.lang.String)
    123	     */
    124	    public boolean hasWritableProperty(Object object, String 
propertyName) {
    125	        //
    126	        return PropertyUtils.isWriteable(object,propertyName);
    127	    }
    128	    /**
    129	     * @see 
com.ibatis.common.beans.Probe#hasReadableProperty(java.lang.Object, 
java.lang.String)
    130	     */
    131	    public boolean hasReadableProperty(Object object, String 
propertyName) {
    132	        //
    133	        return PropertyUtils.isReadable(object,propertyName);
    134	    }
    135	}


-- 
Takacs Otto                            takacsot@freemail.hu


Mime
View raw message