Author: mrdon Date: Sun Apr 10 19:50:40 2005 New Revision: 160820 URL: http://svn.apache.org/viewcvs?view=rev&rev=160820 Log: * Adding variable registrars to allow code that uses flow to register either static or call-specific variables. Default use is to register a Struts object as 'struts' * Renamed JSContext to JSFlow, so now the main variable in the flow will be 'flow' instead of the confusing 'context'. * Changed the Struts helper methods into Struts object described above
Added: struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java (with props) struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java (with props) struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultStaticVariableRegistrar.java (with props) struts/flow/trunk/src/java/org/apache/struts/flow/core/JSFlow.java - copied, changed from r160805, struts/flow/trunk/src/java/org/apache/struts/flow/core/JSContext.java struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java (with props) Removed: struts/flow/trunk/src/java/org/apache/struts/flow/core/JSContext.java struts/flow/trunk/src/java/struts.js Modified: struts/flow/trunk/src/examples/WEB-INF/guess/numberguess.js struts/flow/trunk/src/examples/WEB-INF/remote/numberguess.js struts/flow/trunk/src/examples/WEB-INF/templates/numberguess.js struts/flow/trunk/src/examples/WEB-INF/wizard/wizard-flow.js struts/flow/trunk/src/examples/WEB-INF/wizard/wizard.js struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java struts/flow/trunk/src/java/org/apache/struts/flow/core/JSWebContinuation.java struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java struts/flow/trunk/src/java/system.js Modified: struts/flow/trunk/src/examples/WEB-INF/guess/numberguess.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/examples/WEB-INF/guess/numberguess.js?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/examples/WEB-INF/guess/numberguess.js (original) +++ struts/flow/trunk/src/examples/WEB-INF/guess/numberguess.js Sun Apr 10 19:50:40 2005 @@ -13,7 +13,7 @@ "guesses" : guesses} ); // process user's guess - var guess = parseInt( getRequestParams().guess ); + var guess = parseInt( struts.param.guess ); guesses++; if (guess) { if (guess > random) { Modified: struts/flow/trunk/src/examples/WEB-INF/remote/numberguess.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/examples/WEB-INF/remote/numberguess.js?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/examples/WEB-INF/remote/numberguess.js (original) +++ struts/flow/trunk/src/examples/WEB-INF/remote/numberguess.js Sun Apr 10 19:50:40 2005 @@ -16,9 +16,9 @@ "guesses" : guesses}, ["cheat"]); - print("processing a user guess "+getRequestParams().guess); + print("processing a user guess "+struts.param.guess); // process user's guess - var guess = parseInt( getRequestParams().guess ); + var guess = parseInt( struts.param.guess ); guesses++; if (guess) { if (guess > random) { Modified: struts/flow/trunk/src/examples/WEB-INF/templates/numberguess.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/examples/WEB-INF/templates/numberguess.js?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/examples/WEB-INF/templates/numberguess.js (original) +++ struts/flow/trunk/src/examples/WEB-INF/templates/numberguess.js Sun Apr 10 19:50:40 2005 @@ -1,4 +1,4 @@ -context.load("/templates/template.js"); +flow.load("/templates/template.js"); var random; var guesses; @@ -19,7 +19,7 @@ ["cheat"]); // process user's guess - var guess = parseInt( getRequestParams().guess ); + var guess = parseInt( struts.param.guess ); guesses++; if (guess) { if (guess > random) { @@ -58,11 +58,11 @@ // Use default ttl value from continuation manager var timeToLive = 0; - var kont = new WebContinuation(context, k, lastContinuation, timeToLive); + var kont = new WebContinuation(flow, k, lastContinuation, timeToLive); bizdata.contid = kont.id; - res = context.chainContext.response; - stream = context.chainContext.context.getResourceAsStream("/WEB-INF/templates/"+page+".jt"); + res = flow.context.response; + stream = struts.servletContext.getResourceAsStream("/WEB-INF/templates/"+page+".jt"); if (stream != null) { text = new String(stream.getText()); html = text.process(bizdata); Modified: struts/flow/trunk/src/examples/WEB-INF/wizard/wizard-flow.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/examples/WEB-INF/wizard/wizard-flow.js?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/examples/WEB-INF/wizard/wizard-flow.js (original) +++ struts/flow/trunk/src/examples/WEB-INF/wizard/wizard-flow.js Sun Apr 10 19:50:40 2005 @@ -1,5 +1,5 @@ importPackage(Packages.java.util); -context.load("/WEB-INF/wizard/wizard.js"); +flow.load("/WEB-INF/wizard/wizard.js"); function main() { var model = new HashMap(); @@ -20,13 +20,13 @@ } function populate() { - m = context.chainContext.paramValues; + m = struts.paramValues; for (i = m.keySet().iterator(); i.hasNext(); ) { key = i.next(); this.model.put(key, m.get(key)[0]); } // Bug in commons-chain prevents this - //this.model.putAll(context.chainContext.getParamValues()); + //this.model.putAll(struts.paramValues); } function validate() { Modified: struts/flow/trunk/src/examples/WEB-INF/wizard/wizard.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/examples/WEB-INF/wizard/wizard.js?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/examples/WEB-INF/wizard/wizard.js (original) +++ struts/flow/trunk/src/examples/WEB-INF/wizard/wizard.js Sun Apr 10 19:50:40 2005 @@ -70,10 +70,10 @@ */ Wizard.prototype.sendFormAndWait = function(name, lastWebCont, bizdata) { var k = new Continuation(); - var wk = new WebContinuation(context, k, lastWebCont, this.timeToLive); - context.put(Constants.FORWARD_NAME_KEY, name); - context.put(Constants.CONTINUATION_ID_KEY, wk.id); - context.put(Constants.BIZ_DATA_KEY, bizdata); + var wk = new WebContinuation(flow, k, lastWebCont, this.timeToLive); + flow.put(Packages.org.apache.struts.flow.Constants.FORWARD_NAME_KEY, name); + flow.put(Packages.org.apache.struts.flow.Constants.CONTINUATION_ID_KEY, wk.id); + flow.put(Packages.org.apache.struts.flow.Constants.BIZ_DATA_KEY, bizdata); suicide(); } @@ -135,7 +135,7 @@ // Determine if next button is pressed and should stop showing if ((doValidate && !keepShowing) || !doValidate) { keepShowing = true; - params = getRequestParams(); + params = struts.param; for (id in exitIds) { if (params[exitIds[id]] != null || params[exitIds[id]+'.x'] != null) { exitId = exitIds[id]; @@ -162,8 +162,8 @@ // This can be overridden by declaring a "prevId" variable outside the function var prevId = (this.prevId != null ? this.prevId : "prev"); - log.debug("Previous Id:"+context.chainContext.param.get(prevId)); - if (context.chainContext.param.get(prevId) != null) { + log.debug("Previous Id:"+struts.param.get(prevId)); + if (struts.param.get(prevId) != null) { log.debug("going back"); k = kont.webContinuation; for (x=0; x<3; x++) { Modified: struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java Sun Apr 10 19:50:40 2005 @@ -35,7 +35,7 @@ import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessages; -import org.apache.struts.flow.core.JSContext; +import org.apache.struts.flow.core.JSFlow; import org.apache.struts.flow.core.JavaScriptInterpreter; import org.apache.struts.flow.json.JSONArray; import org.apache.struts.flow.json.JSONSerializer; @@ -146,12 +146,12 @@ interp.callFunction(func, args, context); // retrieve page, continuation ID, and attributes from chain context - String page = (String) JSContext.jsobjectToObject(context.get(Constants.FORWARD_NAME_KEY)); + String page = (String) JSFlow.jsobjectToObject(context.get(Constants.FORWARD_NAME_KEY)); contid = (String) context.get(Constants.CONTINUATION_ID_KEY); Scriptable bizdata = (Scriptable) context.get(Constants.BIZ_DATA_KEY); Map atts = null; if (bizdata != null) { - atts = JSContext.jsobjectToMap(bizdata); + atts = JSFlow.jsobjectToMap(bizdata); } return dispatchToPage(request, response, mapping, page, contid, atts); } @@ -189,7 +189,7 @@ Scriptable bizdata = (Scriptable) context.get(Constants.BIZ_DATA_KEY); Map atts = null; if (bizdata != null) { - atts = JSContext.jsobjectToMap(bizdata); + atts = JSFlow.jsobjectToMap(bizdata); } // if a flowcall, return pure json @@ -234,7 +234,7 @@ Iterator attkeys = atts.keySet().iterator(); while (attkeys.hasNext()) { String attkey = (String) attkeys.next(); - request.setAttribute(attkey, JSContext.jsobjectToObject(atts.get(attkey))); + request.setAttribute(attkey, JSFlow.jsobjectToObject(atts.get(attkey))); } } 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?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java Sun Apr 10 19:50:40 2005 @@ -28,6 +28,7 @@ import org.apache.struts.config.ModuleConfig; import org.apache.struts.flow.core.Factory; import org.apache.struts.flow.core.JavaScriptInterpreter; +import org.apache.struts.flow.core.DefaultCallVariableRegistrar; import org.apache.struts.flow.sugar.SugarWrapFactory; /** @@ -228,7 +229,7 @@ interp.setWrapFactory(new SugarWrapFactory()); interp.initialize(); interp.register("/system.js"); - interp.register("/struts.js"); + interp.addVariableRegistrar(new DefaultCallVariableRegistrar(Struts.class, "struts")); return interp; } Added: 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?view=auto&rev=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java (added) +++ struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java Sun Apr 10 19:50:40 2005 @@ -0,0 +1,118 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts.flow; + +import org.apache.struts.flow.core.*; +import org.apache.struts.action.ActionMapping; +import org.apache.struts.action.ActionErrors; +import org.apache.struts.action.ActionMessages; +import javax.servlet.*; +import org.apache.commons.chain.Context; +import org.apache.commons.chain.web.servlet.ServletWebContext; +import org.apache.struts.util.MessageResources; + +import java.util.Map; + +/** + * Access to Struts and Servlet resources + */ +public class Struts { + + protected ServletWebContext ctx = null; + protected static final Logger logger = Factory.getLogger(); + + public Struts() { + throw new IllegalStateException("Cannot create new Struts object in a flow script"); + } + + + /** Constructor for the JSLog object */ + public Struts(Context ctx) { + if (ctx instanceof ServletWebContext) { + this.ctx = (ServletWebContext)ctx; + } else { + logger.warn("Unknown context instance"); + } + } + + public Map getParam() { + return ctx.getParam(); + } + + public Map getParamValues() { + return ctx.getParamValues(); + } + + public Map getRequestScope() { + return ctx.getRequestScope(); + } + + public Map getSessionScope() { + return ctx.getSessionScope(); + } + + public Map getApplicationScope() { + return ctx.getApplicationScope(); + } + + public ServletRequest getRequest() { + return ctx.getRequest(); + } + + public ServletContext getServletContext() { + return ctx.getContext(); + } + + public String getMessage(String key) { + MessageResources res = (MessageResources)ctx.get(Constants.MESSAGE_RESOURCES_KEY); + return res.getMessage(key); + } + + public ActionMapping getMapping() { + return (ActionMapping)ctx.get(Constants.ACTION_CONFIG_KEY); + } + + public boolean isCancelled() { + FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY); + return action.isCancelled(ctx.getRequest()); + } + + public boolean isTokenValid() { + FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY); + return action.isTokenValid(ctx.getRequest()); + } + + public void resetToken() { + FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY); + action.resetToken(ctx.getRequest()); + } + + public void saveErrors(ActionErrors errors) { + FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY); + action.saveErrors(ctx.getRequest(), errors); + } + + public void saveMessages(ActionMessages msgs) { + FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY); + action.saveMessages(ctx.getRequest(), msgs); + } + + public void saveToken() { + FlowAction action = (FlowAction)ctx.get(Constants.ACTION_KEY); + action.saveToken(ctx.getRequest()); + } +} + Propchange: struts/flow/trunk/src/java/org/apache/struts/flow/Struts.java ------------------------------------------------------------------------------ svn:executable = * Added: 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/DefaultCallVariableRegistrar.java?view=auto&rev=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java (added) +++ struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java Sun Apr 10 19:50:40 2005 @@ -0,0 +1,104 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts.flow.core; + +import org.mozilla.javascript.Scriptable; +import org.apache.commons.chain.Context; +import java.lang.reflect.Constructor; + +/** + * Defines a variable registrar used to define a call-specific variable in the + * global scope. Static variables are defined once per global scope, while + * call-specific variables can define instances of themselves for every script + * call. + */ +public class DefaultCallVariableRegistrar implements VariableRegistrar { + + private Class variableClass; + private String variableName; + + public DefaultCallVariableRegistrar(Class variableClass, String variableName) { + this.variableName = variableName; + 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. + * + [EMAIL PROTECTED] scope The scope the variable will be placed in + [EMAIL PROTECTED] ctx The commons chain context for the call, null if defining a + * static variable + [EMAIL PROTECTED] The instance value + */ + public Object getInstance(Scriptable scope, Context ctx) { + try { + Constructor c = null; + try { + c = variableClass.getConstructor(new Class[]{Context.class}); + } catch (NoSuchMethodException ex) { + // ignored + } + if (c != null) { + return c.newInstance(new Object[]{ctx}); + } else { + return variableClass.newInstance(); + } + } catch (Exception ex) { + Factory.getLogger().error(ex); + return null; + } + } + + /** + * 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; + } +} + Propchange: struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultCallVariableRegistrar.java ------------------------------------------------------------------------------ svn:executable = * Added: struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultStaticVariableRegistrar.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultStaticVariableRegistrar.java?view=auto&rev=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultStaticVariableRegistrar.java (added) +++ struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultStaticVariableRegistrar.java Sun Apr 10 19:50:40 2005 @@ -0,0 +1,86 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts.flow.core; + +import org.mozilla.javascript.Scriptable; +import org.apache.commons.chain.Context; + +/** + * Defines a variable registrar used to define a static variable in the global + * scope. Static variables are defined once per global scope, while + * call-specific variables can define instances of themselves for every script + * call. + */ +public class DefaultStaticVariableRegistrar implements VariableRegistrar { + + private String variableName; + private Object variable; + + public DefaultStaticVariableRegistrar(String variableName, Object var) { + this.variableName = variableName; + this.variable = var; + } + + /** + * Gets the Class object for the variable + * + [EMAIL PROTECTED] The classInstance value + */ + public Class getClassInstance() { + return variable.getClass(); + } + + /** + * Gets the name of the variable class + * + [EMAIL PROTECTED] The className value + */ + public String getClassName() { + ; + return variable.getClass().getName(); + } + + /** + * Gets an instance of the variable + * + [EMAIL PROTECTED] scope The scope the variable will be placed in + [EMAIL PROTECTED] ctx The commons chain context for the call, null if defining a + * static variable + [EMAIL PROTECTED] The instance value + */ + public Object getInstance(Scriptable scope, Context ctx) { + return variable; + } + + /** + * 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 false; + } +} + Propchange: struts/flow/trunk/src/java/org/apache/struts/flow/core/DefaultStaticVariableRegistrar.java ------------------------------------------------------------------------------ svn:executable = * Copied: struts/flow/trunk/src/java/org/apache/struts/flow/core/JSFlow.java (from r160805, struts/flow/trunk/src/java/org/apache/struts/flow/core/JSContext.java) URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/core/JSFlow.java?view=diff&rev=160820&p1=struts/flow/trunk/src/java/org/apache/struts/flow/core/JSContext.java&r1=160805&p2=struts/flow/trunk/src/java/org/apache/struts/flow/core/JSFlow.java&r2=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/core/JSContext.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/core/JSFlow.java Sun Apr 10 19:50:40 2005 @@ -44,9 +44,9 @@ [EMAIL PROTECTED] <a href="mailto:[EMAIL PROTECTED]">Ovidiu Predescu</a> [EMAIL PROTECTED] <a href="[EMAIL PROTECTED]">Dave Johnson</a> [EMAIL PROTECTED] March 16, 2002 - [EMAIL PROTECTED] CVS $Id: JSContext.java,v 1.3 2004/11/05 05:12:10 mrdon Exp $ + [EMAIL PROTECTED] CVS $Id: JSFlow.java,v 1.3 2004/11/05 05:12:10 mrdon Exp $ */ -public class JSContext extends ScriptableObject { +public class JSFlow extends ScriptableObject { /** The interpreter */ private JavaScriptInterpreter interpreter; /** The parameters passed by the Flow container */ @@ -58,8 +58,8 @@ private ThreadLocal contextTls = new ThreadLocal(); - /** Constructor for the JSContext object */ - public JSContext() { + /** Constructor for the JSFlow object */ + public JSFlow() { super(); } @@ -70,7 +70,7 @@ [EMAIL PROTECTED] The className value */ public String getClassName() { - return "context"; + return "flow"; } @@ -149,7 +149,7 @@ public Object jsFunction_resolveToPath(String filename) throws JavaScriptException { try { - return interpreter.calculatePath(jsGet_chainContext(), filename); + return interpreter.calculatePath(jsGet_context(), filename); } catch (JavaScriptException e) { throw e; } catch (Exception e) { @@ -253,7 +253,7 @@ * [EMAIL PROTECTED] The chain context */ - public org.apache.commons.chain.Context jsGet_chainContext() { + public org.apache.commons.chain.Context jsGet_context() { return (org.apache.commons.chain.Context) contextTls.get(); } Modified: struts/flow/trunk/src/java/org/apache/struts/flow/core/JSWebContinuation.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/core/JSWebContinuation.java?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/core/JSWebContinuation.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/core/JSWebContinuation.java Sun Apr 10 19:50:40 2005 @@ -32,7 +32,7 @@ public class JSWebContinuation extends ScriptableObject { private WebContinuation wk; private ContinuationsManager continuationsMgr; - private JSContext ctx = null; + private JSFlow ctx = null; /** Constructor for the JSWebContinuation object */ @@ -61,7 +61,7 @@ /** * Creates the JavaScript web continuation - [EMAIL PROTECTED] args Arguments: JSContext context, Object continuation, + [EMAIL PROTECTED] args Arguments: JSFlow context, Object continuation, * JSWebContinuation parent, Number timeToLive [EMAIL PROTECTED] cx The script context [EMAIL PROTECTED] ctorObj The function @@ -74,7 +74,7 @@ boolean inNewExpr) throws Exception { - JSContext ctx = (JSContext) args[0]; + JSFlow ctx = (JSFlow) args[0]; ContinuationsManager contMgr = Factory.getContinuationsManager(); Object kont = args[1]; @@ -149,7 +149,7 @@ * [EMAIL PROTECTED] The context value */ - public JSContext getContext() { + public JSFlow getContext() { return ctx; } Modified: struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java Sun Apr 10 19:50:40 2005 @@ -74,6 +74,8 @@ /** Shared global scope for scripts and other immutable objects */ private JSGlobal scope; + private List registrars = new ArrayList(); + /** * List of <code>String</code> objects that represent files to be read in * by the JavaScript interpreter. @@ -174,6 +176,24 @@ this.wrapFactory = wf; } + /** + * Adds a class that will register a global variable + * + * @param reg The variable registrar + */ + public void addVariableRegistrar(VariableRegistrar reg) { + registrars.add(reg); + } + + /** + * Removes a class that will register a global variable + * + * @param reg The variable registrar + */ + public void removeVariableRegistrar(VariableRegistrar reg) { + registrars.remove(reg); + } + /** * Sets the interval between when the script should be looked at to see if @@ -236,15 +256,20 @@ [EMAIL PROTECTED] context The context [EMAIL PROTECTED] scope The scope to initialize [EMAIL PROTECTED] IllegalAccessException If anything goes wrong - [EMAIL PROTECTED] PropertyException If anything goes wrong [EMAIL PROTECTED] InstantiationException If anything goes wrong [EMAIL PROTECTED] InvocationTargetException If anything goes wrong - [EMAIL PROTECTED] NotAFunctionException If anything goes wrong - [EMAIL PROTECTED] ClassDefinitionException If anything goes wrong [EMAIL PROTECTED] JavaScriptException If anything goes wrong */ protected void initScope(Context context, JSGlobal 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()); + } + // Access to the log ScriptableObject.defineClass(scope, JSLog.class); @@ -252,7 +277,7 @@ ScriptableObject.defineClass(scope, JSWebContinuation.class); // Wrapper for commons-chain context - ScriptableObject.defineClass(scope, JSContext.class); + ScriptableObject.defineClass(scope, JSFlow.class); // Define some functions on the top level scope String[] names = {"print"}; @@ -268,6 +293,16 @@ Scriptable log = context.newObject(scope, "Log", args); ((JSLog) log).enableLogging(getLogger()); scope.put("log", scope, log); + + 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)); + } + } + + } @@ -450,10 +485,10 @@ context.setCompileFunctionsWithDynamicScope(true); // Obtain the JS continuation object from it, and setup the - // JSContext object associated in the dynamic scope of the saved + // JSFlow object associated in the dynamic scope of the saved // continuation with the environment and context objects. JSWebContinuation jswk = (JSWebContinuation) wk.getUserObject(); - JSContext ctx = jswk.getContext(); + JSFlow ctx = jswk.getContext(); final Scriptable kScope = ctx.getParentScope(chainCtx); if (enableDebugger) { getDebugger().setVisible(true); @@ -633,12 +668,27 @@ // Put in the thread scope the context object, which gives access // to the interpreter object and the chain context Object args[] = {}; - JSContext ctx = (JSContext) context.newObject( - thrScope, "context", args); + JSFlow ctx = (JSFlow) context.newObject( + thrScope, "flow", args); ctx.setInterpreter(this); ctx.setContext(chainCtx); ctx.setParentScope(thrScope); - thrScope.put("context", thrScope, ctx); + thrScope.put("flow", thrScope, ctx); + + VariableRegistrar reg; + for (Iterator i = registrars.iterator(); i.hasNext(); ) { + reg = (VariableRegistrar) i.next(); + if (reg.isCallSpecific()) { + WrapFactory factory = context.getWrapFactory(); + Object o = reg.getInstance(scope, chainCtx); + if (o instanceof Scriptable) { + scope.put(reg.getName(), thrScope, (Scriptable)o); + } else { + Scriptable var = factory.wrapAsJavaObject(context, thrScope, o, reg.getClassInstance()); + scope.put(reg.getName(), thrScope, var); + } + } + } } Added: 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/VariableRegistrar.java?view=auto&rev=160820 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java (added) +++ struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java Sun Apr 10 19:50:40 2005 @@ -0,0 +1,58 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts.flow.core; + +import org.mozilla.javascript.Scriptable; +import org.apache.commons.chain.Context; + +/** + * Defines a variable registrar used to define either a static or + * call-specific variable in the global scope. Static variables are defined + * 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(); + + /** + * Gets an instance of the variable + * + * @param scope The scope the variable will be placed in + * @param ctx The commons chain context for the call, null if defining + * a static variable + */ + public Object getInstance(Scriptable scope, Context ctx); + + /** + * Gets the variable name + */ + public String getName(); + + /** + * Returns whether this variable is call-specific or static + */ + public boolean isCallSpecific(); +} + Propchange: struts/flow/trunk/src/java/org/apache/struts/flow/core/VariableRegistrar.java ------------------------------------------------------------------------------ svn:executable = * Modified: struts/flow/trunk/src/java/system.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/system.js?view=diff&r1=160819&r2=160820 ============================================================================== --- struts/flow/trunk/src/java/system.js (original) +++ struts/flow/trunk/src/java/system.js Sun Apr 10 19:50:40 2005 @@ -7,7 +7,7 @@ // Use default ttl value from continuation manager var timeToLive = 0; - var kont = new WebContinuation(context, k, lastWebCont, timeToLive); + var kont = new WebContinuation(flow, k, lastWebCont, timeToLive); if (this.rootContinuation == null) { this.rootContinuation = kont; } @@ -29,7 +29,7 @@ lastContinuation = kont; // process the resulting request differently if a client-side call - func = context.get("func"); + func = flow.get("func"); if (func != null) { var ret; var callFunc = false; @@ -54,7 +54,7 @@ log.warn("Invalid remote function attempt:"+func); } else { var json; - eval("json = "+context.get("json")); + eval("json = "+flow.get("json")); ret = this[func].apply(this, json); } kont = _forwardAndWait("n/a", ret); @@ -70,11 +70,11 @@ // Use default ttl value from continuation manager var timeToLive = 0; - var kont = new WebContinuation(context, k, lastContinuation, timeToLive); + var kont = new WebContinuation(flow, k, lastContinuation, timeToLive); - context.put(Constants.FORWARD_NAME_KEY, name); - context.put(Constants.CONTINUATION_ID_KEY, kont.id); - context.put(Constants.BIZ_DATA_KEY, atts); + flow.put(Packages.org.apache.struts.flow.Constants.FORWARD_NAME_KEY, name); + flow.put(Packages.org.apache.struts.flow.Constants.CONTINUATION_ID_KEY, kont.id); + flow.put(Packages.org.apache.struts.flow.Constants.BIZ_DATA_KEY, atts); suicide(); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]