VCF Automation Blog

from Stefan Schnell

Emulate getModule


/**
 * Example how to emulate getModule method of System class with Node.js.
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 */

var System = {
  log : function(text) {
    console.log(text);
  }
}

var com = {

  vmware : {

    // Module com.vmware.basic
    basic : {
      createDirectory : function(directory) {
        var file = new java.io.File(directory);
        if (!file.exists()) {
          System.log("Creating directory : '" + directory + "'");
          file.mkdirs();
          System.log("Directory '" + directory + "' created.");
        }
      },
      getFileName : function(fileName) {
        if (fileName != null)  {
          var index = fileName.lastIndexOf("/");
          return fileName.substring(index + 1, fileName.length);
        }
        return null;
      }
    },

    // Module com.vmware.constants
    constants : {
      getDefaultCompanyName : function() {
        return "VMware Inc.";
      },
      getDefaultSSHKeyPairPath : function() {
        return "/usr/lib/vco/app-server/conf/vco_key";
      }
    }

  }

}

// Emulate getModule method.
function getModule(object) {
  return Object.create(eval(object));
}

// Main
System.log(
  getModule("com.vmware.constants").getDefaultCompanyName()
);

var constants = getModule("com.vmware.constants");
System.log(constants.getDefaultCompanyName());

node.js call with emulate get module