Adding/ Removing Browser Authentication for tomcat app

May 10th, 2012

There can be many cases in your projects where you would like to add a browser authentication for your project. But there will be a few where you will actually need to remove that. That’s what happen to me today. Checked out code for a project and tried to run on local tomcat instance, everything worked fine. When tried to open up the app like localhost:8080/myapp, browser asked for username and password. I had no idea about username and password.

So started by googling about removing browser authentication in tomcat, that was a mistake. Could not find a single site talking about removal of authentication. After spending 20 minutes, I got the idea to actually look for- how to set the browser authentication. And bam- that worked.

In your apps web.xml, there will be setting for <security-constraint> where you can set up the <login-config> as basic (or actually remove in my case).

<!–
Define the Members-only area, by defining
a “Security Constraint” on this Application, and
mapping it to the subdirectory (URL) that we want
to restrict.
–>
<security-constraint>
<web-resource-collection>
<web-resource-name>
Entire Application
</web-resource-name>
<url-pattern>/members/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>member</role-name>
</auth-constraint>
</security-constraint>
<!– Define the Login Configuration for this Application –>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Club Members-only Area</realm-name>
</login-config>

Source point 5 of  http://oreilly.com/java/archive/tomcat-tips.html

kamal tomcat ,

100 Door puzzle

April 12th, 2012

Puzzle: There are various versions of the puzzle, there are 100 doors (or some other object), and 100 cops (prisoners). All the doors are closed initially. Cop 1 goes and opens up all the the doors, Cop 2 goes and closes door at even number 2,4,6 etc. Cop 3 goes and alters the state of doors divisible by 3 (3,6,9), so if the door was closed it is opened and if it was open, it is closed by cop 3. Cop 4 will alter state of all the doors divisible by 4 and so on. At the end we have to find out how many doors are closed now.

Solution: Before getting to a generic solution, lets take case of few doors 1 by one. We can easily see that a door’s state is same if it was visited even number of time (initially closed than (open + close) (open + close).. ), and it is changed if the door is visited odd number of times.
Case of Door 1: Only visited once by Cop1, so the state changes to open
Case of Door 2: Visited twice by cop1 and cop2, so finally it is closed, no change in state (see it is visited even number of times)
Case of Door 3: Visited by 1 and 3- Closed
Case of Door 4: Visited by 1, 2 and 4- Open
Case of Door 5: Visited by 1 and 5- Closed
Case of Door 6: by 1, 2, 3 and 6- Closed
Case 9: 1, 3 and 9- Open
Case 10: 1,2, 5 and 10- Open

So on careful examination, figure out doors which have even number of divisors are not changing state. When will a number have odd number of divisors? A divisor pair of a number z is x*y (always 2, and hence even)

for 50: 1*50, 2*25, 5*10- Even
for 100: 1 * 100, 2*50, 4*25, 5*20, 10*10- Odd (As 10 appears twice)

So for all the perfect squares, we can see we have odd number of divisors and hence the state of the door will be changed.

kamal General

Installing postgres 9

April 12th, 2012

I was trying to install postgres 9 on my Ubunto 11.04 machine, but the GUI installer simply hanged. A little bit of googling provided the workaround.

Follow the steps

1. sudo apt-get install python-software-properties
2. sudo add-apt-repository ppa:pitti/postgresql
3. sudo apt-get update
4. sudo apt-get install postgresql-9.0 libpq-dev

Source
http://www.dctrwatson.com/2010/09/installing-postgresql-9-0-on-ubuntu-10-04/

kamal DBMS, General ,

Why should I keep my Action classes stateless?

March 30th, 2012

Dependency Injection- simplified

March 30th, 2012

I heard this term ‘Dependency Injection’ (DI) a couple of months back, and since then it was at back of my mind and I was planning to read about it. It sounded like some mysterious concept, but it turned out to be a simple one.

DI (or Inversion of Control) is nothing but transferring responsibility of initialization of instance variable to the calling class. This helps in loose coupling and testing of classes.

A very good post on the topic:

http://jamesshore.com/Blog/Dependency-Injection-Demystified.html

