KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > javascript > fom > FOM_WebContinuation


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.flow.javascript.fom;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.avalon.framework.service.ServiceManager;
22 import org.apache.cocoon.components.flow.ContinuationsManager;
23 import org.apache.cocoon.components.flow.WebContinuation;
24 import org.mozilla.javascript.Context;
25 import org.mozilla.javascript.Function;
26 import org.mozilla.javascript.NativeArray;
27 import org.mozilla.javascript.Scriptable;
28 import org.mozilla.javascript.ScriptableObject;
29 import org.mozilla.javascript.Undefined;
30 import org.mozilla.javascript.Wrapper;
31 import org.mozilla.javascript.continuations.Continuation;
32
33 /**
34  *
35  * @version CVS $Id: FOM_WebContinuation.java 292159 2005-09-28 10:33:42Z sylvain $
36  */

37 public class FOM_WebContinuation extends ScriptableObject {
38
39     WebContinuation wk;
40
41
42     static class UserObject {
43         boolean isBookmark;
44         PageLocalScopeImpl pageLocal;
45     }
46
47     static private boolean isBookmark(WebContinuation wk) {
48         UserObject userObj = (UserObject)wk.getUserObject();
49         if (userObj == null) {
50             return false;
51         }
52         return userObj.isBookmark;
53     }
54
55     public FOM_WebContinuation() {
56         this(null);
57     }
58
59
60     public FOM_WebContinuation(WebContinuation wk) {
61         this.wk = wk;
62     }
63
64     // new FOM_WebContinuation([Continuation] continuation,
65
// [FOM_WebContinuation] parent,
66
// [Number] timeToLive)
67
public static Object JavaDoc jsConstructor(Context cx, Object JavaDoc[] args,
68                                        Function ctorObj,
69                                        boolean inNewExpr)
70         throws Exception JavaDoc {
71         FOM_WebContinuation result = null;
72         if (args.length < 1) {
73             // error
74
}
75         Continuation c = (Continuation)unwrap(args[0]);
76         FOM_WebContinuation parent = null;
77         if (args.length > 1) {
78             parent = (FOM_WebContinuation)args[1];
79         }
80         int timeToLive = 0;
81         if (args.length > 2) {
82             timeToLive =
83                 (int)org.mozilla.javascript.Context.toNumber(args[2]);
84         }
85         WebContinuation wk;
86         Scriptable scope = getTopLevelScope(c);
87         FOM_Cocoon cocoon = (FOM_Cocoon)getProperty(scope, "cocoon");
88         ServiceManager componentManager = cocoon.getServiceManager();
89         ContinuationsManager contMgr = (ContinuationsManager)
90             componentManager.lookup(ContinuationsManager.ROLE);
91         wk = contMgr.createWebContinuation(c,
92                                            (parent == null ? null : parent.getWebContinuation()),
93                                            timeToLive,
94                                            cocoon.getInterpreterId(),
95                                            null);
96         result = new FOM_WebContinuation(wk);
97         result.setParentScope(getTopLevelScope(scope));
98         result.setPrototype(getClassPrototype(scope, result.getClassName()));
99         return result;
100     }
101
102     public String JavaDoc getClassName() {
103         return "FOM_WebContinuation";
104     }
105
106     public Object JavaDoc jsFunction_getAttribute(String JavaDoc name) {
107         return org.mozilla.javascript.Context.toObject(
108                 wk.getAttribute(name),
109                 getParentScope());
110     }
111
112     public void jsFunction_setAttribute(String JavaDoc name, Object JavaDoc value) {
113         wk.setAttribute(name, unwrap(value));
114     }
115
116     public void jsFunction_removeAttribute(String JavaDoc name) {
117         wk.removeAttribute(name);
118     }
119
120     public Object JavaDoc jsFunction_getAttributeNames() {
121         return org.mozilla.javascript.Context.toObject(
122                 wk.getAttributeNames(),
123                 getParentScope());
124     }
125
126     public String JavaDoc jsGet_id() {
127         return wk.getId();
128     }
129
130
131     public Continuation jsGet_continuation() {
132         return (Continuation)wk.getContinuation();
133     }
134
135     public FOM_WebContinuation jsFunction_getParent() {
136         WebContinuation parent = wk.getParentContinuation();
137         if (parent == null) {
138             return null;
139         }
140
141         FOM_WebContinuation pwk = new FOM_WebContinuation(parent);
142         pwk.setParentScope(getParentScope());
143         pwk.setPrototype(getClassPrototype(getParentScope(),
144                                            pwk.getClassName()));
145         return pwk;
146     }
147
148     public NativeArray jsFunction_getChildren() throws Exception JavaDoc {
149         List JavaDoc list = wk.getChildren();
150         NativeArray arr =
151             (NativeArray)org.mozilla.javascript.Context.getCurrentContext().newObject(getParentScope(),
152                                                                                       "Array",
153                                                                                       new Object JavaDoc[]{new Integer JavaDoc(list.size())});
154         Iterator JavaDoc iter = list.iterator();
155         for (int i = 0; iter.hasNext(); i++) {
156             WebContinuation child = (WebContinuation)iter.next();
157             FOM_WebContinuation cwk = new FOM_WebContinuation(child);
158             cwk.setParentScope(getParentScope());
159             cwk.setPrototype(getClassPrototype(getParentScope(),
160                                                cwk.getClassName()));
161             arr.put(i, arr, cwk);
162         }
163         return arr;
164     }
165
166     public void jsFunction_invalidate() throws Exception JavaDoc {
167         ContinuationsManager contMgr = null;
168         FOM_Cocoon cocoon =
169             (FOM_Cocoon)getProperty(getTopLevelScope(this), "cocoon");
170         ServiceManager componentManager = cocoon.getServiceManager();
171         contMgr = (ContinuationsManager)
172             componentManager.lookup(ContinuationsManager.ROLE);
173         contMgr.invalidateWebContinuation(wk);
174     }
175
176     public void jsFunction_display() {
177         wk.display();
178     }
179
180     public WebContinuation getWebContinuation() {
181         return wk;
182     }
183
184     private static Object JavaDoc unwrap(Object JavaDoc obj) {
185         if (obj instanceof Wrapper) {
186             obj = ((Wrapper)obj).unwrap();
187         } else if (obj == Undefined.instance) {
188             obj = null;
189         }
190         return obj;
191     }
192
193     PageLocalScopeImpl getPageLocal() {
194         UserObject userObj = (UserObject)wk.getUserObject();
195         if (userObj == null) return null;
196         return userObj.pageLocal;
197     }
198
199     void setPageLocal(PageLocalScopeImpl pageLocal) {
200         UserObject userObj = (UserObject)wk.getUserObject();
201         if (userObj == null) {
202             userObj = new UserObject();
203             wk.setUserObject(userObj);
204         }
205         userObj.pageLocal = pageLocal;
206     }
207
208     public void jsFunction_setBookmark(boolean value) {
209         UserObject userObj = (UserObject)wk.getUserObject();
210         if (userObj == null) {
211             userObj = new UserObject();
212             wk.setUserObject(userObj);
213         }
214         userObj.isBookmark = value;
215     }
216
217     public boolean jsGet_bookmark() {
218         return isBookmark(wk);
219     }
220
221     public boolean jsFunction_isBookmark() {
222         return isBookmark(wk);
223     }
224
225     public FOM_WebContinuation jsGet_previousBookmark() {
226         WebContinuation c = wk.getParentContinuation();
227         if (c == null) {
228             return null;
229         }
230
231         // If this is a continuation of sendPageAndWait()
232
// and the immediate parent is a bookmark, then
233
// it is the bookmark for this page, so skip it.
234
if (!isBookmark(wk) && isBookmark(c)) {
235             c = c.getParentContinuation();
236         }
237         while (c != null && !isBookmark(c)) {
238             c = c.getParentContinuation();
239         }
240         if (c == null) {
241             return null;
242         }
243
244         FOM_WebContinuation pwk = new FOM_WebContinuation(c);
245         pwk.setParentScope(getParentScope());
246         pwk.setPrototype(getClassPrototype(getParentScope(), pwk.getClassName()));
247         return pwk;
248     }
249
250     /**
251      * Return text representation of the WebContinuation.
252      */

253     public String JavaDoc toString() {
254         return "WC" + wk.getId();
255     }
256 }
257
Popular Tags