Wednesday, July 21, 2010

IE CSS size limit

We ran into an interesting issue with IE, we aggregate CSS at build time per JSP file and recently we introduced Jquery and colorbox into the current release that caused the css size to grow beyond 300K. Everything would work find locally as we dont do aggregation in development mode but things wont work in QA environment on IE, Firefox would work fine. Some of the images wont show up in IE no matter what you do and one of the developer found that IE will ignore any css beyond 288 KB and even gzipping content doesn't matter because its the final size the IE will have to parse that matters. Fortunately the solution was to just create two aggregate css instead of one and that solved the issue.

More details on this are at

SVN revert a changeslist

Just discovered SVN uses merge command to revert a changelist. Suppose some developer checks in a changelist number 20404 in wrong branch, use the below command to revert it

svn up
svn merge -r 20404:20403
svn st
svn diff
svn ci

Just remember to give the higher changelist number before lower one and use svn diff to diff the changes before commiting it.

Saturday, July 17, 2010

Dumping RabbitMQ queue contents

Apparently there is no easy way to look at the content of messages in RabbitMQ. the list_queues only will give you a count but if you want to look at all the messages content then there is no easy way. I had few thumbnail generation messages in the queue that the code failed to ACK because of exceptions in code. Now I want to see what files are stuck for thumbnail generation. The best way is to restart the program and Rabbit would redeliver the messages but we cant restart a live system. So the trick is to just write a python program that would consume the message but wont acknowledge it.

import sys
from amqplib import client_0_8 as amqp

def process_message(msg):
     print "================================================="
     print "Properties ="
     print "Body=" 
     print msg.body

