db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb OJB.properties
Date Sun, 27 Jun 2004 13:50:33 GMT
arminw      2004/06/27 06:50:33

  Modified:    src/java/org/apache/ojb/broker/metadata/fieldaccess
                        PersistentFieldAutoProxyImplNew.java
               src/test/org/apache/ojb OJB.properties
  Log:
  fix problems of new auto-proxy implementation
  
  Revision  Changes    Path
  1.3       +108 -228  db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldAutoProxyImplNew.java
  
  Index: PersistentFieldAutoProxyImplNew.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldAutoProxyImplNew.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PersistentFieldAutoProxyImplNew.java	27 Jun 2004 09:56:09 -0000	1.2
  +++ PersistentFieldAutoProxyImplNew.java	27 Jun 2004 13:50:33 -0000	1.3
  @@ -1,6 +1,11 @@
   package org.apache.ojb.broker.metadata.fieldaccess;
   
  -/* Copyright 2003-2004 The Apache Software Foundation
  +import java.io.Serializable;
  +
  +import org.apache.ojb.broker.metadata.MetadataException;
  +import org.apache.ojb.broker.util.ClassHelper;
  +
  +/* Copyright 2002-2004 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -15,13 +20,6 @@
    * limitations under the License.
    */
   
  -import java.lang.reflect.Field;
  -import java.lang.reflect.Member;
  -
  -import org.apache.commons.beanutils.DynaBean;
  -import org.apache.ojb.broker.metadata.MetadataException;
  -import org.apache.ojb.broker.util.ClassHelper;
  -
   /**
    * PeristentField implementation that attempts to detect the nature of
    * the field it is persisting.
  @@ -32,289 +30,171 @@
    */
   public class PersistentFieldAutoProxyImplNew extends PersistentFieldBase
   {
  -    private static final long serialVersionUID = 6148669985786992032L;
  +    Class[] persistentFieldClasses = new Class[]{
  +        PersistentFieldDirectAccessImplNew.class
  +        , PersistentFieldIntrospectorImplNew.class
  +        , PersistentFieldDynaBeanAccessImpl.class};
  +
  +    private PersistentField currentPF;
  +    private ExceptionWrapper latestException;
  +    int index = 0;
   
  -    /** The actual field handler */
  -    private PersistentField wrapped;
  -    /** Whether we actually found a field handler, or it is still the default one */
  -    private boolean hasBeenDetermined = false;
  -    
  -    /**
  -     * Being nice to Class.newInstance() seems popular.
  -     */
       public PersistentFieldAutoProxyImplNew()
       {
  -        super();
       }
   
  -    /**
  -     * Creates a new auto handler object for the indicated field.
  -     * 
  -     * @param type      The type of the object containing the field
  -     * @param fieldName The name of the field
  -     */
  -    public PersistentFieldAutoProxyImplNew(Class type, String fieldName)
  -    {
  -        super(type, fieldName);
  -    }
  -
  -    /**
  -     * Creates an instance of the given field handler type.
  -     * 
  -     * @param handlerType The field handler type
  -     * @return The handler instance
  -     * @throws MetadataException If the handler could not be found, or cannot handle the
field
  -     */
  -    private PersistentField getFieldImpl(Class handlerType) throws MetadataException
  +    public PersistentFieldAutoProxyImplNew(Class clazz, String fieldname)
       {
  -        Class[]  types = { Class.class, String.class };
  -        Object[] args  = { rootObjectType, super.getName() };
  +        super(clazz, fieldname);
  +    }
   
  -        try
  -        {
  -            return (PersistentField)ClassHelper.newInstance(handlerType, types, args);
  -        }
  -        catch (Exception ex)
  +    private PersistentField getCurrent()
  +    {
  +        if (currentPF == null)
           {
  -            if (ex instanceof MetadataException)
  +            if(index >= persistentFieldClasses.length)
               {
  -                throw (MetadataException)ex;
  +                index = 0;
  +                currentPF = null;
  +                throw new AuoDetectException("Can't autodetect valid PersistentField implementation:
"
  +                        + latestException.message, latestException.exception);
               }
  -            else
  +            try
  +            {
  +                currentPF = createPersistentFieldForIndex();
  +            }
  +            catch (Exception e)
               {
  -                throw new MetadataException(ex);
  +                throw new AuoDetectException("Can't create instance for " + persistentFieldClasses[index],
e);
               }
           }
  +        return currentPF;
       }
   
  -    /**
  -     * Returns an instance of the field-direct handler.
  -     * 
  -     * @return The handler instance
  -     * @throws MetadataException If the handler could not be found, or cannot handle the
field
  -     */
  -    private PersistentField getDirectImpl() throws MetadataException
  -    {
  -        return getFieldImpl(PersistentFieldDirectAccessImplNew.class);
  -    }
  -
  -    /**
  -     * Returns an instance of the privileged field-direct handler.
  -     * 
  -     * @return The handler instance
  -     * @throws MetadataException If the handler could not be found, or cannot handle the
field
  -     */
  -    private PersistentField getPrivilegedDirectImpl() throws MetadataException
  -    {
  -        return getFieldImpl(PersistentFieldPrivilegedImplNew.class);
  -    }
  -
  -    /**
  -     * Returns an instance of the dyna bean handler.
  -     * 
  -     * @return The handler instance
  -     * @throws MetadataException If the handler could not be found, or cannot handle the
field
  -     */
  -    private PersistentField getDynaBeanImpl() throws MetadataException
  -    {
  -        return getFieldImpl(PersistentFieldDynaBeanAccessImpl.class);
  -    }
  -
  -    /**
  -     * Returns an instance of the bean-property handler.
  -     * 
  -     * @return The handler instance
  -     * @throws MetadataException If the handler could not be found, or cannot handle the
field
  -     */
  -    private PersistentField getPropertyImpl() throws MetadataException
  -    {
  -        return getFieldImpl(PersistentFieldIntrospectorImplNew.class);
  -    }
  -
  -    /**
  -     * Returns the actual field handler. If none is determined yet, it first tries to
  -     * find one for the field.
  -     * 
  -     * @return The wrapped field handler
  -     * @throws MetadataException If no handler for the field could be found
  -     */
  -    private PersistentField getWrapped() throws MetadataException
  -    {
  -        // 1. direct field 
  -        // 2. bean
  -        // 3. dyna bean
  -        if (wrapped != null)
  -        {
  -            return wrapped;
  -        }
  -
  -        /*
  -         * this switches on a series of exceptions. Sadly, it is the only real way to detect
  -         * which one works. --Brian
  -         */
  -        String fieldName = super.getName();
  -
  -        try
  -        {
  -            // TODO: Is this correct ?
  -            PersistentFieldDirectAccessImplNew helper = new PersistentFieldDirectAccessImplNew(rootObjectType,
fieldName);
  -            Field                              field  = helper.getField();
  +    private void handleException(String message, Exception e)
  +    {
  +        latestException = new ExceptionWrapper(message, e);
  +        currentPF = null;
  +        ++index;
  +    }
   
  -            if (field != null)
  -            {
  -                SecurityManager manager = System.getSecurityManager();
   
  -                // ok we have a real field,
  -                // however, for non-public fields we have to check whether we're allowed
to access
  -                // it directly; this is guarded by the security manager, so we ask it
  -                try
  -                {
  -                    if ((manager != null) && !field.isAccessible())
  -                    {
  -                        // this throws an exception if we're not allowed to the access
the declared members
  -                        manager.checkMemberAccess(field.getDeclaringClass(), Member.DECLARED);
  -                    }
  -                    // no security manager or we're allowed to access the field
  -                    wrapped = getDirectImpl();
  -                }
  -                catch (SecurityException ex)
  -                {
  -                    wrapped = getPrivilegedDirectImpl();
  -                }
  -                hasBeenDetermined = true;
  -                return wrapped;
  -            }
  -        }
  -        catch (MetadataException ex)
  +    public Object get(Object anObject) throws MetadataException
  +    {
  +        try
           {
  -            // no direct field to use
  +            return getCurrent().get(anObject);
           }
  -
  -        try
  +        catch (Exception e)
           {
  -            if (PersistentFieldIntrospectorImpl.findPropertyDescriptor(rootObjectType,
fieldName) != null)
  +            if(e instanceof AuoDetectException)
               {
  -                wrapped           = getPropertyImpl();
  -                hasBeenDetermined = true;
  -                return wrapped;
  +                throw (MetadataException) e;
  +            }
  +            else
  +            {
  +                handleException("Can't extract field value for field " + getName()
  +                        + " from object " + (anObject != null ? anObject.getClass() : null),
e);
  +                return get(anObject);
               }
           }
  -        catch (MetadataException ex)
  -        {
  -            // no bean property either
  -        }
  +    }
   
  -        // Only option left is for it to be a dynabean which we'll use as the default
  -        // as we can check it only when actually doing something
  -        // of course, we can only use the dyna bean if the library is present
  +    public void set(Object obj, Object value) throws MetadataException
  +    {
           try
           {
  -            wrapped = getDynaBeanImpl();
  -            return wrapped;
  +            getCurrent().set(obj, value);
           }
  -        catch (NoClassDefFoundError ex)
  +        catch (Exception e)
           {
  -            String message = "Could not use dyna beans (not in classpath), and no field
or property matches " + rootObjectType.getName() + "." + fieldName;
  -
  -            getLog().error(message);
  -            throw new MetadataException(message);
  +            if(e instanceof AuoDetectException)
  +            {
  +                throw (MetadataException) e;
  +            }
  +            else
  +            {
  +                handleException("Can't set value for field " + getName()
  +                        + " to object " + (obj != null ? obj.getClass() : null), e);
  +                set(obj, value);
  +            }
           }
       }
   
  -    /**
  -     * Returns the wrapped field handler. This method is also able to check whether the
default
  -     * handler (dyna bean) can actually be used as it has an actual object whose field
shall be
  -     * accessed (which then must be a dyna bean).
  -     * 
  -     * @param targetObject The object whose field is accessed
  -     * @return The wrapped field handler
  -     * @throws MetadataException If no handler for the field could be found
  -     */
  -    private PersistentField getWrapped(Object targetObject) throws MetadataException
  +    public Class getType()
       {
  -        // ensure that we at least tried to find the handler
  -        PersistentField result = getWrapped();
  -
  -        if (!hasBeenDetermined)
  +        try
  +        {
  +            return getCurrent().getType();
  +        }
  +        catch (Exception e)
           {
  -            // still the default handler, so this is the first time that we can check for
dyna beans
  -            if (targetObject instanceof DynaBean)
  +            if(e instanceof AuoDetectException)
               {
  -                // the default dyna bean is ok
  -                hasBeenDetermined = true;
  +                throw (MetadataException) e;
               }
               else
               {
  -                // no handler could be found -> error
  -                String message = "No field, property, or DynaProperty can be found to match
" + rootObjectType.getName() + "." + super.getName();
  -
  -                getLog().error(message);
  -                throw new MetadataException(message);
  +                handleException("Can't identify field type for field " + getName(), null);
  +                return getType();
               }
           }
  -        return result;
       }
   
  -    /* These are all the implementation methods delagating to the wrapped impl*/
  -
  -    /**
  -     * Set the field value for the given target object.
  -     * 
  -     * @param targetObject The object that contains the field to set
  -     * @param value        The value to set
  -     */
  -    public void set(Object targetObject, Object value)
  +    protected boolean makeAccessible()
       {
  -        getWrapped(targetObject).set(targetObject, value);
  +        return false;
       }
   
  -    /**
  -     * Gets the field value from the given target object.
  -     * 
  -     * @param targetObject The object that contains the field to set
  -     * @return The field's value
  -     */
  -    public Object get(Object targetObject)
  +    public boolean usesAccessorsAndMutators()
       {
  -        return getWrapped(targetObject).get(targetObject);
  +        return false;
       }
   
  -    public Class getDeclaringClass()
  +    private PersistentField createPersistentFieldForIndex() throws Exception
       {
  -        return getWrapped().getDeclaringClass();
  +        return newInstance(persistentFieldClasses[index]);
       }
   
  -    public boolean usesAccessorsAndMutators()
  +    private PersistentField newInstance(Class pfClass) throws Exception
       {
  -        return getWrapped().usesAccessorsAndMutators();
  +        Class[] types = new Class[]{Class.class, String.class};
  +        Object[] args = new Object[]{getDeclaringClass(), getName()};
  +        return (PersistentField) ClassHelper.newInstance(pfClass, types, args);
       }
   
  -    public String getName()
  +    class ExceptionWrapper implements Serializable
       {
  -        return getWrapped().getName();
  -    }
  +        Exception exception;
  +        String message;
   
  -    public Class getType()
  -    {
  -        return getWrapped().getType();
  +        public ExceptionWrapper(String message, Exception exception)
  +        {
  +            this.message = message;
  +            this.exception = exception;
  +        }
       }
   
  -    public boolean makeAccessible()
  +    class AuoDetectException extends MetadataException
       {
  -        PersistentField wrapped = getWrapped();
  +        public AuoDetectException()
  +        {
  +            super();
  +        }
   
  -        if (wrapped instanceof PersistentFieldBase)
  +        public AuoDetectException(Throwable t)
           {
  -            return ((PersistentFieldBase)wrapped).makeAccessible();
  +            super(t);
           }
  -        else
  +
  +        public AuoDetectException(String message)
           {
  -            return true;
  +            super(message);
           }
  -    }
   
  -    public String toString()
  -    {
  -        return getWrapped().toString();
  +        public AuoDetectException(String message, Throwable t)
  +        {
  +            super(message, t);
  +        }
       }
   }
  
  
  
  1.74      +2 -1      db-ojb/src/test/org/apache/ojb/OJB.properties
  
  Index: OJB.properties
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/OJB.properties,v
  retrieving revision 1.73
  retrieving revision 1.74
  diff -u -r1.73 -r1.74
  --- OJB.properties	26 Jun 2004 23:43:38 -0000	1.73
  +++ OJB.properties	27 Jun 2004 13:50:33 -0000	1.74
  @@ -406,6 +406,7 @@
   PersistentFieldClass=org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectAccessImplNew
   #PersistentFieldClass=org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldPrivilegedImplNew
   #PersistentFieldClass=org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImplNew
  +#PersistentFieldClass=org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldAutoProxyImpl
   #
   #
   #----------------------------------------------------------------------------------------
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message