VMware {code} Community
admin
Immortal
Immortal
Jump to solution

Pass Array from javascript to java method in Plugin

Hi,
How can I pass Array  from javascript to java method?
I have specified java method param as  type"=[MyClass" in vso.xml
But, I am getting  following error while passing Javascript Array to Java  method:
"Can not convert  7.6897897E7 to MyClass[]"
Regards
Sameer
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Hi Sameer,

Your Java method signature expects 4 parameters, but your are passing 5 in your JavaScript action. There is an extra parameter (contextId) I think, which cause timestamp to be mapped to layer2FirewallRules. That's why it says that it cannot convert a number to an array.

View solution in original post

0 Kudos
8 Replies
admin
Immortal
Immortal
Jump to solution

For example obj.MyJavascriptMethod(string_param, int_param) is properly  mapped to java method obj.MyJavaMethod(string_param,  int_param)
But  when Param is Array then obj.MyJavascriptMethod(string_param_array[], int_param_array[]) is NOT properly mapped to java method obj.MyJavaMethod(string_param_array[],  int_param_array[])
0 Kudos
tschoergez
Leadership
Leadership
Jump to solution

Hi!

I guess the reason for this is that the Arrays you create in JavaScript are not equal to Arrays in Java. The Rhinoengine is used in background, so for further research you can start with:

http://www.mozilla.org/rhino/ScriptingJava.html (The "Creating Java Arrays" Chapter) and http://stackoverflow.com/questions/1433382/how-to-convert-rhino-javascript-arrays-to-java-arrays

Regards,

Joerg

0 Kudos
tschoergez
Leadership
Leadership
Jump to solution

BTW: this might also be interesting...: http://communities.vmware.com/thread/324338?tstart=0

0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi.

1)java.lang.reflect.Array.newInstance is not working as it wont understand "newInstance" method.
I am getting error "newInstance is not function.It is org.mozilla.javascript.NativeJavaPackage".

2)As per your second reply, I am already using JS Array type. But I am getting error when RhinoEngine is trying to convert JS array to Java array(to call Java method wuth Array param).

0 Kudos
sanchezs
VMware Employee
VMware Employee
Jump to solution

Hi Sameer,

I think the error during the conversion is related to the way you instantiate the array of elements that you want to pass. How do you create the array inside your scripting code?

I'm writing you an example that should work, since we are using the same approach within different plug-ins and they all work fine.

Define the method inside the vso.xml like that:

<object script-name="MyObject"...
...
        <method java-name="myMethod" return-type="void" script-name="myMethod">
            <parameters>
                <parameter name="items" type="[MyObjectItem" />
            </parameters>
        </method>
...
</object>

Define the method inside the Java class like that:

public class MyObject {
...
    public void myMethod(MyObjectItem[] items) {
        ...
    }
...
}

Create the array inside the scripting code and invoke the method like that:

var items = new Array(3);
items[0] = new MyObjectItem();
items[1] = new MyObjectItem();
items[2] = new MyObjectItem();

var myObject = new MyObject();
myObject.myMethod(items);

I hope it helps!

Regards,

Sergio

0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi Sergio,

I have already implemented same thing as follows.

I am getting error


vso.xml

<method script-name="publishConfig" java-name="publishConfig" return-type="[FailedPublishInfoDto">
     <description>publish config</description>
     <parameters>
          <parameter type="String" name="contextId"></parameter>
          <parameter type="Boolean" name="provisioned"></parameter>
          <parameter type="Long" name="timestamp"></parameter>
          <parameter type="[Layer2FirewallRuleDto" name="layer2FirewallRules"></parameter>
          <parameter type="[Layer3FirewallRuleDto" name="layer3FirewallRules"></parameter>
     </parameters>
</method>

App.java

public ArrayList<FailedPublishInfoDto> publishConfig(

     Boolean provisioned,

     Long timestamp,

    Layer2FirewallRuleDto[] layer2FirewallRules,

      Layer3FirewallRuleDto[] layer3FirewallRules) {

...

}

JS Action:

var layer2FirewallRule = app.composeLayer2FirewallRule(id, "ALLOW"/*actionE.name*/, "HIGH"/*precedence.name*/, disabled, logged, notes, srcExclude, srcId, destProtocol, destProtocolNumber, destExclude, destIpId);

var l2Arr = new Array(1);

l2Arr[0] =  layer2FirewallRule; // Even tried "new Layer2FirewallRuleDto()"

var l3Arr = new Array(0);

app.publishConfig(contextId , provisioned, timestamp , l2Arr , l3Arr);

***************************************************************************************************************************

I am getting error

Cannot convert 576557.0 to Layer2FirewallRuleDto[]

I have also included Layer2FirewallRuleDto in both Finder and Object in vso.xml.

0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi Sameer,

Your Java method signature expects 4 parameters, but your are passing 5 in your JavaScript action. There is an extra parameter (contextId) I think, which cause timestamp to be mapped to layer2FirewallRules. That's why it says that it cannot convert a number to an array.

0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi dnachev,

Thanks.

That is good catch.

It was my bad. I forgot to delete one of the method param and I was thinking of possible error of mapping between JS array and Java arrray.

Regards

Sameer

0 Kudos