Sunday, July 8, 2012

Eclipse Juno release new Features

Eclipse has recently announced the release of Eclipse Juno which brings new features such as Detecting resource leaks for both Java7 and Java6 etc.

Previous Plugins to Install

I firstly install all my plugins from previous eclipse version. The process is quite simple
  • import and install from existing installation
  • refer to old eclipse location
  • Eclipse will show the list of plugins available and then asked for installation
  • After accepting license, installation should complete smoothly.

New Features available

  • Java 7 coding support in an IDE. Since Java 7 contains many new feature including dynamically-typed languages support and other small enhancements from Coin project, eclipse Java development tooling will include support for these features of Java 7.

  • Detecting resource leaks of Closeable/Autocloseable resources. This feature is really cool and it also works with “old code”. The common IO and JDBC resources now implement the relevant interfaces and the warnings are good enough to be shown on these.
    Resource leaks can be occurred in the following situations:
         1. A resource opened but not closed
         2. A resource may not closed on all control flows
         3. A resource may not closed at a method exit point
         4. In a Java 7 program a resource is closed but the code could still be improved by using a try-with-resources statement.

  • The new version include the global search bar that gives user quick access to almost any Eclipse feature.
  • Code Recommenders This is amazing feature that analyzes code of existing applications and extracts common patterns of how other developers have used. It helps deal with the complexity of large APIs using intelligent code completion attribute.

    More examples can be found from below links,

    http://eclipsesource.com/blogs/2012/06/26/code-recommenders-top-eclipse-juno-feature-2/
    http://code-recommenders.blogspot.com/

  • The addition of integrated debugging of JVM-based domain specific languages in the Xtext development framework, The below link provide extensive examples regarding the mentioned feature, http://eclipsesource.com/blogs/2012/06/25/xtext-xtend-top-eclipse-juno-feature-3/

  • Eclipse for Mobile Developers - now developers can use eclipse more easily with a variety of mobile SDKs, including the Google Android SDK

Saturday, April 21, 2012

How to Run Elastic MapReduce Job Using Custom Jar - Amazon EMR Tutorial


Amazon EMR is a web service using which developers can easily and efficiently process enormous amounts of data. It uses an hosted Hadoop framework running on the web-scale infrastructure of Amazon EC2 and Amazon S3.
Amazon EMR removes most of the cumbersome details of Hadoop, while take care for provisioning of Hadoop, running the job flow, terminating the job flow, moving the data between Amazon EC2 and Amazon S3, and optimizing Hadoop.
In this tutorial, we will first going to developed WordCount java example using MapReduce framework Hadoop and thereafter, we execute our program on Amazon Elastic MapReduce.

Prerequisites

You must have valid AWS account credentials.You should also have a general familiarity with using the Eclipse IDE before you begin. The reader can also use any other IDE of their choice.

Step 1 – Develop MapReduce WordCount Java Program

In this section, we will first going to develop WordCount application. A WordCount program will determine how many times different words appear in a set of files.
  • 1. In Eclipse (or whatever the IDE you are using), Create simple Java Project with name "WordCount".
  • 2. Create a java class name Map and override the map method as follow,
    public class Map extends Mapper {
     private final static IntWritable one = new IntWritable(1);
     private Text word = new Text();
    
     @Override
     public void map(LongWritable key, Text value, Context context)
       throws IOException, InterruptedException {
      String line = value.toString();
      StringTokenizer tokenizer = new StringTokenizer(line);
      while (tokenizer.hasMoreTokens()) {
       word.set(tokenizer.nextToken());
       context.write(word, one);
      }
     }
    }
    
  • 3.Create a java class name Reduce and override the reduce method as below,
    public class Reduce extends Reducer {
     @Override
     protected void reduce(
       Text key,
       java.lang.Iterable values,
       org.apache.hadoop.mapreduce.Reducer.Context context)
       throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable value : values) {
       sum += value.get();
      }
      context.write(key, new IntWritable(sum));
     }
    }
    
  • 4. Create a java class name WordCount and defined the main method as below,
    public static void main(String[] args) throws Exception {
      Configuration conf = new Configuration();
    
      Job job = new Job(conf, "wordcount");
      job.setJarByClass(WordCount.class);
    
      job.setOutputKeyClass(Text.class);
      job.setOutputValueClass(IntWritable.class);
    
      job.setMapperClass(Map.class);
      job.setReducerClass(Reduce.class);
    
      job.setInputFormatClass(TextInputFormat.class);
      job.setOutputFormatClass(TextOutputFormat.class);
    
      FileInputFormat.addInputPath(job, new Path(args[0]));
      FileOutputFormat.setOutputPath(job, new Path(args[1]));
    
      job.waitForCompletion(true);
    }
    
  • 5. Export the WordCount program in a jar using eclipse and save it to some location on disk. Make sure that you have provided the Main Class (WordCount.jar) during extracting the jar file as shown below.
                                 ur jar is ready!!


