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 2003/07/02 19:05:38 UTC
cvs commit: cocoon-2.1/src/scratchpad/src/org/apache/cocoon/components/flow/javascript/fom FOM_Cocoon.java FOM_JavaScriptInterpreter.java
coliver 2003/07/02 10:05:37
Modified: src/scratchpad/src/org/apache/cocoon/components/flow/javascript/fom
FOM_Cocoon.java FOM_JavaScriptInterpreter.java
Log:
Added code to synchronize on thread scope and added support for the JS 'in' operator
Revision Changes Path
1.8 +58 -23 cocoon-2.1/src/scratchpad/src/org/apache/cocoon/components/flow/javascript/fom/FOM_Cocoon.java
Index: FOM_Cocoon.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/scratchpad/src/org/apache/cocoon/components/flow/javascript/fom/FOM_Cocoon.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- FOM_Cocoon.java 30 Jun 2003 19:11:10 -0000 1.7
+++ FOM_Cocoon.java 2 Jul 2003 17:05:37 -0000 1.8
@@ -52,6 +52,9 @@
import java.io.OutputStream;
import java.util.Map;
+import java.util.List;
+import java.util.LinkedList;
+import java.util.Enumeration;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
@@ -206,10 +209,10 @@
public Object jsFunction_getComponent( String id ) {
Object o = null;
try {
- o = this.componentManager.lookup( id );
- } catch (ComponentException e) {
- o = null;
- }
+ o = this.componentManager.lookup( id );
+ } catch (ComponentException e) {
+ o = null;
+ }
return o;
}
@@ -219,14 +222,11 @@
* @param component - an <code>Object</code> that is an instance
* of <code>org.apache.avalon.framework.component.Component</code>
*/
- public void jsFunction_releaseComponent( Object component )
- throws JavaScriptException {
+ public void jsFunction_releaseComponent( Object component ) throws Exception {
try {
- this.componentManager.release( (Component) component );
+ this.componentManager.release( (Component) unwrap(component) );
} catch( ClassCastException cce ) {
throw new JavaScriptException( "Only components can be released!" );
- } catch( Exception e ) {
- throw new JavaScriptException( "Error during release of component occurred!" + e.getMessage() );
}
}
@@ -238,20 +238,14 @@
* @exception JavaScriptException if an error occurs
*/
public Object jsFunction_load( String filename )
- throws JavaScriptException {
+ throws Exception {
org.mozilla.javascript.Context cx =
org.mozilla.javascript.Context.getCurrentContext();
- try {
- Scriptable scope = getParentScope();
- Script script = interpreter.compileScript( cx,
- environment,
- filename );
- return script.exec( cx, scope );
- } catch( JavaScriptException e ) {
- throw e;
- } catch( Exception e ) {
- throw new JavaScriptException( e );
- }
+ Scriptable scope = getParentScope();
+ Script script = interpreter.compileScript( cx,
+ environment,
+ filename );
+ return script.exec( cx, scope );
}
public static class FOM_Request extends ScriptableObject {
@@ -284,7 +278,6 @@
public void jsFunction_setAttribute(String name,
Object value) {
-
request.setAttribute(name, unwrap(value));
}
@@ -299,6 +292,20 @@
return result;
}
+ public Object[] getIds() {
+ if (request != null) {
+ List list = new LinkedList();
+ Enumeration e = request.getAttributeNames();
+ while (e.hasMoreElements()) {
+ list.add(e.nextElement());
+ }
+ Object[] result = new Object[list.size()];
+ list.toArray(result);
+ return result;
+ }
+ return super.getIds();
+ }
+
public String jsFunction_getCharacterEncoding() {
return request.getCharacterEncoding();
}
@@ -476,6 +483,20 @@
return "FOM_Session";
}
+ public Object[] getIds() {
+ if (session != null) {
+ List list = new LinkedList();
+ Enumeration e = session.getAttributeNames();
+ while (e.hasMoreElements()) {
+ list.add(e.nextElement());
+ }
+ Object[] result = new Object[list.size()];
+ list.toArray(result);
+ return result;
+ }
+ return super.getIds();
+ }
+
public Object get(String name, Scriptable start) {
Object result = super.get(name, start);
if (result == NOT_FOUND && session != null) {
@@ -566,6 +587,20 @@
public Object jsFunction_getInitParameter(String name) {
return context.getInitParameter(name);
+ }
+
+ public Object[] getIds() {
+ if (context != null) {
+ List list = new LinkedList();
+ Enumeration e = context.getAttributeNames();
+ while (e.hasMoreElements()) {
+ list.add(e.nextElement());
+ }
+ Object[] result = new Object[list.size()];
+ list.toArray(result);
+ return result;
+ }
+ return super.getIds();
}
public Object get(String name, Scriptable start) {
1.5 +114 -127 cocoon-2.1/src/scratchpad/src/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java
Index: FOM_JavaScriptInterpreter.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/scratchpad/src/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FOM_JavaScriptInterpreter.java 24 Jun 2003 16:59:20 -0000 1.4
+++ FOM_JavaScriptInterpreter.java 2 Jul 2003 17:05:37 -0000 1.5
@@ -345,17 +345,10 @@
* @return a <code>Scriptable</code> value
* @exception Exception if an error occurs
*/
- protected Scriptable enterContext(Environment environment)
- throws Exception
- {
- Context context = Context.enter();
- context.setOptimizationLevel(OPTIMIZATION_LEVEL);
- context.setGeneratingDebug(true);
- context.setCompileFunctionsWithDynamicScope(true);
- context.setErrorReporter(errorReporter);
- Scriptable thrScope = null;
-
-
+ private void setupContext(Environment environment,
+ org.mozilla.javascript.Context context,
+ Scriptable thrScope)
+ throws Exception {
// Try to retrieve the scope object from the session instance. If
// no scope is found, we create a new one, but don't place it in
// the session.
@@ -365,7 +358,7 @@
// the session object, where it's later retrieved from here. This
// behaviour allows multiple JavaScript functions to share the
// same global scope.
- thrScope = getSessionScope(environment);
+
FOM_Cocoon cocoon = (FOM_Cocoon)thrScope.get("cocoon", thrScope);
long lastExecTime = ((Long)thrScope.get(LAST_EXEC_TIME,
thrScope)).longValue();
@@ -425,26 +418,9 @@
}
}
}
- return thrScope;
}
/**
- * Remove the Cocoon object from the JavaScript thread scope so it
- * can be garbage collected, together with all the objects it
- * contains.
- */
- protected void exitContext(Scriptable thrScope)
- {
- // thrScope may be null if an exception occurred compiling a script
- if (thrScope != null) {
- FOM_Cocoon cocoon = (FOM_Cocoon)thrScope.get("cocoon", thrScope);
- cocoon.invalidate();
- }
- Context.exit();
- }
-
-
- /**
* Compile filename as JavaScript code
* @param cx Rhino context
* @param environment source resolver
@@ -500,60 +476,67 @@
Environment environment)
throws Exception
{
- Scriptable thrScope = null;
- try {
- thrScope = enterContext(environment);
-
- Context context = Context.getCurrentContext();
- FOM_Cocoon cocoon = (FOM_Cocoon)thrScope.get("cocoon", thrScope);
- if (enableDebugger) {
- if (!getDebugger().isVisible()) {
- // only raise the debugger window if it isn't already visible
- getDebugger().setVisible(true);
+ Context context = Context.enter();
+ context.setOptimizationLevel(OPTIMIZATION_LEVEL);
+ context.setGeneratingDebug(true);
+ context.setCompileFunctionsWithDynamicScope(true);
+ context.setErrorReporter(errorReporter);
+ FOM_Cocoon cocoon = null;
+ Scriptable thrScope = getSessionScope(environment);
+ synchronized (thrScope) {
+ try {
+ setupContext(environment, context, thrScope);
+ cocoon = (FOM_Cocoon)thrScope.get("cocoon", thrScope);
+ if (enableDebugger) {
+ if (!getDebugger().isVisible()) {
+ // only raise the debugger window if it isn't already visible
+ getDebugger().setVisible(true);
+ }
}
- }
- int size = (params != null ? params.size() : 0);
- Object[] funArgs = new Object[size];
- NativeArray parameters = new NativeArray(size);
- if (size != 0) {
- for (int i = 0; i < size; i++) {
- Interpreter.Argument arg = (Interpreter.Argument)params.get(i);
- funArgs[i] = arg.value;
- if (arg.name == null) arg.name = "";
- parameters.put(arg.name, parameters, arg.value);
+ int size = (params != null ? params.size() : 0);
+ Object[] funArgs = new Object[size];
+ NativeArray parameters = new NativeArray(size);
+ if (size != 0) {
+ for (int i = 0; i < size; i++) {
+ Interpreter.Argument arg = (Interpreter.Argument)params.get(i);
+ funArgs[i] = arg.value;
+ if (arg.name == null) arg.name = "";
+ parameters.put(arg.name, parameters, arg.value);
+ }
}
+ //cocoon.setParameters(parameters);
+ Object fun = ScriptableObject.getProperty(thrScope, funName);
+ if (fun == Scriptable.NOT_FOUND) {
+ fun = funName; // this will produce a better error message
+ }
+ ScriptRuntime.call(context, fun, thrScope,
+ funArgs, thrScope);
+ } catch (JavaScriptException ex) {
+ EvaluatorException ee =
+ Context.reportRuntimeError(ToolErrorReporter.getMessage("msg.uncaughtJSException",
+ ex.getMessage()));
+ Throwable unwrapped = unwrap(ex);
+ if (unwrapped instanceof ProcessingException) {
+ throw (ProcessingException)unwrapped;
+ }
+
+ throw new CascadingRuntimeException(ee.getMessage(), unwrapped);
+ } catch (EcmaError ee) {
+ String msg = ToolErrorReporter.getMessage("msg.uncaughtJSException", ee.toString());
+ if (ee.getSourceName() != null) {
+ Context.reportRuntimeError(msg,
+ ee.getSourceName(),
+ ee.getLineNumber(),
+ ee.getLineSource(),
+ ee.getColumnNumber());
+ } else {
+ Context.reportRuntimeError(msg);
+ }
+ throw new CascadingRuntimeException(ee.getMessage(), ee);
+ } finally {
+ cocoon.invalidate();
+ Context.exit();
}
- //cocoon.setParameters(parameters);
- Object fun = ScriptableObject.getProperty(thrScope, funName);
- if (fun == Scriptable.NOT_FOUND) {
- fun = funName; // this will produce a better error message
- }
- ScriptRuntime.call(context, fun, thrScope,
- funArgs, thrScope);
- } catch (JavaScriptException ex) {
- EvaluatorException ee =
- Context.reportRuntimeError(ToolErrorReporter.getMessage("msg.uncaughtJSException",
- ex.getMessage()));
- Throwable unwrapped = unwrap(ex);
- if (unwrapped instanceof ProcessingException) {
- throw (ProcessingException)unwrapped;
- }
-
- throw new CascadingRuntimeException(ee.getMessage(), unwrapped);
- } catch (EcmaError ee) {
- String msg = ToolErrorReporter.getMessage("msg.uncaughtJSException", ee.toString());
- if (ee.getSourceName() != null) {
- Context.reportRuntimeError(msg,
- ee.getSourceName(),
- ee.getLineNumber(),
- ee.getLineSource(),
- ee.getColumnNumber());
- } else {
- Context.reportRuntimeError(msg);
- }
- throw new CascadingRuntimeException(ee.getMessage(), ee);
- } finally {
- exitContext(thrScope);
}
}
@@ -582,49 +565,51 @@
// continuation with the environment and context objects.
Continuation k = (Continuation)wk.getContinuation();
Scriptable kScope = k.getParentScope();
- FOM_Cocoon cocoon = (FOM_Cocoon)kScope.get("cocoon", kScope);
- cocoon.setup(this, environment, manager);
- if (enableDebugger) {
- getDebugger().setVisible(true);
- }
- //int size = (params != null ? params.size() : 0);
- //NativeArray parameters = new NativeArray(size);
-
- //if (size != 0) {
- //for (int i = 0; i < size; i++) {
- //Interpreter.Argument arg = (Interpreter.Argument)params.get(i);
- //parameters.put(arg.name, parameters, arg.value);
- //}
- //}
- //cocoon.setParameters(parameters);
- Object[] args = new Object[] {k};
- try {
- ScriptableObject.callMethod(cocoon, "handleContinuation", args);
- } catch (JavaScriptException ex) {
- EvaluatorException ee =
- Context.reportRuntimeError(ToolErrorReporter.getMessage("msg.uncaughtJSException",
- ex.getMessage()));
- Throwable unwrapped = unwrap(ex);
- if (unwrapped instanceof ProcessingException) {
- throw (ProcessingException)unwrapped;
+ synchronized (kScope) {
+ FOM_Cocoon cocoon = (FOM_Cocoon)kScope.get("cocoon", kScope);
+ cocoon.setup(this, environment, manager);
+ if (enableDebugger) {
+ getDebugger().setVisible(true);
}
-
- throw new CascadingRuntimeException(ee.getMessage(), unwrapped);
- } catch (EcmaError ee) {
- String msg = ToolErrorReporter.getMessage("msg.uncaughtJSException", ee.toString());
- if (ee.getSourceName() != null) {
- Context.reportRuntimeError(msg,
- ee.getSourceName(),
- ee.getLineNumber(),
- ee.getLineSource(),
- ee.getColumnNumber());
- } else {
- Context.reportRuntimeError(msg);
+ //int size = (params != null ? params.size() : 0);
+ //NativeArray parameters = new NativeArray(size);
+
+ //if (size != 0) {
+ //for (int i = 0; i < size; i++) {
+ //Interpreter.Argument arg = (Interpreter.Argument)params.get(i);
+ //parameters.put(arg.name, parameters, arg.value);
+ //}
+ //}
+ //cocoon.setParameters(parameters);
+ Object[] args = new Object[] {k};
+ try {
+ ScriptableObject.callMethod(cocoon, "handleContinuation", args);
+ } catch (JavaScriptException ex) {
+ EvaluatorException ee =
+ Context.reportRuntimeError(ToolErrorReporter.getMessage("msg.uncaughtJSException",
+ ex.getMessage()));
+ Throwable unwrapped = unwrap(ex);
+ if (unwrapped instanceof ProcessingException) {
+ throw (ProcessingException)unwrapped;
+ }
+
+ throw new CascadingRuntimeException(ee.getMessage(), unwrapped);
+ } catch (EcmaError ee) {
+ String msg = ToolErrorReporter.getMessage("msg.uncaughtJSException", ee.toString());
+ if (ee.getSourceName() != null) {
+ Context.reportRuntimeError(msg,
+ ee.getSourceName(),
+ ee.getLineNumber(),
+ ee.getLineSource(),
+ ee.getColumnNumber());
+ } else {
+ Context.reportRuntimeError(msg);
+ }
+ throw new CascadingRuntimeException(ee.getMessage(), ee);
+ } finally {
+ cocoon.invalidate();
+ Context.exit();
}
- throw new CascadingRuntimeException(ee.getMessage(), ee);
- } finally {
- cocoon.invalidate();
- Context.exit();
}
}
@@ -639,15 +624,17 @@
return e;
}
+ // package access as this is called by FOM_Cocoon
void forwardTo(Scriptable scope, FOM_Cocoon cocoon,
- String uri, Object bizData,
- WebContinuation continuation,
- Environment environment)
+ String uri, Object bizData,
+ WebContinuation continuation,
+ Environment environment)
throws Exception {
setupView(scope, cocoon ,environment);
super.forwardTo(uri, bizData, continuation, environment);
}
+ // package access as this is called by FOM_Cocoon
boolean process(Scriptable scope, FOM_Cocoon cocoon,
String uri, Object bizData,
OutputStream out, Environment environment)