From: <ga...@us...> - 2009-10-30 17:59:04
|
Revision: 4199 http://jaffa.svn.sourceforge.net/jaffa/?rev=4199&view=rev Author: gautamj Date: 2009-10-30 17:58:58 +0000 (Fri, 30 Oct 2009) Log Message: ----------- Enhanced the class-level rules for FlexBean instances to obtain values from the associated persistent object: - Added a getPersistentObject() method to the FlexBean. - Corrected the UOW-based rules to not create any local UOW instances. They will be executed only if the target object exposes a uow. This will reduce unnecessary queries. Modified Paths: -------------- trunk/JaffaCore/source/java/org/jaffa/flexfields/FlexBean.java trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractRuleInterceptor.java trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractSetterInterceptorWithUOW.java trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractValidateInterceptorWithUOW.java trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/ForeignKeyInterceptor.java trunk/JaffaRules/source/test/junit/test/rules/Child2.java trunk/JaffaRules/source/test/junit/test/rules/Extension1.java trunk/JaffaRules/source/test/junit/test/rules/Strings1.java Modified: trunk/JaffaCore/source/java/org/jaffa/flexfields/FlexBean.java =================================================================== --- trunk/JaffaCore/source/java/org/jaffa/flexfields/FlexBean.java 2009-10-30 00:48:38 UTC (rev 4198) +++ trunk/JaffaCore/source/java/org/jaffa/flexfields/FlexBean.java 2009-10-30 17:58:58 UTC (rev 4199) @@ -240,6 +240,14 @@ getset(false, null, null, flexParam); } + /** + * Getter for the property persistentObject. + * @return the value of the property persistentObject. + */ + public IPersistent getPersistentObject() { + return persistentObject; + } + // ***************************** // **** ADDITIONAL METHODS ***** // ***************************** Modified: trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractRuleInterceptor.java =================================================================== --- trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractRuleInterceptor.java 2009-10-30 00:48:38 UTC (rev 4198) +++ trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractRuleInterceptor.java 2009-10-30 17:58:58 UTC (rev 4199) @@ -49,7 +49,6 @@ package org.jaffa.rules.jbossaop.interceptors; -import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.LinkedHashMap; @@ -103,18 +102,6 @@ } } - // The following Field is needed to obtain a UOW from a FlexBean instance - private static Field c_flexFieldPersistentObject; - static { - try { - c_flexFieldPersistentObject = FlexBean.class.getDeclaredField("persistentObject"); - c_flexFieldPersistentObject.setAccessible(true); - } catch (Exception e) { - if (log.isDebugEnabled()) - log.debug("Exception thrown while trying to get a handle on the 'persistentObject' field of FlexBean class, which is used for obtaining a UOW", e); - } - } - /** Creates an instance. * @param name the name of this Interceptor. */ @@ -304,13 +291,9 @@ if (targetObject instanceof IPersistent) uow = ((IPersistent) targetObject).getUOW(); else if (targetObject instanceof FlexBean) { - try { - IPersistent persistentObject = (IPersistent) c_flexFieldPersistentObject.get(targetObject); - if (persistentObject != null) - uow = persistentObject.getUOW(); - } catch (Exception e) { - // do nothing.. will simply return a null. - } + IPersistent persistentObject = ((FlexBean) targetObject).getPersistentObject(); + if (persistentObject != null) + uow = persistentObject.getUOW(); } else { try { Object obj = BeanHelper.getField(targetObject, "UOW"); Modified: trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractSetterInterceptorWithUOW.java =================================================================== --- trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractSetterInterceptorWithUOW.java 2009-10-30 00:48:38 UTC (rev 4198) +++ trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractSetterInterceptorWithUOW.java 2009-10-30 17:58:58 UTC (rev 4199) @@ -46,7 +46,6 @@ * SUCH DAMAGE. * ==================================================================== */ - package org.jaffa.rules.jbossaop.interceptors; import java.util.List; @@ -59,16 +58,16 @@ * It passes an UOW instance to the concrete implementation. */ public abstract class AbstractSetterInterceptorWithUOW extends AbstractSetterInterceptor { - + private static Logger log = Logger.getLogger(AbstractSetterInterceptorWithUOW.class); - + /** Creates an instance. * @param name the name of this Interceptor. */ public AbstractSetterInterceptorWithUOW(String name) { super(name); } - + /** Injects the necessary behavior. * @param invocation the Invocation. * @param targetClassName The target Class. @@ -83,17 +82,15 @@ protected Object invoke(MethodInvocation invocation, String targetClassName, Object targetObject, String targetPropertyName, Object currentPropertyValue, Object newPropertyValue, List<RuleMetaData> rules) throws Throwable { UOW uow = getUOW(targetObject); - boolean localUow = (uow == null); - try { - if (localUow) - uow = new UOW(); + if (uow != null) return invoke(invocation, targetClassName, targetObject, targetPropertyName, currentPropertyValue, newPropertyValue, rules, uow); - } finally { - if (localUow && uow != null) - uow.rollback(); + else { + if (log.isDebugEnabled()) + log.debug(getName() + " check not performed since UOW is not available on the targetObject " + targetObject); + return invocation.invokeNext(); } } - + /** Injects the necessary behavior. * @param invocation the Invocation. * @param targetClassName The target Class. @@ -108,5 +105,4 @@ */ protected abstract Object invoke(MethodInvocation invocation, String targetClassName, Object targetObject, String targetPropertyName, Object currentPropertyValue, Object newPropertyValue, List<RuleMetaData> rules, UOW uow) throws Throwable; - } Modified: trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractValidateInterceptorWithUOW.java =================================================================== --- trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractValidateInterceptorWithUOW.java 2009-10-30 00:48:38 UTC (rev 4198) +++ trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/AbstractValidateInterceptorWithUOW.java 2009-10-30 17:58:58 UTC (rev 4199) @@ -80,8 +80,7 @@ @Override protected void invoke(MethodInvocation invocation, String targetClassName, Object targetObject, Map<String, List<RuleMetaData>> ruleMap) throws Throwable { UOW uow = getUOW(targetObject); - boolean localUow = (uow == null); - try { + if (uow != null) { for (Map.Entry<String, List<RuleMetaData>> me : ruleMap.entrySet()) { String targetPropertyName = me.getKey(); List<RuleMetaData> rules = me.getValue(); @@ -92,14 +91,13 @@ log.debug(getName() + " check not performed since the property " + targetPropertyName + " has not been modified"); continue; } - if (localUow && uow == null) - uow = new UOW(); invoke(invocation, targetClassName, targetObject, targetPropertyName, rules, uow); } } - } finally { - if (localUow && uow != null) - uow.rollback(); + } else { + if (log.isDebugEnabled()) + log.debug(getName() + " check not performed since UOW is not available on the targetObject " + targetObject); + invocation.invokeNext(); } } Modified: trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/ForeignKeyInterceptor.java =================================================================== --- trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/ForeignKeyInterceptor.java 2009-10-30 00:48:38 UTC (rev 4198) +++ trunk/JaffaRules/source/java/org/jaffa/rules/jbossaop/interceptors/ForeignKeyInterceptor.java 2009-10-30 17:58:58 UTC (rev 4199) @@ -53,6 +53,7 @@ import java.util.List; import org.apache.log4j.Logger; import org.jaffa.datatypes.exceptions.InvalidForeignKeyException; +import org.jaffa.flexfields.FlexBean; import org.jaffa.persistence.UOW; import org.jaffa.rules.meta.RuleMetaData; import org.jaffa.rules.rulemeta.InvalidRuleParameterException; @@ -106,7 +107,25 @@ } catch (Exception e) { if (log.isDebugEnabled()) log.debug("Exception thrown while trying to get value for the property " + fkFields[i] + ". It may not exist in the targetObject", e); - return; + if (targetObject instanceof FlexBean) { + try { + Object persistentObject = ((FlexBean) targetObject).getPersistentObject(); + if (persistentObject != null) { + value = BeanHelper.getField(persistentObject, fkFields[i]); + if (log.isDebugEnabled()) + log.debug("Value of property " + fkFields[i] + " obtained from the FlexBean's associated persistent object is '" + value + '\''); + } else { + if (log.isDebugEnabled()) + log.debug("The property " + fkFields[i] + " cannot be determined since the FlexBean has not been associated with a persistent object"); + return; + } + } catch (Exception ex) { + if (log.isDebugEnabled()) + log.debug("Exception thrown while trying to get value for the property " + fkFields[i] + " from a FlexBean's associated persistent object", ex); + return; + } + } else + return; } if (value == null) { if (log.isDebugEnabled()) Modified: trunk/JaffaRules/source/test/junit/test/rules/Child2.java =================================================================== --- trunk/JaffaRules/source/test/junit/test/rules/Child2.java 2009-10-30 00:48:38 UTC (rev 4198) +++ trunk/JaffaRules/source/test/junit/test/rules/Child2.java 2009-10-30 17:58:58 UTC (rev 4199) @@ -1,5 +1,8 @@ package test.rules; +import org.jaffa.persistence.UOW; +import org.jaffa.persistence.exceptions.UOWException; + /** * * @author GautamJ @@ -12,6 +15,7 @@ * 3- field3 is overridden, but it also invokes the corresponding 'super' method */ + private UOW uow = null; private String field2 = null; private String field4 = null; @@ -55,4 +59,11 @@ this.field4 = field4; } + + /** This method has been added to support rules that require UOW. */ + public UOW getUOW() throws UOWException { + if (uow == null) + uow = new UOW(); + return uow; + } } Modified: trunk/JaffaRules/source/test/junit/test/rules/Extension1.java =================================================================== --- trunk/JaffaRules/source/test/junit/test/rules/Extension1.java 2009-10-30 00:48:38 UTC (rev 4198) +++ trunk/JaffaRules/source/test/junit/test/rules/Extension1.java 2009-10-30 17:58:58 UTC (rev 4199) @@ -49,8 +49,11 @@ package test.rules; import org.jaffa.datatypes.DateOnly; +import org.jaffa.persistence.UOW; +import org.jaffa.persistence.exceptions.UOWException; public class Extension1 { + private UOW uow = null; private String field1 = "Aaaa"; private String field2 = "Bbbb"; private String field3 = "Cccc"; @@ -89,4 +92,11 @@ public void validate() { } + + /** This method has been added to support rules that require UOW. */ + public UOW getUOW() throws UOWException { + if (uow == null) + uow = new UOW(); + return uow; + } } Modified: trunk/JaffaRules/source/test/junit/test/rules/Strings1.java =================================================================== --- trunk/JaffaRules/source/test/junit/test/rules/Strings1.java 2009-10-30 00:48:38 UTC (rev 4198) +++ trunk/JaffaRules/source/test/junit/test/rules/Strings1.java 2009-10-30 17:58:58 UTC (rev 4199) @@ -50,8 +50,11 @@ package test.rules; import org.jaffa.datatypes.ValidationException; +import org.jaffa.persistence.UOW; +import org.jaffa.persistence.exceptions.UOWException; public class Strings1 { + private UOW uow = null; private String field1 = StringsTest.DATA1; // mixed private String field2 = StringsTest.DATA2; // lower, min 2 private String field3 = StringsTest.DATA3; // upper, max 10 @@ -70,4 +73,11 @@ public void validate() throws ValidationException { // The validation code will be applied using aspects } + + /** This method has been added to support rules that require UOW. */ + public UOW getUOW() throws UOWException { + if (uow == null) + uow = new UOW(); + return uow; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |