Sunday, August 12, 2007

Configuring SSO

After analyzing JOSSO and CAS as candidates for SSO , the JOSSO implementation seems very promising.
What is SSO ?

Josso
-----

Positives
--------------------
1) Just works. Can easily embed my application to the SSO framework.
2) Out-of-the box seem less integration with custom made tomcat. Inbuilt SSL configurations.
3) To-Do steps
i) Download Josso tomcat
ii) Refer to http://www.josso.org/tomcat55-howto.html
iii) JAVA_OPTS=-Djava.security.auth.login.config=..\conf\jaas.conf (Spent a romantic night for this. Had to write it )
iv) For getting the login data from the DB refer to this


Negatives
--------------------
1) Pathetic Docs
2) In-active community.

CAS
---
I could not get CAS working on my system. I know it works, but somehow the jigsaw is not complete. The community is very active and vibrant.Updated wiki . Let me know if anyone gets it working.

Thursday, August 09, 2007

"C" the Bug - Part I

1. Find the bug.

All necessary Headers included like stdio.h, stdlib.h, string.h etc.

int main()
{
char *str = NULL;
int i = 1;
str = (char*)malloc(sizeof(char)*2);
if(!str) {
printf("Malloc Failed\n");
exit(1);
}
strcpy(str, "Hi");
printf("%c\n", str[i*i+i/i]);
str = (char *)realloc(str, sizeof(char)*20);
if(!str) {
printf("Realloc Failed\n");
exit(1);
}
strcpy(str, "Hello World\n");
i = 2;
printf("%c\n", str[i*i+i/i]);
return 0;
}

2. In a doubly linked list, the nodes are paired and these pairs have to be reversed.

For Ex: If the linked is as below,
1--2--3--4--5--6
Output should be:
5--6--3--4--1--2

Struct is as below:
typedef struct node {
int data;
struct node *next;
struct node *prev;
} NODE;

Handle all the conditions. This question was asked in Microsoft Interview.
Deadline is on 18th Aug 2007.
Send it to me at arvind.bhushan@gmail.com or post in the comment.

Challenging the Code
--------------------
* You need to have trust in me.
* Specify in your mail or comments as "Challenge".
* Rs.50/person.
* Max. challenge amount = Rs.(No. of Persons challenging * 50). Conditions Apply.
* If your code breaks under any conditions, you will lose the amount.
* If your code works smooth, you will win the Total Challenged amount.
#Note#
The challenge amount increases if anyone supports me. Specify in your mail or comments as "Support". For those supporting me will definitely get a share if someone loses the challenge.

If not interested in challenge or support, you can also mail or post comment as "Fun".

Friday, August 03, 2007

Curiously Recurring Template Pattern (CRTP)

The funny named phrase is used to describe the use of a derived class type as a template parameter for its own base class! In simplest form, this is what I'm talking about:

template < typename T >
class Base{ };

class Derived : public Base< Derived > { };

Let me give a very simple example of where this might be useful. Suppose you want to keep track of how many objects of a particular class are alive at any point in time:

Simple approach is:
1) Keep a private static "count" member in the class
2) Increment the count in every constructor
3) Decrement the count in the destructor
4) Provide a public interface to query the value of the count

This approach is fine. However you have to do this for every class that you want to count the objects of. CRTP can pitch in and provide an elegant solution that is highly reusable.

template < typename CountedType >
class ObjectCounter
{
  private:
    static int counter;

  protected:
    ObjectCounter()
    {
      ++( ObjectCounter< CountedType >::counter );
    }

    ObjectCounter( const ObjectCounter< CountedType >& obj )
    {
      ++( ObjectCounter< CountedType >::counter );
    }

    ~ObjectCounter()
    {
      --( ObjectCounter< CountedType >::counter );
    }

  public:
    static int live()
    {
      return ObjectCounter< CountedType >::counter;
    }
};

template < typename CountedType >
int ObjectCounter< CountedType >::counter = 0;

Any class that wants to do object counting will inherit from the above class as follows:

class TestClass : public ObjectCounter< TestClass >{ };

TestClass::live() will give the number of objects alive at that point in time.