Bookmark and Share Share

Generating Mashup Scripts Dynamically

When the logic of a mashup must change dynamically, you need to create a mashup script on the fly and execute it. You can use the built-in executeDynamicEMML macro to do this. This macro is installed with the EMML Reference Runtime Engine and is available for use in any mashup script.

The executeDynamicEMML macro allows you to generate a mashup script dynamically using the <script> statement and JavaScript or JRuby scripts or using Java classes. See <script> for more information on using JavaScript or JRuby. See Using Java Classes in a <script> Statement for information on using Java classes in a mashup.

<macro:externalDynamicEMML>

Use the following attributes to define the input parameters for this macro and receive the results:

Attribute Required Description

script

yes

A string containing the dynamically generated EMML code to execute.

Mashup scripts that are generated dynamically have these restrictions:

  • <input> statements are not allowed as you cannot pass parameters to the mashup.

  • Variable access is limited to the variables defined within the generated code. Generated scripts do not have access to variables in the parent mashup.

outputvariable

yes

The name of the variable to receive the results of the generated mashup script.

This variable should be the same type as the expected results of the mashup script. Typically this is a document type.

<macro:executeDynamicEMML> Example

In this use case, data for the mashup comes from several different sources as either web services or from databases. But the the access method for a particular data source can change at any given time based on configuration. The mashup uses this configuration to generate a mashup that invokes each data source using the appropriate method:

.... 
<output name="dynamicResult" type="document"/> 
  <variables> 
    <variable name="serviceConfig" type="document"/> 
    <variable name="dynamicScript" type="document"/> 
  </variables> 
  <invoke service="ServiceConfig" operation="getConfig" 
   outputvariable="serviceConfig"/> 
  <script type="javascript" inputvariables="$serviceConfig" 
     outputvariable="$dynamicScript"> 
    <![CDATA[ 
      dynamicScript = "<mashup " + 
        "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + 
        "xsi:schemaLocation=\"http://www.openemml.org/2009-04-15/EMMLSchema " + 
        "../schema/EMMLSpec.xsd\" "+ 
        "xmlns=\"http://www.openemml.org/2009-04-15/EMMLSchema\" " + 
        "name=\"myDynamicMashup\"> " + 
        "<operation name=\"getFeed\"> " + 
        " <output name=\"result\" type=\"document\"/> "; 
        if (serviceConfig.serviceA = 'web') { 
          dynamicScript = dynamicScript + 
            "<directinvoke endpoint=\"" + serviceA.url + "\" method=\"GET\" " + 
            "outputvariable=\"resultA\" />"; 
        } 
        //more logic to generate mashup script statements 
        dynamicScript = dynamicScript + 
        " </operation> " + 
        " </mashup> "; 
    ]]> 
  </script> 
  <macro:executeDynamicEMML script="$dynamicScript" 
    outputvariable="$dynamicResult"/> 
...