001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.flow.javascript.fom;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.avalon.framework.service.ServiceManager;
023: import org.apache.cocoon.components.flow.ContinuationsManager;
024: import org.apache.cocoon.components.flow.WebContinuation;
025: import org.mozilla.javascript.Context;
026: import org.mozilla.javascript.Function;
027: import org.mozilla.javascript.NativeArray;
028: import org.mozilla.javascript.Scriptable;
029: import org.mozilla.javascript.ScriptableObject;
030: import org.mozilla.javascript.Undefined;
031: import org.mozilla.javascript.Wrapper;
032: import org.mozilla.javascript.continuations.Continuation;
033:
034: /**
035: *
036: * @version CVS $Id: FOM_WebContinuation.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public class FOM_WebContinuation extends ScriptableObject {
039:
040: WebContinuation wk;
041:
042: static class UserObject {
043: boolean isBookmark;
044: PageLocalScopeImpl pageLocal;
045: }
046:
047: static private boolean isBookmark(WebContinuation wk) {
048: UserObject userObj = (UserObject) wk.getUserObject();
049: if (userObj == null) {
050: return false;
051: }
052: return userObj.isBookmark;
053: }
054:
055: public FOM_WebContinuation() {
056: this (null);
057: }
058:
059: public FOM_WebContinuation(WebContinuation wk) {
060: this .wk = wk;
061: }
062:
063: // new FOM_WebContinuation([Continuation] continuation,
064: // [FOM_WebContinuation] parent,
065: // [Number] timeToLive)
066: public static Object jsConstructor(Context cx, Object[] args,
067: Function ctorObj, boolean inNewExpr) throws Exception {
068: FOM_WebContinuation result = null;
069: if (args.length < 1) {
070: // error
071: }
072: Continuation c = (Continuation) unwrap(args[0]);
073: FOM_WebContinuation parent = null;
074: if (args.length > 1) {
075: parent = (FOM_WebContinuation) args[1];
076: }
077: int timeToLive = 0;
078: if (args.length > 2) {
079: timeToLive = (int) org.mozilla.javascript.Context
080: .toNumber(args[2]);
081: }
082: WebContinuation wk;
083: Scriptable scope = getTopLevelScope(c);
084: FOM_Cocoon cocoon = (FOM_Cocoon) getProperty(scope, "cocoon");
085: ServiceManager componentManager = cocoon.getServiceManager();
086: ContinuationsManager contMgr = (ContinuationsManager) componentManager
087: .lookup(ContinuationsManager.ROLE);
088: wk = contMgr.createWebContinuation(c, (parent == null ? null
089: : parent.getWebContinuation()), timeToLive, cocoon
090: .getInterpreterId(), null);
091: result = new FOM_WebContinuation(wk);
092: result.setParentScope(getTopLevelScope(scope));
093: result.setPrototype(getClassPrototype(scope, result
094: .getClassName()));
095: return result;
096: }
097:
098: public String getClassName() {
099: return "FOM_WebContinuation";
100: }
101:
102: public Object jsFunction_getAttribute(String name) {
103: return org.mozilla.javascript.Context.toObject(wk
104: .getAttribute(name), getParentScope());
105: }
106:
107: public void jsFunction_setAttribute(String name, Object value) {
108: wk.setAttribute(name, unwrap(value));
109: }
110:
111: public void jsFunction_removeAttribute(String name) {
112: wk.removeAttribute(name);
113: }
114:
115: public Object jsFunction_getAttributeNames() {
116: return org.mozilla.javascript.Context.toObject(wk
117: .getAttributeNames(), getParentScope());
118: }
119:
120: public String jsGet_id() {
121: return wk.getId();
122: }
123:
124: public Continuation jsGet_continuation() {
125: return (Continuation) wk.getContinuation();
126: }
127:
128: public FOM_WebContinuation jsFunction_getParent() {
129: WebContinuation parent = wk.getParentContinuation();
130: if (parent == null) {
131: return null;
132: }
133:
134: FOM_WebContinuation pwk = new FOM_WebContinuation(parent);
135: pwk.setParentScope(getParentScope());
136: pwk.setPrototype(getClassPrototype(getParentScope(), pwk
137: .getClassName()));
138: return pwk;
139: }
140:
141: public NativeArray jsFunction_getChildren() throws Exception {
142: List list = wk.getChildren();
143: NativeArray arr = (NativeArray) org.mozilla.javascript.Context
144: .getCurrentContext().newObject(getParentScope(),
145: "Array",
146: new Object[] { new Integer(list.size()) });
147: Iterator iter = list.iterator();
148: for (int i = 0; iter.hasNext(); i++) {
149: WebContinuation child = (WebContinuation) iter.next();
150: FOM_WebContinuation cwk = new FOM_WebContinuation(child);
151: cwk.setParentScope(getParentScope());
152: cwk.setPrototype(getClassPrototype(getParentScope(), cwk
153: .getClassName()));
154: arr.put(i, arr, cwk);
155: }
156: return arr;
157: }
158:
159: public void jsFunction_invalidate() throws Exception {
160: ContinuationsManager contMgr = null;
161: FOM_Cocoon cocoon = (FOM_Cocoon) getProperty(
162: getTopLevelScope(this ), "cocoon");
163: ServiceManager componentManager = cocoon.getServiceManager();
164: contMgr = (ContinuationsManager) componentManager
165: .lookup(ContinuationsManager.ROLE);
166: contMgr.invalidateWebContinuation(wk);
167: }
168:
169: public void jsFunction_display() {
170: wk.display();
171: }
172:
173: public WebContinuation getWebContinuation() {
174: return wk;
175: }
176:
177: private static Object unwrap(Object obj) {
178: if (obj instanceof Wrapper) {
179: obj = ((Wrapper) obj).unwrap();
180: } else if (obj == Undefined.instance) {
181: obj = null;
182: }
183: return obj;
184: }
185:
186: PageLocalScopeImpl getPageLocal() {
187: UserObject userObj = (UserObject) wk.getUserObject();
188: if (userObj == null)
189: return null;
190: return userObj.pageLocal;
191: }
192:
193: void setPageLocal(PageLocalScopeImpl pageLocal) {
194: UserObject userObj = (UserObject) wk.getUserObject();
195: if (userObj == null) {
196: userObj = new UserObject();
197: wk.setUserObject(userObj);
198: }
199: userObj.pageLocal = pageLocal;
200: }
201:
202: public void jsFunction_setBookmark(boolean value) {
203: UserObject userObj = (UserObject) wk.getUserObject();
204: if (userObj == null) {
205: userObj = new UserObject();
206: wk.setUserObject(userObj);
207: }
208: userObj.isBookmark = value;
209: }
210:
211: public boolean jsGet_bookmark() {
212: return isBookmark(wk);
213: }
214:
215: public boolean jsFunction_isBookmark() {
216: return isBookmark(wk);
217: }
218:
219: public FOM_WebContinuation jsGet_previousBookmark() {
220: WebContinuation c = wk.getParentContinuation();
221: if (c == null) {
222: return null;
223: }
224:
225: // If this is a continuation of sendPageAndWait()
226: // and the immediate parent is a bookmark, then
227: // it is the bookmark for this page, so skip it.
228: if (!isBookmark(wk) && isBookmark(c)) {
229: c = c.getParentContinuation();
230: }
231: while (c != null && !isBookmark(c)) {
232: c = c.getParentContinuation();
233: }
234: if (c == null) {
235: return null;
236: }
237:
238: FOM_WebContinuation pwk = new FOM_WebContinuation(c);
239: pwk.setParentScope(getParentScope());
240: pwk.setPrototype(getClassPrototype(getParentScope(), pwk
241: .getClassName()));
242: return pwk;
243: }
244:
245: /**
246: * Return text representation of the WebContinuation.
247: */
248: public String toString() {
249: return "WC" + wk.getId();
250: }
251: }
|