Thursday, November 22, 2012

Shame: Arresing someone for expressing an opinion on Facebook?

This is the perfect example of how the democratic nation should *not* behave. It is more shameful because it happened in the world's biggest democracy - India. I was taught in the school that in a democratic nation you get a freedom of speech.

Background:

There was a "shutdown" announced in Mumbai for one day after the death of Shri Balasaheb Thackeray. A girl expressed her opinion on the shutdown. She was arrested based on the FIR lodged by the same party member. Moreover, her friend was also arrested for "like"ing that post. Before arrest a clinic of an uncle of the girl was also ransacked.




I deeply admire Balasaheb for his personality, sensitivity and of course sharp fearless remarks. I think he also might not have liked this kind of arrest where freedom of citizens is at risk. What amuses me more is when an FIR is lodged, without investigation a young girl was spontaneously arrested. Though later released on bail it is questionable that was it necessary and more importantly legal to arrest someone like this? Even more miserable is the friend who was arrested, because she just "liked" the post.

Now the government of Maharashtra is facing strong remarks from across the nation. Justice Katju was nice to write personally a series of emails to chief minister of the state to release them immediately, dismiss the charges and take actions against those who were involved. I admire you sir that you work out of your way to uphold the values of democracy and freedom of the people.

In the hope to have a better and safe future "Satyamev Jayate"!

Monday, October 29, 2012

Gujarat Assembly Elections: what an occation!

Gujarat Assembly elections are set to be held from 13th to 17th December. Well what a magnificent time to be in Gujarat and it can never be any bigger! When I was a school kid, elections meant to me just for some school holidays but now when I am concerned about the country and especially the region where I grew up, its feels amazing to follow the news.

On one hand, Modi is preparing hard to become the fourth time Chief Minister of one of the fastest developing states contributing the most to the national economy. On the other side talks are taking strolls about him to take a bigger national role.

Last month I was in Trento, Italy and with some friends we were discussing the Modi (with passionate Dhaval talking about "Vikas" -> "Development" in Gujarat) about what can be his strategy this time to win.

In any case, the Assembly elections were never so interesting without Modi. I grew up in Gandhinagar - the capital of Gujarat where all these ministers live and do campaign very often. I remember last time when Modi set the stage just few meters away from my house and how the people gatherd! He is really an ear-candy! His typical outspoken, blunt and direct personal remarks take heart whether it can be Prime Minister of India, President of opposition party or Army Chief of Pakistan. This time in news I see Modi calling Prime Minister "Moun Mohan Singh" and Shashi Throor's wife "50-crore-rupee girlfriend"!! I can imagine how it will be in Gujarat and I miss it, hopefully when I am back to Gandhinagar this December, at least once I see him live.



On the other hand, If he fails this time which looks extremely difficult, it would be chaos in Gujarat, because the next CM will definietely not be able to keep up with the speed he has created. I have always be opposer of Modi and probably will be because of 2003 elections but I think the Time and Nation really needs some one like him. USA rejected his visa and he responded "I don't need visa of US but I want to see them taking visa of Gujarat.." and ironically in Gujarat Vibrant Summits many international delegates just queue up to invest. After running the state for last 3 terms cumulated to more than 10 years, Gujarat is all set to evaluate the government and it would really be make or break time for him. Lets see!

Update: He won the fourth time. From then, he has started addressing public in "Hindi", increased his visits and appearances in Delhi, there is excitement in BJP about him with outlook for 2014 national elections but there is agitation too over him. 

Tomcat JSP MySQL Linux -- A Starting Kit

This is just to get you started if you are interested in the above configuration. I spent a lot of time exploring many forums for the errors encountered meanwhile which I try to dump here.

First you need following packages:

1. tomcat, tomcat-webapps, tomcat-admin-webapps -- try to get through yum or apt-get repositories.
2. mysql -- again try to do the same to get it

Start all the services:
service mysqld start
service httpd start
service tomcat start

To check if these things are working properly:

1. mysql:

mysql -hlocalhost -uuser -ppassword -e "show databases;"


Note: there is no space between "-u" and "user".

This should show the present databases created by the specified user.

Alternatively you can access the mysql from the browser through:

http://localhost/phpMyAdmin/

For this will need the package "phpmyadmin" installed. Make sure, each time you do some administrative changes, restart services.

