Author: mrdon
Date: Sun Nov 27 09:16:43 2005
New Revision: 349264

URL: http://svn.apache.org/viewcvs?rev=349264&view=rev
Log:
Redesigned how variables are introduced to the flow

Added:
    
struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultFlowVariableFactory.java
      - copied, changed from r349187, 
struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java
    
struts/flow/trunk/src/java/org/apache/struts/flow/core/FlowVariableFactory.java
      - copied, changed from r349187, 
struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java
Removed:
    
struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java
    
struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultStaticVariableRegistrar.java
    
struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java
Modified:
    struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java
    struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java
    
struts/flow/trunk/src/java/org/apache/struts/flow/core/javascript/fom/FOM_JavaScriptInterpreter.java
    struts/flow/trunk/src/java/org/apache/struts/flow/portlet/FlowPortlet.java
    struts/flow/trunk/src/java/org/apache/struts/flow/portlet/Portlet.java

Modified: struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java?rev=349264&r1=349263&r2=349264&view=diff
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java (original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java Sun Nov 
27 09:16:43 2005
@@ -29,7 +29,7 @@
 import org.apache.struts.flow.core.Factory;
 import org.apache.struts.flow.core.CompilingInterpreter;
 import org.apache.struts.flow.core.javascript.fom.FOM_JavaScriptInterpreter;
-import org.apache.struts.flow.core.DefaultCallVariableRegistrar;
+import org.apache.struts.flow.core.DefaultFlowVariableFactory;
 import org.apache.struts.flow.sugar.SugarWrapFactory;
 import org.apache.struts.flow.ibatis.SqlMap;
 import org.apache.struts.flow.core.source.impl.ChainSourceResolver;
@@ -259,8 +259,8 @@
         interp.setWrapFactory(new SugarWrapFactory());
         interp.initialize(classesToRegister);
         
interp.register("/org/apache/struts/flow/core/javascript/fom/fom_system.js");
-        interp.addVariableRegistrar(new 
DefaultCallVariableRegistrar(Struts.class, "struts"));
-        interp.addVariableRegistrar(new 
DefaultCallVariableRegistrar(SqlMap.class, "sqlMap"));
+        interp.addFlowVariable("struts", new 
DefaultFlowVariableFactory(Struts.class));
+        interp.addFlowVariable("sqlMap", new 
DefaultFlowVariableFactory(SqlMap.class));
         return interp;
     }
 