Step 2 – Upload the WordCount JAR and Input Files to Amazon S3

Now we are going to upload the WordCount jar to Amazon S3. First, go to the following URL: https://console.aws.amazon.com/s3/home Next, click “Create Bucket”, give your bucket a name, and click the “Create” button. Select your new S3 bucket in the left-hand pane. Upload the WordCount JAR and sample input file for counting the words.

Step 3 – Running an Elastic MapReduce job

Now that the JAR is uploaded into S3, all we need to do is to create a new Job flow. let's execute the below steps. (I encourage reader to check out the following link for details regarding each step, How to Create a Job Flow Using a Custom JAR )
  • 1. Sign in to the AWS Management Console and open the Amazon Elastic MapReduce console at https://console.aws.amazon.com/elasticmapreduce/
  • 2. Click Create New Job Flow.
  • 3. In the DEFINE JOB FLOW page, enter the following details,

    a. Job Flow Name = WordCountJob
    b. Select Run your own application
    c. Select Custom JAR in the drop-down list
    d. Click Continue

  • 4. In the SPECIFY PARAMETERS page, enter values in the boxes using the following table as a guide, and then click Continue.
    JAR Location = bucketName/jarFileLocation
    JAR Arguments =
    s3n://bucketName/inputFileLocation
    s3n://bucketName/outputpath

    Please note that the output path must be unique each time we execute the job. The Hadoop always create folder with same name specify here.

After executing job, just wait and monitor your job that runs through the Hadoop flow. You can also look for errors by using the Debug button. The job should be complete within 10 to 15 minutes (can also depend on the size of input). After completing job, You can view results in the S3 Browser panel. You can also download the files from S3 and can analyze the outcome of the job.

Amazon Elastic MapReduce Resources

Tuesday, March 22, 2011

Free Open Source CRM and Helpdesk Software

Last week, I was surfing on web to search for an free open source CRM and help desk solution. This post will share the experience and recommend some free open source CRM and Help Desk solution.

Help desk and CRM software's are always essential for any company to provide best customer support. This tools not only helps companies in increasing the customer satisfaction but also customer retention as well. In this post, we'll examine and recommend some free and open source CRM and Help desk solution.

SugarCRM and vtiger

In my opinion, vtiger is the best option as free CRM and helpdesk software. It will work both as an Help desk and CRM.

Comparing SugarCRM and vtiger, both are providing the following major functionalities,

SugarCRM
Marketing
Sales
Support
Activities
Collaboration
Reports
Vtiger
Marketing
Sales
Support
Quick Create
Tools
Analytics
 

There are many different sub actions defined within these categories which include Accounts, Contracts, Leads, Documents etc. Therefore, after getting some picture of CRM, the overall interface of vtiger seems to be good for me now.

There are also many positive feedbacks available for vtiger on the web. You can find the links in 'more information' section.

There is also community version available for SugarCRM [6] however, it does not contain reporting feature.  We might able to add some plug-ins for that [5] but it might need to explore more regarding how much work would need for this.

Kayako and SugarCRM

These are both awesome and renowned tools, however they are commercial and not free.Also for registered charities and open-source projects, Kayako Fusion, Kayako Resolve and Kayako Engage licenses are available free of cost.
Kayako Download licenses for Kayako Fusion, Kayako Resolve and Kayako Engage are available free of charge to registered charities and open-source projects. Free, perpetual licenses with unlimited users and no strings attached. Finally; some viable, business-class, free help desk software!

Other CRM and helpdesk option

Since vtiger will fulfill the requirement for both help desk and CRM feature, therefore you might not require any free separate help desk software. However, if you still would like to explore, I will highly recommend the below two tools.


Both software are available free and source code can also be downloadable. The source code is in PHP.

I find Trellis Desk awesome as its interface is good and it also provide good functionalities for help desk management systems.

Other tools include (not in PHP)

References and More Information

Monday, October 18, 2010

Getter Setter: To use or not to use

Why do we keep instance variables private? We don’t want other classes to depend on them. Moreover it gives the flexibility to change a variable’s type or implementation on a whim or an impulse. Why, then programmers automatically add getters and setters to their objects, exposing their private variables as if they were public?

Accessor methods