if __name__ == '__main__':
    if len(sys.argv) < 5:
       print "Usage python mq_url mq_user mq_pass mq_vhost mq_exchange mq_queue_name mq_routing_key"
    mq_url = sys.argv[1]
    mq_user = sys.argv[2]
    mq_pass = sys.argv[3]
    mq_vhost = sys.argv[4]
    mq_exchange = sys.argv[5]
    mq_queue_name = sys.argv[6]
    mq_routing_key = sys.argv[7]
    conn = amqp.Connection(host=mq_url,
    chan =;
    chan.queue_declare(queue=mq_queue_name, durable=True,
        exclusive=False, auto_delete=False);
    chan.exchange_declare(exchange=mq_exchange, type="direct", durable=True,
    chan.queue_bind(queue=mq_queue_name, exchange=mq_exchange,

    print "Consumer consuming messages from %s press CTRL+C when all messages are dumped" % mq_queue_name
    chan.basic_consume(queue=mq_queue_name, no_ack=False,
        while chan.callbacks:

Thursday, July 15, 2010

Parsing xml file using SAX file in python

Again I love simplicity of python. I had a xml file that sometimes can range from 1KB to 100MB so I can't use ElementTree. Python SAX  seems similar to java minus the verbose nature of java.

My xml file looks something like
<restorezipmeta messageid="UUID" outputzippath="" resultendpoint="http://XXX.7080/rest/private/RestoreRestService/1.0" userid="1">

  <restoreentry logicalpath="/Shared/kpatel/CustomRules.rtf" physicalpath="/home/kpatel/try/About_Ubuntu_[Russian].rtf"></restoreentry>

  <restoreentry logicalpath="/Shared/kpatel/BrowserPAC.doc" physicalpath="/home/kpatel/try/Derivatives_of_Ubuntu.doc"></restoreentry>

 and here is a small sample to parse the xml using SAX api and create the zip. All you need to do is to create the ContentHandler like you do in Java minus the verbose nature

import zipfile
import os, sys
import logging
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
logger = logging.getLogger("restore_consumer")

class RestoreXmlHandler(ContentHandler):
    def __init__ (self):
        print "init"
    def startElement(self, name, attrs):
        if name == "RestoreZipMeta":
            self.zipFilePath = attrs.get('outputZipPath')
  "Creating zip %s" % self.zipFilePath)
            self.restoreZip = zipfile.ZipFile(self.zipFilePath, "w", zipfile.ZIP_DEFLATED)
        elif name == "RestoreEntry":
            physicalPath = attrs.get('physicalPath')
            logicalPath = attrs.get('logicalPath')
            logger.debug("Adding physicalPath=%s to zip %s at logicalPath=%s" % (physicalPath, self.zipFilePath, logicalPath))
            self.restoreZip.write(physicalPath, logicalPath)

    def characters (self, ch):

    def endElement(self, name):
        if name == "RestoreZipMeta":
  "closed zip %s" % self.zipFilePath)

def processMessage(restoreMetaFile):
    parser = make_parser()   
    curHandler = RestoreXmlHandler()
    f = open(restoreMetaFile)

if __name__ == '__main__':

creating zip file in python and compression

I am a java programmerr but sometimes program in python.I must say python is very easy to do stuff that requires very verbose coding in java. To create a zip file in python all you need to do is :

import zipfile
restoreZip = zipfile.ZipFile("", "w")
restoreZip.write(physicalPathOfFile, logicalPathOfFileInZip)

Now python doesnt compress by default so you need to add compression mode while opening zip file

restoreZip = zipfile.ZipFile("", "w", zipfile.ZIP_DEFLATED)

solved the issue

Tuesday, July 6, 2010

Java redis client for begineers

I had to use redis in our project in both python and Java. In this post I will cover a basic example of generating counters using Redis. I had to use cassandra db to push data but cassandra doesnt have autoincrement counters at this moment so we will use redis till the new version of cassandra supports it. Redis has memcache like API but the advantage is that its has atomic operations and the data is saved so server restarts will survive the data. The only disadvantage I see is that the java client yet doesnt support consistent hashing but eventually it will.

  1.  Install the redis server by following
  2.  run it using ./redis-server
  3.  Download JRedis client from
  4.  Run the below program.

import org.jredis.JRedis;
import org.jredis.RedisException;
import org.jredis.connector.ConnectionSpec;
import org.jredis.ri.alphazero.JRedisService;
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;

public class RedisCounterService {

private String host;

private int port;

private int connectionCount = 3;

private JRedis jredis;

public void setHost(String host) { = host;

public void setPort(int port) {
this.port = port;

public void setConnectionCount(int connectionCount) {
this.connectionCount = connectionCount;

public void init() {
ConnectionSpec connectionSpec = DefaultConnectionSpec.newSpec(host, port, 0, null);
/*This is a connection pool*/
jredis = new JRedisService(connectionSpec, connectionCount);

public Long getNextRevisionNumber(String counterName) throws RedisException {
//Redis API is thread safe so you dont need to synchronize
long nextRevisionNumber = jredis.incr(counterName);
return nextRevisionNumber;

public static void main(String []args) throws Exception {
RedisCounterService redis = new RedisCounterService();
String counter = "test" + System.currentTimeMillis();
System.out.println(1==redis.getNextRevisionNumber(counter + "new"));

Eclipse disable pydev CTRL+SHIFT+T and enable Java open type binding

I am a java programmer but I can code in python too so for some work I had to code in python and I installed pydev in eclipse but It messed up with my Open Type (CTRL +SHIFT+ T) binding and when I now hit that it asks me a popup to tell whether to open python class browser or java open type, off course I hate it because 99% of the time I want Java open Type. The way to get it working back was to go to

Windows->preferences->General->Keys  and go to the python "Show Class Browser" binding and instead of choosing "In Windows" choose "Pydev editor scope" and you are all set.

Friday, July 2, 2010

Ubuntu VirtualBox and no sound in Windows7 issue

My Windows7 crashed and as we do our most of the development in ubuntu it makes sense to install ubuntu as host. But when I installed Windows7 there was no sound coming in it. Thanks to ubuntu community the solution was tedious due to my mistake but finally I did it. The way to do is :

1) Make sure you have proper virtualbox version. I had the OSE version but my ubuntu is lucid version so I needed that virtual box.  If you get "Error: Conflicts with the installed package 'virtualbox-ose'" that means you have to use Applications->Ubuntu Software Centre to uninstall all virtualbox OSE package (in my case there were 3 packages).
2)  Download and install the virtualbox lucid version. It will automatically detect old hardisks and upgrade it.
3) You need to VM settings in virtualbox and go to audio and select Alsa Ac97 driver.
4) start your VM and now go to realtek AC97 driver 
5) When installing if it complains choose install anyways option and restart your VM

And you should have sound :).