Books I Like

Sunday, October 25, 2009

Dual Pivot Quicksort in Java

This is from the paper published by Vladimir Yaroslavskiy (Read it for more details : here

Sorting data is one of the most fundamental problems in Computer Science, especially if the arranging objects are primitive ones, such as integers, bytes, floats, etc. Since sorting methods play an important role in the operation of computers and other data processing systems, there has been an interest in seeking new algorithms better than the existing ones. We compare sorting methods by the number of the most "expensive" operations, which influence on effectiveness of the sorting techniques, — comparisons and swaps. Quicksort algorithm is an effective and wide-spread sorting procedure with C*n *ln(n) operations, where n is the size of the arranged array. The problem is to find an algorithm with the least coefficient C. There were many attempts to improve the classical variant of the Quicksort algorithm:

1. Pick an element, called a pivot, from the array.
2. Reorder the array so that all elements, which are less than the pivot, come before the pivot and all elements greater than the pivot come after it (equal values can go
either way). After this partitioning, the pivot element is in its final position.
3. Recursively sort the sub-array of lesser elements and the sub-array of greater elements.

We can show that using two pivot elements (or partitioning to three parts) is
more effective, especially on large arrays. We suggest the new Dual-Pivot Quicksort
scheme, faster than the known implementations, which improves this situation. The
implementation of the Dual-Pivot Quicksort algorithm has been investigated on different inputs and primitive data types. Its advantages in comparison with one of the most effective known implementations of the classical Quicksort algorithm [1], [2], and implementation of Quicksort in JDK™ 6.0 platform [3] have been shown.

It is proved that for the Dual-Pivot Quicksort the average number of
comparisons is 2*n*ln(n), the average number of swaps is 0.8*n*ln(n),
whereas classical Quicksort algorithm has 2*n*ln(n) and 1*n*ln(n)
respectively.




Here is the source code for the implementation : Code by Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch.


Comparision between Current JDK Quicksort vs Dual Pivot Quick sort : comparision

Friday, October 23, 2009

Solving Euler Project Problem #4 using Ruby

Problem Statement : A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

Find the largest palindrome made from the product of two 3-digit numbers

Solution : Please check the comments before the code which explains the logic/approach to the problem. We are trying to minimize the iterations to check against the generated palindromic number. Also this divisibility by holds true only for all the even numbered palindroms.



#
#A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
#
#Find the largest palindrome made from the product of two 3-digit numbers
#
# Observations :
# Number = xyzzyx = 100001*x + 100010*y + 1100*z (which is divisible by 11)
#
#




def largest_palindrome_for_even_digits(n)
max_val = 0
multiplier_x = get_multiplier_x(n)
n.downto(100) { |y|
multiplier_x.each { |x|
product = x * y
if product.to_s == product.to_s.reverse
max_val = [max_val, product].max
end
}
}
max_val
end


def get_multiplier_x (n)
multipliers = []
current_val = n/11*11
while current_val > 100
multipliers << current_val
current_val-=11
end
multipliers
end

puts largest_palindrome_for_even_digits(999)

Solving Euler Project Problem #3 using Ruby

Problem Statement : The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143

Solution : Please check the code comments which explains the logic.

#The prime factors of 13195 are 5, 7, 13 and 29.
#
#What is the largest prime factor of the number 600851475143
#
#Start dividing the number by the first prime (i.e. 2). Keep dividing by the current factor
#till the remainder is zero. So, the number left is n/(current_prime_factor)**m, where m is the no. of times
#current factor was able to divide the number without remainder. Repeat this process with the next
#prime factor and the number left as a result of previous division and so on, unless the result of
#division becomes 1. As the prime factor we are using to divide keeps on increasing, and the loop ends
#when the number is completely divided (or say n become 1), return the current prime factor. This is our
#largest prime factor

require 'mathn'

def find_largest_prime(n)
primes = Prime.new
while n > 1
current_prime_factor = primes.next
while n % current_prime_factor == 0
n /= current_prime_factor
end
end
current_prime_factor
end

puts find_largest_prime(600851475143)

Wednesday, October 21, 2009

Google Guice : Part 2 : Inject Config Value

Was Spring 3.0 just released ?

Right, Here is a link mentioning the same Spring3.0 release. I am really excited to check it out, specially the REST part of it. But yeah, let us save it for other posts.

I directly came here, where is the part One ?

If you haven't gone through the part 1 of this article, and are totally new to Google Guice, please check it out and set up your environment as described there.

What is the goal for this article ?

Since in the previous article we were able to set up a working project/program. Let us extend it further. Almost all of the applications needs to have some configuration properties, today we will learn to inject the property value from a config file.

Ummm.. before you dive into it, I just forgot what was the spring bean I used to configure it in a Spring App?

I guess you are probably talking about PropertyPlaceholderConfigurer.java. By the way, the other day I blogged about extending it.

Well...Lets code it :


public interface IPrinter {
public void print();
}

public class MyDummyPrinter implements IPrinter {
public void print() {
System.out.println("Article Printed !");
}
}

public interface IMailSender {
public void sendMail();
}

public class MyDummyMailSender implements IMailSender {
public void sendMail() {
System.out.println("Mail Sent !");
}
}

public interface ArticleService {
public void publishArticle();
}

import com.google.inject.Inject;
import com.google.inject.name.Named;
public class Article implements ArticleService {
private IMailSender mailSender;
private IPrinter printer;

private String advertizedAt;
private String articleHeading;

@Inject
Article(
@Named("advertised.at") String advertizedAt,
@Named("article.name") String articleHeading,
IMailSender mailSender, IPrinter printer) {
this.mailSender = mailSender;
this.printer = printer;
this.advertizedAt = advertizedAt;
this.articleHeading = articleHeading;
System.out.println("This article has been advertized at :" + advertizedAt +
" with the heading :" + articleHeading);
}

public void publishArticle() {
mailSender.sendMail();
printer.print();
}
}



Check out the Article.java code. We want to inject two properties namely "advertizedAt" and "articleHeading" throgh a config file. As you can see Guice comes with a built in annotation - named. So, as it appears, Guice should inject the value for these keys as defined in the config file.

Well, let us define our property file :


advertised.at=dZone
article.name=googleGuicePartTwo


But How will Guice know the property value unless it loads it. Does it has something like PropertyPlaceholderConfigurer ?

You are right, we are going to write some code to load the property file. As of now, I am not aware about any helper class who does it for us. If you know one please comment, or else I would update it once I know.


import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import java.util.Properties;
import java.net.URL;

public class MyArticleModule extends AbstractModule {
@Override
protected void configure() {
try {
Properties props = loadProperties();
Names.bindProperties(binder(),props);
} catch (Exception e) {
e.printStackTrace();
}
bind(IPrinter.class).to(MyDummyPrinter.class);
bind(IMailSender.class).to(MyDummyMailSender.class);
}

private static Properties loadProperties() throws Exception {
Properties properties = new Properties();
ClassLoader loader = MyArticleModule.class.getClassLoader();
URL url = loader.getResource("config.properties");
properties.load(url.openStream());
return properties;
}
}


So, before injection we are loading the config.properties. The code is straight forward. The piece important here to note is the use of com.google.inject.name.bindProperties which creates a constant binding to @Named(key) for each property.

Well , Let us see it in action :


import com.google.inject.Injector;
import com.google.inject.Guice;

public class MyBootStrapInvoker {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new MyArticleModule());
Article article = injector.getInstance(Article.class);
article.publishArticle();
}
}