Accessors (also known as getters and setters) are methods that let you read and write the value of an instance variable of an object.

public class AccessorExample {
private String attribute;

public String getAttribute() {
return attribute;
}

public void setAttribute(String attribute) {
this.attribute = attribute;
}
}

Why Accessors?


There are actually many good reasons to consider using accessors rather than directly exposing fields of a class

Getter and Setter make API more stable. For instance, consider a field public in a class which is accessed by other classes. Now later on, you want to add any extra logic while getting and setting the variable. This will impact the existing client that uses the API. So any changes to this public field will require change to each class that refers it. On the contrary, with accessor methods, one can easily add some logic like cache some data, lazily initialize it later. Moreover, one can fire a property changed event if the new value is different from the previous value. All this will be seamless to the class that gets value using accessor method.

Should I have Accessor Methods for all my fields?


Fields can be declared public for package-private or private nested class. Exposing fields in these classes produces less visual clutter compare to accessor-method approach, both in the class definition and in the client code that uses it.

If a class is package-private or is a private nested class, there is nothing inherently wrong with exposing its data fields—assuming they do an adequate job of describing the abstraction provided by the class.

Such code is restricted to the package where the class is declared, while the client code is tied to class internal representation. We can change it without modifying any code outside that package. Moreover, in the case of a private nested class, the scope of the change is further restricted to the enclosing class.

Another example of a design that uses public fields is JavaSpace entry objects. Ken Arnold described the process they went through to decide to make those fields public instead of private with get and set methods here

Now this sometimes makes people uncomfortable because they've been told not to have public fields; that public fields are bad. And often, people interpret those things religiously. But we're not a very religious bunch. Rules have reasons. And the reason for the private data rule doesn't apply in this particular case. It is a rare exception to the rule. I also tell people not to put public fields in their objects, but exceptions exist. This is an exception to the rule, because it is simpler and safer to just say it is a field. We sat back and asked: Why is the rule thus? Does it apply? In this case it doesn't.

Private fields + Public accessors == encapsulation


Consider the example below
public class A {
public int a;
}

Usually this is considered bad coding practice as it violates encapsulation. The alternate approach is

public class A {
private int a;

public void setA(int a) {
this.a =a;
}

public int getA() {
return this.a;
}
}


It is argued that this encapsulate the attribute. Now is this really encapsulation?

Fact is, Getters/setters have nothing to do with encapsulation. Here the data isn't more hidden or encapsulated than it was in a public field. Other objects still have intimate knowledge of the internals of the class. Changes made to the class might ripple out and enforce changes in dependent classes. Getter and setter in this way are generally break encapsulation. A truly well-encapsulated class has no setters and preferably no getters either. Rather than asking a class for some data and then compute something with it, the class should be responsible to compute something with its data and then return the result.

Consider an example below,

public class Screens {
private Map screens = new HashMap();

public Map getScreens() {
return screens;
}

public void setScreens(Map screens) {
this.screens = screens;
}
// remaining code here
}

If we need to get a particular screen, we do code like below,

Screen s = (Screen)screens.get(screenId);

There are things worth noticing here....

The client needs to get an Object from the Map and casting it to the right type. Moreover, the worst is that any client of the Map has the power to clear it which may not be the case we usually want.

Alternative implementation of the same logic is:

public class Screens {
private Map screens = new HashMap();

public Screen getById(String id) {
return (Screen) screens.get(id);
}
// remaining code here
}

Here the Map instance and the interface at the boundary (Map) are hidden.

Getters and Setters are highly Overused


Creating private fields and then using the IDE to automatically generate getters and setters
for all these fields is almost as bad as using public fields.

One reason for the overuse is that in an IDE it’s just now a matter of few clicks to create these accessors. The completely meaningless getter/setter code is at times longer than the real logic in a class and you will read these functions many times even if you don't want to.

All fields should be kept private, but with setters only when they make sense which makes object Immutable. Adding an unnecessary getter reveals internal structure, which is an opportunity for increased coupling. To avoid this, every time before adding the accessor, we should analyze if we can encapsulate the behavior instead.

Let’s take another example,

public class Money {
private double amount;

public double getAmount() {
return amount;
}

public void setAmount(double amount) {
this.amount = amount;
}

//client
Money pocketMoney = new Money();
pocketMoney.setAmount(15d);
double amount = pocketMoney.getAmount();  // we know its double
pocketMoney.setAmount(amount + 10d);
}

With the above logic, later on, if we assume that double is not a right type to use and should use BigDecimal instead, then the existing client that uses this class also breaks.

Let’s restructure the above example,

