| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Seam
Revision: 8910
Author: pete.muir@jboss.org
Date: 04 Sep 2008 11:45:22
Changes:And commit the changes needed for moving rootCause :(
Files:| ... | ...@@ -1,26 +1,13 @@ | |
| 1 | 1 | package org.jboss.seam.persistence; |
| 2 | 2 | |
| 3 | 3 | import static org.jboss.seam.ScopeType.CONVERSATION; |
| 4 | import static org.jboss.seam.util.JSF.DATA_MODEL; | |
| 5 | import static org.jboss.seam.util.JSF.getWrappedData; | |
| 6 | import static org.jboss.seam.util.JSF.setWrappedData; | |
| 7 | 4 | |
| 8 | import java.lang.reflect.Field; | |
| 9 | import java.lang.reflect.Modifier; | |
| 10 | import java.util.List; | |
| 11 | import java.util.Map; | |
| 12 | import java.util.Set; | |
| 13 | ||
| 14 | import org.jboss.seam.Seam; | |
| 15 | import org.jboss.seam.annotations.In; | |
| 16 | 5 | import org.jboss.seam.annotations.intercept.AroundInvoke; |
| 17 | 6 | import org.jboss.seam.annotations.intercept.Interceptor; |
| 18 | import org.jboss.seam.contexts.Contexts; | |
| 19 | 7 | import org.jboss.seam.core.BijectionInterceptor; |
| 20 | 8 | import org.jboss.seam.intercept.AbstractInterceptor; |
| 21 | 9 | import org.jboss.seam.intercept.InvocationContext; |
| 22 | 10 | import org.jboss.seam.transaction.Transaction; |
| 23 | import org.jboss.seam.util.Reflections; | |
| 24 | 11 | |
| 25 | 12 | /** |
| 26 | 13 | * Swizzles entity references around each invocation, maintaining |
| ... | ...@@ -34,6 +21,9 @@ | |
| 34 | 21 | @Interceptor(around=BijectionInterceptor.class) |
| 35 | 22 | public class ManagedEntityIdentityInterceptor extends AbstractInterceptor |
| 36 | 23 | { |
| 24 | ||
| 25 | private static ManagedEntityStateManager managedEntityStateManager = new ManagedEntityStateManager(); | |
| 26 | ||
| 37 | 27 | private boolean reentrant; |
| 38 | 28 | //TODO: cache the non-ignored fields, probably on Component |
| 39 | 29 | |
| ... | ...@@ -49,12 +39,12 @@ | |
| 49 | 39 | return ctx.proceed(); |
| 50 | 40 | } else { |
| 51 | 41 | reentrant = true; |
| 52 | entityIdsToRefs(ctx); | |
| 42 | managedEntityStateManager.entityIdsToRefs(ctx.getTarget(), getComponent()); | |
| 53 | 43 | try { |
| 54 | 44 | return ctx.proceed(); |
| 55 | 45 | } finally { |
| 56 | 46 | if (!isTransactionRolledBackOrMarkedRollback()) { |
| 57 | entityRefsToIds(ctx); | |
| 47 | managedEntityStateManager.entityRefsToIds(ctx.getTarget(), getComponent()); | |
| 58 | 48 | reentrant = false; |
| 59 | 49 | } |
| 60 | 50 | } |
| ... | ...@@ -73,139 +63,5 @@ | |
| 73 | 63 | } |
| 74 | 64 | } |
| 75 | 65 | |
| 76 | public void entityRefsToIds(InvocationContext ctx) throws Exception | |
| 77 | { | |
| 78 | if ( touchedContextsExist() ) | |
| 79 | { | |
| 80 | Object bean = ctx.getTarget(); | |
| 81 | Class beanClass = bean.getClass(); | |
| 82 | for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass()) | |
| 83 | { | |
| 84 | for ( Field field: beanClass.getDeclaredFields() ) | |
| 85 | { | |
| 86 | if ( !ignore(field) ) | |
| 87 | { | |
| 88 | Object value = getFieldValue(bean, field); | |
| 89 | if (value!=null) | |
| 90 | { | |
| 91 | Object dataModel = null; | |
| 92 | if ( DATA_MODEL.isInstance(value) ) | |
| 93 | { | |
| 94 | dataModel = value; | |
| 95 | value = getWrappedData(dataModel); | |
| 96 | } | |
| 97 | if ( isRef(value) ) | |
| 98 | { | |
| 99 | saveWrapper(bean, field, dataModel, value); | |
| 100 | } | |
| 101 | else | |
| 102 | { | |
| 103 | clearWrapper(field); | |
| 104 | } | |
| 105 | } | |
| 106 | else | |
| 107 | { | |
| 108 | clearWrapper(field); | |
| 109 | } | |
| 110 | } | |
| 111 | } | |
| 112 | } | |
| 113 | } | |
| 114 | } | |
| 115 | ||
| 116 | public void entityIdsToRefs(InvocationContext ctx) throws Exception | |
| 117 | { | |
| 118 | if ( touchedContextsExist() ) | |
| 119 | { | |
| 120 | Object bean = ctx.getTarget(); | |
| 121 | Class beanClass = bean.getClass(); | |
| 122 | for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass()) | |
| 123 | { | |
| 124 | for ( Field field: beanClass.getDeclaredFields() ) | |
| 125 | { | |
| 126 | if ( !ignore(field) ) | |
| 127 | { | |
| 128 | Object value = getFieldValue(bean, field); | |
| 129 | Object dataModel = null; | |
| 130 | if (value!=null && DATA_MODEL.isInstance(value) ) | |
| 131 | { | |
| 132 | dataModel = value; | |
| 133 | } | |
| 134 | //TODO: be more selective | |
| 135 | getFromWrapper(bean, field, dataModel); | |
| 136 | } | |
| 137 | } | |
| 138 | } | |
| 139 | } | |
| 140 | } | |
| 141 | ||
| 142 | private boolean isRef(Object value) | |
| 143 | { | |
| 144 | //TODO: could do better by checking if the | |
| 145 | // collection really contains an entity | |
| 146 | return value instanceof List || | |
| 147 | value instanceof Map || | |
| 148 | value instanceof Set || | |
| 149 | Seam.getEntityClass(value.getClass()) != null; | |
| 150 | } | |
| 151 | ||
| 152 | private Object getFieldValue(Object bean, Field field) throws Exception | |
| 153 | { | |
| 154 | if ( !field.isAccessible() ) field.setAccessible(true); | |
| 155 | Object value = Reflections.get(field, bean); | |
| 156 | return value; | |
| 157 | } | |
| 158 | ||
| 159 | private boolean ignore(Field field) | |
| 160 | { | |
| 161 | return Modifier.isTransient( field.getModifiers() ) || | |
| 162 | Modifier.isStatic( field.getModifiers() ) | |
| 163 | || field.isAnnotationPresent(In.class); | |
| 164 | } | |
| 165 | ||
| 166 | private boolean touchedContextsExist() | |
| 167 | { | |
| 168 | PersistenceContexts touchedContexts = PersistenceContexts.instance(); | |
| 169 | return touchedContexts!=null && touchedContexts.getTouchedContexts().size()>0; | |
| 170 | } | |
| 171 | ||
| 172 | private String getFieldId(Field field) | |
| 173 | { | |
| 174 | return getComponent().getName() + '.' + field.getName(); | |
| 175 | } | |
| 176 | ||
| 177 | private void saveWrapper(Object bean, Field field, Object dataModel, Object value) throws Exception | |
| 178 | { | |
| 179 | Contexts.getConversationContext().set( getFieldId(field), value ); | |
| 180 | if (dataModel==null) | |
| 181 | { | |
| 182 | Reflections.set(field, bean, null); | |
| 183 | } | |
| 184 | else | |
| 185 | { | |
| 186 | setWrappedData(dataModel, null); | |
| 187 | } | |
| 188 | } | |
| 189 | ||
| 190 | private void clearWrapper(Field field) throws Exception | |
| 191 | { | |
| 192 | Contexts.getConversationContext().remove( getFieldId(field) ); | |
| 193 | } | |
| 194 | ||
| 195 | private void getFromWrapper(Object bean, Field field, Object dataModel) throws Exception | |
| 196 | { | |
| 197 | Object value = Contexts.getConversationContext().get( getFieldId(field) ); | |
| 198 | if (value!=null) | |
| 199 | { | |
| 200 | if (dataModel==null) | |
| 201 | { | |
| 202 | Reflections.set(field, bean, value); | |
| 203 | } | |
| 204 | else | |
| 205 | { | |
| 206 | setWrappedData(dataModel, value); | |
| 207 | } | |
| 208 | } | |
| 209 | } | |
| 210 | ||
| 66 | ||
| 211 | 67 | } |
| ... | ...@@ -0,0 +1,162 @@ | |
| 1 | package org.jboss.seam.persistence; | |
| 2 | ||
| 3 | import static org.jboss.seam.util.JSF.DATA_MODEL; | |
| 4 | import static org.jboss.seam.util.JSF.getWrappedData; | |
| 5 | import static org.jboss.seam.util.JSF.setWrappedData; | |
| 6 | ||
| 7 | import java.lang.reflect.Field; | |
| 8 | import java.lang.reflect.Modifier; | |
| 9 | import java.util.List; | |
| 10 | import java.util.Map; | |
| 11 | import java.util.Set; | |
| 12 | ||
| 13 | import org.jboss.seam.Component; | |
| 14 | import org.jboss.seam.Seam; | |
| 15 | import org.jboss.seam.annotations.In; | |
| 16 | import org.jboss.seam.contexts.Contexts; | |
| 17 | import org.jboss.seam.util.Reflections; | |
| 18 | ||
| 19 | /** | |
| 20 | * @author Gavin King | |
| 21 | * @author Pete Muir | |
| 22 | * @author Norman Richards | |
| 23 | * | |
| 24 | */ | |
| 25 | public class ManagedEntityStateManager | |
| 26 | { | |
| 27 | ||
| 28 | public void entityRefsToIds(Object controllerBean, Component component) throws Exception | |
| 29 | { | |
| 30 | if ( touchedContextsExist() ) | |
| 31 | { | |
| 32 | Class beanClass = controllerBean.getClass(); | |
| 33 | for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass()) | |
| 34 | { | |
| 35 | for ( Field field: beanClass.getDeclaredFields() ) | |
| 36 | { | |
| 37 | if ( !ignore(field) ) | |
| 38 | { | |
| 39 | Object value = getFieldValue(controllerBean, field); | |
| 40 | if (value!=null) | |
| 41 | { | |
| 42 | Object dataModel = null; | |
| 43 | if ( DATA_MODEL.isInstance(value) ) | |
| 44 | { | |
| 45 | dataModel = value; | |
| 46 | value = getWrappedData(dataModel); | |
| 47 | } | |
| 48 | if ( isRef(value) ) | |
| 49 | { | |
| 50 | saveWrapper(controllerBean, component, field, dataModel, value); | |
| 51 | } | |
| 52 | else | |
| 53 | { | |
| 54 | clearWrapper(component, field); | |
| 55 | } | |
| 56 | } | |
| 57 | else | |
| 58 | { | |
| 59 | clearWrapper(component, field); | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | } | |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | public void entityIdsToRefs(Object controllerBean, Component component) throws Exception | |
| 68 | { | |
| 69 | if ( touchedContextsExist() ) | |
| 70 | { | |
| 71 | Class beanClass = controllerBean.getClass(); | |
| 72 | for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass()) | |
| 73 | { | |
| 74 | for ( Field field: beanClass.getDeclaredFields() ) | |
| 75 | { | |
| 76 | if ( !ignore(field) ) | |
| 77 | { | |
| 78 | Object value = getFieldValue(controllerBean, field); | |
| 79 | Object dataModel = null; | |
| 80 | if (value!=null && DATA_MODEL.isInstance(value) ) | |
| 81 | { | |
| 82 | dataModel = value; | |
| 83 | } | |
| 84 | //TODO: be more selective | |
| 85 | getFromWrapper(controllerBean, component, field, dataModel); | |
| 86 | } | |
| 87 | } | |
| 88 | } | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | private boolean isRef(Object value) | |
| 93 | { | |
| 94 | //TODO: could do better by checking if the | |
| 95 | // collection really contains an entity | |
| 96 | return value instanceof List || | |
| 97 | value instanceof Map || | |
| 98 | value instanceof Set || | |
| 99 | Seam.getEntityClass(value.getClass()) != null; | |
| 100 | } | |
| 101 | ||
| 102 | private Object getFieldValue(Object bean, Field field) throws Exception | |
| 103 | { | |
| 104 | if ( !field.isAccessible() ) field.setAccessible(true); | |
| 105 | Object value = Reflections.get(field, bean); | |
| 106 | return value; | |
| 107 | } | |
| 108 | ||
| 109 | private boolean ignore(Field field) | |
| 110 | { | |
| 111 | return Modifier.isTransient( field.getModifiers() ) || | |
| 112 | Modifier.isStatic( field.getModifiers() ) | |
| 113 | || field.isAnnotationPresent(In.class); | |
| 114 | } | |
| 115 | ||
| 116 | private boolean touchedContextsExist() | |
| 117 | { | |
| 118 | PersistenceContexts touchedContexts = PersistenceContexts.instance(); | |
| 119 | return touchedContexts!=null && touchedContexts.getTouchedContexts().size()>0; | |
| 120 | } | |
| 121 | ||
| 122 | private String getFieldId(Component component, Field field) | |
| 123 | { | |
| 124 | return component.getName() + '.' + field.getName(); | |
| 125 | } | |
| 126 | ||
| 127 | private void saveWrapper(Object bean, Component component, Field field, Object dataModel, Object value) throws Exception | |
| 128 | { | |
| 129 | Contexts.getConversationContext().set( getFieldId(component, field), value ); | |
| 130 | if (dataModel==null) | |
| 131 | { | |
| 132 | Reflections.set(field, bean, null); | |
| 133 | } | |
| 134 | else | |
| 135 | { | |
| 136 | setWrappedData(dataModel, null); | |
| 137 | } | |
| 138 | } | |
| 139 | ||
| 140 | private void clearWrapper(Component component, Field field) throws Exception | |
| 141 | { | |
| 142 | Contexts.getConversationContext().remove( getFieldId(component, field) ); | |
| 143 | } | |
| 144 | ||
| 145 | private void getFromWrapper(Object bean, Component component, Field field, Object dataModel) throws Exception | |
| 146 | { | |
| 147 | Object value =Contexts.getConversationContext().get( getFieldId(component, field) ); | |
| 148 | if (value!=null) | |
| 149 | { | |
| 150 | if (dataModel==null) | |
| 151 | { | |
| 152 | Reflections.set(field, bean, value); | |
| 153 | } | |
| 154 | else | |
| 155 | { | |
| 156 | setWrappedData(dataModel, value); | |
| 157 | } | |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 161 | ||
| 162 | } |
| ... | ...@@ -9,7 +9,7 @@ | |
| 9 | 9 | import org.jboss.seam.annotations.intercept.Interceptor; |
| 10 | 10 | import org.jboss.seam.intercept.AbstractInterceptor; |
| 11 | 11 | import org.jboss.seam.intercept.InvocationContext; |
| 12 | import org.jboss.seam.util.EJB; | |
| 12 | import org.jboss.seam.util.Exceptions; | |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * Before invoking the component, inject all dependencies. After |
| ... | ...@@ -108,9 +108,9 @@ | |
| 108 | 108 | catch (Exception e) |
| 109 | 109 | { |
| 110 | 110 | Exception root = e; |
| 111 | while (EJB.getCause(root) != null) | |
| 111 | while (Exceptions.getCause(root) != null) | |
| 112 | 112 | { |
| 113 | root = EJB.getCause(root); | |
| 113 | root = Exceptions.getCause(root); | |
| 114 | 114 | } |
| 115 | 115 | if (root instanceof CyclicDependencyException) |
| 116 | 116 | { |
| ... | ...@@ -27,7 +27,6 @@ | |
| 27 | 27 | import org.jboss.seam.log.LogProvider; |
| 28 | 28 | import org.jboss.seam.log.Logging; |
| 29 | 29 | import org.jboss.seam.navigation.Pages; |
| 30 | import org.jboss.seam.util.EJB; | |
| 31 | 30 | import org.jboss.seam.util.Reflections; |
| 32 | 31 | import org.jboss.seam.util.Strings; |
| 33 | 32 | import org.jboss.seam.util.XML; |
| ... | ...@@ -56,7 +55,7 @@ | |
| 56 | 55 | |
| 57 | 56 | //build a list of the nested exceptions |
| 58 | 57 | List<Exception> causes = new ArrayList<Exception>(); |
| 59 | for (Exception cause=e; cause!=null; cause=EJB.getCause(cause)) | |
| 58 | for (Exception cause=e; cause!=null; cause=org.jboss.seam.util.Exceptions.getCause(cause)) | |
| 60 | 59 | { |
| 61 | 60 | causes.add(cause); |
| 62 | 61 | } |
| ... | ...@@ -37,7 +37,6 @@ | |
| 37 | 37 | import org.jboss.seam.mock.MockFacesContext; |
| 38 | 38 | import org.jboss.seam.transaction.Transaction; |
| 39 | 39 | import org.jboss.seam.transaction.UserTransaction; |
| 40 | import org.jboss.seam.util.EJB; | |
| 41 | 40 | |
| 42 | 41 | /** |
| 43 | 42 | * Delegate uncaught exceptions to Seam exception handling. |
| ... | ...@@ -66,7 +65,7 @@ | |
| 66 | 65 | catch (Exception e) |
| 67 | 66 | { |
| 68 | 67 | log.warn( "handling uncaught exception", e ); |
| 69 | log.warn( "exception root cause", EJB.getCause(e) ); | |
| 68 | log.warn( "exception root cause", org.jboss.seam.util.Exceptions.getCause(e) ); | |
| 70 | 69 | endWebRequestAfterException( (HttpServletRequest) request, (HttpServletResponse) response, e); |
| 71 | 70 | } |
| 72 | 71 | } |