Sample Run :-


This article has been advertized at :dZone with the heading :googleGuicePartTwo
Mail Sent !
Article Printed !


Is this the best approach to do this way :

I am sure there must be a better way, as Guice always discourages bindings that uses a string, because the compiler can not check the string. So that means, if there is some spelling mistakes, we would see run time error (like we see in spring, if you make spelling mistakes). Let me know, if you know a better way.
Would appreciate your comments.

Solving Euler Project Problem #2 using Ruby

Problem Statement : Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

Solution : For the explanation please see the comments. Later I realized that I could have probably used Binet's formula, however this solution also kind of looks good.



#Each new term in the Fibonacci sequence is generated by adding the previous two terms.
#By starting with 1 and 2, the first 10 terms will be:
#
#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
#
#Find the sum of all the even-valued terms in the sequence which do not exceed four million.
#
#Obersevations : starting with 2 every 3rd number is even (it must be, as to make an even number
#one needs to add two odds (or two even unlikely here))
#So, lets try to decompose the fibonacci series in terms of n-3,n-6,n-9, etc...as it make us do
#less iteration while adding
#
# F(n) = F(n-1)+F(n-2) = F(n-2)+F(n-3)+F(n-2) = F(n-3)+2(F(n-2)) = F(n-3) + 2(F(n-3)+ F(n-4))
# = 3F(n-3) + 2F(n-4)
# = 3F(n-3) + F(n-4)+F(n-5)+F(n-6) = 4F(n-3) + F(n-6)
#
#


def find_sum_of_even_terms(limit)
if(limit<2)
return 0
end
if(limit<8)
return 2
end
second_last_even_number = 2
last_even_number = 8
current_number = 4*last_even_number+second_last_even_number
sum=second_last_even_number+last_even_number

while current_number<=limit
sum+=current_number
second_last_even_number = last_even_number
last_even_number = current_number
current_number = 4*last_even_number+second_last_even_number
end
sum
end

puts find_sum_of_even_terms(4000000)

Solving Euler Project Problem #1 using Ruby

