VMware vCenter

 View Only
  • 1.  Inconsistent behaviour when accessing java.util.*-classes

    Broadcom Employee
    Posted Aug 09, 2011 10:32 AM

    Hi!

    In a workflow I try to work with some java.util.*-Classes, and get some strange behaviour:

    Working with java.util.Random works as expected, I can access methods of the class without problems.

    Trying the same with java.util.Calendar or GregorianCalendar does not work: I always get a "Cannot find function ....()" error when calling a mehtod of Calendar / GregorianCalendar!

    (See example code below)

    What do I miss?

    Download the full example-workflow from the attachment...

    Regards,

    Joerg

    The Code:

    //ACCESS java.util.Random works as expected
    var rnd = new java.util.Random();
    //print out all attributes of rnd
    for (var i in rnd) {
    System.debug("i: " + i );
    }

    System.debug("rnd: " + rnd);
    var rndint = rnd.nextInt();
    System.debug("rndint: " + rndint);

    //ACCESS java.util.TimeZone works as expected
    var ids = java.util.TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
    System.debug("ids: " + ids);
    var  pdt = new java.util.SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
    System.debug("pdt: " + pdt);

    //ACCESS Constants in java.util.Calendar works as expected
    System.debug("APRIL: " + java.util.Calendar.APRIL);

    // set up rules for daylight savings time
    pdt.setStartRule(java.util.Calendar.APRIL, 1, java.util.Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    pdt.setEndRule(java.util.Calendar.OCTOBER, -1, java.util.Calendar.SUNDAY, 2 * 60 * 60 * 1000);

    // create a GregorianCalendar with the Pacific Daylight time zone
    // and the current date and time
    var calendar = new java.util.GregorianCalendar(pdt);
    System.debug("calendar: " + calendar);
    //(try to) print out all attributes of calendar: shows nothing!?!?!
    for ( i in calendar) {
    System.debug("i: " + i );
    }

    System.debug("========");

    //create a calender, and set time to yesterday...
    var trialTime = new Date((System.getCurrentTime() - (24 * 60 * 60 * 1000)));
    System.debug("trialTime: " + trialTime);
    calendar.setTime(trialTime);
    System.debug("calendar: " + calendar);

    // Creating a java.util.Calendar works
    var cl =  java.util.Calendar.getInstance();
    System.debug("cl: " + cl);

    //**** ACCESS methods of java.util.calendar does not work!
    var test = cl.getFirstDayOfWeek();



  • 2.  RE: Inconsistent behaviour when accessing java.util.*-classes
    Best Answer

    Broadcom Employee
    Posted Aug 11, 2011 10:30 AM

    Hi,

    I tested locally and indeed it is the same problem. However this doesn’t seem to be vCO specific problem. I debugged the server and it seems that the Mozilla Rhino JS engine, that we are using, maps the java.util.Calendar and GregorianCalendar to a native (JS) date type. When you try to invoke a method, the Rhino engine tries to call this method on this NativeObject and it doesn’t have it declared and that’s why you get this exception. I don’t know how urgent for you is to use java.util.Calendar, but I would recommend not using it. You can use our JS Date type instead.

    By the way this problem is not only with Calendar. It is a generic problem. If you create a new ArrayList, for example, the JS engine will map it to a native array and you won’t be able to call ArrayList’s methods.

    Although I didn’t solve your problem, hope this helps you understand the JS integration with Java.



  • 3.  RE: Inconsistent behaviour when accessing java.util.*-classes

    Broadcom Employee
    Posted Aug 12, 2011 08:23 PM

    Hi!

    Thank you very much for your time to do these tests!

    And good to know that neither I did miss a thing nor there is some bug in vCO related to this :smileyhappy:.

    I tried to create a plugin, and one of it's classes needed a calendar as input (crazy: alternatively it uses an ArrayList with properties, so thanks to your explanation I will save a lot of time trying this as replacement :-) )...

    Regards,

    Joerg