Author: arminw
Date: Sat Jan 7 16:33:18 2006
New Revision: 366957
URL: http://svn.apache.org/viewcvs?rev=366957&view=rev
Log:
minor refactoring, improve logging messages
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldBase.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldIntrospectorImpl.java
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldBase.java
URL:
http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldBase.java?rev=366957&r1=366956&r2=366957&view=diff
==============================================================================
---
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldBase.java
(original)
+++
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldBase.java
Sat Jan 7 16:33:18 2006
@@ -18,6 +18,10 @@
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
+import java.beans.PropertyDescriptor;
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.IntrospectionException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
@@ -27,9 +31,9 @@
import org.apache.ojb.broker.util.logging.LoggerFactory;
/**
- * Abstract [EMAIL PROTECTED] PersistentField} base implementation class.
+ * Abstract base implementation class for [EMAIL PROTECTED] PersistentField}
with
+ * some additional (protected) helper methods for real implementation classes.
*
- * @author <a href="mailto:[EMAIL PROTECTED]">Armin Waibel</a>
* @version $Id$
*/
public abstract class PersistentFieldBase implements PersistentField
@@ -131,9 +135,51 @@
}
}
+ /**
+ * Helper method for bean based (setter/getter based) implementations: Get
the
+ * [EMAIL PROTECTED] java.beans.PropertyDescriptor} of specified class and
property.
+ *
+ * @param aClass The target class.
+ * @param aPropertyName The name of the property.
+ * @return The
+ */
+ protected PropertyDescriptor findPropertyDescriptor(Class aClass, String
aPropertyName)
+ {
+ BeanInfo info;
+ PropertyDescriptor[] pd;
+ PropertyDescriptor descriptor = null;
+
+ try
+ {
+ // use stop class to find inherited properties
+ info = Introspector.getBeanInfo(aClass);
+ pd = info.getPropertyDescriptors();
+ for (int i = 0; i < pd.length; i++)
+ {
+ if (pd[i].getName().equals(aPropertyName))
+ {
+ descriptor = pd[i];
+ break;
+ }
+ }
+ if (descriptor == null)
+ {
+ throw new MetadataException("Can't find property '" +
aPropertyName + "' in " + aClass);
+ }
+ return descriptor;
+ }
+ catch (IntrospectionException ex)
+ {
+ throw new MetadataException("Can't find property '" +
aPropertyName + "' in " + aClass, ex);
+ }
+ }
+
+ /**
+ * Lookup the logging instance.
+ */
protected Logger getLog()
{
- return LoggerFactory.getLogger("PersistentField");
+ return LoggerFactory.getLogger(this.getClass());
}
public String toString()
@@ -145,7 +191,7 @@
}
/**
- * Build a String representation of given arguments.
+ * Helper method: Build set-error string for field access based
implementations.
*/
protected String buildErrorSetMsg(Object obj, Object value, Field aField)
{
@@ -164,7 +210,7 @@
}
/**
- * Build a String representation of given arguments.
+ * Helper method: Build get-error string for field access based
implementations.
*/
protected String buildErrorGetMsg(Object obj, Field aField)
{
@@ -177,6 +223,63 @@
.append(eol + "target field type: " + (aField != null ?
aField.getType() : null))
.append(eol + "target field declared in: " + (aField != null ?
aField.getDeclaringClass().getName() : null))
.append(eol + "]");
+ return buf.toString();
+ }
+
+ /**
+ * Helper method: Build getting-error string for setter/getter based
implementations based on given arguments.
+ */
+ protected String buildGetterErrorMsg(Class returnType, Object source,
String msg)
+ {
+ return buildPropertyErrorMsg(returnType, source, null, msg, false);
+ }
+
+ /**
+ * Helper method: Build setting-error string for setter/getter based
implementations based on given arguments.
+ */
+ protected String buildSetterErrorMsg(Class setterArgType, Object target,
Object aValue, String msg)
+ {
+ return buildPropertyErrorMsg(setterArgType, target, aValue, msg, true);
+ }
+
+ /**
+ * Build error string for setter/getter based implementations based on
given arguments.
+ */
+ private String buildPropertyErrorMsg(Class returnOrArgumentType, Object
anObject, Object aValue, String msg, boolean isSetter)
+ {
+ String eol = SystemUtils.LINE_SEPARATOR;
+ StringBuffer buf = new StringBuffer();
+ String type = (isSetter ? "setter" : "getter");
+ buf
+ .append(eol + "["
+ + (msg == null ? "try to handle " + type + " property
call" : type + " property call: " + msg))
+ .append(eol + "Declaring class [" + getDeclaringClass().getName()
+ "]")
+ .append(eol + "Property Name [" + getName() + "]")
+ .append(eol + "Property Type ["
+ + (returnOrArgumentType != null ?
returnOrArgumentType.getName() : "not available") + "]");
+
+ if (anObject != null)
+ {
+ //buf.append("the " + (isSetter ? "target" : "source") + " object
was [" + anObject + "]");
+ buf.append(eol + "the "
+ + (isSetter ? "target" : "source") + " object type was ["
+ anObject.getClass().getName() + "]");
+ }
+ else
+ {
+ buf.append(eol + "the " + (isSetter ? "target" : "source") + "
object was 'null'");
+ }
+ if(isSetter)
+ {
+ if (aValue != null)
+ {
+ buf.append(eol + "the value was [" + aValue + "]");
+ buf.append(eol + "the value type was [" +
aValue.getClass().getName() + "]");
+ }
+ else
+ {
+ buf.append(eol + "the value was 'null'");
+ }
+ }
return buf.toString();
}
}
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldIntrospectorImpl.java
URL:
http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldIntrospectorImpl.java?rev=366957&r1=366956&r2=366957&view=diff
==============================================================================
---
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldIntrospectorImpl.java
(original)
+++
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldIntrospectorImpl.java
Sat Jan 7 16:33:18 2006
@@ -15,9 +15,6 @@
* limitations under the License.
*/
-import java.beans.BeanInfo;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -27,7 +24,6 @@
import org.apache.ojb.broker.core.proxy.ProxyHelper;
import org.apache.ojb.broker.metadata.MetadataException;
import org.apache.ojb.broker.util.ClassHelper;
-import org.apache.ojb.broker.util.logging.Logger;
/**
* A [EMAIL PROTECTED] PersistentField} implementation using
@@ -125,13 +121,16 @@
}
catch (Throwable e)
{
- logProblem(pd, target, null, "Can't read value from given
object");
- throw new MetadataException("Error invoking method:" +
m.getName() + " in object " + target.getClass().getName(), e);
+ String msg = buildGetterErrorMsg(pd.getPropertyType(), target,
"Can't read value from given object");
+ getLog().error(msg);
+ throw new MetadataException("Error invoking method '" +
m.getName() + "' in object '"
+ + target.getClass().getName() + "'", e);
}
}
else
{
- throw new MetadataException("Can't get ReadMethod for property:" +
pd.getName() + " in object " + target.getClass().getName());
+ throw new MetadataException("Can't get ReadMethod for property '"
+ pd.getName()
+ + "' in object " + target.getClass().getName());
}
}
@@ -156,13 +155,16 @@
}
catch (Throwable e)
{
- logProblem(pd, target, value, "Can't set value on given
object.");
- throw new MetadataException("Error invoking method:" +
m.getName() + " in object:" + target.getClass().getName(), e);
+ String msg = buildSetterErrorMsg(pd.getPropertyType(), target,
value, "Can't set value on given object.");
+ getLog().error(msg);
+ throw new MetadataException("Error invoking method '" +
m.getName()
+ + "' in object '" + target.getClass().getName() + "'",
e);
}
}
else
{
- throw new MetadataException("Can't get WriteMethod for property:"
+ pd.getName() + " in object:" + target.getClass().getName());
+ throw new MetadataException("Can't get WriteMethod for property '"
+ + pd.getName() + "' in object '" +
target.getClass().getName() + "'");
}
}
@@ -197,47 +199,6 @@
}
/**
- * Get the PropertyDescriptor for aClass and aPropertyName
- */
- protected static PropertyDescriptor findPropertyDescriptor(Class aClass,
String aPropertyName)
- {
- BeanInfo info;
- PropertyDescriptor[] pd;
- PropertyDescriptor descriptor = null;
-
- try
- {
- info = Introspector.getBeanInfo(aClass);
- pd = info.getPropertyDescriptors();
- for (int i = 0; i < pd.length; i++)
- {
- if (pd[i].getName().equals(aPropertyName))
- {
- descriptor = pd[i];
- break;
- }
- }
- if (descriptor == null)
- {
- /*
- * Daren Drummond: Throw here so we are
consistent
- * with
PersistentFieldDefaultImpl.
- */
- throw new MetadataException("Can't find property " +
aPropertyName + " in " + aClass.getName());
- }
- return descriptor;
- }
- catch (IntrospectionException ex)
- {
- /*
- * Daren Drummond: Throw here so we are consistent
- * with
PersistentFieldDefaultImpl.
- */
- throw new MetadataException("Can't find property " + aPropertyName
+ " in " + aClass.getName(), ex);
- }
- }
-
- /**
* Returns the PropertyDescriptor.
*
* @return java.beans.PropertyDescriptor
@@ -263,34 +224,5 @@
public boolean usesAccessorsAndMutators()
{
return true;
- }
-
- /**
- * Let's give the user some hints as to what could be wrong.
- */
- protected void logProblem(PropertyDescriptor pd, Object anObject, Object
aValue, String msg)
- {
- Logger logger = getLog();
- logger.error("Error in [PersistentFieldPropertyImpl], " + msg);
- logger.error("Declaring class [" + getDeclaringClass().getName() +
"]");
- logger.error("Property Name [" + getName() + "]");
- logger.error("Property Type [" + pd.getPropertyType().getName() + "]");
-
- if (anObject != null)
- {
- logger.error("anObject was class [" +
anObject.getClass().getName() + "]");
- }
- else
- {
- logger.error("anObject was null");
- }
- if (aValue != null)
- {
- logger.error("aValue was class [" + aValue.getClass().getName() +
"]");
- }
- else
- {
- logger.error("aValue was null");
- }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]