Modified: struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java?rev=349264&r1=349263&r2=349264&view=diff
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java (original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java Sun Nov 27 
09:16:43 2005
@@ -16,6 +16,7 @@
 package org.apache.struts.flow;
 
 import org.apache.struts.flow.core.*;
+import org.apache.struts.flow.core.javascript.fom.FOM_Flow;
 import org.apache.struts.action.ActionMapping;
 import org.apache.struts.action.ActionErrors;
 import org.apache.struts.action.ActionMessages;
@@ -33,7 +34,7 @@
  */
 public class Struts {
     
-    protected ServletWebContext ctx = null;
+    protected FOM_Flow flow = null;
     protected static final Logger logger = Factory.getLogger();
 
     public Struts() {
@@ -42,11 +43,16 @@
             
 
     /**  Constructor for the JSLog object */
-    public Struts(WebContext ctx) {
+    public Struts(FOM_Flow flow) {
+        this.flow = flow;
+    }
+    
+    private ServletWebContext getContext() {
+        WebContext ctx = flow.getWebContext();
         if (ctx instanceof ServletWebContext) {
-            this.ctx = (ServletWebContext)ctx;
+            return (ServletWebContext)ctx;
         } else {
-            logger.warn("Unknown context instance");
+            throw new IllegalStateException("The web context must be the 
ServletWebContext");
         }
     }
     
@@ -54,49 +60,49 @@
      *  Gets a map of request parameters as Strings
      */
     public Map getParam() {
-        return ctx.getParam();
+        return getContext().getParam();
     }
     
     /**
      *  Gets a map of request parameters as String arrays
      */
     public Map getParamValues() {
-        return ctx.getParamValues();
+        return getContext().getParamValues();
     }
     
     /**
      *  Gets a map of request attributes
      */
     public Map getRequestScope() {
-        return ctx.getRequestScope();
+        return getContext().getRequestScope();
     }
     
     /**
      *  Gets a map of session attributes
      */
     public Map getSessionScope() {
-        return ctx.getSessionScope();
+        return getContext().getSessionScope();
     }
     
     /**
      *  Gets a map of application attributes
      */
     public Map getApplicationScope() {
-        return ctx.getApplicationScope();
+        return getContext().getApplicationScope();
     }
     
     /**
      *  Gets the servlet request
      */
     public ServletRequest getRequest() {
-        return ctx.getRequest();
+        return getContext().getRequest();
     }
     
     /**
      *  Gets the servlet context
      */
     public ServletContext getServletContext() {
-        return ctx.getContext();
+        return getContext().getContext();
     }
 
     /**
@@ -105,7 +111,7 @@
      * @param key The message key
      */
     public String getMessage(String key) {
-        MessageResources res = 
(MessageResources)ctx.get(Constants.MESSAGE_RESOURCES_KEY);
+        MessageResources res = 
(MessageResources)getContext().get(Constants.MESSAGE_RESOURCES_KEY);
         return res.getMessage(key);
     }
     
@@ -113,31 +119,31 @@
      *  Gets the action mapping
      */
     public ActionMapping getMapping() {
-        return (ActionMapping)ctx.get(Constants.ACTION_CONFIG_KEY);
+        return (ActionMapping)getContext().get(Constants.ACTION_CONFIG_KEY);
     }
     
     /**
      * Gets if the action has been canceled
      */
     public boolean isCanceled() {
-        FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY);
-        return action.isCancelled(ctx.getRequest());
+        FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
+        return action.isCancelled(getContext().getRequest());
     }
     
     /**
      *  Gets if the current token is valid
      */
     public boolean isTokenValid() {
-        FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY);
-        return action.isTokenValid(ctx.getRequest());
+        FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
+        return action.isTokenValid(getContext().getRequest());
     }
     
     /**
      *  Resets the current transation token
      */
     public void resetToken() {
-        FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY);
-        action.resetToken(ctx.getRequest());
+        FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
+        action.resetToken(getContext().getRequest());
     }
     
     /**
@@ -146,8 +152,8 @@
      * @param errors The action errors
      */
     public void saveErrors(ActionErrors errors) {
-        FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY);
-        action.saveErrors(ctx.getRequest(), errors);
+        FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
+        action.saveErrors(getContext().getRequest(), errors);
     }
     
     /** 
@@ -156,16 +162,16 @@
      * @param msgs The action messages
      */
     public void saveMessages(ActionMessages msgs) {
-        FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY);
-        action.saveMessages(ctx.getRequest(), msgs);
+        FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
+        action.saveMessages(getContext().getRequest(), msgs);
     }
     
     /**
      *  Saves a transaction token in the request
      */
     public void saveToken() {
-        FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY);
-        action.saveToken(ctx.getRequest());
+        FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
+        action.saveToken(getContext().getRequest());
     }
 }
 

Copied: 
struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultFlowVariableFactory.java
 (from r349187, 
struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java)
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultFlowVariableFactory.java?p2=struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultFlowVariableFactory.java&p1=struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java&r1=349187&r2=349264&rev=349264&view=diff
==============================================================================
--- 
struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java
 (original)
+++ 
struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultFlowVariableFactory.java
 Sun Nov 27 09:16:43 2005
@@ -16,7 +16,7 @@
 package org.apache.struts.flow.core;
 
 import org.mozilla.javascript.Scriptable;
-import org.apache.commons.chain.web.WebContext;
+import org.apache.struts.flow.core.javascript.fom.FOM_Flow;
 import java.lang.reflect.Constructor;
 
 /**
@@ -25,36 +25,15 @@
  *  call-specific variables can define instances of themselves for every script
  *  call.
  */