Problem statement : If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Solution :


#If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
#The sum of these multiples is 23.
#
#Find the sum of all the multiples of 3 or 5 below 1000.
#
#3(1+2+3+....+333)+5(1+2+3+.....+199)-15(1+2+3+...+66)
#puts 3*333*334/2+5*199*200/2-15*66*67/2
#

def sum_of_n_consecutive_numbers (n)
n*(n+1)/2
end

def sum_of_multiples_of_m(max,m)
m*sum_of_n_consecutive_numbers(max/m)
end

puts sum_of_multiples_of_m(999,3)+sum_of_multiples_of_m(999,5)-sum_of_multiples_of_m(999,15)

Monday, October 19, 2009

Solving Project Euler Problem #5 in Ruby

The problem statement :

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

Solution/s :

One of the solution is straight forward as Ruby exposes such a nice library (Actually it feels like a cheat :))


require 'rational'
puts (1..20).inject(1) { |result, n| result.lcm n }


The other solution involves a little bit of math/algorithm. Hopefully you will enjoy it.


#
# Directly using the ruby library
# require 'rational'
# puts (1..20).inject(1) { |result, n| result.lcm n }
#
# The other solution (Code below) Steps :
# 1. Fetch all the primes less than "n" in an array (it would be automatically sorted, which will save our efforts later)
# 2. For the smallest member "k" of the prime list, find the maxm power "m" such that k**m <=n (For, e.g. 2**4<20)
# 3. Replace the smallest member from the array with k**m (For e.g. replace 2 with 16, because anything divisible by k**m
# would be itself be divisible by k**0..m-1)
# 4. Repeat the steps 2-3, for the next member of the array (starting with the power #m-1). when the power becomes 1, no need
# to iterate.
# 5. Multiply the members of the final array.
#
# Sample runs :
# For n = 10
#
# All the primes :
# [2, 3, 5, 7]
# All the multipliers :
# [8, 9, 5, 7]
# 2520
#
# For n = 20
#
# All the primes :
# [2, 3, 5, 7, 11, 13, 17, 19]
# All the multipliers :
# [16, 9, 5, 7, 11, 13, 17, 19]
# 232792560
#

require 'mathn'

def collect_primes(n)
primes_list = []
primes = Prime.new
primes.each { |x| break if x >= n; primes_list<<x; }
puts "All the primes :"
p primes_list
primes_list
end

def root(arg, base)
arg**(1/base)
end

def get_all_the_multipliers(n)
primes_list = collect_primes(n)
max_base = get_max_base_for_two(n)

primes_list.each_with_index do |item, index|
primes_list[index]=item**max_base
max_base = get_max_base_for_current_number(n,max_base,primes_list[index+1])
if max_base <=1 then break
end
end
puts "All the multipliers :"
p primes_list
primes_list
end

def get_mulitiplied_value(n)
list_of_multipliers = get_all_the_multipliers(n)
list_of_multipliers.inject(1){|total, i| total*i}
end

def get_max_base_for_two(n)
i=2
while root(n,i) >= 2
i = i+1;
end
return i-1
end

def get_max_base_for_current_number(n,max_base, current_number)
base = max_base - 1
while current_number**base > n
base = base - 1
end
return base
end


puts get_mulitiplied_value(20)
#puts get_mulitiplied_value(10)



Comments are welcome .

Sunday, October 18, 2009

Google Guice : Part 1

Over the weekend I was going through this video presented at Google IO 2009.
Please watch it if DI (a.k.a Dependency Injection) interests you.


Where is Guice being used in production :-
The video mentions Google uses it for Google Wave, Google Adwords, GMail, Orkut, Google Docs, Youtube...pretty much every new project .

This is how I talked with myself, in case it is gonna help you let us learn together.

Should I care to learn Guice, I already know/use Spring :-
Spring framework is much more than a DI framework.It made DI popular (although there were older DI framework available before it). Spring competes with JEE stack.
So, yeah we can say Spring is a super set of Google Guice. Guice wholly embraces annotations and generics, thereby enabling you to wire together and test objects with measurably less effort. Guice proves you can use annotations instead of error-prone, refactoring-adverse string identifiers.And of course it is much light weight.

Spring uses annotations too :- That is true. The newer versions (probably 2+) focuses more to use annotations. We may discuss the benefits of XML declarations vs annotations in a separate post, as it is a topic in itself .

There are so many annotations for different DI libraries, shouldn't it be standardized :- You are in luck. Check it out JSR330- http://code.google.com/p/atinject/

Well...so show me some code :-

I won't boar you with the definition(s) of DI, IOC (search it on google rather in case you are new to this). Goal of this post is to make you run a simple Guice Program.

Download the guice library, and add it in your classpath.

The interfaces and it's implementations are self explanatory :-



public interface IPrinter {
public void print();
}

