change in extension-mechanism:

initScripting() now returns a HashMap (key=varname, value=ESValue) which
will be added to the global object by FesiEvaluator. This ensures that global
vars are (re-)set before each request.
DemoExtension was updated to reflect this change.
This commit is contained in:
stefanp 2002-08-01 14:37:25 +00:00
parent a4a0405241
commit 236a819d86
3 changed files with 33 additions and 30 deletions

View file

@ -1,5 +1,7 @@
package helma.extensions;
import java.util.HashMap;
import helma.framework.core.Application;
import helma.main.Server;
import helma.scripting.ScriptingEngine;
@ -19,26 +21,25 @@ public abstract class HelmaExtension {
public abstract void init (Server server) throws ConfigurationException;
/**
* called when an Application is started.
* This should be synchronized. Every application starting calls this method. Otherwise this could
* lead to problems when more than one application starts at the same time.
* called when an Application is started. This should be <b>synchronized</b> when
* any self-initialization is performed.
*/
public abstract void applicationStarted (Application app) throws ConfigurationException;
/**
* called when an Application is stopped.
* This should be synchronized. Otherwise this could lead to problems when more than one
* application gets stopped.
* This should be <b>synchronized</b> when any self-destruction is performed.
*/
public abstract void applicationStopped (Application app);
/**
* called by the ScriptingEngine when it is initizalized. throws a ConfigurationException
* when this type of ScriptingEngine is not supported.
* This should be synchronized. Every application starting calls this method. Otherwise this could
* lead to problems when more than one application starts at the same time.
* called by the ScriptingEngine when it is initizalized. Throws a ConfigurationException
* when this type of ScriptingEngine is not supported. New methods and prototypes can be
* added to the scripting environment. New global vars should be returned in a HashMap
* with pairs of varname and ESObjects. This method should be <b>synchronized</b>, if it
* performs any other self-initialization outside the scripting environment.
*/
public abstract void initScripting (Application app, ScriptingEngine engine) throws ConfigurationException;
public abstract HashMap initScripting (Application app, ScriptingEngine engine) throws ConfigurationException;
public abstract String getName ();

View file

@ -1,5 +1,7 @@
package helma.extensions.demo;
import java.util.HashMap;
import helma.extensions.HelmaExtension;
import helma.extensions.ConfigurationException;
import helma.framework.core.Application;
@ -41,29 +43,22 @@ public class DemoExtension extends HelmaExtension {
app.logEvent ("DemoExtension stopped on app " + app.getName () );
}
public void initScripting (Application app, ScriptingEngine engine) throws ConfigurationException {
if (engine instanceof FesiEvaluator) {
try {
initFesi (app, engine);
app.logEvent("initScripting DemoExtension with " + app.getName () + " and " + engine.toString() );
} catch (EcmaScriptException ecma) {
throw new ConfigurationException (ecma.getMessage());
}
} else {
public HashMap initScripting (Application app, ScriptingEngine engine) throws ConfigurationException {
if (!(engine instanceof FesiEvaluator))
throw new ConfigurationException ("scripting engine " + engine.toString () + " not supported in DemoExtension");
}
app.logEvent("initScripting DemoExtension with " + app.getName () + " and " + engine.toString() );
// fesi-specific code:
Evaluator evaluator = ((FesiEvaluator)engine).getEvaluator ();
// initialize prototypes and global vars here, but don't add them to fesi's global object
ESWrapper demo = new ESWrapper(Server.getServer (), evaluator);
HashMap globals = new HashMap ();
globals.put ("demo",demo);
return globals;
}
public String getName () {
return "DemoExtension";
}
private void initFesi (Application app, ScriptingEngine engine) throws EcmaScriptException {
Evaluator evaluator = ((FesiEvaluator)engine).getEvaluator ();
ESObject demo = new ESWrapper(Server.getServer (), evaluator);
GlobalObject go = evaluator.getGlobalObject();
go.putHiddenProperty ("demo", demo);
}
}

View file

@ -60,6 +60,8 @@ public final class FesiEvaluator implements ScriptingEngine {
// do lazy cleanup
Map lastGlobals = null;
// the global vars set by extensions
HashMap extensionGlobals;
public FesiEvaluator (Application app, RequestEvaluator reval) {
this.app = app;
@ -78,13 +80,15 @@ public final class FesiEvaluator implements ScriptingEngine {
mailx.setProperties (app.getProperties ());
Database dbx = (Database) evaluator.addExtension ("helma.scripting.fesi.extensions.Database");
dbx.setApplication (app);
// load extensions defined in server.properties
// load extensions defined in server.properties:
extensionGlobals = new HashMap ();
Vector extVec = Server.getServer ().getExtensions ();
for (int i=0; i<extVec.size(); i++ ) {
HelmaExtension ext = (HelmaExtension)extVec.get(i);
try {
ext.initScripting (app,this);
HashMap tmpGlobals = ext.initScripting (app,this);
if (tmpGlobals!=null)
extensionGlobals.putAll(tmpGlobals);
} catch (ConfigurationException e) {
app.logEvent ("Couldn't initialize extension " + ext.getName () + ": " + e.getMessage ());
}
@ -279,6 +283,7 @@ public final class FesiEvaluator implements ScriptingEngine {
*/
public void enterContext (HashMap globals) throws ScriptingException {
// set globals on the global object
globals.putAll(extensionGlobals);
if (globals != null && globals != lastGlobals) {
// loop through global vars and set them
for (Iterator i=globals.keySet().iterator(); i.hasNext(); ) {
@ -313,6 +318,8 @@ public final class FesiEvaluator implements ScriptingEngine {
sv = new ESBeanWrapper (new SessionBean ((Session)v), this);
} else if ("app".equals (k)) {
sv = new ESBeanWrapper (new ApplicationBean ((Application)v), this);
} else if (v instanceof ESValue) {
sv = (ESValue)v;
} else {
sv = ESLoader.normalizeValue (v, evaluator);
}