Monday, October 13, 2008

Java code to send message to your gtalk friends

Have used a library called Smack (http://www.igniterealtime.org/projects/smack/index.jsp)

import java.util.Collection;

import org.jivesoftware.smack.*;

public class GtalkClient {

public static void main(String[] args) throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login("< username >","< password >");

// Below is the code to get the users


// Roster roster = connection.getRoster();
// Collection rosterEntries = roster.getEntries();
//
// System.out.println("\n\n" + rosterEntries.size() + " friend(s):");
// for(RosterEntry rosterEntry:rosterEntries)
// {
// System.out.println(rosterEntry.getUser());
// }

// Here is a code to send message to a friend

MessageListener messageListener = null;
Chat chat = connection.getChatManager().createChat("shantanu.gg@gmail.com",messageListener);
chat.sendMessage("Hello this is a ping from a java program");
}
}

Tuesday, September 30, 2008

Sending email using GMAIL using Java Mail Api

Note: Search for changeme and replace with appropriate terms.





import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class MailSender {
final String senderEmailID = "changeme @ changeme .com";
final String senderPassword = "changeme";
final String emailSMTPserver = "smtp.gmail.com";
final String emailServerPort = "465";
String receiverEmailID = null;
String emailSubject = null;
String emailBody = null;

public MailSender(String receiverEmailID, String emailSubject, String emailBody) {
this.receiverEmailID=receiverEmailID;
this.emailSubject=emailSubject;
this.emailBody=emailBody;


Properties props = new Properties();
props.put("mail.smtp.user",senderEmailID);
props.put("mail.smtp.host", emailSMTPserver);
props.put("mail.smtp.port", emailServerPort);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", emailServerPort);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// session.setDebug(true);

MimeMessage msg = new MimeMessage(session);
msg.setText(emailBody);
msg.setSubject(emailSubject);
msg.setFrom(new InternetAddress(senderEmailID));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(receiverEmailID));
Transport.send(msg);
} catch (Exception mex) {
mex.printStackTrace();
}


}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderEmailID, senderPassword);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MailSender mailSender=new MailSender("changeme @ changeme .com","Test Mail from Puretechie","Here goes the body");
}

}

Monday, September 22, 2008

Common bad practice in database calculations

create table x ( i int, j varchar(10))
insert into x values (1,'a')
insert into `x`(`i`,`j`) values ( '2',NULL)


select count(*) from x
2
Select count(j) from x
1

Note the difference when the count(col_name) is used.

count(col_name) is used under the impression that it is faster than count(*) , however quite the opposite is true.

MYISAM table MySQL has cached number of rows in the table. Thats the reason why MYISM is able to instantly answer COUNT(*) query, but not COUNT(col_name).

Why ? Because say if col_name column is not defined as NOT NULL there can be some NULL values in it and so MySQL have to perform table scan to find out. This is also why result is different for the second query.

Using count(*) instead of count(col_name) falls in the best practice category.

Saturday, June 21, 2008

Ways to manage RSS feeds

RSS feeds aim to manage the information load.

I for example blog at these places :

a) http://dinchari.blogspot.com/ --- my Personal blog
b) http://puretechie.blogspot.com/ --- were a group of like minded tech enthusiasts post about topics of their interest.

Ideally I would like my friends to have be updated with my thoughts on either blogs. Currently they have to refer to the individual rss feeds separately.


To ease matters, I have used Yahoo Pipes to create a combined RSS feed for the consumers as a single combined RSS feed.

Click here for the new combined RSS link


Now this essentially is simple.

We can develop on this use case extensively.

Initially,
a) Now in the tech blog: there are many other authors. With the above URL , the consumers will also get the updates from these other authors. There needs to be filtering mechanism based on Author.

Adding more complexity,
b) Suppose you like all my articles about the puzzles and are not interested in any other topics. This is where the whole thing gets tricky. Using filtering its possible, but I have to explore the capabilities of customizing to individual consumers.

Tuesday, May 27, 2008

Processing RSS feeds --the Python way

[shantanu@myjunkyard rss]$ cat a1.py
#!/usr/bin/env python
import feedparser
import sys
d = feedparser.parse(sys.argv[1])
for i in d['entries']:
for j in i['links']:
print i['title'] + "######" + j['href']


[shantanu@myjunkyard rss]$ cat rssFeed.list
http://puretechie.blogspot.com/atom.xml

[shantanu@myjunkyard rss]$ for i in `cat rssFeed.list `; do ./a1.py $i; done

See it Yourself :)

Thursday, May 15, 2008

Ruby on Rails --- it time

After many futile attempts RoR finally is happening this year. Though I tried my best to use Java for the project(as usual , cox thats a comfort zone) , somehow winds changed direction.

For statistics The TIOBE Programming Community index which gives an indication of the popularity of programming languages, ranks Ruby 10th in popularity with 2.66% of programmers.


My initial thoughts

1) There is a need to think in a language, understand its philosophy. An interesting story to share from RoR for dummies book about the name Ruby on Rails. Since the year 2000, teams of Java programmers have been using a framework named Struts. But the word strut means something in the construction industry. (A strut is a horizontal brace, and a sturdy one at that.) Well, a rail is also a kind of horizontal brace.
And like Ruby, the word Rail begins with the letter R. Thus the name Ruby on Rails.

2) Understanding MVC is ingrained
http://wiki.rubyonrails.org/rails/pages/UnderstandingRailsMVC

Best part of the language is that it forces MVC , similar to Java forcing OO.

These are still baby steps , but has been exciting.

Tuesday, May 13, 2008

Powerset is released today

Powerset is a first of its kind .... a context sensitive natural language based search engine.
Currently powered by Wikipedia, this is a potentially disruptive technology.


Simple queries like


Where is PESIT?
or
vice chancellor of Visvesvaraya Technological University


Interesting results!!!!

Friday, April 11, 2008

Punishment for 3 days.

For 3 days I was not able to access my system with my ID.

I kinda circumvented the problem by allowing remote root ssh to my machine(which is criminal).

The problem was this:
root@shantanu>ssh sg202516@shantanu
Password:
Last login: Fri Apr 11 17:19:51 2008 from shantanu
NO LOGINS: System going down in 30 seconds
Connection to shantanu closed.
root@shantanu>

Untill today I was ok with it, but I could not access my screen sessions remotely.

So finally decided to debug it.

The issue was the presence of a file called /etc/nologin

root@shantanu>cat /etc/nologin
NO LOGINS: System going down in 30 seconds

Removing the file solves the crap.

With a feeling of achievement I told my neighbor about this.

The reply came back "Check the date of the creation of the file. It should be Apr 8th. You had not locked the system!!!"

Related Old link:http://dinchari.blogspot.com/2006/07/assholes-learn-this-way_27.html