public class MyDummyPrinter implements IPrinter {
public void print() {
System.out.println("Article Printed !");
}
}

public interface IMailSender {
public void sendMail();
}

public class MyDummyMailSender implements IMailSender {
public void sendMail() {
System.out.println("Mail Sent !");
}
}

public interface ArticleService {
public void publishArticle();
}

import com.google.inject.Inject;
public class Article implements ArticleService{
private IMailSender mailSender;
private IPrinter printer;

@Inject
Article (IMailSender mailSender, IPrinter printer) {
this.mailSender = mailSender;
this.printer = printer;
}
public void publishArticle() {
mailSender.sendMail();
printer.print();
}
}


As shown in the above program, we want the mailSender and printer to be injected.
So, note the @inject annotation over the Article Constructor.

Lets inject the dependencies. Well, if you have worked with Spring's XML declarations for defining and injecting the dependencies, then think the next java code to be it's counter part in Guice.


import com.google.inject.AbstractModule;
public class MyArticleModule extends AbstractModule {
protected void configure() {
bind(IPrinter.class).to(MyDummyPrinter.class);
bind(IMailSender.class).to(MyDummyMailSender.class);
}
}


What I like here is the readability (It is like I am coding in Ruby :)). The code clearly shows that IPrinter.class is bound to MyDummyPrinter.class. Similarly, with the other line.

Cool..Now let us launch/bootstrap the Guice. For the Spring folks, think it similar to the code you write to load spring config beans (remember : ApplicationContext ctx = new ClasspathApplicationContext("example.xml")).


import com.google.inject.Injector;
import com.google.inject.Guice;

public class MyBootStrapInvoker {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new MyArticleModule());
Article article = injector.getInstance(Article.class);
article.publishArticle();
}
}


And that's it. You are running Guice !!!

Hopefully, we will dive more in depth in the second post on the same topic.
Please let me know your comments, so the next time I could offer more.

Introducing JRugged library : Make your Java Programs Rock Solid




This book, is definitely among one of the best books, I've read this year. It lays out some critical aspects to creating and rolling out stable software systems. Each of the case studies discussed is based on real events.

While reading the book you will feel, how nice it would be if there is a library you could use to gain the advantages discussed to make your programs/software/systems rock solid.

The JRugged is such a library written to be used in Java. To summarize, what it offers , here it is (directly from the project home page) :-

The jrugged library provides straightforward add-ons to existing code to make it more robust and easier to manage.

Although there are several lower-level building block classes here available for custom solutions, most clients will find they can use just the following classes:

CircuitBreaker provides a way to wrap a service in a way that provides a bulkhead against system failure; when a remote service fails, further calls to the service are prevented for a period of time to allow the remote service to recover.

Initializer provides a way to decouple service construction from initialization/startup and allows the latter to occur in the background, even retrying if initial attempts to initialize fail.

PerformanceMonitor provides a way to wrap a service and collect a series of useful statistics about its latency and throughput, even calculating moving averages across different time windows.

Monitorable, Status, and RolledUpStatus are related classes that provide a simple RED/YELLOW/GREEN encoding for service health, possibly useful for constructing operational dashboards.

Generally speaking, most of these classes implement a ServiceWrapper interface to allow them to be applied around existing services using the Decorator design pattern. Those classes providing operational information, such as PerformanceMonitor and Monitorable can be wrapped in classes that expose their data to JMX controls or on web-accessible health status pages as desired.



Here I would show you, how easy it to use this in your existing code base. It provides it by the classic use of the Decorator Pattern.



import org.fishwife.jrugged.CircuitBreaker;
import org.fishwife.jrugged.PerformanceMonitor;
import org.fishwife.jrugged.Status;
import org.springframework.jmx.export.annotation.ManagedAttribute;