2. tomcat:

You should be able to view the examples provided by the tomcat in the browser. To start with a new project, the place of interest will be "webapps" directory where you have to put your project root directory. Create a directory say "test" in "webapps" and in which create a small static html page say "test.html". Try to access the page from your browser with URL

http://localhost:8080/test/test.html

With that you are ready with your basic set up and should move forward with the mysql integration.
Now the basic page which accesses some table of a mysql database is shown below:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.sql.*" %>

<form>
<head>
<title>Testing the structure of jsp, mysql and tomcat!</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<div>

<%

Statement stmt = null;
Connection con = null;
ResultSet resultSet = null;
String url = "jdbc:mysql://localhost/dbname?characterEncoding=UTF-8";
%>lang1
<h2>Display of data from Database</h2>
<table>
 <tr>
  <th>col1</th>
  <th>col2</th>
  <th>col3</th>
  <th>col4</th>
 </tr>
 <%
 try {
  String input = request.getParameter("lang1");
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection(url, "username", "passwd"); 

  stmt = con.createStatement();
  resultSet = stmt.executeQuery("select * from table where col1 = \""+input+"\"");
  while (resultSet.next()) {%>
   <tr>
    <td><%=resultSet.getString(1)%></td>
    <td><%=resultSet.getString(2)%></td>
    <td><%=resultSet.getFloat(3)%></td>
    <td><%=resultSet.getInt(4)%></td>

   </tr>
  <%}
 } catch (SQLException e) {
  e.printStackTrace(System.out);
 } 
 finally {
  try {stmt.close();} catch (Exception e) {
  }
  try {resultSet.close();} catch (Exception e) {
  }
  try {
   if (null != con) {con.close();}
  } catch (SQLException ex) {
   ex.printStackTrace(System.out);
  }
 }
 %>

</table>

</form>
</body>
</html>
In summary, the above code gets a string from the previous page some input in terms of form data. Tries to access the database and gets all the records where col1 value is equal to input and prints it. If you get some java-like errors saying that MySQL is unknown class then you should perform following operations.
The files of interest is:
/usr/share/tomcat/conf/catalina.policy

First you should grant the permission to access the mysql.
grant codeBase "file:${catalina.base}/webapps/test/-" {

     permission java.net.SocketPermission "localhost:8080", "connect";

};

And then make mysql known to tomcat by adding jar in classpath and for that, put mysql-connector-xyz.jar in tomcat_home/lib directory. Add its reference to the classpath by adding following line in tomcat_home/bin/catalina-tasks.xml between

<fileset file="${catalina.home}/lib/mysql-connector-xyz.jar"></fileset>

and restart tomcat service!

Sunday, July 1, 2012

Two-Days-a-City

Recently I have started travelling around a bit. My travel pattern is quite weird because I usually go to a country or most of the time city for 1 week duration to attend a conference or something similar and then I spare two days for me to visit the places around. Under this pattern, I have visited Kolkata, Geneve, Istanbul and soon Seoul. Until Istanbul, it was not so exciting because I was a new traveller and had not much idea how to get maximum out of those two days.

The first trip was to Kolkata, during the time I was in India in 2010. I and Sameer aka Shambaji visited IIT Kharagpur to attend a conference and then we used two days to roam Kolkata city. That was the first trip under the series of Two-days-a city. We were not sure how to do the most out of it. What we did was to roam one day on our own to eat the street food, see the neighbourhood that we stayed (Park Street) and have a local feel of the city. The second day we realised, at the end of the day we were just localised in the neighbourhood and did not get a big picture of the city. So second day, we collected some names from the Guest house reception - some names of famous places to visit. Then we hunted for some taxi drivers with the motto "Maximum places - Minimum price". Once we set that trade-off with a taxi driver, we got off. It was fantastic. Just to make sure everything goes good, we did not bargain too hard in order not to spoil the mood of the driver, because he was going to be the guide of the day too!


In the end, it was fantastic and Shambaji was too happy to say "Chal yaar 50 Rupaiye jyada de dete hai.."  means "Buddy lets give 50 Rupees more.."

Then next, I visited the Switzerland for a winter school in Zinal, Sion. At the end I planned to visit Geneve again under the scheme of "two-days-a-city". Ultimately there were more attendees of the school like me who also adopted the same scheme hence we merged up.  