Additional Info:
http://stackoverflow.com/questions/130794/what-is-dependency-injection

kamal Design Patterns, Software Engineering

Using Reflection with Android

March 30th, 2012

Faced an interesting problem today. In my android app I needed to show some random strings on the UI (textview).  So obvious solution was to add the strings in string.xml (for supproting i18n), and then fetch the text based on the random number generated.

textView.setText(R.id.text_1);

or if random number is 2

textView.setText(R.id.text_2);

and so on

I realized that only change happening is the last one digit, so this is a good candidate for using reflections.

Class c =  Class.forName("com.startpage.mobile.R$string");
Field field = c.getDeclaredField("tip_"+tip);
tipTextView.setText(field.get(null)));

But this actually gave an Exception for ClassCast, so I realized that field.get(null) is actually returning the resource Id (as shown in R.java class) instead of string value.

The fix found was to use getResources().getString(resource_id) to fetch the string from the resource id

Class c =  Class.forName("com.startpage.mobile.R$string");
Field field = c.getDeclaredField("tip_"+tip);
tipTextView.setText(getResources().getString((Integer)field.get(null)));

kamal Android, Java ,

Isolation levels of a database

March 12th, 2012

In my last post I talked about ACID properties of the database. Out of these, Isolation is one of the most important (and a bit complex) property as this makes sure that no “dirty-ready” occur. There can be various levels of database isolation.

1. Read Uncommitted: Lowest level of isolation, does not implement any checks, a transaction can read while other transaction is writing and hence dirty read can happen.

2. Read Committed. Reads only committed data. Handles dirty read problem. But say in same transaction, the data for a row in table is read multiple times, and if the data gets updated by some other transaction and committed, between the gap period of the two reads in this transaction, the transaction has read different data at different time intervals for same row.

e.g. select name from employee where id=10;

– do something which takes time

select name from employee where id=10;

commit;

The data can change between the two reads

3. Repeatable Reads: This solves the problem mentioned above with the Read Committed. That is, this level does not allow any modifications or deletion in the data which is being accessed by some other transaction. So in last example, if we read the same data multiple times, it is guaranteed to return the same info. Though we cannot update or delete the data in table being accessed by a transaction, we can still add more data. So if the query was

select name from employee;

instead of select name from employee where id=10;

we still could have different data set.

4. Serializable: This is the highest level of restriction added on transactions. This states, that no updation, deletion or addition can happen on a dataset/ table which is being accessed by another transaction. This will solve the problem which is mentioned in previous example. But the solution comes at a high cost as this restriction level will slow down the rate of transactions being executed.

kamal DBMS

ACID test for your database

March 12th, 2012

ACID is a set of properties that each database system needs to guarantee in order to make sure that all the transactions will be processed reliably

Atomicity: A transaction is always committed as a full. That means if the transaction had 10 statements, all 10 are executed fully and committed. A partial execution of transaction will never occur.

Consistency: Database state is always stable. So if a transaction moves database from state A to B, both states are stable.

Isolation: All transactions will execute in isolation and do not interfere with one another (one transaction is updating the data and other is reading at the same time – dirty read is handled).

Durability: Once a transaction is committed, the changes will not be reverted by some hardware or software failure.

kamal DBMS

Interesting Java Facts

March 1st, 2012

I am working with Java for more than 7 years now and still it surprises me with some very basic stuff

Can you guess what is the output of this statement in Java

System.out.println(1+2+” = “+1+2);

3=12

Why? Apparently Java starts treating everything as a String once it has encountered a string in System out statement

kamal Java, kool stuff

Apache downloading the PHP page instead of executing

February 27th, 2012

Faced a weird problem after installing Apache 2 and PHP 5 on my Ubunto 10 machine today. The browser was giving an option to download the page whenever I tried clicking a PHP page link. Clearly the PHP was not getting executed properly by Apache and it was treating it as an unknown file format.

After a couple of hrs figured out this solution.

In httpd.conf file, add following lines

<IfModule mod_php5.c>
  AddType application/x-httpd-php .php .phtml .php3
  AddType application/x-httpd-php-source .phps
</IfModule>

kamal PHP