public abstract class AbstractMonitor implements IMonitorable {

/** Circuit breaker to detect server oveloading */
protected final CircuitBreaker cBreaker = new CircuitBreaker();

/** Performance stat monitor */
protected final PerformanceMonitor perfMonitor = new PerformanceMonitor(5);


@ManagedAttribute(description = "My System status")
public String getHealthCheck() {
return cBreaker.getStatus().getSignal();
}


@ManagedAttribute
public double getAverageFailureLatencyLastDay() {
return perfMonitor.getAverageFailureLatencyLastDay();
}

@ManagedAttribute
public double getAverageFailureLatencyLastHour() {
return perfMonitor.getAverageFailureLatencyLastHour();
}

@ManagedAttribute
public double getAverageFailureLatencyLastMinute() {
return perfMonitor.getAverageFailureLatencyLastMinute();
}

@ManagedAttribute
public double getAverageSuccessLatencyLastDay() {
return perfMonitor.getAverageSuccessLatencyLastDay();
}

@ManagedAttribute
public double getAverageSuccessLatencyLastHour() {
return perfMonitor.getAverageSuccessLatencyLastHour();
}

@ManagedAttribute
public double getAverageSuccessLatencyLastMinute() {
return perfMonitor.getAverageSuccessLatencyLastMinute();
}

@ManagedAttribute
public long getFailureCount() {
return perfMonitor.getFailureCount();
}

@ManagedAttribute
public double getFailureRateLastDay() {
return perfMonitor.getFailureRateLastDay();
}

@ManagedAttribute
public double getFailureRateLastHour() {
return perfMonitor.getFailureRateLastHour();
}

@ManagedAttribute
public double getFailureRateLastMinute() {
return perfMonitor.getFailureRateLastMinute();
}

@ManagedAttribute
public double getFailureRateLifetime() {
return perfMonitor.getFailureRateLifetime();
}

@ManagedAttribute
public long getRequestCount() {
return perfMonitor.getRequestCount();
}

@ManagedAttribute
public double getRequestRateLastDay() {
return perfMonitor.getRequestRateLastDay();
}

@ManagedAttribute
public double getRequestRateLastHour() {
return perfMonitor.getRequestRateLastHour();
}

@ManagedAttribute
public double getRequestRateLastMinute() {
return perfMonitor.getRequestRateLastMinute();
}

@ManagedAttribute
public double getRequestRateLifetime() {
return perfMonitor.getRequestRateLifetime();
}

@ManagedAttribute
public long getSuccessCount() {
return perfMonitor.getSuccessCount();
}

@ManagedAttribute
public double getSuccessRateLastDay() {
return perfMonitor.getSuccessRateLastDay();
}

@ManagedAttribute
public double getSuccessRateLastHour() {
return perfMonitor.getSuccessRateLastHour();
}

@ManagedAttribute
public double getSuccessRateLastMinute() {
return perfMonitor.getSuccessRateLastMinute();
}

@ManagedAttribute
public double getSuccessRateLifetime() {
return perfMonitor.getSuccessRateLifetime();
}

public CircuitBreaker getCBreaker() {
return cBreaker;
}

public PerformanceMonitor getPerfMonitor() {
return perfMonitor;
}

public Status getStatus (){
return cBreaker.getStatus();
}
}



Now, your class just needs to extend this abstract class inherits all those JMX statistics as well as the handles to the Circuitbreaker and PerformanceMonitor Object.


@ManagedResource(objectName = "com.example:type=manager,name=mysystem",
description = "shows the running status of mysystem")
public class MySystem extends AbstractMonitor {

.........
.........
//This is the method I want to decorate
public BackEndData processArgument(final String myArg) {
final BackEndService theBackend = backend;
try {
return cBreaker.invoke(new Callable< BackEndData >() {
public BackEndData call() throws Exception {
return perfMonitor.invoke(new Callable< BackEndData >() {
//This is the actual method which you were using earlier without decoration
public BackEndData call() throws Exception {
return theBackend.processArgument(myArg);
}
});
}
});
} catch (RuntimeException re) {
throw re;
} catch (Exception e) {
throw new RuntimeException("wrapped", e);
}
}

}



You probably would change this code a little bit depending on the definitions of your critical components, and the health status (But you get the idea).


Now just fire up the JConsole and you can see all the statistics for your classes. And it handles failures as well, showing you the status of the system as Green/Red/Yellow (depending on your definition of the critical system/components).

Please check it out for details, as the code base is pretty terse.

You may wish to extend it to be used as an Aspect (AOP), so that your classes could avail the benefits without changing your java code. The project mentions, it is looking to offer AOP wrapper to make it easier to do drop-in decoration of existing code.
So wait if you could or if you implement it yourself, please post it so others could use it.

Thursday, October 15, 2009

Intellij Idea IDE OpenSourced - The current state of the Java IDE's

It is a great news that Jetbrains has opensourced the Intellij Idea, a well known Java Editor. I have used it along with both Eclipse and Netbeans in the past.

Last year, I decided to buy a professional license for the intellij and never looked back. So, what would I say about eclipse, Netbeans and Intellij ?

Well, frankly speaking, they all have their positives. In fact, they are all competing on a very high level now.

Lets compare them based on few criterias :

1. User Adoption : Eclipse wins easily. However, I guess with today's announcement, it is going to be exciting. Don't forget Netbeans is releasing the 6.8 beta shortly, and it has significantlly improved.

2. Company Backing : Although Eclipse was started by IBM, Jetbrains owns intellij and Netbeans is owned by Sun (ohh.. I guess it is Oracle), but now the open source has changed the game. It is all about users, developers, speed and plugins.

3. Speed :- I don't have a benchmark, however the last time I felt, Eclipse and Intellij both were better. Netbeans still needs to work on it (I last tried the full j2ee version of it - 6.5, never tried 6.7).

