MyThinkPond

Random Thoughts on Java, Scala, J2EE, JVM, Linux and Computer Technology

Minimal Maintainable HTML5 Page – Template – index.html

Posted by Venkatt Guhesan on February 25, 2012

Sometimes I keep searching for a starter HTML5 “hello-world” – index.html page. So I thought I’d blog about it so that others who need it can also take advantage of it.

<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Sample HTML5 index.html from MyThinkPond</title>
</head>
<body>
<p>Sample HTML5 index.html from MyThinkPond.wordpress.com</p>
</body>
</html>

Feel free to copy and use as needed.

At some point of time, you’re going to need something more than a ‘hello-world’ and when you cross that road go download the “HTML5 Boilerplate“. Below is a quote from their site on what it’s all about.

HTML5 Boilerplate is the professional badass’s base HTML/CSS/JS template for a fast, robust and future-safe site.

Cheers!

Posted in HTML5 | Tagged: , , , | Leave a Comment »

How-To Install mod_python and the compile error – apxs:Error: Command failed with rc=65536

Posted by Venkatt Guhesan on December 28, 2011

Below are my steps to installing mod_python under Apache HTTPD in CentOS 6.2.

Download mod_python-3.3.1.tgz or a later version of mod_python available here:

http://archive.apache.org/dist/httpd/modpython/

curl -O http://archive.apache.org/dist/httpd/modpython/mod_python-3.3.1.tgz
tar zxvf mod_python-3.3.1.tgz
cd mod_python-3.3.1
./configure
make

You may encounter the following error at this point:
apxs:Error: Command failed with rc=65536

If so… no worries…

This bug has been addressed here:

https://bugzilla.redhat.com/show_bug.cgi?id=465246

