You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by th...@apache.org on 2003/03/16 13:03:45 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata ClassDescriptor.java

thma        2003/03/16 04:03:45

  Modified:    src/java/org/apache/ojb/broker/metadata ClassDescriptor.java
  Log:
  add support for materialization-method on class-descriptor
  add performance improvement for constructor lookup
  
  Revision  Changes    Path
  1.50      +147 -2    db-ojb/src/java/org/apache/ojb/broker/metadata/ClassDescriptor.java
  
  Index: ClassDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ClassDescriptor.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- ClassDescriptor.java	30 Jan 2003 15:12:57 -0000	1.49
  +++ ClassDescriptor.java	16 Mar 2003 12:03:45 -0000	1.50
  @@ -61,10 +61,14 @@
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
   import org.apache.ojb.broker.util.ClassHelper;
   import org.apache.ojb.broker.util.SqlHelper;
  +import org.apache.ojb.broker.util.configuration.Configuration;
  +import org.apache.ojb.broker.util.configuration.Configurator;
  +import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
   import java.io.Serializable;
   import java.lang.reflect.Constructor;
  +import java.lang.reflect.Method;
   import java.lang.reflect.Modifier;
   import java.sql.Timestamp;
   import java.util.ArrayList;
  @@ -176,6 +180,12 @@
        * does the described class represent an interface?
        */
       private boolean isInterface = false;
  +    
  +    /**
  +     * the zero argument constructor for this class
  +     */
  +    private transient Constructor zeroArgumentConstructor = null;
  +    
       /**
        * the constructor defined by m_Class to initialze all scalar attributes
        * described in m_FieldDescriptions
  @@ -197,6 +207,22 @@
       private Map m_fieldDescriptorNameMap = new HashMap();
       private Map m_collectionDescriptorNameMap = new HashMap();
       private Map m_objectReferenceDescriptorsNameMap = new HashMap();
  +    
  +    /**
  +     * optional method to be invoked after instance fields are initialized
  +     */
  +    private transient Method initializationMethod;
  +
  +
  +    
  +    /**
  +     * whether we have already tried to look up the zero argument constructor
  +     */
  +    private transient boolean alreadyLookedupZeroArguments = false;
  +    
  +    private static final Class[] NO_PARAMS = {};
  +
  +
   
       /**
        * Constructor declaration
  @@ -222,7 +248,12 @@
       {
           if (rowReader == null)
           {
  -            setRowReader(RowReaderDefaultImpl.class.getName());
  +            Configurator configurator = OjbConfigurator.getInstance();
  +            Configuration config = configurator.getConfigurationFor(null);
  +            Class rrClass = config.getClass("RowReaderDefaultClass",
  +                RowReaderDefaultImpl.class);
  +     
  +            setRowReader(rrClass.getName());
           }
           return this.rowReader;
       }
  @@ -970,6 +1001,45 @@
       }
   
       /**
  +     * returns the zero argument constructor for the class represented by this class descriptor
  +     * or null if a zero argument constructor does not exist.  If the zero argument constructor
  +     * for this class is not public it is made accessible before being returned.
  +     */
  +    public Constructor getZeroArgumentConstructor()
  +    {
  +        if (zeroArgumentConstructor == null && !alreadyLookedupZeroArguments)
  +        {
  +            try
  +            {
  +                zeroArgumentConstructor = getClassOfObject().getConstructor(NO_PARAMS);
  +            }
  +            catch (NoSuchMethodException e)
  +            {
  +                //no public zero argument constructor available let's try for a private/protected one
  +                try
  +                {
  +                    zeroArgumentConstructor = getClassOfObject().getDeclaredConstructor(NO_PARAMS);
  +     
  +                    //we found one, now let's make it accessible
  +                    zeroArgumentConstructor.setAccessible(true);
  +                }
  +                catch (NoSuchMethodException e2)
  +                {
  +                    //out of options, log the fact and let the method return null
  +     
  +                    LoggerFactory.getDefaultLogger().warn(this.getClass().getName() + ": " +
  +                        "No zero argument constructor defined for "
  +                        + this.getClassOfObject());
  +                }
  +           }
  +
  +           alreadyLookedupZeroArguments = true;
  +        }
  +  
  +        return zeroArgumentConstructor;
  +    }
  +
  +    /**
        * returns a Constructor that takes all persistent attributes
        * of the class as arguments.
        * Returns null, if such a constructor does not exist.
  @@ -1089,6 +1159,13 @@
               result += "        " + tags.getAttribute(ACCEPT_LOCKS, "false") + eol;
           }
           // sequence manager attribute not yet implemented
  +               
  +        // initialization method is optional
  +        if (this.getInitializationMethod() != null)
  +        {
  +            result += "    " + tags.getAttribute(INITIALIZATION_METHOD, this.getInitializationMethod().getName()) + eol;
  +        }
  +
   
           result += "  >" + eol;
   
  @@ -1364,4 +1441,72 @@
                   append("fieldDescriptions", getFieldDescriptions()).
                   toString();
       }
  +    
  +    /**
  +     * sets the initialization method for this descriptor
  +     */
  +    public void setInitializationMethod(Method newMethod)
  +    {
  +        if (newMethod != null)
  +        {
  +            // make sure it's a no argument method
  +            if (newMethod.getParameterTypes().length > 0)
  +            {
  +				throw new MetadataException("Initialization methods must be zero argument methods: "
  +					+ newMethod.getClass().getName() + "." + newMethod.getName());
  +			}
  +			
  +			// make it accessible if it's not already
  +			if (!newMethod.isAccessible())
  +			{
  +				newMethod.setAccessible(true);
  +			}
  +		}		
  +		this.initializationMethod = newMethod;
  +	}
  +
  +	/**
  +	 * sets the initialization method for this descriptor by name
  +	 */
  +	public void setInitializationMethod(String newMethodName)
  +	{
  +		Method newMethod = null;
  +		
  +		if (newMethodName != null)
  +		{
  +			try
  +			{
  +				// see if we have a publicly accessible method by the name
  +				newMethod = getClassOfObject().getMethod(newMethodName, NO_PARAMS);
  +			}
  +			catch (NoSuchMethodException e)
  +			{
  +				try
  +				{
  +					// no publicly accessible method, see if there is a private/protected one
  +					newMethod = getClassOfObject().getDeclaredMethod(newMethodName, NO_PARAMS);
  +				}
  +				catch (NoSuchMethodException e2)
  +				{
  +					// there is no such method available
  +					throw new MetadataException("Invalid initialization method, there is not"
  +						+ " a zero argument method named " + newMethodName
  +						+ " on class " + getClassOfObject().getName() + ".");
  +				}				
  +			}
  +		}
  +		
  +		setInitializationMethod(newMethod);
  +	}
  +
  +	/**
  +	 * Returns the initialization method for this descriptor or null if no
  +	 * initialization method is defined.
  +	 */
  +	public synchronized Method getInitializationMethod()
  +	{
  +		return this.initializationMethod;
  +	}
  +
  +
   }