-public class DefaultCallVariableRegistrar implements VariableRegistrar {
+public class DefaultFlowVariableFactory implements FlowVariableFactory {
 
     private Class variableClass;
-    private String variableName;
 
-    public DefaultCallVariableRegistrar(Class variableClass, String 
variableName) {
-        this.variableName = variableName;
+    public DefaultFlowVariableFactory(Class variableClass) {
         this.variableClass = variableClass;
     }
 
     /**
-     *  Gets the Class object for the variable
-     *
-     [EMAIL PROTECTED]    The classInstance value
-     */
-    public Class getClassInstance() {
-        return variableClass;
-    }
-
-    /**
-     *  Gets the name of the variable class
-     *
-     [EMAIL PROTECTED]    The className value
-     */
-    public String getClassName() {
-        ;
-        return variableClass.getName();
-    }
-
-    /**
      *  Gets an instance of the variable. First tries to call constructor that
      *  takes a single argument of the Context. If not found, it calls the 
empty
      *  constructor.
@@ -64,16 +43,16 @@
      *      static variable
      [EMAIL PROTECTED]        The instance value
      */
-    public Object getInstance(Scriptable scope, WebContext ctx) {
+    public Object getInstance(Scriptable scope, FOM_Flow flow) {
         try {
             Constructor c = null;
             try {
-                c = variableClass.getConstructor(new 
Class[]{WebContext.class});
+                c = variableClass.getConstructor(new Class[]{flow.getClass()});
             } catch (NoSuchMethodException ex) {
                 // ignored
             }
             if (c != null) {
-                return c.newInstance(new Object[]{ctx});
+                return c.newInstance(new Object[]{flow});
             } else {
                 return variableClass.newInstance();
             }
@@ -83,22 +62,5 @@
         }
     }
 
-    /**
-     *  Gets the variable name
-     *
-     [EMAIL PROTECTED]    The name value
-     */
-    public String getName() {
-        return variableName;
-    }
-
-    /**
-     *  Returns whether this variable is call-specific or static
-     *
-     [EMAIL PROTECTED]    The callSpecific value
-     */
-    public boolean isCallSpecific() {
-        return true;
-    }
 }
 

Copied: 
struts/flow/trunk/src/java/org/apache/struts/flow/core/FlowVariableFactory.java 
(from r349187, 
struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java)
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/core/FlowVariableFactory.java?p2=struts/flow/trunk/src/java/org/apache/struts/flow/core/FlowVariableFactory.java&p1=struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java&r1=349187&r2=349264&rev=349264&view=diff
==============================================================================
--- 
struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java 
(original)
+++ 
struts/flow/trunk/src/java/org/apache/struts/flow/core/FlowVariableFactory.java 
Sun Nov 27 09:16:43 2005
@@ -16,7 +16,7 @@
 package org.apache.struts.flow.core;
 
 import org.mozilla.javascript.Scriptable;
-import org.apache.commons.chain.web.WebContext;
+import org.apache.struts.flow.core.javascript.fom.FOM_Flow;
 
 /**
  *  Defines a variable registrar used to define either a static or 
@@ -24,17 +24,7 @@
  *  once per global scope, while call-specific variables can define instances
  *  of themselves for every script call.
  */
-public interface VariableRegistrar {
-
-    /**
-     *  Gets the Class object for the variable
-     */
-    public Class getClassInstance();
-
-    /**
-     *  Gets the name of the variable class
-     */
-    public String getClassName();
+public interface FlowVariableFactory {
 
     /**
      *  Gets an instance of the variable
@@ -43,16 +33,7 @@
      * @param ctx The commons chain context for the call, null if defining
      *            a static variable
      */
-    public Object getInstance(Scriptable scope, WebContext ctx);
+    public Object getInstance(Scriptable scope, FOM_Flow flow);
 
-    /**
-     *  Gets the variable name
-     */
-    public String getName();
-
-    /**
-     *  Returns whether this variable is call-specific or static
-     */
-    public boolean isCallSpecific();
 }
 

Modified: 
struts/flow/trunk/src/java/org/apache/struts/flow/core/javascript/fom/FOM_JavaScriptInterpreter.java
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/core/javascript/fom/FOM_JavaScriptInterpreter.java?rev=349264&r1=349263&r2=349264&view=diff
==============================================================================
--- 
struts/flow/trunk/src/java/org/apache/struts/flow/core/javascript/fom/FOM_JavaScriptInterpreter.java
 (original)
+++ 
struts/flow/trunk/src/java/org/apache/struts/flow/core/javascript/fom/FOM_JavaScriptInterpreter.java
 Sun Nov 27 09:16:43 2005
@@ -127,7 +127,8 @@
     private boolean enableDebugger;
     
     private WrapFactory wrapFactory;
-    private List registrars = new ArrayList();
+    private Map flowVars = new HashMap();
+    private Map globalVars = new HashMap();
 
     /**
      * JavaScript debugger: there's only one of these: it can debug multiple
@@ -179,8 +180,8 @@
      *
      * @param reg The variable registrar
      */
-    public void addVariableRegistrar(VariableRegistrar reg) {
-        registrars.add(reg);
+    public void addGlobalVariable(String name, Object var) {
+        globalVars.put(name, var);
     }
 
     /**
@@ -188,9 +189,27 @@
      *
      * @param reg The variable registrar
      */
-    public void removeVariableRegistrar(VariableRegistrar reg) {
-        registrars.remove(reg);
-    } 
+    public void removeGlobalVariable(String name) {
+        globalVars.remove(name);
+    }
+    
+    /**
+     *  Adds a class that will register a global variable
+     *
+     * @param reg The variable registrar
+     */
+    public void addFlowVariable(String name, FlowVariableFactory fac) {
+        flowVars.put(name, fac);
+    }
+
+    /**
+     *  Removes a class that will register a global variable
+     *
+     * @param reg The variable registrar
+     */
+    public void removeFlowVariable(String name) {
+        flowVars.remove(name);
+    }
     
 
     /**
@@ -290,25 +309,18 @@
     protected void initScope(Context context, Global scope) throws 
IllegalAccessException,
             InstantiationException, InvocationTargetException, 
JavaScriptException {
         
-        VariableRegistrar reg;
-        List lst;
-        for (Iterator i = registrars.iterator(); i.hasNext(); ) {
-            reg = (VariableRegistrar) i.next();
-            ScriptableObject.defineClass(scope, reg.getClassInstance());
-        }
-
-        // Define some global variables in JavaScript
-        Object args[] = {};
-
-        for (Iterator i = registrars.iterator(); i.hasNext(); ) {
-            reg = (VariableRegistrar) i.next();
-            if (!reg.isCallSpecific()) {
-                Scriptable var = context.newObject(scope, reg.getClassName(), 
args);
-                scope.put(reg.getName(), scope, reg.getInstance(scope, null));
-            }    
-        }
-
-
+        WrapFactory factory = context.getWrapFactory();
+        for (Iterator i = globalVars.keySet().iterator(); i.hasNext(); ) {
+            String name = (String) i.next();
+            Object bean = globalVars.get(name);
+            
+            if (bean instanceof Scriptable) {
+                scope.put(name, scope, (Scriptable)bean);
+            } else {
+                Scriptable var = factory.wrapAsJavaObject(context, scope, 
bean, bean.getClass());
+                scope.put(name, scope, var);
+            }
+        }        
     }
 
 
@@ -385,7 +397,7 @@
         /**
          * Initializes new top-level scope.
          */
-        public ThreadScope(Global scope) throws Exception {
+        public ThreadScope(Global scope, Map flowVars) throws Exception {
             final Context context = Context.getCurrentContext();
 
             final String[] names = { "importClass" };
@@ -413,6 +425,17 @@
                                                                args);
             flow.setParentScope(this);
             super.put("flow", this, flow);
+            
+            WrapFactory factory = context.getWrapFactory();
+            for (Iterator i = flowVars.keySet().iterator(); i.hasNext(); ) {
+                String name = (String) i.next();
+                FlowVariableFactory varfactory = (FlowVariableFactory) 
flowVars.get(name);
+                
+                Object bean = varfactory.getInstance(this, flow);
+                
+                Scriptable var = (Scriptable) factory.wrap(context, this, 
bean, bean.getClass());
+                super.put(name, this, var);
+            }  
 
             defineProperty(LAST_EXEC_TIME,
                            new Long(0),
@@ -495,7 +518,7 @@
     }
 
     private ThreadScope createThreadScope() throws Exception {
-        return new ThreadScope(scope);
+        return new ThreadScope(scope, flowVars);
     }
 
     /**
@@ -543,21 +566,6 @@
         thrScope.setupPackages(contextClassloader);
         flow.pushCallContext(this, webctx, getLogger(), null);
         
-        VariableRegistrar reg;
-        for (Iterator i = registrars.iterator(); i.hasNext(); ) {
-            reg = (VariableRegistrar) i.next();
-            if (reg.isCallSpecific()) {
-                WrapFactory factory = context.getWrapFactory();
-                Object o = reg.getInstance(thrScope, webctx);
-                if (o instanceof Scriptable) {
-                    thrScope.put(reg.getName(), thrScope, (Scriptable)o);
-                } else {
-                    Scriptable var = factory.wrapAsJavaObject(context, 
thrScope, o, reg.getClassInstance());
-                    thrScope.put(reg.getName(), thrScope, var);
-                }
-            }    
-        }
-
         // Check if we need to compile and/or execute scripts
         synchronized (compiledScripts) {
             List execList = new ArrayList();

Modified: 
struts/flow/trunk/src/java/org/apache/struts/flow/portlet/FlowPortlet.java
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/portlet/FlowPortlet.java?rev=349264&r1=349263&r2=349264&view=diff
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/portlet/FlowPortlet.java 
(original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/portlet/FlowPortlet.java 
Sun Nov 27 09:16:43 2005
@@ -48,7 +48,7 @@
         interp.setWrapFactory(new SugarWrapFactory());
         interp.initialize();
         
interp.register("/org/apache/struts/flow/core/javascript/fom/fom_system.js");
-        interp.addVariableRegistrar(new 
DefaultCallVariableRegistrar(Portlet.class, "portlet"));
+        interp.addFlowVariable("portlet", new 
DefaultFlowVariableFactory(Portlet.class));
         //interp.addVariableRegistrar(new 
DefaultCallVariableRegistrar(SqlMap.class, "sqlMap"));
         return interp;
     }

Modified: struts/flow/trunk/src/java/org/apache/struts/flow/portlet/Portlet.java
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/portlet/Portlet.java?rev=349264&r1=349263&r2=349264&view=diff
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/portlet/Portlet.java 
(original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/portlet/Portlet.java Sun 
Nov 27 09:16:43 2005
@@ -17,8 +17,9 @@
 
 import org.apache.struts.flow.core.*;
 import javax.portlet.*;
-import org.apache.commons.chain.Context;
+import org.apache.commons.chain.web.WebContext;
 import org.apache.commons.chain.web.portlet.PortletWebContext;
+import org.apache.struts.flow.core.javascript.fom.FOM_Flow;
 
 import java.util.Map;
 
@@ -29,7 +30,7 @@
  */
 public class Portlet {
     
-    protected PortletWebContext ctx = null;
+    protected FOM_Flow flow = null;
     protected static final Logger logger = Factory.getLogger();
 
     public Portlet() {
@@ -38,11 +39,16 @@
             
 
     /**  Constructor for the JSLog object */
-    public Portlet(Context ctx) {
+    public Portlet(FOM_Flow flow) {
+        this.flow = flow;
+    }
+    
+    private PortletWebContext getContext() {
+        WebContext ctx = flow.getWebContext();
         if (ctx instanceof PortletWebContext) {
-            this.ctx = (PortletWebContext)ctx;
+            return (PortletWebContext)ctx;
         } else {
-            logger.warn("Unknown context instance");
+            throw new IllegalStateException("The web context must be the 
PortletWebContext");
         }
     }
     
@@ -50,49 +56,49 @@
      *  Gets a map of request parameters as Strings
      */
     public Map getParam() {
-        return ctx.getParam();
+        return getContext().getParam();
     }
     
     /**
      *  Gets a map of request parameters as String arrays
      */
     public Map getParamValues() {
-        return ctx.getParamValues();
+        return getContext().getParamValues();
     }
     
     /**
      *  Gets a map of request attributes
      */
     public Map getRequestScope() {
-        return ctx.getRequestScope();
+        return getContext().getRequestScope();
     }
     
     /**
      *  Gets a map of session attributes
      */
     public Map getSessionScope() {
-        return ctx.getSessionScope();
+        return getContext().getSessionScope();
     }
     
     /**
      *  Gets a map of application attributes
      */
     public Map getApplicationScope() {
-        return ctx.getApplicationScope();
+        return getContext().getApplicationScope();
     }
     
     /**
      *  Gets the portlet request
      */
     public PortletRequest getRequest() {
-        return ctx.getRequest();
+        return getContext().getRequest();
     }
     
     /**
      *  Gets the portlet context
      */
     public PortletContext getPortletContext() {
-        return ctx.getContext();
+        return getContext().getContext();
     }
 }
 



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

Reply via email to