You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by co...@apache.org on 2004/01/28 06:46:21 UTC
cvs commit: cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom FOM_Cocoon.java FOM_WebContinuation.java fom_system.js
coliver 2004/01/27 21:46:21
Modified: src/java/org/apache/cocoon/components/flow
AbstractInterpreter.java
src/java/org/apache/cocoon/components/flow/javascript/fom
FOM_Cocoon.java FOM_WebContinuation.java
fom_system.js
Log:
Added createPageLocal() and createWebContinuation()
Revision Changes Path
1.17 +7 -2 cocoon-2.1/src/java/org/apache/cocoon/components/flow/AbstractInterpreter.java
Index: AbstractInterpreter.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/flow/AbstractInterpreter.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- AbstractInterpreter.java 19 Jan 2004 13:05:11 -0000 1.16
+++ AbstractInterpreter.java 28 Jan 2004 05:46:21 -0000 1.17
@@ -70,6 +70,7 @@
import org.apache.cocoon.components.CocoonComponentManager;
import org.apache.cocoon.environment.Context;
import org.apache.cocoon.environment.Environment;
+import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.wrapper.EnvironmentWrapper;
import org.apache.excalibur.source.SourceUtil;
@@ -267,7 +268,11 @@
Map objectModel = environment.getObjectModel();
FlowHelper.setWebContinuation(objectModel, continuation);
FlowHelper.setContextObject(objectModel, bizData);
- PipelinesNode.getRedirector(environment).redirect(false, uri);
+ Redirector redirector = PipelinesNode.getRedirector(environment);
+ if (redirector.hasRedirected()) {
+ throw new IllegalStateException("Pipeline has already been processed for this request");
+ }
+ redirector.redirect(false, uri);
} else {
throw new Exception("uri is not allowed to contain a scheme (cocoon:/ is always automatically used)");
}
1.27 +83 -38 cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_Cocoon.java
Index: FOM_Cocoon.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_Cocoon.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- FOM_Cocoon.java 21 Jan 2004 14:31:25 -0000 1.26
+++ FOM_Cocoon.java 28 Jan 2004 05:46:21 -0000 1.27
@@ -115,6 +115,8 @@
Scriptable parameters;
FOM_Log log;
WebContinuation lastContinuation;
+ FOM_WebContinuation fwk;
+ PageLocalScopeImpl currentPageLocal;
public CallContext(CallContext caller,
FOM_JavaScriptInterpreter interp,
@@ -132,6 +134,34 @@
this.avalonContext = avalonContext;
this.logger = logger;
this.lastContinuation = lastContinuation;
+ if (lastContinuation != null) {
+ fwk = new FOM_WebContinuation(lastContinuation);
+ Scriptable scope = FOM_Cocoon.this.getParentScope();
+ fwk.setParentScope(scope);
+ fwk.setPrototype(getClassPrototype(scope, fwk.getClassName()));
+ this.currentPageLocal = fwk.getPageLocal();
+ }
+ if (this.currentPageLocal != null) {
+ // "clone" the page local scope
+ this.currentPageLocal = this.currentPageLocal.duplicate();
+ } else {
+ this.currentPageLocal = new PageLocalScopeImpl(getTopLevelScope(FOM_Cocoon.this));
+ }
+ pageLocal.setDelegate(this.currentPageLocal);
+ }
+
+ public FOM_WebContinuation getLastContinuation() {
+ return fwk;
+ }
+
+ public void setLastContinuation(FOM_WebContinuation fwk) {
+ this.fwk = fwk;
+ if (fwk != null) {
+ pageLocal.setDelegate(fwk.getPageLocal());
+ this.lastContinuation = fwk.getWebContinuation();
+ } else {
+ this.lastContinuation = null;
+ }
}
public FOM_Session getSession() {
@@ -205,11 +235,13 @@
}
private CallContext currentCall;
+ private PageLocalScopeHolder pageLocal;
public String getClassName() {
return "FOM_Cocoon";
}
+
// Called by FOM_JavaScriptInterpreter
static void init(Scriptable scope) throws Exception {
defineClass(scope, FOM_Cocoon.class);
@@ -220,6 +252,7 @@
defineClass(scope, FOM_Context.class);
defineClass(scope, FOM_Log.class);
defineClass(scope, FOM_WebContinuation.class);
+ defineClass(scope, PageLocalImpl.class);
}
void pushCallContext(FOM_JavaScriptInterpreter interp,
@@ -229,6 +262,9 @@
Context avalonContext,
Logger logger,
WebContinuation lastContinuation) {
+ if (pageLocal == null) {
+ pageLocal = new PageLocalScopeHolder(getTopLevelScope(this));
+ }
this.currentCall = new CallContext(currentCall, interp, env, manager,
serviceManager, avalonContext,
logger, lastContinuation);
@@ -241,49 +277,40 @@
request.removeAttribute(FOM_JavaScriptFlowHelper.FOM_SCOPE);
}
this.currentCall = this.currentCall.caller;
+ // reset current page locals
+ if (this.currentCall != null) {
+ pageLocal.setDelegate(this.currentCall.currentPageLocal);
+ } else {
+ pageLocal.setDelegate(null);
+ }
}
- private FOM_WebContinuation forwardTo(String uri, Object bizData,
- Continuation continuation)
- throws Exception {
- WebContinuation wk = null;
- if (continuation != null) {
- ContinuationsManager contMgr = (ContinuationsManager)
- getComponentManager().lookup(ContinuationsManager.ROLE);
- wk = contMgr.createWebContinuation(continuation,
- currentCall.lastContinuation,
- 0,
- null);
- }
-
- String redUri = uri;
-
- FOM_WebContinuation fom_wk =
- new FOM_WebContinuation(wk);
- fom_wk.setParentScope(getParentScope());
- fom_wk.setPrototype(getClassPrototype(getParentScope(),
- fom_wk.getClassName()));
- getInterpreter().forwardTo(getParentScope(), this, redUri,
- bizData, fom_wk, getEnvironment());
-
- FOM_WebContinuation result = null;
- if (wk != null) {
- result = new FOM_WebContinuation(wk);
- result.setParentScope(getParentScope());
- result.setPrototype(getClassPrototype(getParentScope(),
- result.getClassName()));
- }
- return result;
+ public FOM_WebContinuation jsGet_continuation() {
+ return currentCall.getLastContinuation();
+ }
+
+ public void jsSet_continuation(Object obj) {
+ FOM_WebContinuation fwk = (FOM_WebContinuation)unwrap(obj);
+ currentCall.setLastContinuation(fwk);
}
public FOM_WebContinuation jsFunction_sendPage(String uri,
Object obj,
- Object continuation)
+ Object wk)
throws Exception {
- return forwardTo(uri, unwrap(obj), (Continuation)unwrap(continuation));
+ FOM_WebContinuation fom_wk = (FOM_WebContinuation)unwrap(wk);
+ if (fom_wk != null) {
+ // save page locals
+ fom_wk.setPageLocal(pageLocal.getDelegate());
+ }
+ forwardTo(uri, unwrap(obj), fom_wk);
+ return fom_wk;
}
+ public Scriptable jsFunction_createPageLocal() {
+ return pageLocal.createPageLocal();
+ }
public void jsFunction_processPipelineTo(String uri,
Object map,
@@ -1106,10 +1133,7 @@
}
public void jsFunction_setAttribute(String name, Object value) {
- if (value instanceof NativeJavaObject) {
- value = ((NativeJavaObject) value).unwrap();
- }
- session.setAttribute(name, value);
+ session.setAttribute(name, unwrap(value));
}
public void jsFunction_removeAttribute(String name) {
@@ -1520,6 +1544,27 @@
list.add(arg);
}
getInterpreter().handleContinuation(kontId, list, getEnvironment());
+ }
+
+ /**
+ * Create a Bookmark WebContinuation from a JS Continuation with the last
+ * continuation of sendPageAndWait as its parent.
+ * PageLocal variables will be shared with the continuation of
+ * the next call to sendPageAndWait().
+ * @param k The JS continuation
+ * @param ttl Lifetime for this continuation (zero means no limit)
+ */
+ public FOM_WebContinuation jsFunction_makeWebContinuation(Object k,
+ Object ttl)
+ throws Exception {
+ double d = org.mozilla.javascript.Context.toNumber(ttl);
+ FOM_WebContinuation result =
+ makeWebContinuation((Continuation)unwrap(k),
+ jsGet_continuation(),
+ (int)d);
+ result.setPageLocal(pageLocal.getDelegate());
+ currentCall.setLastContinuation(result);
+ return result;
}
/**
1.4 +128 -6 cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_WebContinuation.java
Index: FOM_WebContinuation.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_WebContinuation.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FOM_WebContinuation.java 22 Jan 2004 16:53:57 -0000 1.3
+++ FOM_WebContinuation.java 28 Jan 2004 05:46:21 -0000 1.4
@@ -56,9 +56,15 @@
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.cocoon.components.flow.ContinuationsManager;
import org.apache.cocoon.components.flow.WebContinuation;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeArray;
+import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.Wrapper;
+import org.mozilla.javascript.continuations.Continuation;
+
/**
*
@@ -68,14 +74,63 @@
WebContinuation wk;
+
+ static class UserObject {
+ boolean isBookmark;
+ PageLocalScopeImpl pageLocal;
+ }
+
+ static private boolean isBookmark(WebContinuation wk) {
+ UserObject userObj = (UserObject)wk.getUserObject();
+ if (userObj == null) {
+ return false;
+ }
+ return userObj.isBookmark;
+ }
+
public FOM_WebContinuation() {
}
- public FOM_WebContinuation(Object wk) {
- if (wk instanceof Wrapper) {
- wk = ((Wrapper)wk).unwrap();
+
+ public FOM_WebContinuation(WebContinuation wk) {
+ this.wk = wk;
+ }
+
+ // new FOM_WebContinuation([Continuation] continuation,
+ // [FOM_WebContinuation] parent,
+ // [Number] timeToLive)
+ public static Object jsConstructor(Context cx, Object[] args,
+ Function ctorObj,
+ boolean inNewExpr)
+ throws Exception {
+ FOM_WebContinuation result = null;
+ if (args.length < 1) {
+ // error
}
- this.wk = (WebContinuation)wk;
+ Continuation c = (Continuation)unwrap(args[0]);
+ FOM_WebContinuation parent = null;
+ if (args.length > 1) {
+ parent = (FOM_WebContinuation)args[1];
+ }
+ int timeToLive = 0;
+ if (args.length > 2) {
+ timeToLive =
+ (int)org.mozilla.javascript.Context.toNumber(args[2]);
+ }
+ WebContinuation wk;
+ Scriptable scope = getTopLevelScope(c);
+ FOM_Cocoon cocoon = (FOM_Cocoon)getProperty(scope, "cocoon");
+ ComponentManager componentManager = cocoon.getComponentManager();
+ ContinuationsManager contMgr = (ContinuationsManager)
+ componentManager.lookup(ContinuationsManager.ROLE);
+ wk = contMgr.createWebContinuation(c,
+ (parent == null ? null : parent.getWebContinuation()),
+ timeToLive,
+ null);
+ result = new FOM_WebContinuation(wk);
+ result.setParentScope(getTopLevelScope(scope));
+ result.setPrototype(getClassPrototype(scope, result.getClassName()));
+ return result;
}
public String getClassName() {
@@ -86,6 +141,11 @@
return wk.getId();
}
+
+ public Continuation jsGet_continuation() {
+ return (Continuation)wk.getContinuation();
+ }
+
public FOM_WebContinuation jsFunction_getParent() {
WebContinuation parent = wk.getParentContinuation();
if (parent == null) return null;
@@ -95,7 +155,7 @@
pwk.getClassName()));
return pwk;
}
-
+
public NativeArray jsFunction_getChildren() throws Exception {
List list = wk.getChildren();
NativeArray arr =
@@ -132,5 +192,67 @@
public WebContinuation getWebContinuation() {
return wk;
+ }
+
+ private static Object unwrap(Object obj) {
+ if (obj instanceof Wrapper) {
+ obj = ((Wrapper)obj).unwrap();
+ } else if (obj == Undefined.instance) {
+ obj = null;
+ }
+ return obj;
+ }
+
+ PageLocalScopeImpl getPageLocal() {
+ UserObject userObj = (UserObject)wk.getUserObject();
+ if (userObj == null) return null;
+ return userObj.pageLocal;
+ }
+
+ void setPageLocal(PageLocalScopeImpl pageLocal) {
+ UserObject userObj = (UserObject)wk.getUserObject();
+ if (userObj == null) {
+ userObj = new UserObject();
+ wk.setUserObject(userObj);
+ }
+ userObj.pageLocal = pageLocal;
+ }
+
+ public void jsFunction_setBookmark(boolean value) {
+ UserObject userObj = (UserObject)wk.getUserObject();
+ if (userObj == null) {
+ userObj = new UserObject();
+ wk.setUserObject(userObj);
+ }
+ userObj.isBookmark = value;
+ }
+
+ public boolean jsGet_bookmark() {
+ return isBookmark(wk);
+ }
+
+ public boolean jsFunction_isBookmark() {
+ return isBookmark(wk);
+ }
+
+ public FOM_WebContinuation jsGet_previousBookmark() {
+ WebContinuation c = wk.getParentContinuation();
+ if (c == null) return null;
+ // If this is a continuation of sendPageAndWait()
+ // and the immediate parent is a bookmark, then
+ // it is the bookmark for this page, so skip it.
+ if (!isBookmark(wk) && isBookmark(c)) {
+ c = c.getParentContinuation();
+ }
+ while (c != null && !isBookmark(c)) {
+ c = c.getParentContinuation();
+ }
+ if (c == null) return null;
+ FOM_WebContinuation pwk = new FOM_WebContinuation(c);
+ pwk.setParentScope(getParentScope());
+ pwk.setPrototype(getClassPrototype(getParentScope(),
+ pwk.getClassName()));
+ return pwk;
+
}
}
1.4 +17 -6 cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/fom_system.js
Index: fom_system.js
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/fom_system.js,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- fom_system.js 10 Jan 2004 16:53:43 -0000 1.3
+++ fom_system.js 28 Jan 2004 05:46:21 -0000 1.4
@@ -1,12 +1,14 @@
FOM_Cocoon.suicide = new Continuation();
-FOM_Cocoon.prototype.sendPageAndWait = function(uri, bizData, fun) {
- this.sendPage(uri, bizData, new Continuation());
+FOM_Cocoon.prototype.sendPageAndWait = function(uri, bizData, fun, ttl) {
+ this.sendPage(uri, bizData,
+ new FOM_WebContinuation(new Continuation(),
+ this.continuation, ttl));
if (fun) {
- if (!(fun instanceof Function)) {
- throw "Expected a function instead of: " + fun;
- }
- fun();
+ if (!(fun instanceof Function)) {
+ throw "Expected a function instead of: " + fun;
+ }
+ fun();
}
FOM_Cocoon.suicide();
}
@@ -14,3 +16,12 @@
FOM_Cocoon.prototype.handleContinuation = function(k, wk) {
k(wk);
}
+
+FOM_Cocoon.prototype.createWebContinuation = function(ttl) {
+ var wk = this.makeWebContinuation(new Continuation(), ttl);
+ wk.setBookmark(true);
+ return wk;
+}
+
+
+