added extension mechanism
This commit is contained in:
parent
0a09f6e2ed
commit
f68ed83394
3 changed files with 118 additions and 0 deletions
9
src/helma/extensions/ConfigurationException.java
Normal file
9
src/helma/extensions/ConfigurationException.java
Normal file
|
@ -0,0 +1,9 @@
|
|||
package helma.extensions;
|
||||
|
||||
public class ConfigurationException extends RuntimeException {
|
||||
|
||||
public ConfigurationException (String msg) {
|
||||
super (msg);
|
||||
}
|
||||
|
||||
}
|
40
src/helma/extensions/HelmaExtension.java
Normal file
40
src/helma/extensions/HelmaExtension.java
Normal file
|
@ -0,0 +1,40 @@
|
|||
package helma.extensions;
|
||||
|
||||
import helma.framework.core.Application;
|
||||
import helma.main.Server;
|
||||
import helma.scripting.ScriptingEngine;
|
||||
|
||||
/**
|
||||
* Helma extensions have to subclass this. The extensions to be loaded are
|
||||
* defined in <code>server.properties</code> by setting <code>extensions =
|
||||
* packagename.classname, packagename.classname</code>.
|
||||
*/
|
||||
|
||||
public abstract class HelmaExtension {
|
||||
|
||||
/**
|
||||
* called by the Server at startup time. should check wheter the needed classes
|
||||
* are present and throw a ConfigurationException if not.
|
||||
*/
|
||||
public abstract void init (Server server) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* called when an Application is started.
|
||||
*/
|
||||
public abstract void applicationStarted (Application app) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* called when an Application is stopped.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
public abstract void initScripting (Application app, ScriptingEngine engine) throws ConfigurationException;
|
||||
|
||||
public abstract String getName ();
|
||||
|
||||
}
|
||||
|
69
src/helma/extensions/demo/DemoExtension.java
Normal file
69
src/helma/extensions/demo/DemoExtension.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
package helma.extensions.demo;
|
||||
|
||||
import helma.extensions.HelmaExtension;
|
||||
import helma.extensions.ConfigurationException;
|
||||
import helma.framework.core.Application;
|
||||
import helma.main.Server;
|
||||
import helma.scripting.ScriptingEngine;
|
||||
|
||||
// fesi-related stuff:
|
||||
import FESI.Data.ESObject;
|
||||
import FESI.Data.ESWrapper;
|
||||
import FESI.Data.GlobalObject;
|
||||
import FESI.Exceptions.EcmaScriptException;
|
||||
import FESI.Interpreter.Evaluator;
|
||||
import helma.scripting.fesi.FesiEvaluator;
|
||||
|
||||
|
||||
/**
|
||||
* a demo extension implementation, to activate this add <code>extensions =
|
||||
* helma.extensions.demo.DemoExtensions</code> to your <code>server.properties</code>.
|
||||
* a new global object <code>demo</code> that wraps helma.main.Server
|
||||
* will be added to the scripting environment.
|
||||
*/
|
||||
|
||||
public class DemoExtension extends HelmaExtension {
|
||||
|
||||
public void init (Server server) throws ConfigurationException {
|
||||
try {
|
||||
// just a demo with the server class itself (which is always there, obviously)
|
||||
Class check = Class.forName("helma.main.Server");
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new ConfigurationException("helma-library not present in classpath. make sure helma.jar is included. get it from http://www.helma.org/");
|
||||
}
|
||||
}
|
||||
|
||||
public void applicationStarted (Application app) throws ConfigurationException {
|
||||
app.logEvent ("DemoExtension init with app " + app.getName () );
|
||||
}
|
||||
|
||||
public void applicationStopped (Application app) {
|
||||
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 {
|
||||
throw new ConfigurationException ("scripting engine " + engine.toString () + " not supported in DemoExtension");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue