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

View file

@ -1,5 +1,7 @@
package helma.extensions.demo; package helma.extensions.demo;
import java.util.HashMap;
import helma.extensions.HelmaExtension; import helma.extensions.HelmaExtension;
import helma.extensions.ConfigurationException; import helma.extensions.ConfigurationException;
import helma.framework.core.Application; import helma.framework.core.Application;
@ -41,29 +43,22 @@ public class DemoExtension extends HelmaExtension {
app.logEvent ("DemoExtension stopped on app " + app.getName () ); app.logEvent ("DemoExtension stopped on app " + app.getName () );
} }
public void initScripting (Application app, ScriptingEngine engine) throws ConfigurationException { public HashMap initScripting (Application app, ScriptingEngine engine) throws ConfigurationException {
if (engine instanceof FesiEvaluator) { 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 {
throw new ConfigurationException ("scripting engine " + engine.toString () + " not supported in DemoExtension"); 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 () { public String getName () {
return "DemoExtension"; 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 // do lazy cleanup
Map lastGlobals = null; Map lastGlobals = null;
// the global vars set by extensions
HashMap extensionGlobals;
public FesiEvaluator (Application app, RequestEvaluator reval) { public FesiEvaluator (Application app, RequestEvaluator reval) {
this.app = app; this.app = app;
@ -78,13 +80,15 @@ public final class FesiEvaluator implements ScriptingEngine {
mailx.setProperties (app.getProperties ()); mailx.setProperties (app.getProperties ());
Database dbx = (Database) evaluator.addExtension ("helma.scripting.fesi.extensions.Database"); Database dbx = (Database) evaluator.addExtension ("helma.scripting.fesi.extensions.Database");
dbx.setApplication (app); dbx.setApplication (app);
// load extensions defined in server.properties:
// load extensions defined in server.properties extensionGlobals = new HashMap ();
Vector extVec = Server.getServer ().getExtensions (); Vector extVec = Server.getServer ().getExtensions ();
for (int i=0; i<extVec.size(); i++ ) { for (int i=0; i<extVec.size(); i++ ) {
HelmaExtension ext = (HelmaExtension)extVec.get(i); HelmaExtension ext = (HelmaExtension)extVec.get(i);
try { try {
ext.initScripting (app,this); HashMap tmpGlobals = ext.initScripting (app,this);
if (tmpGlobals!=null)
extensionGlobals.putAll(tmpGlobals);
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
app.logEvent ("Couldn't initialize extension " + ext.getName () + ": " + e.getMessage ()); 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 { public void enterContext (HashMap globals) throws ScriptingException {
// set globals on the global object // set globals on the global object
globals.putAll(extensionGlobals);
if (globals != null && globals != lastGlobals) { if (globals != null && globals != lastGlobals) {
// loop through global vars and set them // loop through global vars and set them
for (Iterator i=globals.keySet().iterator(); i.hasNext(); ) { 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); sv = new ESBeanWrapper (new SessionBean ((Session)v), this);
} else if ("app".equals (k)) { } else if ("app".equals (k)) {
sv = new ESBeanWrapper (new ApplicationBean ((Application)v), this); sv = new ESBeanWrapper (new ApplicationBean ((Application)v), this);
} else if (v instanceof ESValue) {
sv = (ESValue)v;
} else { } else {
sv = ESLoader.normalizeValue (v, evaluator); sv = ESLoader.normalizeValue (v, evaluator);
} }