Tuesday, April 20, 2010

J2EE: This hack on Google Appengine lets you create a HSSF Workbook.

The Problem : Not able to create a HSSFWorkbook on Google App Engine.

But, I was able to receive an Excel sheet uploaded from a browser , parse it into something meaningfull using the POI API http://poi.apache.org/ (Which i should say is really cool, considering that i'm a newbie to web application development, i never knew it was that easy to parse an Excel file).

Now, when i try to create an Excel sheet and send it back to the browser dynamically - google appengine sent back a 500 Internal server error. On further reading i realised that POI Api was not supported on app engine. But half my work was already done, the creation part had to work otherwise the whole effort would be a waste of time. Being an Open Source Software user,  i downloaded POI Sources from the link mentioned above. There was good documentation on how to build it. The idea was i would remove the unnecessary parts if any and rebuild it (Which , i later on figured out was NOT needed ) with the white listed classes , if possible.

First things first. I went and looked at the logs to figure of what exactly went wrong. I saw it to find out which line caused the exception, so that i can start from there. The logs on appengine said :

/GetExcelFile
java.lang.NullPointerException
at org.apache.poi.hssf.record.WriteAccessRecord.setUsername(WriteAccessRecord.java:101)
at org.apache.poi.hssf.model.Workbook.createWriteAccess(Workbook.java:1071)
at org.apache.poi.hssf.model.Workbook.createWorkbook(Workbook.java:335)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:170)
at com.devicemantra.coordinate.util.CExcelWriter.(CExcelWriter.java:48)
at com.devicemantra.coordinate.ds.tests.GetExcelFile.doGet(GetExcelFile.java:27)


Now,  i fired up vim and opened Workbook.java  +1071. Following is the line of code which caused the exception :


 private static WriteAccessRecord createWriteAccess() {
        WriteAccessRecord retval = new WriteAccessRecord();

        try {
            retval.setUsername(System.getProperty("user.name"));
        } catch (AccessControlException e) {
                // AccessControlException can occur in a restricted context
                // (client applet/jws application or restricted security server)
                retval.setUsername("POI");
        }
        return retval;



The code below tried to get System.getProperty("user.name").  And used this as a parameter to setUserName(). Since this system variable was not set, it sent null and hence the NullPointerException.

I thought that the probable solution was to set this property in appengine-web.xml. So that this call  System.getProperty("user.name") would return something and not raise a Null Pointer Exception. 
I modified appengine-web.xml to have this : 





Now, i presumed that the call System.getProperty("user.name")  would return voicestreams and i would see some other exception. So i searched the files for System.getProperty( to see whether any other calls were made, so that i could add entries in appengine-web.xml. But, i got lucky there were no other call except for this one.  

I built my project and deployed it on appengine. I got my excel file back to the browser, i opened it and i saw what i expected it to have :) !. 

Conclusion: 

A call to the Constructor  new HSSFWorkbook() fails on appengine production, but will not raise an exception on Development server. The above hack solved it. Hope this post helps someone, somewhere. As an aside, the call to HSSFWorkbook(POIFSFileSystem fs) never failed. I had used this as below for receving uploaded excel files.

HSSFWorkbook lExcelWorkBook = new HSSFWorkbook(new POIFSFileSystem(mInputStream)); 
and it worked. But, the call to new HSSFWorkbook() fails on GAE production. 

The other alternative is to upload an Excel file to GAE and store it as a blob, when you need to create an Excel file read the blob into an InputStream and use  new HSSFWorkbook(new POIFSFileSystem(mInputStream));  to create the  workbook. I have not tried this but i think it will work, since it worked for me while reading the uploaded Excel sheet. 

Monday, December 21, 2009

Creating a thread pool library in C++.

This blog is a continuation of the previous blog , which showed you how to create a Thread class in c++.

Monday, September 14, 2009

Simple threads implementation in C++.

I was looking for a simple thread library implementation in C++, something like Java's Thread class. I could not find anything ready and easy to use. I have come up with a simple implementation of a Thread class in C++. Please note that I do not claim to have implemented threads, i have just modelled an abstraction called Thread, which can be sub classed by your classes.
POSIX pthreads are used for threading. This library is just a wrapper around it.

I was actually trying to build a ThreadPool library using C++. So, I had to model a Thread Class so that the Thread Pool implementation will be easier (i thought) to conceptualise.

So this is how the Thread class looks like.

#ifndef __VTHREAD_H__
#define __VTHREAD_H__

#include

/* Abstract base class for Thread */

class Thread
{
        public:
                        Thread();
                        int start();
                        virtual void execute()=0;
        private:
                        static void *threadExecFunc(void *pData);
                        pthread_t mThreadId;
};
#endif


As it appears from the code, this is an abstract class which is sub-classed by other classes which want to run as a thread. All sub-classes will implement void execute() ,  to get their work done in a thread. 

Following is the implementation of the Thread class:
 

#include
#include
#include

using namespace std;

Thread::Thread()
{
}

int Thread::start()
{
        int ret = pthread_create(&mThreadId, NULL, threadExecFunc, this);
        return ret;
}

void *Thread::threadExecFunc(void *pData)
{
        Thread *lThread = (Thread *) pData;
        lThread->execute();
        return NULL;
}
 



As you can see, all you have to do is sub-class Thread class and implement the execute() function in the class. Following is an example of how to implement the Thread class in your programs.


class MyThread: public Thread
{
     public:
                    MyThread(/* Get some data to work on */) 
                   {}

                    void execute()
                    {
                            while( bHasWorkToDo == true )
                            {
                                    doSomeWork();
                            }

                    }

}
                 

int main()
{
           Thread *lMyThread  = new MyThread(/* Pass some data to work on */);
            lMyThread->start();
}      

        
 

First, I create an instance of MyThread and then i call start() on the object. 
This results in MyThread::execute() being executed.  Note that all the calls to the pthread library have been encapsulated within the Thread class.


There are many features missing though , for example there is no way to join, destroy , yield etc. I have not even thought about how to implement those methods on this Thread class. 


Please take a look at the Java's Thread class here to get an idea of what else can be added to this class here -  http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html




















Followers

About Me

I'm a software developer with interests in Design Patterns, Distributed programming, Big Data, Machine Learning and anything which excites me. I like to prototype new ideas and always on the lookout for tools which help me get the job done faster. Currently, i'm loving node.js + Mongodb.