Commits

Anonymous committed ea6a286

Added support for retrieving the scope key for the scoped model driven bean

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1140e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (4)

src/java/com/opensymphony/xwork2/config/providers/InterceptorBuilder.java

                     LOG.warn("Unable to load config class "+config.getClassName()+" at "+
                             ex.getLocation()+" probably due to a missing jar, which might "+
                             "be fine if you never plan to use the "+config.getName()+" interceptor");
-                    LOG.debug("Actual exception", ex);
+                    LOG.error("Actual exception", ex);
                 }
                 
             } else if (referencedConfig instanceof InterceptorStackConfig) {

src/java/com/opensymphony/xwork2/interceptor/ScopedModelDriven.java

      * Sets the model
      */
     void setModel(T model);
+    
+    /**
+     * Sets the key under which the model is stored
+     * @param key The model key
+     */
+    void setScopeKey(String key);
+    
+    /**
+     * Gets the key under which the model is stored
+     */
+    String getScopeKey();
 }

src/java/com/opensymphony/xwork2/interceptor/ScopedModelDrivenInterceptor.java

     private String name;
     private String className;
     
-    protected Object resolveModel(ObjectFactory factory, Map session, String modelClassName, String modelScope, String modelName) throws Exception {
+    protected Object resolveModel(ObjectFactory factory, ActionContext actionContext, String modelClassName, String modelScope, String modelName) throws Exception {
         Object model = null;
-        if (modelName == null) {
-            modelName = modelClassName;
-        }
+        Map scopeMap = actionContext.getContextMap();
         if ("session".equals(modelScope)) {
-            model = session.get(modelName);
-            if (model == null) {
-                model = factory.buildBean(modelClassName, null);
-                session.put(modelName, model);
-            }
-        } else {
+            scopeMap = actionContext.getSession();
+        }
+        
+        model = scopeMap.get(modelName);
+        if (model == null) {
             model = factory.buildBean(modelClassName, null);
+            scopeMap.put(modelName, model);
         }
         return model;
     }
                         throw new XWorkException("The " + GET_MODEL + "() is not defined in action " + action.getClass() + "", config);
                     }
                 }
-                Object model = resolveModel(ObjectFactory.getObjectFactory(), ctx.getSession(), cName, scope, name);
+                String modelName = name;
+                if (modelName == null) {
+                    modelName = cName;
+                }
+                Object model = resolveModel(ObjectFactory.getObjectFactory(), ctx, cName, scope, modelName);
                 modelDriven.setModel(model);
+                modelDriven.setScopeKey(modelName);
             }
         }
         return invocation.invoke();

src/test/com/opensymphony/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java

 
 import junit.framework.TestCase;
 
+import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.XWorkTestCase;
 
-public class ScopedModelDrivenInterceptorTest extends TestCase {
+public class ScopedModelDrivenInterceptorTest extends XWorkTestCase {
 
     protected ScopedModelDrivenInterceptor inter = null;
     
-    public ScopedModelDrivenInterceptorTest(String name) throws Exception {
-        super(name);
-    }
-
-
     public static void main(String args[]) {
         junit.textui.TestRunner.run(ScopedModelDrivenInterceptorTest.class);
     }
     }
 
     public void testResolveModel() throws Exception {
+        ActionContext ctx = ActionContext.getContext();
+        ctx.setSession(new HashMap());
+        
         ObjectFactory factory = ObjectFactory.getObjectFactory();
-        Object obj = inter.resolveModel(factory, null, "java.lang.String", "request", null);
+        Object obj = inter.resolveModel(factory, ctx, "java.lang.String", "request", "foo");
         assertNotNull(obj);
         assertTrue(obj instanceof String);
+        assertTrue(obj == ctx.get("foo"));
 
-        HashMap session = new HashMap();
-        obj = inter.resolveModel(factory, session, "java.lang.String", "session", "foo");
+        
+        obj = inter.resolveModel(factory, ctx, "java.lang.String", "session", "foo");
         assertNotNull(obj);
         assertTrue(obj instanceof String);
-        assertTrue(obj == session.get("foo"));
+        assertTrue(obj == ctx.getSession().get("foo"));
 
-        obj = inter.resolveModel(factory, session, "java.lang.String", "session", "foo");
+        obj = inter.resolveModel(factory, ctx, "java.lang.String", "session", "foo");
         assertNotNull(obj);
         assertTrue(obj instanceof String);
-        assertTrue(obj == session.get("foo"));
+        assertTrue(obj == ctx.getSession().get("foo"));
+        
     }
 }