Author: keith
Date: Wed Apr 30 01:24:15 2008
New Revision: 16372

Log:

Fixing the calling of init funxtions so that calling host objects from within 
the init function does not break anything.


Modified:
   trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java
   
trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java
==============================================================================
--- trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java     
(original)
+++ trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java     
Wed Apr 30 01:24:15 2008
@@ -22,6 +22,8 @@
 
     public static final String MASHUP_JS_SERVICE = "MashupJSService";
     public static final String MASHUP_DESTROY_FUNCTION = 
"MashupDestroyFunction";
+    public static final String MASHUP_INIT_FUNCTION = "MashupInitFunction";
+    public static final String MASHUP_INIT_SERVICES = "MashupInitServices";
     public static final String CONTENT_TYPE_QUERY_PARAM = "content-type";
     public static final String REGISTRY_CONFIG = "RegistryConfig";
     public static final String USERMANAGER_CONFIG = "UsermanagerConfig";

Modified: 
trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java
==============================================================================
--- 
trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java
   (original)
+++ 
trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java
   Wed Apr 30 01:24:15 2008
@@ -67,6 +67,7 @@
 import org.wso2.registry.session.UserRegistry;
 import org.wso2.registry.users.UserRealm;
 import org.wso2.wsas.ServerConstants;
+import org.wso2.wsas.ServerManager;
 
 import javax.xml.namespace.QName;
 import java.io.BufferedReader;
@@ -76,6 +77,7 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -86,6 +88,7 @@
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
+import java.net.URL;
 
 /**
  * This is a custom Axis2 deployer written for deploying JavaScript services.
@@ -547,9 +550,9 @@
               print("destroy");
             }            
             */
-            if (init != null) {
-                init.call(engine.getCx(), engine, engine, new Object[0]);
-            }
+            processInitMethod(axisService, engine, init);
+
+
             if (destroy != null) {
                 
axisService.addParameter(MashupConstants.MASHUP_DESTROY_FUNCTION, destroy);
             }
@@ -585,6 +588,115 @@
         }
     }
 
+    private void processInitMethod(AxisService axisService, JavaScriptEngine 
engine, Function init)
+            throws AxisFault {
+        // If a service has an init function defined we need to first check 
weather this service
+        // is been deployed while the server is starting (The check
+        // serverManager.configContext == null is used to check this). If its 
deployed while the
+        // server is starting we do not call the init function here, but 
rather put it into a
+        // parameter and set that to the axisConfiguration. Thhis is picked up 
later by the
+        // MashupServerInitializer (which runs after the Mashup Server starts) 
and the init
+        // function of each service is called. The reason we do this is cause 
if the init
+        // function have used any host objects we need the server in a running 
state, if not
+        // they may fail.
+        ServerManager serverManager = ServerManager.getInstance();
+        if (serverManager.configContext == null) {
+            if (init != null) {
+                axisService.addParameter(MashupConstants.MASHUP_INIT_FUNCTION, 
init);
+                ArrayList initServices =
+                        (ArrayList) axisConfig
+                                
.getParameterValue(MashupConstants.MASHUP_INIT_SERVICES);
+                if (initServices == null) {
+                    initServices = new ArrayList();
+                }
+                initServices.add(axisService.getName());
+                axisConfig.addParameter(MashupConstants.MASHUP_INIT_SERVICES, 
initServices);
+            }
+        } else {
+            if (init != null) {
+                init.call(engine.getCx(), engine, engine, new Object[0]);
+            }
+            // Check weather we need to call the init functions of any services
+            // Check weather we need to call the init functions of any services
+            Parameter parameter =
+                    
axisConfig.getParameter(MashupConstants.MASHUP_INIT_SERVICES);
+            try {
+                if (parameter != null && parameter.getValue() != null) {
+                    ArrayList initServices = (ArrayList) axisConfig
+                            
.getParameterValue(MashupConstants.MASHUP_INIT_SERVICES);
+                    Iterator iterator = initServices.iterator();
+                    while (iterator.hasNext()) {
+
+                        // Get each service that the init function should be 
called on and call it
+                        String serviceName = (String) iterator.next();
+                        AxisService service =
+                                
axisConfig.getServiceForActivation(serviceName);
+                        if (service == null) {
+                            log.error("Could not run init function of service 
" +
+                                    serviceName);
+                        } else {
+
+                            // Initialize the JavaScript engine and read in 
the JavaScript file
+                            engine = new JavaScriptEngine(serviceName);
+                            engine.getCx()
+                                    
.putThreadLocal(JavaScriptEngineConstants.AXIS2_SERVICE,
+                                                    service);
+                            engine.getCx().putThreadLocal(
+                                    
JavaScriptEngineConstants.AXIS2_CONFIGURATION_CONTEXT,
+                                    axisConfig);
+                            // Load the JavaScriptHostObjects that are 
spefified using the parameter
+                            // <parameter name="javascript.hostobjects"> in 
the axis2.xml
+                            JavaScriptEngineUtils
+                                    .loadHostObjects(engine, axisConfig);
+
+                            // Load the Global JavaScriptHostObjects that are 
spefified using the
+                            // parameter
+                            // <parameter 
name="javascript.global.propertyobjects"> in the axis2.xml
+                            JavaScriptEngineUtils
+                                    .loadGlobalPropertyObjects(engine, 
axisConfig);
+
+                            URL repoURL = axisConfig.getRepository();
+                            if (repoURL != null) {
+                                JavaScriptEngine.axis2RepositoryLocation =
+                                        repoURL.getPath();
+                            }
+
+                            Reader reader = MashupUtils.readJS(service);
+
+                            //support for importing javaScript files
+                            String scripts = 
MashupUtils.getImportScriptsList(service);
+
+                            //Loading imported JavaScript files if there are 
any
+                            if (scripts != null) {
+                                // Generate load command out of the parameter 
scripts
+                                scripts = "load(" +
+                                        ("[\"" + scripts + "\"]")
+                                                .replaceAll(",", "\"],[\"") + 
")";
+                                engine.getCx().evaluateString(engine, scripts,
+                                                              "Load Included 
JavaScript File(s)",
+                                                              0,
+                                                              null);
+                            }
+
+                            //Evaluating the JavaScript service file
+                            engine.getCx()
+                                    .evaluateReader(engine, reader, 
serviceName, 1, null);
+
+                            Function initFunction = (Function) service
+                                    .getParameterValue(
+                                            
MashupConstants.MASHUP_INIT_FUNCTION);
+                            initFunction
+                                    .call(engine.getCx(), engine, engine, new 
Object[0]);
+                        }
+                    }
+                    axisConfig.removeParameter(parameter);
+                }
+            } catch (IOException e) {
+                log.error("Could not run init function of service " + 
e.getMessage(), e);
+            }
+        }
+    }
+
     /**
      * Creates a set of default endpoints for this service
      * @param axisService The AxisService that the endpoints are created for

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to