public class Money {
private BigDecimal amount;

public Money(String amount) {
this.amount = new BigDecimal(amount);
}

public void add(Money toAdd) {
amount = amount.add(toAdd.amount);
}

// client
Money balance1 = new Money("10.0");
Money balance2 = new Money("6.0");
balance1.add(balance2);

}

Now instead of asking for a value, the class has responsibility to increase its own value. With this approach, the change request for any other datatype in future requires no change in the client code. Here not only the data is encapsulated but also the data which is stored, or even the fact that it exist at all.

Conclusions


Use of accessors to restrict direct access to field variable is preferred over the use of public fields, however, making getters and setter for each and every field is overkill. It also depends on the situation though, sometimes you just want a dumb data object. Accessors should be added for field where they're really required. A class should expose larger behavior which happens to use its state, rather than a repository of state to be manipulated by other classes.

More Reading



http://c2.com/cgi/wiki?TellDontAsk


http://c2.com/cgi/wiki?AccessorsAreEvil

Effective Java

Sunday, August 29, 2010

Java 7 Automatic Resource Management

One of the features in Java 7 is Automatic Resource Management. The idea was presented by Josh Bloch. The IO resources in Java need to close manually like FileInputStream, java.io.InputStream, OutputStream, Reader, Writer etc. The idea with this proposal is that it should not be the responsibility of the developer for disposing out these resources much like automatic garbage collection concept.

We usually write this kind of code for IO resources as below.

FileInputStream input = null;            
try {              
    input = new FileInputStream(configResource);               
    wfConfiguration = wfConfiguration.parseInputStream(input);           
} catch (IOException ioe) {               
      throw new RuntimeException(ioe);           
} finally {               
      if (input != null) {                   
           try {                       
             input.close();                   
           } catch (IOException ioe2) {                       
             throw new RuntimeException(ioe2);                   
           }               
      }           
}

The same code can be written in Java 7 as
FileInputStream input = null;            
try (input = new FileInputStream(configResource)) {         
      wfConfiguration = wfConfiguration.parseInputStream(input);     
} catch (IOException ioe) {               
      throw new RuntimeException(ioe);       
}

Hope you like the new syntax.

Project Coin: Updated ARM Spec

Friday, July 23, 2010

Java pass by reference or pass by value?

Does Java pass by reference or pass by value?

In Java everything is passed by value. Sometime this is confusing but the point to understand is that in Java, when we pass an parameter to the method, a copy of the parameter is made.

If the argument is the primitive type, then a copy of the value of the primitive is passed. If the argument being passed is an object reference, then a copy of the value of the reference is passed. i.e., the object reference is passed by value.

Consider the example below,

public static void main(String[] args) {
   A a = new A();
   A b = new A();
   a.attribute = 5;
   b.attribute = 7;
   System.out.println(a.attribute);
   System.out.println(b.attribute);
   changeAttribute(a,b);
   System.out.println(a.attribute);
   System.out.println(b.attribute);
}

public static void changeAttribute(A a, A b) {
   a = b;
   System.out.println(a.attribute);
}   

Since Java is pass by value, the caller still does not see the change, even after calling the method.

In short, Java is pass by value for all data types. Non-primitive type variables are references to objects. They are not the objects themselves. Because variables are not the objects themselves, the objects never passed to a method; only a reference to the object is passed.

More Readings

Saturday, June 26, 2010

Find the Java Bugs with FindBugs

This post is regarding the static analysis tool that finds defects in Java programs. Static analysis tools can find real bugs and real issues in the code. We can effectively incorporate static analysis into our software development process.

FindBugs
FindBugs is an open source static analysis tool that analyzes Java class files, looking for programming defects. The analysis engine reports nearly 300 different bug patterns. Each bug pattern is grouped into a category (e.g., correctness, bad practice, performance and internationalization), and each report of a bug pattern is assigned a priority, high, medium or low.

Let’s start with some of the selected bug categories with the examples.

Correctness
Comparing incompatable types for equality

Consider the following code,


if ((!value.equals(null)) && (!value.equals(""))) {
Map spaces =
(Map) vm.get(SpaceConstants.AVAILABLESPACEMAP);
}

One would expect that the condition would true, when value is not null and is not empty. However, value.equals(null) according to the contract of the equals() method, would always return false.

Consider the another similar example,


if ((bean.getNoteRate() != null) &&
!bean.getNoteRate().equals("") &&
(bean.getNoteRate() > 0)) {
item.setNoteRate(bean.getNoteRate());
}

We might expect that the condition would true, when noteRate is not null, not empty and is greater than 0. However, the condition would never be true.

