Enterprise Integration Zone is brought to you in partnership with:

Madhuka is Software Engineer and currently works on the Jaggery, UES, WSO2 Gadget Server team. He holds a B.Sc (Hons) degree in Information Technology from the Faculty of Information Madhuka is a DZone MVB and is not an employee of DZone and has posted 52 posts at DZone. You can read more from them at their website. View Full User Profile

Adding Module for Jaggery

02.22.2013
| 1930 views |
  • submit to reddit
If you need any module or (jaggery code) to reuse in jaggery, we can add a module into jaggery easily.  I will be explain how to do it in this article.

We can reuse modules from jaggery applications in each particular jaggery server. I need to add some custom new JavaScript APIs that are mainly for your applications. Modules are he best option you have.

We can write a module in javascript or java (hostobject). In the module('<jaggery-home>\modules\') you have to point out the particular file with its location. Here I am explaining how to achieve this.

[1]Create a folder with your module name inside a '<jaggery-home>\modules\' (My module is for 'phone')

Create a module.xml inside this folder. Add your module names like you see below

<module name="phone" xmlns="http://wso2.org/projects/jaggery/module.xml">
  <script>
  <name>phone</name>
  <path>scripts/phone.js</path>
  </script>
</module>

[2] Add the js file in ‘scripts’ (Phone will have type and number, we can get phone information from the object – the phone I own)

phone.js

var phone = function  (number) {
  this.type = "Apple";
  this.number = number;
  this.getInfo = getPhoneInfo;
}
 
var getPhoneInfo = function () {
  return this.number + ' is ' + this.type + ' phone';
}

[3] Now start Jaggery server, jaggery will have a javascript API for Phone that we create.  Let's try that now.

[4] Create jaggery Applications ‘testPhone’ contains ‘phone.jag’ Here is a jaggery file that I am going to use with the phone API that we created.

<%
  function getPhone(phoneNumber){
 
  var phone = require('phone');
  var myPhone = new phone.phone(phoneNumber);
  return myPhone.getInfo();
 
  }
 
  print(getPhone("94774066666"));
%>

[5] now go to http://localhost:9763/testPhone/phone.jag

image

[6] Now think you buy new Phone and your phone type is now changed to Nokia

<%
  function getPhone(phoneNumber){
 
  var phone = require('phone');
  var myPhone = new phone.phone(phoneNumber);
  //buy new phone
  myPhone.type = "Nokia";
  return myPhone.getInfo();
  }
 
  print(getPhone("94774066666"));
%>

image

Published at DZone with permission of Madhuka Udantha, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)