| Method from org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptInterpreter Detail: |
public void callFunction(String funName,
List params,
Redirector redirector) throws Exception {
Context context = Context.enter();
context.setOptimizationLevel(OPTIMIZATION_LEVEL);
context.setGeneratingDebug(true);
context.setCompileFunctionsWithDynamicScope(true);
context.setErrorReporter(errorReporter);
LocationTrackingDebugger locationTracker = new LocationTrackingDebugger();
if (!enableDebugger) {
//FIXME: add a "tee" debugger that allows both to be used simultaneously
context.setDebugger(locationTracker, null);
}
ThreadScope thrScope = getSessionScope();
synchronized (thrScope) {
ClassLoader savedClassLoader =
Thread.currentThread().getContextClassLoader();
FOM_Cocoon cocoon = null;
try {
try {
setupContext(redirector, context, thrScope);
cocoon = (FOM_Cocoon) thrScope.get("cocoon", thrScope);
// Register the current scope for scripts indirectly called from this function
FOM_JavaScriptFlowHelper.setFOM_FlowScope(cocoon.getObjectModel(), 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];
Scriptable parameters = context.newObject(thrScope);
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;
try {
fun = context.compileReader (
thrScope, new StringReader(funName), null, 1, null
).exec (context, thrScope);
} catch (EcmaError ee) {
throw new ResourceNotFoundException (
"Function \"javascript:" + funName + "()\" not found"
);
}
// Check count of arguments
if (fun instanceof BaseFunction) {
if (((BaseFunction)fun).getArity() != 0) {
getLogger().error("Function '" + funName + "' must have no declared arguments! " +
"Use cocoon.parameters to reach parameters passed from the sitemap into the function.");
}
}
thrScope.setLock(true);
ScriptRuntime.call(context, fun, thrScope, funArgs, thrScope);
} catch (JavaScriptException ex) {
throw locationTracker.getException("Error calling flowscript function " + funName, ex);
} catch (EcmaError ee) {
throw locationTracker.getException("Error calling flowscript function " + funName, ee);
}
} finally {
thrScope.setLock(false);
setSessionScope(thrScope);
if (cocoon != null) {
cocoon.popCallContext();
}
Context.exit();
Thread.currentThread().setContextClassLoader(savedClassLoader);
}
}
}
Calls a JavaScript function, passing params as its
arguments. In addition to this, it makes available the parameters
through the cocoon.parameters JavaScript array
(indexed by the parameter names). |
Script compileScript(Context cx,
String fileName) throws Exception {
Source src = this.sourceresolver.resolveURI(fileName);
if (src != null) {
synchronized (compiledScripts) {
ScriptSourceEntry entry =
(ScriptSourceEntry)compiledScripts.get(src.getURI());
Script compiledScript = null;
if (entry == null) {
compiledScripts.put(src.getURI(),
entry = new ScriptSourceEntry(src));
} else {
this.sourceresolver.release(src);
}
boolean needsRefresh = reloadScripts &&
(entry.getCompileTime() + checkTime < System.currentTimeMillis());
compiledScript = entry.getScript(cx, this.scope, needsRefresh, this);
return compiledScript;
}
}
throw new ResourceNotFoundException(fileName + ": not found");
}
Compile filename as JavaScript code |
protected Script compileScript(Context cx,
Scriptable scope,
Source src) throws Exception {
PushbackInputStream is = new PushbackInputStream(src.getInputStream(), ENCODING_BUF_SIZE);
try {
String encoding = findEncoding(is);
Reader reader = encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding);
reader = new BufferedReader(reader);
Script compiledScript = cx.compileReader(scope, reader,
src.getURI(), 1, null);
return compiledScript;
} finally {
is.close();
}
}
|
public void configure(Configuration config) throws ConfigurationException {
super.configure(config);
String loadOnStartup = config.getChild("load-on-startup").getValue(null);
if (loadOnStartup != null) {
register(loadOnStartup);
}
String debugger = config.getChild("debugger").getValue(null);
enableDebugger = "enabled".equalsIgnoreCase(debugger);
if (reloadScripts) {
String classPath = config.getChild("classpath").getValue(null);
synchronized (javaClassRepository) {
if (classPath != null) {
StringTokenizer izer = new StringTokenizer(classPath, ";");
int i = 0;
javaSourcePath = new String[izer.countTokens() + 1];
javaSourcePath[javaSourcePath.length - 1] = "";
while (izer.hasMoreTokens()) {
javaSourcePath[i++] = izer.nextToken();
}
} else {
javaSourcePath = new String[]{""};
}
updateSourcePath();
}
}
}
|
String findEncoding(PushbackInputStream is) throws IOException {
// Read some bytes
byte[] buffer = new byte[ENCODING_BUF_SIZE];
int len = is.read(buffer, 0, buffer.length);
// and push them back
is.unread(buffer, 0, len);
// Interpret them as an ASCII string
String str = new String(buffer, 0, len, "ASCII");
RE re = new RE(encodingRE);
if (re.match(str)) {
return re.getParen(1);
}
return null;
}
Find the encoding of the stream, or null if not specified |
public void forwardTo(Scriptable scope,
FOM_Cocoon cocoon,
String uri,
Object bizData,
FOM_WebContinuation fom_wk,
Redirector redirector) throws Exception {
setupView(scope, cocoon, fom_wk);
super.forwardTo(uri, bizData,
fom_wk == null ? null : fom_wk.getWebContinuation(),
redirector);
}
|
static synchronized Main getDebugger() {
if (debugger == null) {
final Main db = new Main("Cocoon Flow Debugger");
db.pack();
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
size.width *= 0.75;
size.height *= 0.75;
db.setSize(size);
db.setExitAction(new Runnable() {
public void run() {
db.setVisible(false);
}
});
db.setOptimizationLevel(OPTIMIZATION_LEVEL);
db.setVisible(true);
debugger = db;
Context.addContextListener(debugger);
}
return debugger;
}
|
protected ServiceManager getServiceManager() {
return manager;
}
Needed to get things working with JDK 1.3. Can be removed once we
don't support that platform any more. |
public void handleContinuation(String id,
List params,
Redirector redirector) throws Exception {
WebContinuation wk = continuationsMgr.lookupWebContinuation(id, getInterpreterID());
if (wk == null) {
/*
* Throw an InvalidContinuationException to be handled inside the
* < map:handle-errors > sitemap element.
*/
throw new InvalidContinuationException("The continuation ID " + id + " is invalid.");
}
Context context = Context.enter();
context.setOptimizationLevel(OPTIMIZATION_LEVEL);
context.setGeneratingDebug(true);
context.setCompileFunctionsWithDynamicScope(true);
LocationTrackingDebugger locationTracker = new LocationTrackingDebugger();
if (!enableDebugger) {
//FIXME: add a "tee" debugger that allows both to be used simultaneously
context.setDebugger(locationTracker, null);
}
// Obtain the continuation object from it, and setup the
// FOM_Cocoon object associated in the dynamic scope of the saved
// continuation with the environment and context objects.
Continuation k = (Continuation)wk.getContinuation();
ThreadScope kScope = (ThreadScope)k.getParentScope();
synchronized (kScope) {
ClassLoader savedClassLoader =
Thread.currentThread().getContextClassLoader();
FOM_Cocoon cocoon = null;
try {
Thread.currentThread().setContextClassLoader(kScope.getClassLoader());
cocoon = (FOM_Cocoon)kScope.get("cocoon", kScope);
kScope.setLock(true);
cocoon.pushCallContext(this, redirector, manager,
avalonContext,
getLogger(), wk);
// Register the current scope for scripts indirectly called from this function
FOM_JavaScriptFlowHelper.setFOM_FlowScope(cocoon.getObjectModel(), kScope);
if (enableDebugger) {
getDebugger().setVisible(true);
}
Scriptable parameters = context.newObject(kScope);
int size = params != null ? params.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);
FOM_WebContinuation fom_wk = new FOM_WebContinuation(wk);
fom_wk.setParentScope(kScope);
fom_wk.setPrototype(ScriptableObject.getClassPrototype(kScope,
fom_wk.getClassName()));
Object[] args = new Object[] {k, fom_wk};
try {
ScriptableObject.callMethod(cocoon,
"handleContinuation", args);
} catch (JavaScriptException ex) {
throw locationTracker.getException("Error calling continuation", ex);
} catch (EcmaError ee) {
throw locationTracker.getException("Error calling continuation", ee);
}
} finally {
kScope.setLock(false);
setSessionScope(kScope);
if (cocoon != null) {
cocoon.popCallContext();
}
Context.exit();
Thread.currentThread().setContextClassLoader(savedClassLoader);
}
}
}
|
public void initialize() throws Exception {
if (enableDebugger) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Flow debugger enabled, creating");
}
getDebugger().doBreak();
}
Context context = Context.enter();
context.setOptimizationLevel(OPTIMIZATION_LEVEL);
context.setCompileFunctionsWithDynamicScope(true);
context.setGeneratingDebug(true);
// add support for Rhino objects to JXPath
JXPathIntrospector.registerDynamicClass(Scriptable.class,
ScriptablePropertyHandler.class);
JXPathContextReferenceImpl.addNodePointerFactory(new ScriptablePointerFactory());
try {
scope = new Global(context);
// Access to Cocoon internal objects
FOM_Cocoon.init(scope);
errorReporter = new JSErrorReporter(getLogger());
} catch (Exception e) {
Context.exit();
e.printStackTrace();
throw e;
}
}
|
void process(Scriptable scope,
FOM_Cocoon cocoon,
String uri,
Object bizData,
OutputStream out) throws Exception {
setupView(scope, cocoon, null);
super.process(uri, bizData, out);
}
|