The reason is that bean.getNoteRate().equals("") would always return false regardless of being equal value. According to the contract of equals(), objects of different classes should always compare as unequal; therefore, according to the contract defined by java.lang.Object.equals(Object), the result of this comparison will always be false at runtime.

Null pointer dereference

Consider the following code,


if ((list == null) && (list.size() == 0)) {
return null;
}

This will lead to a NullPointerException when the code is executed when list is null.

Suspicious reference comparison

Consider the following code,


if (bean.getPaymentAmount() != null
&& bean.getPaymentAmount() !=
currBean.getPrincipalPaid()) {
// code to execute
}

This code compares two reference values (Double paymentAmount) using the != operator, where the correct way to compare instances of this type is generally with the equals() method. It is possible to create distinct instances that are equal but do not compare as == since they are different objects.

Doomed test for equality to NaN

Consider the following code,


if ((newValue == Double.NaN) || (newValue < 0d)) {
// the code to execute
}

This code checks to see if a floating point value is equal to the special Not A Number value. However, because of the special semantics of NaN, no value is equal to Nan, including NaN. Thus, x == Double.NaN always evaluates to false. To check to see if a value contained in x is the special Not A Number value, use Double.isNaN(x) (or Float.isNaN(x) if x is floating point precision).

Also see How can you compare NaN values? .

Method whose return value should not ignore
string is immutable object. So ignoring the return value of the method would consider as bug.
String name = "Muhammad";
name.toUpper();
if (name.equals("MUHAMMAD"))


Performance

Method invokes inefficient Boolean constructor;use Boolean.valueOf(...) instead

Consider the following code,


if ((record.getAmount() != null)
&& !record.getAmount().equals(
new Boolean(bean.isCapitalizing()))) {
// code to execute
}

Creating new instances of java.lang.Boolean wastes memory, since Boolean objects are immutable and there are only two useful values of this type. Use the Boolean.valueOf() method (or Java 1.5 autoboxing) to create Boolean objects instead.

Inefficient use of keySet iterator instead of entrySet iterator
Consider the following code,


Iterator iter = balances.keySet().iterator();
while (iter.hasNext()) {
// code to execute
}

This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) lookup.

Method invokes inefficient Number constructor; use static valueOf instead

Consider the following code,



Integer number1 = new Integer(123);
Integer number2 = Integer.valueOf(123);

System.out.println("number1 = " + number1);
System.out.println("number2 = " + number2);

Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the class library, or JVM. Using of cached values avoids object allocation and the code will be faster.

Also see: http://faq.javaranch.com/java/JavaIntermediateFaq#integerAutoBoxing

Method concatenates strings using + in a loop


for (int x = 0; x < exceptions.size(); x++) {
errorMessage += getStackTrace(
exceptions.get(x) + "\n");
}

In each iteration, the String is converted to a StringBuffer/StringBuilder, appended to, and converted back to a String. This can lead to a cost quadratic in the number of iterations, as the growing string is recopied in each iteration.

Better performance can be obtained by using a StringBuilder explicitly.

Dodgy

Code that is confusing, anomalous, or written in a way that leads itself to errors. Examples include dead local stores, switch fall through, unconfirmed casts, and redundant null check of value known to be null.

instanceof will always return true

The instanceof test will always return true (unless the value being tested is null)

NodeList nodeList = root.getElementsByTagName("node");

int nodeListLength = nodeList.getLength();

for (int i = 0; i < nodeListLength; i++) {
Node node = nodeList.item(i);
if (node instanceof Node &&
node.getParentNode() == root) {
//do code
}
}

Test for floating point equality

Consider the following code,

private double value = 0d;

if (value > diff) {
// code to excute
} else if (value == diff) {
// code to excute
}

The above code compares two floating point values for equality. Because floating point calculations may involve rounding, calculated float and double values may not be accurate. For values that must be precise, such as monetary values, BigDecimal would be more appropriate.

See Floating-Point Operations

Also see Effective Java 2nd Ed, Item 48:Avoid float and double if exact answers are required

Integral division result cast to double or float

Consider the code,

int x = 2;
int y = 5;

// Wrong: yields result 0.0
double value1 = x / y;

This code casts the result of an integral division operation to double or float. Doing division on integers truncates the result to the integer value closest to zero. The fact that the result was cast to double suggests that this precision should have been retained. We should cast one or both of the operands to double before performing the division like

// Right: yields result 0.4
double value2 = x / (double) y;

References:

Download the latest version of FindBugs.

Visit the blog.

Using the FindBugs Ant task.

Using the FindBugs Eclipse plugin.

PMD