From:Ruchira Wageesha (ruch...@wso2.com)List:org.wso2.architectureAttachments:
BAM (1).png - 49kHi All,
In BAM Gadgets, we need to get data from the BAM backend to the browser.
Those data sources are basically Web Services. Among these, Data Services
and serveral Admin Services can bee seen. The previous model of the data
retrieval flow was as below.
All bam gadgets call a jsp with the a particular string key(i.e. name of the
operation such as getAllServers). Depending on the key,
- Jsp get the correct service and operation
- Invoke the service
- Result is converted either to a string/json and send back to the browser.
In the above model, there was a long list of if-else check for each
operation. Further, if someone wanted to get data from a new service, then
he will have to add certain set of logic into the JSP as well.
In order to get rid of that model, I created a tryit proxy like jsp which
allow us to call any service without any changes to that JSP. Further,
I tried to use Axis2 JSON support to invoke the services as in the 1 diagram
of the attached image. But, it didn't allow as to
invoke several services(such as admin services) due to a limitation of Axis2
JSON support, but it worked for Data services.
Then, I though of changing jsp proxy-to-Web Service call into a SOAP call
and return the response as a JSON. At this level, JSP receives only XML
responses from its services and there we should be able to convert the XML
into JSON without any service/operation specific logic. So, I though of
using Badgerfish conversion for that(although it too has limitations, it was
the only method to convert ANY XML to JSON without loosing much information
and knowing about the structure of the XML). So, Badgerfish was used as it
was the best convention to keep even namespaces of the original XML in the
resulting JSON.
Although in JSON world, there is nothing like XML namespaces, I thought of
keeping that up as we are dealing with XML responses. As an example, I will
assume that a web service respond us with an XML as below.
<p:name xmlns:p="p.com">
<a:name xmlns:a="a.com">Ruchira</a:name>
<a:name xmlns:a="aa.com">Wageesha</a:name>
</p:name>
So, this XML has 2 name element in two different namespaces. i.e. One will
need to differentiate two name elements depending on the namespace.(This is
a small example, but there are many occasions where one will need to do
namespace aware queries in a gadget).
Again, lets assume that withing a gadget, I wanted to get the value of
"name" in the "aa.com" namespace. i.e. Wageesha. So, if we loose the
namespace information, then it would be impossible. Btw, we can ask the JSON
to give us the second "name" element. But again, the order might not
be consistent(depending on the output schema).
Like wise, although we are in an environment where namespaces aren't, we
will have think about them as we are dealing with namespace aware XML data.
So, Badgerfish was the solution.
Now, lets see what is the badgerfish representation of above XML.
{
"p:name": {
"@xmlns": {
"p": "p.com"
},
"a:name": [{
"@xmlns": {
"p": "p.com",
"a": "a.com"
},
"$": "Ruchira"
},
{
"@xmlns": {
"p": "p.com",
"a": "aa.com"
},
"$": "Wageesha"
}]
}
}
It is an unreadable JSON even for the above small XML. Geting the value of
"name" element in "aa.com" namespace, directly from the JSON is even a
little bit harder. We can't get it simply using favorite "." notation(as we
aren't aware of the order of the "a:name" array in the above JSON). So we
will have to iterate through the JSON and find the matching element.
Writing code to iterate over the JSON, each time we want to get an element
value might make the code messy. Then I though of writing a simple
"Axiom"(name "axiom" might give you a wrong expression) like JSON wrapper
over the Badgerfish JSON. I.e. one can create an object named "Axiom" and
access elements as we do in XML environment or E4X environment. For our
above scenario, it would be
var xml = new Axiom(badgerfishJSON);
var name = xml.getChildrenWithNamespaceURI("aa.com")[0];
Axiom object will have a pointer to the passed "badgerfishJSON". So once the
"getChildrenWithNamespaceURI" method is called, it will iterate through the
properties and return the relevant value. So the iteration logics for
reading namespace aware elements/attributes won't be duplicated. That was
the main idea behind using Axiom like wrapper over JSON.(Creating an new
Axiom object won't duplicate the whole JSON, rather it will keep several
pointers to the elements of the original JSON, so it won't take much browser
memory)
If you are thinking it as an effort to use XML/namespaces in JSON
environment and it shouldn't be done, please let this thread know. But point
I though is if we are dealing with XML data, then there should be a good
replacement for XML manipulation in the browser environment. Otherwise can
send customized simple JSON, but the structure depends on each and every
response. Further, not all the browsers doens't support XML DOM[2], but HTML
DOM[1]. In HTML DOM, we can't do namespace aware queries.
One other thing I noted during gadget development using wso2vis is,
wso2vis.js - 7605 lines of codes with 653K
wso2vis-min.js - 914 lines of codes with the size of 555K(I can't open it
even in gedit)
So, each time we use a wso2vis or any big library within a gadget, what it
does is read those JS files and put the content in the gadget xml. As those
are put inline within the gadget xml, they won't get cached at least in the
browser. So, for 10 gadgets, it load 10 times and keep everything in
the memory. So, if we are using wso2vis or any other big libraries, we will
have to load either the needed modules like google does or dynamically load
from a common url. Btw, I am not aware whether it violates the gadget spec.
Just a thought.
[1] http://www.w3schools.com/jsref/dom_obj_all.asp
[2] http://www.w3schools.com/dom/dom_element.asp
--
Ruchira Wageesha
Software Engineer - WSO2 Inc. www.wso2.com
Email: ruch...@wso2.com Blog: ruch...@blogspot.com
Mobile: +94771083017
Lean . Enterprise . Middleware
_______________________________________________
Architecture mailing list
Arch...@wso2.org
https://mail.wso2.org/cgi-bin/mailman/listinfo/architecture