Thursday, April 28, 2011

How to increase JVM Heap Size and Perm Gen space

If you are dealing with applications which have large code bases, then you might quickly run out of Java memory. Probably you might get either java.lang.OutOfMemoryError: PermGen or java.lang.OutOfMemoryError: Java heap space. In order to fix that, just having sufficient memory in your machine is not sufficient. So you need to increase the JVM Heap size and Permanent Generation space.

Setting the following environment variable before starting your application, you can increase them easily.

Unix
export JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=1024m"

Windows
set JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=1024m"

How to Remote Debug on Apache Tomcat

If you want to debug on your application on Apache Tomcat, then you might need to set the following environment variables and start Tomcat using the command below where 8000 is the port that you are going to connect from your IDE.

Windows
set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket

bin/catalina.bat jpda start

Unix
export JPDA_ADDRESS=8000
export JPDA_TRANSPORT=dt_socket

bin/catalina.sh jpda start

Tuesday, February 22, 2011

Parse XML String into DOM with Javascript

You can use following function to convert any XML string into a DOM object from a browser.
function parseXML(xml) {
    xml = xml.replace(/^[\s\n\r\t]*[<][\?][xml][^<^>]*[\?][>]/, "");
    var doc;
    if (window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = false;
        doc.loadXML(xml);
    } else
        doc = (new DOMParser()).parseFromString(xml, "application/xml");
    return doc;
}

Online XML to JSON Converter using Badgerfish Notation

I have hosted an online XML to JSON converter at http://gogooog.com. It has been implemented purely with Javascript. You can read about the xml to badgerfish conversion at http://ruchirawageesha.blogspot.com/2011/01/xml-to-badgerfish-converter-in.html.

Friday, January 28, 2011

XML to BadgerFish Conversion in Javascript

Recently I came across the need of converting an xml into a JSON format. There I used BadgerFish notation to transform the xml into JSON.

You can use following Javascript function to convert any DOM node into the relevant BadgerFish notation. You can convert any XML string into a DOM object using method described at Parse XML String into DOM with Javascript

Please find the hosted version of XML to Badgerfish converter at gogooog.com
function xml2bf(node) {
    var json = {};
    var cloneNS = function(ns) {
        var nns = {};
        for (var n in ns) {
            if (ns.hasOwnProperty(n)) {
                nns[n] = ns[n];
            }
        }
        return nns;
    };
    var process = function (node, obj, ns) {
        if (node.nodeType === 3) {
            if (!node.nodeValue.match(/[\S]+/)) return;
            if (obj["$"] instanceof Array) {
                obj["$"].push(node.nodeValue);
            } else if (obj["$"] instanceof Object) {
                obj["$"] = [obj["$"], node.nodeValue];
            } else {
                obj["$"] = node.nodeValue;
            }
        } else if (node.nodeType === 1) {
            var p = {};
            var nodeName = node.nodeName;
            for (var i = 0; node.attributes && i < node.attributes.length; i++) {
                var attr = node.attributes[i];
                var name = attr.nodeName;
                var value = attr.nodeValue;
                if (name === "xmlns") {
                    ns["$"] = value;
                } else if (name.indexOf("xmlns:") === 0) {
                    ns[name.substr(name.indexOf(":") + 1)] = value;
                } else {
                    p["@" + name] = value;
                }
            }
            for (var prefix in ns) {
                if (ns.hasOwnProperty(prefix)) {
                    p["@xmlns"] = p["@xmlns"] || {};
                    p["@xmlns"][prefix] = ns[prefix];
                }
            }
            if (obj[nodeName] instanceof Array) {
                obj[nodeName].push(p);
            } else if (obj[nodeName] instanceof Object) {
                obj[nodeName] = [obj[nodeName], p];
            } else {
                obj[nodeName] = p;
            }
            for (var j = 0; j < node.childNodes.length; j++) {
                process(node.childNodes[j], p, cloneNS(ns));
            }
        } else if (node.nodeType === 9) {
            for (var k = 0; k < node.childNodes.length; k++) {
                process(node.childNodes[k], obj, cloneNS(ns));
            }
        }
    };
    process(node, json, {});
    return json;
}

Wednesday, January 26, 2011

All about Javascript Functions

If you're a Javascript fan, it's worth reading JavaScript, 5 ways to call a function which nicely explains must to know concepts behind Javascript functions.

Saturday, October 2, 2010

How to build mod_v8 in Ubuntu

mod_v8 is a simple Apache module which enable Server Side JavaScript(SSJS) support in Apache Server. It uses Google V8 engine for the JavaScript execution. What this module basically does is, it adds a Handler for a particular file extension(.v8) and then, those files are treated as SSJS files which then get executed via V8 engine.

While I was building this amazing mod_v8 module, I had to do small modification to the process. So I though it might be worth to have a post with the steps I followed.

1. Checkout V8 development branch
svn checkout http://v8.googlecode.com/svn/branches/bleeding_edge/ v8

2. Build V8 using scons with the library=shared option. Optionally you might need to add arch option too. You can find more info about it at Building V8 in Ubuntu 64 bit
scons library=shared arch=x64

3. Assuming your v8 checkout is at /home/wageesha/v8, building process will create following obj, libv8.so etc.. directories and files within v8 directory. You need to copy this libv8.so into /usr/lib.
cp ~/wageesha/v8/libv8.so /usr/lib

4. Checkout mod_v8 code
svn checkout https://svn.i-want-a-pony.com/repos/mod_v8/trunk/ mod_v8

5. Build mod_v8 using scons. Depending on your scons version, you might need to change
opts = Options('build.py') into opts = Variables('build.py')
and
opts.Add(PathOption(....)) into opts.Add(PathVariable(....))

in the SConstruct. Further, if you haven't copied libv8.so into the /usr/lib directory as in step 3, build process will fail saying
/usr/bin/ld: cannot find -lv8

6. Then open /etc/apache2/apache2.conf and add following lines, with the correct path to libmod_v8.so
LoadModule v8_module /home/wageesha/mod_v8/trunk/libmod_v8.so

<IfModule mod_v8.cpp>
#Adds the v8 handler
AddHandler v8-script .v8
</IfModule>

7. Copy /home/wageesha/mod_v8/trunk/test.v8 into /var/www folder.

8. Restart apache and access test.v8
http://localhost/test.v8

which will then print
Hello World!

in the browser.