Sunday, March 24, 2013

Identifying Classes using modelling

Identifying the right classes for a System is an Art rather than a skill.Modelling too many classes results in too complex code which is difficult to maintain and may also result in poor performance.On the other hand if there are too few classes then it may result in tight coupling.Striking a balance between these provides the basis for a good design.
When designing a system,always believe that the best design is yet to come.Revisiting and Refining the proposed design will always aid to carve out the best design.The domain knowledge is essential key factor to come up with a good design.
Since it may not be feasible for a Designer to be a domain expert right from the begining ,there are some design guidelines to identify the classes.The most commonly used approaches are
1.Noun/Noun Pharses approach:
In this approach ,all the noun and noun phrases are identified from the requirements.
2. CRC card approach:
Using the Class Responsibility Collaborator approach,address a single requirement ,identify the similar objects and group them into a class .Add the responsibility and collaborations for this class.Extending the same to each requirement will render a list of Candidate classes.
Using these approaches,the list of candidate classes can be identified.From the list remove the classes which are duplicate and those which are not related to the system.On analysing the classes,if you find that the class does have significant purpose then define it as an attibute instead of a class .At the end of this analysis a number of candidate classes would be eliminated and the design classes would be derived from this list.
As the design progresses its very common to find that a few of the classes which were identified as the core classes may not be actually required .Having said that it is also possible to come up with totally new classes which were missed in the initial candidate class.
The right set of classes would eventually emerge after a number of interative and incremental cycles.

Saturday, January 19, 2013

Apache Hadoop

Apache Hadoop is an open source Java framework for processing large amount of distributed data.Hadoop is a Yahoo! initiated project to enable processing of very large data sets in a cost effective way.
It is based on Google's MapReduce algorithm which is a parallel and distributed solution approach for processing large datasets. MapReduce is utilized by Google and Yahoo for their websearch.Using MapReduce,data is transformed into a set of Key/Value pairs and later these are aggregated to provide the search results.
Data in Hadoop is broken down into smaller blocks and distributed throughout the cluster.This aids to execute the MapReduce functions on smaller subsets of the large data set.Apart from Java,Hadoop also provides linker for  C++ ,Perl,etc.
Hadoop is ideal for storing large amounts of data, like terabytes and petabytes.The Hadoop Distributed File System(HDFS) is employed as the storage system which is highly fault-tolerant and is designed to be deployed on low-cost hardware.It manages storage on the cluster by breaking incoming data into pieces, called “blocks,” and storing each of the blocks redundantly across the pool of servers. By default,
HDFS stores three complete copies of each file by copying each piece to three different servers but the number of copies is configurable.
Hadoop is very flexible,scalable  and can process large amount of data in parallel .This can be deployed on large clusters of commodity hardware,hence it is very cost effective.
Its ideal to use Hadoop  when there is a need to analyze enormous amount of information to understand the usage pattern and to predict demand.

Sunday, November 11, 2012

Nailing the Root Cause in Java Exceptions

Java provides chained exception facility which enables to nest an exception within another .But to get the deepest level exception,there are no direct methods available as of now.
Packages like  Apache's common lang provides convenience method to  get the root cause.
(http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/exception/ExceptionUtils.html)
The root cause can also be retrieved by manually iterating down the stack trace using getCause().
The following snippet is a recursive method that is used to retrieve the Root Cause.

public Throwable getMostSpecificException( Throwable exp ) { 
        Throwable t = exp;
        logger.debug( "exp.getMessage()-->" + exp.getMessage(  ) );
        if ( exp.getCause(  ) != null ) {
            t = exp.getCause(  );
            // recursive call
            getMostSpecificException( t );
        }
        return t;
    }

Monday, February 6, 2012

Integrating with PayPal

PayPal is one of the largest online payment processors in the world.

It serves as a digital wallet which allows to purchase online in a secured manner. The PayPal System stores the User details and provides the flexibility to purchase by just using the PayPal Account Id .

To use PayPal,the user should create a PayPal account and provide information about the modes which they would like to use for payment like Credit Card ,Bank account etc. The preference order can be configured for the Payment Options. If the user has insufficient fund in their preferred option then the next available option would be used to complete the transaction.

