arminw      2005/02/19 10:27:19

  Modified:    src/java/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
                        Identity.java
  Log:
  improve exception handling
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.36.2.5  +38 -17    db-ojb/src/java/org/apache/ojb/broker/Identity.java
  
  Index: Identity.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/Identity.java,v
  retrieving revision 1.36.2.4
  retrieving revision 1.36.2.5
  diff -u -r1.36.2.4 -r1.36.2.5
  --- Identity.java     15 Feb 2005 15:34:18 -0000      1.36.2.4
  +++ Identity.java     19 Feb 2005 18:27:19 -0000      1.36.2.5
  @@ -108,7 +108,7 @@
           m_objectsTopLevelClass = topLevel;
           m_objectsRealClass = realClass;
           m_pkValues = pkValues;
  -        checkForPrimaryKeys();
  +        checkForPrimaryKeys(null);
       }
   
       public Identity(Object objectToIdentitify, PersistenceBroker 
targetBroker)
  @@ -149,10 +149,15 @@
                   // BRJ: definitely do NOT convertToSql
                   // conversion is done when binding the sql-statement
                   BrokerHelper helper = targetBroker.serviceBrokerHelper();
  -                m_pkValues = helper.extractValueArray( 
helper.getKeyValues(cld, objectToIdentify, false) );
  +                ValueContainer[] pkValues = helper.getKeyValues(cld, 
objectToIdentify, false);
  +                if(pkValues == null || pkValues.length == 0)
  +                {
  +                    createException("Can't extract PK value fields", 
objectToIdentify, null);
  +                }
  +                m_pkValues = helper.extractValueArray(pkValues);
               }
   
  -            checkForPrimaryKeys();
  +            checkForPrimaryKeys(objectToIdentify);
           }
           catch (Exception e)
           {
  @@ -162,12 +167,7 @@
               }
               else
               {
  -                String eol = SystemUtils.LINE_SEPARATOR;
  -                throw new ClassNotPersistenceCapableException("Can not init 
Identity for given object." + eol +
  -                    "objectTopLevelClass=" + (m_objectsTopLevelClass != null 
? m_objectsTopLevelClass.getName() : null) + eol +
  -                    "objectRealClass=" + (m_objectsRealClass != null ? 
m_objectsRealClass.getName() : null) + eol +
  -                    "pkValues=" + (m_pkValues != null ? 
ArrayUtils.toString(m_pkValues) : null) + eol +
  -                    "object to identify: " + objectToIdentify, e);
  +                throw createException("Can not init Identity for given 
object.", objectToIdentify, e);
               }
           }
       }
  @@ -282,20 +282,16 @@
        * @exception ClassNotPersistenceCapableException thrown if no primary 
key is
        * specified for the objects class
        */
  -    protected void checkForPrimaryKeys() throws 
ClassNotPersistenceCapableException
  +    protected void checkForPrimaryKeys(Object realObject) throws 
ClassNotPersistenceCapableException
       {
           // if no PKs are specified OJB can't handle this class !
           if (m_pkValues == null || m_pkValues.length == 0)
           {
  -            String eol = SystemUtils.LINE_SEPARATOR;
  -            throw new ClassNotPersistenceCapableException("OJB needs at 
least one primary key attribute for class: " + eol +
  -                    "objectTopLevelClass=" + (m_objectsTopLevelClass != null 
? m_objectsTopLevelClass.getName() : null) + eol +
  -                    "objectRealClass=" + (m_objectsRealClass != null ? 
m_objectsRealClass.getName() : null) + eol +
  -                    "pkValues=" + (m_pkValues != null ? 
ArrayUtils.toString(m_pkValues) : null) + eol);
  +            throw createException("OJB needs at least one primary key 
attribute for class: ", realObject, null);
   
           }
           if(m_pkValues[0] instanceof ValueContainer)
  -            throw new RuntimeException("Can't handle pk values of type 
"+ValueContainer.class.getName());
  +            throw new OJBRuntimeException("Can't handle pk values of type 
"+ValueContainer.class.getName());
       }
   
       /**
  @@ -372,4 +368,29 @@
           }
           return m_hashCode.intValue();
       }
  +
  +    private ClassNotPersistenceCapableException createException(String msg, 
Object objectToIdentify, Exception e)
  +    {
  +        String eol = SystemUtils.LINE_SEPARATOR;
  +        if(msg == null)
  +        {
  +            msg = "Unexpected error:";
  +        }
  +        if(e != null)
  +        {
  +            return new ClassNotPersistenceCapableException(msg + eol +
  +                        "objectTopLevelClass=" + (m_objectsTopLevelClass != 
null ? m_objectsTopLevelClass.getName() : null) + eol +
  +                        "objectRealClass=" + (m_objectsRealClass != null ? 
m_objectsRealClass.getName() : null) + eol +
  +                        "pkValues=" + (m_pkValues != null ? 
ArrayUtils.toString(m_pkValues) : null) +
  +                        (objectToIdentify != null ? (eol + "object to 
identify: " + objectToIdentify) : ""), e);
  +        }
  +        else
  +        {
  +            return new ClassNotPersistenceCapableException(msg + eol +
  +                        "objectTopLevelClass=" + (m_objectsTopLevelClass != 
null ? m_objectsTopLevelClass.getName() : null) + eol +
  +                        "objectRealClass=" + (m_objectsRealClass != null ? 
m_objectsRealClass.getName() : null) + eol +
  +                        "pkValues=" + (m_pkValues != null ? 
ArrayUtils.toString(m_pkValues) : null) +
  +                        eol + "object to identify: " + objectToIdentify);
  +        }
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to