4. Plugins :- Again Eclipse wins by the numbers. As far as quality is concerned, Intellij would probably be the best. Again, used mostly the in-built plugins for the netbeans (Correct me, and add comments if you find it better). However, now I see all the three editors have got a nice plugin framework and lots of developers writing for them.

5. Maven Support :- Hmm....Here the Netbeans is the one, closely followed by Intellij.

6. Usability :- Power users like Intellij with the best Refactoring offering , intelligent shortcuts, the awesome customer care support.

7.Dynamic Language Support (Ruby/JRuby/Groovy) :- NetBeans is the best IDE for Ruby/JRuby(I am not including Rubymine from Jetbrains, as it is a separate product). For Groovy, my vote goes to Intellij again.

I think as now Intellij has provided the free editor (Community Edition), more people would give it a try, and hence would be in a better position to compare it (And who knows may be then buy the Professional Edition eventually).

What do you think ?

Update : Here is the comparison chart for the OpenSource version vs the Commercial One.

Tuesday, October 13, 2009

Simple Feed Aggregator in Ruby

Ruby already has few RSS standard libraries (0.9, 1.0 , 2.0) in built, and it is good enough for simple purpose.

Today, I am trying to work on a simple feed aggregator, which could take multiple feed source url's and enlists the stories published there. I was tempted to use the standard library, however since it would not support the Atom standard, started looking for another library which supports both.

SimpleRSS lib by Lucas Carlson looks great.

So, here goes the code. You may extend it if you wish.


require 'simple-rss'
require 'open-uri'

class FeedAggregator
def initialize(urls)
@urls = urls
@feeds = []
read_feeds
end

def read_feeds
@urls.each { |url| @feeds.push(SimpleRSS.new(open(url).read)) }
end

def feed_source_counts
@feeds.each_with_index do |feed, index|
feed_source = "Feed Source [#{index}]: #{feed.channel.title}"
published_articles = "Published Articles: #{feed.items.size}"
puts feed_source + ', ' + published_articles
end
end

def list_articles(id)
puts "-----Feed Source [#{id}] : #{@feeds[id].channel.title}-----"
@feeds[id].items.each { |item| puts ' ' + item.title }
end

def list_all_articles
@feeds.each_with_index { |f, i| list_articles(i) }
end
end

MyFeedAggregator = FeedAggregator.new(ARGV)
MyFeedAggregator.feed_source_counts
puts "------------ Feed Details ------------------"
MyFeedAggregator.list_all_articles



Here is the test run :


C:\Ruby\bin\ruby.exe -e STDOUT.sync=true;STDERR.sync=true;load($0=ARGV.shift) C:/Users/anshu/RubymineProjects/TestRubyProjectTwo/test_two.rb http://rubyforge.org/export/rss_sfnewreleases.php http://www.dzone.com/links/feed/frontpage/rss.xml
Feed Source [0]: RubyForge New Releases, Published Articles: 10
Feed Source [1]: dzone.com: latest front page, Published Articles: 25
------------ Feed Details ------------------
-----Feed Source [0] : RubyForge New Releases-----
Dr Nic's Utilities
WysMacs Trainable Text and Code Editor
Loofah
autotest-fsevent
state_pattern
codeforpeople
Seattle.rb Projects
rjack
snk's open source
Spidr
-----Feed Source [1] : dzone.com: latest front page-----
Non-Technical Factors that Lead to Poor Architectures
My Tools of the Trade รข€“ 2009
Week Calendar using Jquery
Oracle Taunts IBM with $10M Challenge
Rant about developers or how I stopped worrying and love the simplicity
ScalaTest 1.0 Release "A Path Forward" to Scala
Resolving a Relative URL to a Full URL in ASP.NET
JxBrowser 2.0 Early Access Program
PrimeFaces UI 0.9.3 Released
Mozilla Join Microsoft in Slamming Google Chrome Frame
TDD is not test-first. TDD is specify-first and test-last.
Five super-secret features in Windows 7
Git# Offers Git Access for .NET and Mono Projects
Getting Started with Scrum
Apparent risk and actual risk
8 Hand Drawn Icon Sets For Bloggers
Optimize your Mac experience with the LaunchBar multitasker
18 Really Beautiful and Creative Web Designs
Google Released Page Speed for Firefox
Objective-C's niche: why it survives in a world of alternatives
Mobile Ajax Push for iPhone with JSF and PrimeFaces
Book Review - Spring in Practice
Tips for Designing an Awesome Coming Soon Page
Abstracting away Dependencies for Simpler code
MacRuby 0.5 Pulls the Plug on YARV

Process finished with exit code 0

How To : A Simple Generic SOAP Test Client written in Java

There are lots of libraries which are available to consume/generate a web service, like Spring Webservices, XFire, Apache Axis, to name a few.