The tourist office of Geneve was having a lot of good material for the guided tours and we opted one and it was fine. Still I missed some more important places like UN building.

Then, together with my advisor, Paolo, I went to Istanbul. The different element of Istanbul compared to Geneve was its size. Istanbul is world's second most populated city and falls under the category of really large cities. Again, we had a conference there and spared 2 days for tourism. I had a sense that any of the former plans (Taxi or Guided tour) are not going to work for me and we will need some real course to follow to experience the city.


Then, Paolo introduced me to "Lonely Planet" concept. Its a series of Lonely Planet city guides. One day I had a time to do tourism and Paolo was working, so that day Lonely Planet helped me in such a big way that I felt like living that city. The biggest help was zeroing down my choice to buy something for my dear ones. So, the lesson learnt was if you are going to do "two-days-a-city" do buy one of such professional guides even though sometimes they are quite expensive but they do have an edge over most of the tourism literature that you may find elsewhere.


Inspired by that experience, I have already bought the book for Seoul yet another large city next on my list of "Two-Days-a City" series and I am looking forward to it!! If you are also following this kind of travel pattern then try such a book once. Disclaimer: This is not the advertisement of a particular brand of books but this is the suggestion of the concept :D. Ciao! 

Thursday, May 31, 2012

External process calls from java: Runtime.exec() hangs

My recent blog was about calling wget from Java which was talking about a temporal solution. The solution was just to mute the output of the external process so that the output buffer never gets exhausted. Unfortunately, it doesn't solve the bigger problem of calling external processes from Java through Runtime.exec() which can very easily exhaust the system buffer and cause a deadlock at Process.waitFor().

I read a hell lot of articles and spent a couple of hours, looking for the solutions. I got many but all of them were not solving my problem. Then I checked one of the solutions and discussed it at #fedora-java at freenode to see if the problem is really with Java in Fedora! With the help of mizdebsk I was able to spot the concurrency issue in the code which I have posted here. The conclusion after the incorporation of the following code was the external process took some abnormally more time at some point in the program but in the end it was successful to finish rather than waiting forever. I have just defined the block to handle output here, similar block for ErrorStream should be included and if your process expects some input then it should be added too.


class ExecCommand {
private Semaphore outputSem;
private String output;
private Semaphore errorSem;
private String error;
private Process p;

private class OutputReader extends Thread {
 public OutputReader() {
  try {
  outputSem = new Semaphore(1);
  outputSem.acquireUninterruptibly();
  } 
  finally {
   outputSem.release();
  }
 }

