1 16 package org.apache.cocoon.components.flow.javascript.fom; 17 18 import java.util.Iterator ; 19 import java.util.List ; 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 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 public static Object jsConstructor(Context cx, Object [] args, 68 Function ctorObj, 69 boolean inNewExpr) 70 throws Exception { 71 FOM_WebContinuation result = null; 72 if (args.length < 1) { 73 } 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 getClassName() { 103 return "FOM_WebContinuation"; 104 } 105 106 public Object jsFunction_getAttribute(String name) { 107 return org.mozilla.javascript.Context.toObject( 108 wk.getAttribute(name), 109 getParentScope()); 110 } 111 112 public void jsFunction_setAttribute(String name, Object value) { 113 wk.setAttribute(name, unwrap(value)); 114 } 115 116 public void jsFunction_removeAttribute(String name) { 117 wk.removeAttribute(name); 118 } 119 120 public Object jsFunction_getAttributeNames() { 121 return org.mozilla.javascript.Context.toObject( 122 wk.getAttributeNames(), 123 getParentScope()); 124 } 125 126 public String 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 { 149 List list = wk.getChildren(); 150 NativeArray arr = 151 (NativeArray)org.mozilla.javascript.Context.getCurrentContext().newObject(getParentScope(), 152 "Array", 153 new Object []{new Integer (list.size())}); 154 Iterator 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 { 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 unwrap(Object 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 (!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 253 public String toString() { 254 return "WC" + wk.getId(); 255 } 256 } 257 | Popular Tags |