While each one has its own pro/cons (that we can leave for a separate blog entry), many a times, one needs a simple (1 or 2) methods of a webservice calls to make. She may or may not care about the response, as long as the response is not leading to error.

For testing purposes, many use soapui, which is a wonderful tool. You may use it to point towards your soap end point, and it will generate request stub for you . Just fill in few params required by the method you are calling.

Here, I am ind of demonstrating the same thing, using a simple java program.
I am not using any WSDL2JAVA tool kind of stuff, to generate the stubs. [Please note, these stubs help you with the xml to java bindings, so if you are planning to use many methods, or you have sufficient time and want to do it right way, please follow that way].

Here is the code, you may use to call any end point. please note, it has some security headers just to show you. You may/may not need it.



package example;

import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.message.SOAPEnvelope;

import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.net.URL;


public class TestClient {
public static String wsURL =
"{YOUR_END_POINT}?WSDL";

public static String mySoapRequest =
new StringBuilder().
append("\n").
append(" \n").
#If your soap end point doesn't need security just remove these elements, or if it implements other sceurity standards, replace here
append(" \n").
append(" \n").
append(" \n").
append(" testusername\n").
append(" testpassword\n").
append("
\n").
append("
\n").
append("
\n").
append("
\n").
append(" \n").
#Here goes your request body
.......................
.......................
append("
\n").
append("
").toString();

public static void test() throws Exception {
InputStream input = new ByteArrayInputStream(mySoapRequest.getBytes());
Service service = new Service();
Call call = (Call) service.createCall();
SOAPEnvelope soapEnvelope = new SOAPEnvelope(input);
call.setTargetEndpointAddress(new URL(wsURL));
call.setUseSOAPAction(true);
System.out.println("Request:\n"+ mySoapRequest);
soapEnvelope = call.invoke(soapEnvelope);
System.out.println("Response:\n" + soapEnvelope.toString());
}

public static void main(String args[]) throws Exception {
TestClient callClient = new TestClient();
callClient.test();
}
}

Monday, October 12, 2009

XDepend : Java Code Base Analysis

Today morning, when I checked my email I was offered a Pro License for XDepend. Thanks to them for letting me explore it. I would post my findings once my hands get a little bit dirty.

The installer which I got was for a PC (I work on mac in my office), probably they have released a linux version as well. Would be nice, if they plan a release for the mac too.

By now what I know about is, it is from the same people who have offered NDepend, a well known code base analyzer in .Net environment.

Here is a short and nice intro to it by Mat Huston :- http://www.coderanch.com/t/462565/Blatant-Advertising/New-Tool-Java-code-base#2065255

Give it a try, as the trial version is available on the XDepend.com.
Once I get chance to explore more, I would update this thread.

Sunday, October 11, 2009

Writing Google Wave Robots using Java on Intellij




I got my google wave invite two days back (friday night, can't be a better time). And within 4 hours I was talking with my "HelloWorldish" Robot, such is the power and ease to write and deploy a robot is.

If you use eclipse, you already are set and this is the best place to start with : http://code.google.com/apis/wave/extensions/robots/java-tutorial.html

In fact, I used the same page for the sourcecode, and the steps. Make sure, you have a google wave id before you start looking at the next steps.

Steps :-

1.Make sure you are using Java 1.6.

2. I am using Intellij Maia (9). If you are on older version install the google appengine plugin.

3. Use this plugin to generate a dummy web project. In the wizard, choose google app engine. We are not going to use either GWT/ JDO or JPA for this project, so just uncheck them.

4. You can see a "AppEngine Dev" settings to run. Run it ,and you should be able to browse the dummy page at localhost:8080

5. To write the Robot, we need few libraries. Download it from http://code.google.com/p/wave-robot-java-client/downloads/list.

6.create a lib directory inside WEB-INF and copy all these jars . Also, add servlet-api-2.4.jar or higher.

7.Make a package src/parroty.server and copy paste the ParrotyServlet code described at http://code.google.com/apis/wave/extensions/robots/java-tutorial.html

8.Make a web/_wave dir. Create a capabilties.xml file and copy paste the code


<?xml version="1.0" encoding="utf-8"?>
<w:robot xmlns:w="http://wave.google.com/extensions/robots/1.0">
<w:capabilities>
<w:capability name="WAVELET_PARTICIPANTS_CHANGED" content="true" />
</w:capabilities>
<w:version>1</w:version>
</w:robot>


9. Register your application on appengine site.

In you appengine-web.xml file substitute your application id (instead of mine, "mytestwaveapp") :-


<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>mytestwaveapp</application>
<version>1</version>
</appengine-web-app>


10. Configure your ParrotyServlet inside the web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Parroty</servlet-name>
<servlet-class>parroty.server.ParrotyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Parroty</servlet-name>
<url-pattern>/_wave/robot/jsonrpc</url-pattern>
</servlet-mapping>
</web-app>


Go to "Tools->Upload AppEngine Application", and you are good enough to deploy this Robot.It would ask you your appengine loginid/pwd.The console would show, that everything went fine. You can confirm by accessing the capabilties.xmlfile :- http://applicationName.appspot.com/_wave/capabilities.xml

11.Well, now you are done. Use this robot as a participant for your wave and it should answer as I have shown in the picture. More Sample Robot Codes :- http://code.google.com/p/google-wave-resources/source/browse/trunk/samples/extensions/robots/java

Happy Coding ! Happy Waving !!!

Easy Graph with Scruffy on Ruby


This weekend, I was going through the first chapter of the book Ruby in Practice. Kind of like it as of yet , although have read only 4-5 pages.

It shows an example of using the graphing library Scruffy. Thought to give it a try on my home PC (Vitsa).

This blog is to save your time, in case you want to use this nice library and run into configuration problem.

1. You need to install the "Scruffy" gems
gem install scruffy

2. Then I tried the example discussed at :- http://scruffy.rubyforge.org. However, was getting error, which made it clear, you need to install another gem - RMagick. Won't bother you with my trial and errors, so to cut long story short, RMagick in turn requires other graphing lib (for e.g., I opted for IMageMagick).

So, download http://rubyforge.org/frs/download.php/38052/RMagick-2.5.0-ImageMagick-6.4.1-5-Q8.zip

3. Extract this to a temp directory, and run the IMageMagick installer.

4. Follow the readme file, which mentions to update your gem and install the RMagick gem.

5.Now, the most important part. Reboot your machine [:)]. Don't forget it.

6.After this, the sample codes described here :- http://scruffy.rubyforge.org/ started to work, except the graphs being generated was exactly as it appears on this site (as of this writing, wonder why it hasn't been fixed ?), i.e. blank images.

7. One blog post which helped me to fix this was :- http://www.ruby-forum.com/topic/193988
It suggests to replace the line 35, of the base.rb (for me it is C:\Ruby\lib\ruby\gems\1.8\gems\scruffy-0.2.6\lib\scruffy\renderers\base.rb)with

svg.svg(:xmlns => "http://www.w3.org/2000/svg", 'xmlns:xlink' =>"http://www.w3.org/1999/xlink",:viewBox => "0 0 #{options[:size].first} #{options[:size].last}") {

After it, the graphs started working like a charm. Looking forward to explore it more. Hope, it saves your time.

Monday, October 5, 2009

Recipe : How to get property value in a Spring Application

If you work with Spring, I am sure you might have come across the scenario when you need to access the value of a property key in your application.

The class "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" is normally used to resolve the runtime key/value pairs. However, if you need to use a property value in your bean, it doesn't expose that to be read.

Here is a very simple work-around :-

1. Extend the PropertyPlaceholderConfigurer


public class OpenPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

Properties mergedProperties;

public Properties getMergedProperties() throws IOException {
if (mergedProperties == null) {

mergedProperties = mergeProperties();

}
return mergedProperties;

}
}



2. Configure it to be used instead of PropertyPlaceholderConfigurer

  
<bean id="propertyConfigurer" class="com.mypackage.OpenPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="false"/>
<property name="locations">
<list>
<value>classpath:common.properties</value>
<value>classpath:${YOUR_ENV}.properties</value>
</list>
</property>
</bean>


3. Inject this "propertyConfigurer" in your defined bean.

4. Get the key value propertyConfigurer.getMergedProperties().getProperty("your.prop.key")

I am sure, there could be some better ways. Let me know, if you know one.

Credit goes to Mat for this recipe.

Mac Terminal Error : Could not determine audit condition

When I launched my terminal today, was welcomed with this error :-

login: PAM Error (line 396): System error
login: Could not determine audit condition

[Process completed]

It is most probably because I was playing with my /usr/bin permissions the other day.
The fix is easy. Just delete the "/usr/bin/login" dir.

But how do I delete it, if I can't access the "Terminal" altogether ?
Come on - You can access any folder using the "Finder".

1. Open "Finder"
2. Open "Go To Folder"
3. Type "/usr/bin/login"
4. Delete it.

You are done.

Sunday, October 4, 2009

Typica library - Added my patch to set connection timeout

Typica :- is a simple API to access Amazon's SQS, EC2, SimpleDB and DevPay LS web services

A couple of month ago, I was using this library to interact with the Amazon's SQS queue. While testing, a bug popped up where in case of a network failure, the code was trying to make the connection endlessly.

Here is the history of this bug. I changed the jar and suggested a patch.

The good news is that they applied my patch to the code base and from the version 1.6 onwards, this bug shouldn't appear.

Get at Amazon