Download the patch and (if you can figure out how to patch like I did, I manually opened the file and examined the contents:

diff -rNu mod_python-3.3.1/src/connobject.c mod_python-3.3.1-atomix/src/connobject.c
--- mod_python-3.3.1/src/connobject.c 2006-12-03 05:36:37.000000000 +0100
+++ mod_python-3.3.1-atomix/src/connobject.c 2008-10-02 14:10:02.000000000 +0200
@@ -139,7 +139,7 @@
bytes_read = 0;

while ((bytes_read < len || len == 0) &&
- !(b == APR_BRIGADE_SENTINEL(b) ||
+ !(b == APR_BRIGADE_SENTINEL(bb) ||
APR_BUCKET_IS_EOS(b) || APR_BUCKET_IS_FLUSH(b))) {

const char *data;

Based on the file contents… Edit the following file below the unzipped folder (/mod_python-3.3.1/src/connobject.c)

Change line 142 from
!(b == APR_BRIGADE_SENTINEL(b) ||
to
!(b == APR_BRIGADE_SENTINEL(bb) ||

Save the file. And then run “make” again.

This time you should see no errors. The last step is to run “make install” and you’re all set.

Now don’t forget to edit your main config and add
LoadModule python_module /usr/lib64/httpd/modules/mod_python.so
and if your configuration uses ClearModuleList, then also
AddModule mod_python.c

Next, Configure mod_python to work with Apache

Edit your httpd.conf (/etc/httpd/conf/httpd.conf), add:


LoadModule python_module /usr/lib64/httpd/modules/mod_python.so

#Under the AddHandler section

<Directory /var/www/html>
    AddHandler mod_python .py
    PythonHandler myscript
    PythonDebug On
</Directory>

To learn what the “AddHandler” does go here:

http://www.modpython.org/live/mod_python-3.2.8/doc-html/tut-what-it-do.html

Stop and Start Apache HTTPD (/etc/init.d/httpd restart)

You’re all set.

To verify, create a file in your DocumentRoot (/var/www/html)

<myscript.py>

#!/usr/bin/python
from mod_python import apache

def handler(req):

    req.content_type = "text/plain"
    req.write("Hello World from Apache HTTPD!")

    return apache.OK

As a last step, goto http://<your_ip>:<your_port_80>/abc.py

You should see, “Hello World from Apache HTTPD!”. Notice that I didn’t goto “myscript.py”. And I have pointed to “myscript” in the AddHandler parameter. This “myscript.py” acts like the Controller in a MVC framework. So if you were installing an application like Django… then Django will act as the gatekeeper passing requests back and forth.

To learn more about the handler, go here:

http://www.modpython.org/live/current/doc-html/inst-testing.html

or here’s an excerpt from that site:

Note that according to the configuration written above, you can also point your browser to any URL ending in .py in the test directory. You can for example point your browser to /test/foobar.py and it will be handled by mptest.py. That’s because you explicitely set the handler to always be mptest, whatever the requested file was. If you want to have many handler files named handler1.py, handler2.py and so on, and have them accessible on /test/handler1.py, /test/handler2.py, etc., then you have to use a higher level handler system such as the mod_python publisher (see 3.1), mpservlets or Vampire. Those are just special mod_python handler that know how to map requests to a dynamically loaded handler.

After writing this blog, I noticed that some users may prefer a PHP like feel. This can be done by adding a “Publisher Handler” (PSP).

Edit your httpd.conf (/etc/httpd/conf/httpd.conf), add:


LoadModule python_module /usr/lib64/httpd/modules/mod_python.so

#Change AddHandler to the following

<Directory /var/www/html>
   AddHandler mod_python .psp .psp_
   PythonHandler mod_python.psp
   PythonDebug On
</Directory>

Stop and start httpd (/etc/init.d/httpd restart).

Now create a file “test.psp” under DocumentRoot (/var/www/html)


<%
import time
weekday = time.strftime('%A', time.localtime(time.time()))
message = 'Today is %s.' % weekday
%>
<html><body>
<h2><%= message %></h2>
</html></body>

When you visit http://<your_ip>:<your_port_80>/test.psp

You should see “Today is —” message.

You can learn more about the PSP handler here:

http://webpython.codepoint.net/mod_python_tutorial

Cheers!

Posted in mod_python, Python | Tagged: , , | Leave a Comment »

How to upgrade to Python 2.7 on CentOS

Posted by Venkatt Guhesan on December 28, 2011

If you tried upgrading to Python 2.7 on CentOS, you will quickly find out that the RPM’s don’t exist for this in the repos. So here’s a short summary of what I did to upgrade my Python to 2.7 on CentOS.

Based on a few Google searches… I discovered that a few dependent packages are required before you try upgrading to Python 2.7.


yum -y groupinstall 'Development Tools'
yum -y install openssl-devel* ncurses-devel* zlib*.x86_64</pre>
yum -y install bzip2 bzip2-devel bzip2-libs

Next download the latest tar/gzip/tgz available here:
http://python.org/ftp/python/2.7/


#Download the latest available at this time
curl -O http://python.org/ftp/python/2.7/Python-2.7.tgz
#Unzip/expand file
tar xfz Python-2.7.tgz
#Change Directory to the unzipped folder
cd Python-2.7
#read README file or you can follow the lines below
./configure
#you could also run configure with threads and shared enabled
#./configure --prefix=/opt/python2.7 --with-threads --enable-shared
#Compile
make
#Install
make install
# Exit from your shell and open a new shell/SSH session
# Use the command below to display which path of python is currently active
which python
#To verify if the install succeeded
python -V
#Use UPPERCASE 'V' - not lower-case.
#Output will be "Python 2.7"
#Sometimes you may need to exit out of your shell
#and them come back in to see the version changes.
#So best exit your current shell prompt and reopen
#a new before checking the version.

You’re all set. You’ve just upgraded to Python 2.7.

The next logical step that you need to perform is to install the setup tools that allows you to install modules. (Please note that setuptools is not available for Python 3.0+, instead use Distrubute available here – http://pypi.python.org/pypi/distribute)

Download the latest setuptools for your version of Python (2.7 in this case) from here:

http://pypi.python.org/pypi/setuptools#downloads


curl -O http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg

chmod 775 setuptools-0.6c11-py2.7.egg

sh setuptools-0.6c11-py2.7.egg

#This should install the egg here: /usr/local/lib/python2.7/site-packages/

Next you want to install “PIP”, this enables the download and install of modules in Python:

$ curl -O http://pypi.python.org/packages/source/p/pip/pip-1.0.tar.gz
$ tar xvfz pip-1.0.tar.gz
$ cd pip-1.0
$ python setup.py install # may need to be root</pre>

Now you’re all set. Suppose you wanted to install a module called ‘simplejson’.

You can now do this using command syntax like this:

pip install simplejson

Cheers.

Posted in Python | Tagged: , , , | Leave a Comment »

How to list the package contents of a yum install?

Posted by Venkatt Guhesan on December 25, 2011

Sometimes you need to see what’s installed as part of a package via yum. To see the package contents you can use a utility that’s available on the RHEL/CentOS.


$repoquery --list **packagename**

If you do not have this utility available, you can install it by using this command


$yum install yum-utils

Here is a sample output from looking at the package contents of Apache httpd.


$repoquery --list httpd

/etc/httpd
/etc/httpd/conf
/etc/httpd/conf.d
/etc/httpd/conf.d/README
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/httpd/logs
/etc/httpd/modules
/etc/httpd/run
/etc/logrotate.d/httpd
/etc/rc.d/init.d/httpd
/etc/sysconfig/httpd
/usr/lib64/httpd
/usr/lib64/httpd/modules
/usr/lib64/httpd/modules/mod_actions.so
/usr/lib64/httpd/modules/mod_alias.so
/usr/lib64/httpd/modules/mod_asis.so
/usr/lib64/httpd/modules/mod_auth_basic.so
/usr/lib64/httpd/modules/mod_auth_digest.so
/usr/lib64/httpd/modules/mod_authn_alias.so
/usr/lib64/httpd/modules/mod_authn_anon.so
/usr/lib64/httpd/modules/mod_authn_dbd.so
/usr/lib64/httpd/modules/mod_authn_dbm.so
/usr/lib64/httpd/modules/mod_authn_default.so
/usr/lib64/httpd/modules/mod_authn_file.so
/usr/lib64/httpd/modules/mod_authnz_ldap.so
/usr/lib64/httpd/modules/mod_authz_dbm.so
/usr/lib64/httpd/modules/mod_authz_default.so
/usr/lib64/httpd/modules/mod_authz_groupfile.so
/usr/lib64/httpd/modules/mod_authz_host.so
/usr/lib64/httpd/modules/mod_authz_owner.so
/usr/lib64/httpd/modules/mod_authz_user.so
/usr/lib64/httpd/modules/mod_autoindex.so
/usr/lib64/httpd/modules/mod_cache.so
/usr/lib64/httpd/modules/mod_cern_meta.so
/usr/lib64/httpd/modules/mod_cgi.so
/usr/lib64/httpd/modules/mod_cgid.so
/usr/lib64/httpd/modules/mod_dav.so
/usr/lib64/httpd/modules/mod_dav_fs.so
/usr/lib64/httpd/modules/mod_dbd.so
/usr/lib64/httpd/modules/mod_deflate.so
/usr/lib64/httpd/modules/mod_dir.so
/usr/lib64/httpd/modules/mod_disk_cache.so
/usr/lib64/httpd/modules/mod_dumpio.so
/usr/lib64/httpd/modules/mod_env.so
/usr/lib64/httpd/modules/mod_expires.so
/usr/lib64/httpd/modules/mod_ext_filter.so
/usr/lib64/httpd/modules/mod_filter.so
/usr/lib64/httpd/modules/mod_headers.so
/usr/lib64/httpd/modules/mod_ident.so
/usr/lib64/httpd/modules/mod_include.so
/usr/lib64/httpd/modules/mod_info.so
/usr/lib64/httpd/modules/mod_ldap.so
/usr/lib64/httpd/modules/mod_log_config.so
/usr/lib64/httpd/modules/mod_log_forensic.so
/usr/lib64/httpd/modules/mod_logio.so
/usr/lib64/httpd/modules/mod_mime.so
/usr/lib64/httpd/modules/mod_mime_magic.so
/usr/lib64/httpd/modules/mod_negotiation.so
/usr/lib64/httpd/modules/mod_proxy.so
/usr/lib64/httpd/modules/mod_proxy_ajp.so
/usr/lib64/httpd/modules/mod_proxy_balancer.so
/usr/lib64/httpd/modules/mod_proxy_connect.so
/usr/lib64/httpd/modules/mod_proxy_ftp.so
/usr/lib64/httpd/modules/mod_proxy_http.so
/usr/lib64/httpd/modules/mod_proxy_scgi.so
/usr/lib64/httpd/modules/mod_reqtimeout.so
/usr/lib64/httpd/modules/mod_rewrite.so
/usr/lib64/httpd/modules/mod_setenvif.so
/usr/lib64/httpd/modules/mod_speling.so
/usr/lib64/httpd/modules/mod_status.so
/usr/lib64/httpd/modules/mod_substitute.so
/usr/lib64/httpd/modules/mod_suexec.so
/usr/lib64/httpd/modules/mod_unique_id.so
/usr/lib64/httpd/modules/mod_userdir.so
/usr/lib64/httpd/modules/mod_usertrack.so
/usr/lib64/httpd/modules/mod_version.so
/usr/lib64/httpd/modules/mod_vhost_alias.so
/usr/sbin/apachectl
/usr/sbin/htcacheclean
/usr/sbin/httpd
/usr/sbin/httpd.event
/usr/sbin/httpd.worker
/usr/sbin/httxt2dbm
/usr/sbin/rotatelogs
/usr/sbin/suexec
/usr/share/doc/httpd-2.2.15
/usr/share/doc/httpd-2.2.15/ABOUT_APACHE
/usr/share/doc/httpd-2.2.15/CHANGES
/usr/share/doc/httpd-2.2.15/LICENSE
/usr/share/doc/httpd-2.2.15/NOTICE
/usr/share/doc/httpd-2.2.15/README
/usr/share/doc/httpd-2.2.15/VERSIONING
/usr/share/man/man8/apachectl.8.gz
/usr/share/man/man8/htcacheclean.8.gz
/usr/share/man/man8/httpd.8.gz
/usr/share/man/man8/rotatelogs.8.gz
/usr/share/man/man8/suexec.8.gz
/var/cache/mod_proxy
/var/lib/dav
/var/log/httpd
/var/run/httpd
/var/www
/var/www/cgi-bin
/var/www/error
/var/www/error/HTTP_BAD_GATEWAY.html.var
/var/www/error/HTTP_BAD_REQUEST.html.var
/var/www/error/HTTP_FORBIDDEN.html.var
/var/www/error/HTTP_GONE.html.var
/var/www/error/HTTP_INTERNAL_SERVER_ERROR.html.var
/var/www/error/HTTP_LENGTH_REQUIRED.html.var
/var/www/error/HTTP_METHOD_NOT_ALLOWED.html.var
/var/www/error/HTTP_NOT_FOUND.html.var
/var/www/error/HTTP_NOT_IMPLEMENTED.html.var
/var/www/error/HTTP_PRECONDITION_FAILED.html.var
/var/www/error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
/var/www/error/HTTP_REQUEST_TIME_OUT.html.var
/var/www/error/HTTP_REQUEST_URI_TOO_LARGE.html.var
/var/www/error/HTTP_SERVICE_UNAVAILABLE.html.var
/var/www/error/HTTP_UNAUTHORIZED.html.var
/var/www/error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
/var/www/error/HTTP_VARIANT_ALSO_VARIES.html.var
/var/www/error/README
/var/www/error/contact.html.var
/var/www/error/include
/var/www/error/include/bottom.html
/var/www/error/include/spacer.html
/var/www/error/include/top.html
/var/www/error/noindex.html
/var/www/html
/var/www/icons
/var/www/icons/README
/var/www/icons/README.html
/var/www/icons/a.gif
/var/www/icons/a.png
/var/www/icons/alert.black.gif
/var/www/icons/alert.black.png
/var/www/icons/alert.red.gif
/var/www/icons/alert.red.png
/var/www/icons/apache_pb.gif
/var/www/icons/apache_pb.png
/var/www/icons/apache_pb2.gif
/var/www/icons/apache_pb2.png
/var/www/icons/apache_pb2_ani.gif
/var/www/icons/back.gif
/var/www/icons/back.png
/var/www/icons/ball.gray.gif
/var/www/icons/ball.gray.png
/var/www/icons/ball.red.gif
/var/www/icons/ball.red.png
/var/www/icons/binary.gif
/var/www/icons/binary.png
/var/www/icons/binhex.gif
/var/www/icons/binhex.png
/var/www/icons/blank.gif
/var/www/icons/blank.png
/var/www/icons/bomb.gif
/var/www/icons/bomb.png
/var/www/icons/box1.gif
/var/www/icons/box1.png
/var/www/icons/box2.gif
/var/www/icons/box2.png
/var/www/icons/broken.gif
/var/www/icons/broken.png
/var/www/icons/burst.gif
/var/www/icons/burst.png
/var/www/icons/c.gif
/var/www/icons/c.png
/var/www/icons/comp.blue.gif
/var/www/icons/comp.blue.png
/var/www/icons/comp.gray.gif
/var/www/icons/comp.gray.png
/var/www/icons/compressed.gif
/var/www/icons/compressed.png
/var/www/icons/continued.gif
/var/www/icons/continued.png
/var/www/icons/dir.gif
/var/www/icons/dir.png
/var/www/icons/diskimg.gif
/var/www/icons/diskimg.png
/var/www/icons/down.gif
/var/www/icons/down.png
/var/www/icons/dvi.gif
/var/www/icons/dvi.png
/var/www/icons/f.gif
/var/www/icons/f.png
/var/www/icons/folder.gif
/var/www/icons/folder.open.gif
/var/www/icons/folder.open.png
/var/www/icons/folder.png
/var/www/icons/folder.sec.gif
/var/www/icons/folder.sec.png
/var/www/icons/forward.gif
/var/www/icons/forward.png
/var/www/icons/generic.gif
/var/www/icons/generic.png
/var/www/icons/generic.red.gif
/var/www/icons/generic.red.png
/var/www/icons/generic.sec.gif
/var/www/icons/generic.sec.png
/var/www/icons/hand.right.gif
/var/www/icons/hand.right.png
/var/www/icons/hand.up.gif
/var/www/icons/hand.up.png
/var/www/icons/icon.sheet.gif
/var/www/icons/icon.sheet.png
/var/www/icons/image1.gif
/var/www/icons/image1.png
/var/www/icons/image2.gif
/var/www/icons/image2.png
/var/www/icons/image3.gif
/var/www/icons/image3.png
/var/www/icons/index.gif
/var/www/icons/index.png
/var/www/icons/layout.gif
/var/www/icons/layout.png
/var/www/icons/left.gif
/var/www/icons/left.png
/var/www/icons/link.gif
/var/www/icons/link.png
/var/www/icons/movie.gif
/var/www/icons/movie.png
/var/www/icons/p.gif
/var/www/icons/p.png
/var/www/icons/patch.gif
/var/www/icons/patch.png
/var/www/icons/pdf.gif
/var/www/icons/pdf.png
/var/www/icons/pie0.gif
/var/www/icons/pie0.png
/var/www/icons/pie1.gif
/var/www/icons/pie1.png
/var/www/icons/pie2.gif
/var/www/icons/pie2.png
/var/www/icons/pie3.gif
/var/www/icons/pie3.png
/var/www/icons/pie4.gif
/var/www/icons/pie4.png
/var/www/icons/pie5.gif
/var/www/icons/pie5.png
/var/www/icons/pie6.gif
/var/www/icons/pie6.png
/var/www/icons/pie7.gif
/var/www/icons/pie7.png
/var/www/icons/pie8.gif
/var/www/icons/pie8.png
/var/www/icons/portal.gif
/var/www/icons/portal.png
/var/www/icons/poweredby.png
/var/www/icons/ps.gif
/var/www/icons/ps.png
/var/www/icons/quill.gif
/var/www/icons/quill.png
/var/www/icons/right.gif
/var/www/icons/right.png
/var/www/icons/screw1.gif
/var/www/icons/screw1.png
/var/www/icons/screw2.gif
/var/www/icons/screw2.png
/var/www/icons/script.gif
/var/www/icons/script.png
/var/www/icons/small
/var/www/icons/small/back.gif
/var/www/icons/small/back.png
/var/www/icons/small/binary.gif
/var/www/icons/small/binary.png
/var/www/icons/small/binhex.gif
/var/www/icons/small/binhex.png
/var/www/icons/small/blank.gif
/var/www/icons/small/blank.png
/var/www/icons/small/broken.gif
/var/www/icons/small/broken.png
/var/www/icons/small/burst.gif
/var/www/icons/small/burst.png
/var/www/icons/small/comp1.gif
/var/www/icons/small/comp1.png
/var/www/icons/small/comp2.gif
/var/www/icons/small/comp2.png
/var/www/icons/small/compressed.gif
/var/www/icons/small/compressed.png
/var/www/icons/small/continued.gif
/var/www/icons/small/continued.png
/var/www/icons/small/dir.gif
/var/www/icons/small/dir.png
/var/www/icons/small/dir2.gif
/var/www/icons/small/dir2.png
/var/www/icons/small/doc.gif
/var/www/icons/small/doc.png
/var/www/icons/small/forward.gif
/var/www/icons/small/forward.png
/var/www/icons/small/generic.gif
/var/www/icons/small/generic.png
/var/www/icons/small/generic2.gif
/var/www/icons/small/generic2.png
/var/www/icons/small/generic3.gif
/var/www/icons/small/generic3.png
/var/www/icons/small/image.gif
/var/www/icons/small/image.png
/var/www/icons/small/image2.gif
/var/www/icons/small/image2.png
/var/www/icons/small/index.gif
/var/www/icons/small/index.png
/var/www/icons/small/key.gif
/var/www/icons/small/key.png
/var/www/icons/small/movie.gif
/var/www/icons/small/movie.png
/var/www/icons/small/patch.gif
/var/www/icons/small/patch.png
/var/www/icons/small/ps.gif
/var/www/icons/small/ps.png
/var/www/icons/small/rainbow.gif
/var/www/icons/small/rainbow.png
/var/www/icons/small/sound.gif
/var/www/icons/small/sound.png
/var/www/icons/small/sound2.gif
/var/www/icons/small/sound2.png
/var/www/icons/small/tar.gif
/var/www/icons/small/tar.png
/var/www/icons/small/text.gif
/var/www/icons/small/text.png
/var/www/icons/small/transfer.gif
/var/www/icons/small/transfer.png
/var/www/icons/small/unknown.gif
/var/www/icons/small/unknown.png
/var/www/icons/small/uu.gif
/var/www/icons/small/uu.png
/var/www/icons/sound1.gif
/var/www/icons/sound1.png
/var/www/icons/sound2.gif
/var/www/icons/sound2.png
/var/www/icons/sphere1.gif
/var/www/icons/sphere1.png
/var/www/icons/sphere2.gif
/var/www/icons/sphere2.png
/var/www/icons/tar.gif
/var/www/icons/tar.png
/var/www/icons/tex.gif
/var/www/icons/tex.png
/var/www/icons/text.gif
/var/www/icons/text.png
/var/www/icons/transfer.gif
/var/www/icons/transfer.png
/var/www/icons/unknown.gif
/var/www/icons/unknown.png
/var/www/icons/up.gif
/var/www/icons/up.png
/var/www/icons/uu.gif
/var/www/icons/uu.png
/var/www/icons/uuencoded.gif
/var/www/icons/uuencoded.png
/var/www/icons/world1.gif
/var/www/icons/world1.png
/var/www/icons/world2.gif
/var/www/icons/world2.png

Posted in Linux | Tagged: , , , | Leave a Comment »

Copy to Clipboard – a browser agnostic way to script this functionality

Posted by Venkatt Guhesan on October 31, 2011

Have you tried to present some code or sample content for the end-user that you wanted to allow them to easily copy to their clip-board?

If you use the JQuery Javascript library then you can use the plugin called zClip available here – zClip

Using zclip you can attach an event to a button or a text-area such that when the event occurs, the content is copied to the clipboard.

Well, if you are not using JQuery as the swiss-army knife for your Javascript toolkit, then here is an alternative that can come in handy:

It’s called “ZeroClipboard” and it’s available from code.google.com

Both tools do the following, they replace or overlay a flash plugin on top which then passes the needed content to the native clipboard. Both solutions do not have a graceful way to do copy to clipboard especially if you do not have flash enabled or installed for your platform – browser.

As the browser matures, maybe this functionality might be allowed natively from a Javascript code but until then, the ZeroClipboard and zClip are the best ways to implement a “copy-to-clipboard”.

 

Posted in Javascript, JQuery | Tagged: , , , , , , | Leave a Comment »

Grails – Groovy – Alternative to HttpBuilder – adding headers to your HTTP request

Posted by Venkatt Guhesan on October 24, 2011

Developing with Grails and Groovy can be a blessing and and pain all at the same time. The development moves at a rapid rate but when you decide to include libraries that depend on other libraries, your pain starts to build up. For example, when you include the module “HttpBuilder”in your project you may run into issues with Xerces and xml-apis, especially when you attempt to deploy the WAR file under Tomcat. These libraries are included as part of Tomcat and so an older version of those classes may give you a heartburn.

If your objective is to use some raw HTTP classes to create your requests and responses, then you can use the basic URL class to do most of the raw connection options. Although using HttpBuilder makes it a clean implementation, the URL class gives you very similar power without all the overhead of including the dependency classes.


def urlConnect = new URL(url)
def connection = urlConnect.openConnection()
//Set all of your needed headers
connection.setRequestProperty("X-Forwarded-For", "<your ip address>")

if(connection.responseCode == 200){
responseText = connection.content.text
}
else{
println "An error occurred:"
println connection.responseCode
println connection.responseMessage
}

So the trick to the Groovy URL class is to use the “openConnection()” method and then gain access to some of the raw functionality.

Cheers.

Posted in Grails, Groovy, Uncategorized | Tagged: , , , , | Leave a Comment »

Grails H2 Database 1.4.M1 Issue

Posted by Venkatt Guhesan on August 28, 2011

I’ve noticed a peculiar behavior that I’m documenting here for others. I’m using Grails 1.4.M1 and it bundles with it H2 database version [H2 1.2.147 (2010-11-21)]

If you decide to run H2 in a server mode, you would most likely download the latest version of H2 Database from the website. As of writing this article, the stable version of H2 is [H2 1.3.158 (2011-07-17)].

Issue: If you run your Grails application using 1.2.147 and your external H2 database happens to be 1.3.158, then the SELECT’s work fine but when you run INSERT or UPDATE statements, they are not committed.

When I switched the H2 version to 1.2.147, then the <DOMAIN>.save() worked fine.

 

Posted in Grails | Tagged: , , | Leave a Comment »

Desktop timer for Pomodoro Technique users

Posted by Venkatt Guhesan on July 14, 2011

If you are looking to improve your productivity when you are working on a mindful task then Pomodoro Technique is the way.

It’s a technique where you sit for twenty-five minutes at a time focused on one task. At the end of the cycle, you take a “required” short break and then start on your next cycle.

Why twenty-five minutes and not thirty? If you find out why, let me know.

Well, Célio Cidral Junior at http://www.tomighty.org/ has created a desktop timer that helps you organize and execute your tasks using the Pomodoro technique.

Give it a try, you won’t be disappointed.

Cheers

 

Posted in General, Pomodoro | Tagged: , | Leave a Comment »

Java Tools for Source Code Optimization and Analysis

Posted by Venkatt Guhesan on July 14, 2011

Below is a list of some tools that can help you examine your Java source code for potential problems:

1. PMD from http://pmd.sourceforge.net/
License: PMD is licensed under a “BSD-style” license

PMD scans Java source code and looks for potential problems like:

* Possible bugs – empty try/catch/finally/switch statements
* Dead code – unused local variables, parameters and private methods
* Suboptimal code – wasteful String/StringBuffer usage
* Overcomplicated expressions – unnecessary if statements, for loops that could be while loops
* Duplicate code – copied/pasted code means copied/pasted bugs

You can download everything from here, and you can get an overview of all the rules at the rulesets index page.

PMD is integrated with JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, CodeGuide, NetBeans/Sun Java Studio Enterprise/Creator, IntelliJ IDEA, TextPad, Maven, Ant, Gel, JCreator, and Emacs.

2. FindBug from http://findbugs.sourceforge.net
License: L-GPL

FindBugs, a program which uses static analysis to look for bugs in Java code. And since this is a project from my alumni university (IEEE – University of Maryland, College Park – Bill Pugh) , I have to definitely add this contribution to this list.

3. Clover from http://www.cenqua.com/clover/
License: Free for Open Source (more like a GPL)

Measures statement, method, and branch coverage and has XML, HTML, and GUI reporting. and comprehensive plug-ins for major IDEs.

* Improve Test Quality
* Increase Testing Productivity
* Keep Team on Track

Fully integrated plugins for NetBeans, Eclipse , IntelliJ IDEA, JBuilder and JDeveloper. These plugins allow you to measure and inspect coverage results without leaving the IDE.
Seamless Integration with projects using Apache Ant and Maven. * Easy integration into legacy build systems with command line interface and API.
Fast, accurate, configurable, detailed coverage reporting of Method, Statement, and Branch coverage.
Rich reporting in HTML, PDF, XML or a Swing GUI
Precise control over the coverage gathering with source-level filtering.
Historical charting of code coverage and other metrics.
Fully compatible with JUnit 3.x & 4.x, TestNG, JTiger and other testing frameworks. Can also be used with manual, functional or integration testing.

4. Macker from http://innig.net/macker/
License: GPL

Macker is a build-time architectural rule checking utility for Java developers. It’s meant to model the architectural ideals programmers always dream up for their projects, and then break — it helps keep code clean and consistent. You can tailor a rules file to suit a specific project’s structure, or write some general “good practice” rules for your code. Macker doesn’t try to shove anybody else’s rules down your throat; it’s flexible, and writing a rules file is part of the development process for each unique project.

5 EMMA from http://emma.sourceforge.net/
License: EMMA is distributed under the terms of Common Public License v1.0 and is thus free for both open-source and commercial development.

Reports on class, method, basic block, and line coverage (text, HTML, and XML).

EMMA can instrument classes for coverage either offline (before they are loaded) or on the fly (using an instrumenting application classloader).

Supported coverage types: class, method, line, basic block. EMMA can detect when a single source code line is covered only partially.

Coverage stats are aggregated at method, class, package, and “all classes” levels.

Output report types: plain text, HTML, XML. All report types support drill-down, to a user-controlled detail depth. The HTML report supports source code linking.

Output reports can highlight items with coverage levels below user-provided thresholds.

Coverage data obtained in different instrumentation or test runs can be merged together.

EMMA does not require access to the source code and degrades gracefully with decreasing amount of debug information available in the input classes.

EMMA can instrument individial .class files or entire .jars (in place, if desired). Efficient coverage subset filtering is possible, too.

Makefile and ANT build integration are supported on equal footing.

EMMA is quite fast: the runtime overhead of added instrumentation is small (5-20%) and the bytecode instrumentor itself is very fast (mostly limited by file I/O speed). Memory overhead is a few hundred bytes per Java class.

EMMA is 100% pure Java, has no external library dependencies, and works in any Java 2 JVM (even 1.2.x).

6. XRadar from http://xradar.sourceforge.net/
License: BSD (me thinks)

The XRadar is an open extensible code report tool currently supporting all Java based systems. The batch-processing framework produces HTML/SVG reports of the systems current state and the development over time – all presented in sexy tables and graphs.

The XRadar gives measurements on standard software metrics such as package metrics and dependencies, code size and complexity, code duplications, coding violations and code-style violations.

7. Hammurapi from Hammurapi Group
License: (if anyone knows the license for this email me Venkatt.Guhesan at Y! dot com)

Hammurapi is a tool for execution of automated inspection of Java program code. Following the example of 282 rules of Hammurabi’s code, we are offered over 120 Java classes, the so-called inspectors, which can, at three levels (source code, packages, repository of Java files), state whether the analysed source code contains violations of commonly accepted standards of coding.

Relevant Links:

http://en.sdjournal.org/products/articleInfo/93

http://wiki.hammurapi.biz/index.php?title=Hammurapi_4_Quick_Start

8. Relief from http://www.workingfrog.org/
License: GPL

Relief is a design tool providing a new look on Java projects. Relying on our ability to deal with real objects by examining their shape, size or relative place in space it gives a “physical” view on java packages, types and fields and their relationships, making them easier to handle.

9. Hudson from http://hudson-ci.org/
License: MIT

Hudson is a continuous integration (CI) tool written in Java, which runs in a servlet container, such as Apache Tomcat or the GlassFish application server. It supports SCM tools including CVS, Subversion, Git and Clearcase and can execute Apache Ant and Apache Maven based projects, as well as arbitrary shell scripts and Windows batch commands.

10. Cobertura from http://cobertura.sourceforge.net/
License: GNU GPL

Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage.

11. SonarSource from http://www.sonarsource.org/ (recommended by Vishwanath Krishnamurthi – thanks)
License: LGPL

Sonar is an open platform to manage code quality. As such, it covers the 7 axes of code quality:

Architecture & Design, Duplications, Unit Tests, Complexity, Potential bugs, Coding rules, Comments.

(Article was originally published on my first blog at blogspot.com)

Posted in Code Analysis, Java | Tagged: , , | 2 Comments »

Not all Tomcat 6 classloaders must be equal

Posted by Venkatt Guhesan on July 1, 2011

Today, while doing some Grails development I came across a peculiar issue that perplexed me and I’m documenting it for all others to benefit. (Also see my other blog from today for the issue that started this journey).

Here are my specifications:

Development Machine

  • Windows-7, 64-bit
  • java version “1.6.0_24″
    Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
    Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)
  • Grails 1.4.0.M1
  • Tomcat – apache-tomcat64-6.0.32

Local Deployment Server

  • Windows Vista, 32-bit
  • java version “1.6.0_26″
    Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
    Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)
  • Grails (does not matter since I’ll be deploying a WAR)
  • Tomcat – apache-tomcat-6.0.32 (32-bit version of Tomcat)

Since the issue we are taking about deals with Grails, let’s get into he details of how I ended up with the WAR. In Grails, I issued the following command:

>grails war abcdefg.war

This creates a war file. When deployed under the 64-bit Tomcat, no issues. But when deployed under the 32-bit Tomcat. I was getting the

Jul 1, 2011 10:08:25 AM org.apache.catalina.core.StandardContext start
SEVERE: Error listenerStart

(See my previous article on how I enabled debugging to get to the root cause of the issue.)

But after some debugging, what it boiled down to was a “Class Cannot Be Found” error:



Jul 1, 2011 11:29:25 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class org.codehaus.groovy.grails.web.util.Log4jConfigListener
java.lang.ClassNotFoundException: org.codehaus.groovy.grails.web.util.Log4jConfigListener

In examining the war file, the JAR file named “grails-web-1.4.0.M1.jar” under the “/WEB-INF/lib” does not contain the needed “Log4jConfigListener” class under “grails-web-1.4.0.M1.jar\org\codehaus\groovy\grails\web\util\”

But clearly this class is inside this other JAR file named “grails-plugin-logging-1.4.0.M1.jar” under “\org\codehaus\groovy\grails\web\util\”.

And this identical war file works under the 64-bit Tomcat but not under the 32-bit Tomcat.

So how did I resolve the issue? I upgraded my 32-bit box to Tomcat 7.0 and this made it work. But clearly, there is an underlying issue with the class-loader under the 32-bit Tomcat 6.0.32.

Maybe someone reading this will have the answer to this issue.

Cheers!

Posted in Grails, Groovy, Java, Tomcat, web development | Tagged: , , , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.