|
From: <tre...@us...> - 2007-09-03 00:22:06
|
Revision: 341
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=341&view=rev
Author: trevorolio
Date: 2007-09-02 17:22:09 -0700 (Sun, 02 Sep 2007)
Log Message:
-----------
Fixed a bug in ObjParser which barfed if the mtl file is references but missing.
Fixed a bug in the integration tests around space membership which assumed that there were no existing memberships and confused the space owner web client with the member web client.
Added destruct and cleanup functions to template scripts, so that templates may tidy up during space shutdown or thing removal, respectively.
Modified Paths:
--------------
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/script/SpaceScriptEngine.java
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/script/SpaceScriptEngine.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/script/SpaceScriptEngine.java 2007-09-02 23:21:36 UTC (rev 340)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/script/SpaceScriptEngine.java 2007-09-03 00:22:09 UTC (rev 341)
@@ -50,8 +50,12 @@
private ScriptSpace scriptSpace = null;
- public static final String CONSTRUCT_FUNCTION_NAME = "construct";
+ public static final String CONSTRUCT_FUNCTION_NAME = "construct"; //called when a things is added to a space or when a space starts
+ public static final String DESTRUCT_FUNCTION_NAME = "destruct"; //called on each thing the space stops
+
+ public static final String CLEANUP_FUNCTION_NAME = "cleanup"; //called on each thing when removed from a space
+
public static final String ONSERVICE_FUNCTION_NAME = "onService";
private static final String ONCLICK_FUNCTION_NAME = "onClick";
@@ -104,7 +108,11 @@
spaceSimulator.getSpace().addListener(this, false);
}
- public void cleanup() {
+ public void cleanup() { //this happens when a space is stopped, not necessarily when it's deleted
+ Object[] scopes = thingScopes.getValues();
+ for (int i = 0; i < scopes.length; i++) {
+ callAndIgnoreFunction(DESTRUCT_FUNCTION_NAME, (ScriptableObject) scopes[i]); //yes, we call destruct in cleanup()... it's weird but right
+ }
scriptSpace.cleanup();
}
@@ -313,9 +321,9 @@
Object value = parameterMap.get(parameterNames[i]);
if (value instanceof String) {
parameterValues[i] = (String) parameterMap.get(parameterNames[i]);
- } else if(value instanceof String[]){
- String[] array = (String[])value;
- if(array.length > 0){
+ } else if (value instanceof String[]) {
+ String[] array = (String[]) value;
+ if (array.length > 0) {
parameterValues[i] = array[0];
}
} else {
@@ -364,8 +372,22 @@
public void thingAdded(Thing thing) {
}
+ private void callAndIgnoreFunction(String functionName, ScriptableObject thingScope){
+ Context context = Context.enter();
+ try {
+ callJavascriptFunction(context, thingScope, functionName, new Object[0]);
+ } catch (Exception e) {
+ //We don't care if a script dies in cleanup, and there's no log once a space
+ } finally {
+ Context.exit();
+ }
+ }
+
public void thingReloaded(Thing thing) {
- thingScopes.removeForward(new Long(thing.getThingID()));
+ ScriptableObject thingScope = (ScriptableObject) thingScopes.removeForward(new Long(thing.getThingID()));
+ if (thingScope != null) {
+ callAndIgnoreFunction(DESTRUCT_FUNCTION_NAME, thingScope);
+ }
scriptSpace.cancelThingTasks(thing.getThingID());
try {
constructThingScript(thing);
@@ -381,7 +403,10 @@
}
public void thingRemoved(Thing thing) {
- thingScopes.removeForward(new Long(thing.getThingID()));
+ ScriptableObject thingScope = (ScriptableObject) thingScopes.removeForward(new Long(thing.getThingID()));
+ if (thingScope != null) {
+ callAndIgnoreFunction(CLEANUP_FUNCTION_NAME, thingScope);
+ }
scriptSpace.cancelThingTasks(thing.getThingID());
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|