The PayPal services can be integrated with the web applications by using either HTML or using API integration. PayPal SDK is another option to integrate with API.

Sandbox is an isolated test environment provided by PayPal to test its functionality. This is claimed to be an identical copy of PayPal Live Site and it is recommended verify all the PayPal calls in the Sandbox before deploying in the Live environment.

Its easy to get lost in the vast documentation of PayPal ,I feel it could have been organized better. The developer community and forums are better options to seek to sort out issues.

Friday, July 22, 2011

Incorrect Order of SQL execution within a Spring Hibernate Transaction

Recently I faced a strange issue when trying to insert proper data into table ,it was halting with Unique key Constraint Exception.In a transaction I had to read a record and update the Unique Key fields and in the next step insert a record .The record inserted should have the same Unique Key values as the record read from the table.The sequence was a simple read,update and insert a new record.Was baffled when this sequence continuous threw a Unique Key Constraint.When I removed the UK constraint from the table,the records were updated and inserted as required.

On checking on the hibernate log found that the insert was fired before the update .I tried to disable the Spring transaction and then the order of execution was correct.
Looks like the order of SQl execution is not guaranteed within a transaction.To solve this issue one of the following needs to be done after the update and before the insert
1.call hibernateTemplate.flush() or
2.call hibernateTemplate.refresh() or
3.again read the record from table ,this internally calls the flush.

Friday, April 29, 2011

Upgrading to Spring MVC 2.5

I was trying to get familiarised with Spring MVC 2.5.I've used MVC 2.0 in the past and was counting to levarage on the experience.Gettings hands on I realised that 2.5 is a lot different from 2.0.The following are the major differences based on my understanding

Controllers:
Spring 2.5 has introduced Annotation based Controllers.Apart from the native controllers like AbstractController, SimpleFormController or MultiActionController, the DispatcherServlet can now handle any kind of action type.The controllers need not extend or implement MVC related classes or interface.A simple java class can serve as a controller by just adding the @Controller sterotype.

The annotated controllers are identified by specifying the context:component-scan in application context file.This specifies the package of the any annotated components that should be searched.

The methods that specify the mapping should be annotated with the @RequestMapping contruct .This maps the web request and specifies the type of method.The handler method signature are flexible and within a controller each mapping method can have different signature.The mapping methods allows to return nothing (void), a String representing a view, or just a model.By convention, if method does not explicitly return a view, the view name is defined by converting the mapped URL into a view name.

@ModelAttribute:
The methods marked with @ModelAttribute are invoked before every request.This can be used prepopulate the data required by the form.The functionality is similiar to the FormBackingObject in SimpleFormController.A controller can have any number of ModelAttribute methods.In most cases one method to populate the required data should suffice.


Validation and Binding Errors:
In Spring 2.0,injecting the validator bean would handle the Validation.But in 2.5,the validator needs to be injected and should be invoked manually.The BindingResult can be used to store the request data binding errors .If a method uses multiple objects a separate BindingResult instance can be defined for each object

Property Editor:
The Property editor can be registered using the method annotated with @InitBinder contruct.This is similiar to the initBinder() method .

The sample code has a simple form that has option in English/French to enter the data.The source code can be downloaded here.

Saturday, February 26, 2011

Reading data from Hashmap using JSTL

The requirement was to retrieve date based on the key from a HashMap and to display it in the UI.This is a common scenario when using the foreign key data.Initially seemed quite straightforward to create a HashMap to store the key and Values.In the JSP front ,the data can be retrieved either by
(i) looping the contents ( construct ) to identify the match or
(ii) retrieve the data from HashMap using the key( map["key"]).
I preferred the later approach since it was more performance effective.

Map map = new HashMap();
map.put(1, "Monday");
map.put(2, "Tuesday");
map.put(3, "Wednesday");

This was not rendering the data in the page.But when looping the contents the match was found and displayed in the page.

By modifying the key type in HashMap to String the values were retrieved and displayed in the Page.
Map map = new HashMap();
map.put("1", "Monday");
map.put("2", "Tuesday");
map.put("3", "Wednesday");

My inference from this is that EL does not support the Interger wrapper class for HashMap.But I could not find any documentation to confirm this.