 public void run() {
  try {
  StringBuffer readBuffer = new StringBuffer();
  BufferedReader isr = new BufferedReader(new InputStreamReader(p
   .getInputStream()));
  String buff = new String();
  while ((buff = isr.readLine()) != null) {
   readBuffer.append(buff);
   System.out.println(buff);
  }
  output = readBuffer.toString();
  outputSem.release();
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
}

public ExecCommand(String command) {
 try {
  p = Runtime.getRuntime().exec(makeArray(command));
  new OutputReader().start();
  new ErrorReader().start();
  p.waitFor();
 } catch (IOException e) {
  e.printStackTrace();
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
}

public String getOutput() {
 try {
   outputSem.acquireUninterruptibly();
 } 
 finally {
   outputSem.release();
 }
 String value = output;
 outputSem.release();
 return value;
}

private String[] makeArray(String command) {
 ArrayList<String> commandArray = new ArrayList<String>();
 String buff = "";
 boolean lookForEnd = false;
 for (int i = 0; i < command.length(); i++) {
  if (lookForEnd) {
  if (command.charAt(i) == '\"') {
   if (buff.length() > 0)
   commandArray.add(buff);
   buff = "";
   lookForEnd = false;
  } else {
   buff += command.charAt(i);
  }
  } else {
  if (command.charAt(i) == '\"') {
   lookForEnd = true;
  } else if (command.charAt(i) == ' ') {
   if (buff.length() > 0)
   commandArray.add(buff);
   buff = "";
  } else {
   buff += command.charAt(i);
  }
  }
 }
 if (buff.length() > 0)
  commandArray.add(buff);

 String[] array = new String[commandArray.size()];
 for (int i = 0; i < commandArray.size(); i++) {
  array[i] = commandArray.get(i);
 }

 return array;
}
}


Add this class to your code and use the following code to call it for you.
String command = "your external command command";
         
ExecCommand e;
e = new ExecCommand(command);

This takes care of almost all the things, parsing the command line and printing the output to the console. Moreover, sometimes I have noticed that adding a sleeptime in the ExecCommand constructor also helps.
p = Runtime.getRuntime().exec(makeArray(command));
  new OutputReader().start();
  new ErrorReader().start();
  int sleeptime = 1000; // Try to make an educated guess 
                        // about the sleeptime
  Thread.sleep(sleeptime);
  p.destroy();


This block of code is the corrected version the code given <Here>. Basically the changes are related to outputSem.acquire() to outputSem.acquireUninterruptibly() and adding the finally block. Without them it was easily leading to a deadlock.

Monday, April 23, 2012

"wget" gets blocked Java

Recently I faced an interesting problem. I wanted to crawl some pages from the internet and chose wget as the weapon. But, as I am a lover of Java, I wanted to call it from Java. Though I regretted a lot last hours  not following my initial plan to call it through Perl (or theoretically any scripting language) but now happy finding a hack.

Why regret? It hangs!! Just after downloading a couple of pages it sleeps to never come back alive. Well, digging out revealed, it completely occupied the stdout and the process is stuck. That means you need to flush the stdout to move forward. This can be handled by either stopping the buffering or continuously flushing the buffer!

So better would be to silent wget before it "silent"s your process. Adding the options -nv and -q, no verbose and quiet respectively, make the wget silent and your process does not get blocked.

So  at the end of the day you choose to call wget from Java, dont worry but mind to add the options -q and  -nv in your command!

Wednesday, April 11, 2012

Bag of 'Bugs'

A couple of days before, I met a coder. We started talking casually about the things and I noticed that he was carrying a small diary. Usually I am very bad at noticing unless something is really conspicuous, just in case my girlfriend is reading this. In the conversation I realised, that was the log of errors and the 'hack' he had come across so far in the life of coding. This reminded me my undergrad days when the easiest measure for geek quotient was the number of bugs one can spontaneously reciprocate. People are also used to search the error messages a lot on the web to seek the solution which I can notice from the "query suggestions" in the search engines. Okay let me say something about how the "query suggestions" work. Basically the search engines keep the track of all the queries fired and accumulate the frequencies and some advanced statistics on that. Based on these statistics, the probability of the most likely query for your already typed word/s or characters is calculated and presented in the decreasing order of probability/score as the suggestions. Mostly the query suggestions come from the previous queries and the top suggestion is the most frequent one and so on. Sometimes, these suggestions become really funny, for the fun type "nobody uses" in Google and see what are the suggestions!

Okay coming back to the original point, I think at the end of the day the knowledge is how many bugs you can solve. Here, the scope of the term "bug" is quite large. I see a strong analogy to this in the life. We call somebody 'veteran', why? Because s/he possesses a long experience of the field and intuitively can handle/hack most of the situations arise in that domain. That knowledge comes from an experience and ends up in some physical or virtual diaries somewhere which I refer as a 'bag of bugs'.

Sunday, February 5, 2012

How you want to be treated?

The first point that arrives in picture when you take any service which can be from simple dining out to a long international trip : "How would I be treated?". More than five years ago, I remember when I underwent a summer training of customer dealing, I was taught "Remember! You are the face of the company. You portray the image of the company to the customer. So think of your actions and words." This is so true. Each good and bad experience with the companies around reminds me the same lines. I try to see the same point from the both perspective an employee and a customer.

The rule of the world is very interesting: the customer is an employee somewhere and deals with fellow customers directly or indirectly. This makes the situation very easy to understand. For example, we all know that how the bureaucracy sucks sometimes, but only one person sitting on the other side of the table has the power to turn our experience upside down or otherwise. If that person "treats the people as he wants to be treated" the world of bureaucracy will be much simpler. Unfortunately, this is somewhat related to corruption but I do not want to talk about it at the moment. On the contrary, the inverse is also true that "I am treated very badly and the same will now be reflected in my services!" :) So these are directly related nodes of the chain "the customer and the employee".

I hope to see the chain in proper direction and would end with related lines: "Treat the people around the same way you want to be treated!"