Thursday, November 29, 2012

TDD : Test Driven Development



 Red -->Green --->Refactor
 Write the tests first.fail the test cases. This is what the main philosophy of TDD This paradigm can also be used in debugging legacy code.In this Blog we are going to talk about TDD methodology.

 Following are the steps for following TDD:
  • Add a test
  • Run all tests and see if the new one fails
  • Write some code
  • Run the automated tests and see them succeed
  • Refactor code
  • Repeat
  • Keep the unit small.
  • If require we can mock objects for eg sum third party services, DB we may use a mocking framework like EasyMock,Jmockit, Mockito.

What are the benefits of TDD?
  • Test Coverage.
  • Test Repeatability.
  • Documentation
  • API Design
  • System Design
  • Reduced Debugging.
  • Your code worked a minute ago
Now the question is what are the costs of TDD?
  •  Programming in tiny cycles can seem inefficient
  •  A lot of test code is produced
  •  A lot of time is spent keeping the tests in sync with the production code
 Some time people says  'SPECIFY FIRST' which means the specify the desired behavior in testms of test case.In the next blog TDD:Most Suitable Scenarios we will see some practical example where TDD suits best.

Thursday, November 22, 2012

Developing Mobile Website

There are various options available for Mobile application. Many times we prefer to develop unique apps for each mobile devices or OS. The other option is to have a single highly-branded web site or application that will work on all popular smartphone, tablet, and desktop platform.


Mobile Web application development: 
Developing a web application for Mobile is going to be definitely faster. It does not require specific skills to develop a web application.You do not need to do OS specific coding.There is no need to hire Android developer and Iphone developers separatly to build device specific Application. However there is also a downfall here which is with respect to usability. 
The user will not get device specific rich interfaces and It will have limited animation. This means we have to take proper judgement on the basis of the feature the website is going to provide.quesion which we should think are
  • Does It require Rich UI? 
  • Will my web application suffice the user needs withe least amount of time without downloading any thing to their device?
Developing a Mobile application
Mobile development is more than cross-browser; it should be cross-platform
Android user could access your site using the native Android browser, or could have also installed Opera Mini or Firefox Mobile.
Lets see few browsers which are available on dfiffent devices:
•Opera Mobile
•Skyfire
•Safari
•Google Android
•Microsoft IE for Mobile
•Firefox Mobile

Redirect to mobile Sub domain:
Based on the user agent the main domain will redirect to its mobile domain.
eg. User Agent for Froyo on a Nexus
Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
The "Mobile" string in the User Agent indicate that the request has come from a mobile device. For eg Facebook will redirect to their mobile web site m.facebook.com.

Apply mobile stylesheets:
Here we will apply the stylesheet which are specific to mobile only.

Jquery Mobile:
The advantage here is that jQuery mobile framework takes the "write less, do more" mantra to the next level: Instead of writing unique apps for each mobile device or OS, the jQuery mobile framework allows you to design a single highly-branded web site or application that will work on all popular Smartphone, tablet, and desktop platform.

The framework includes an Ajax navigation system that brings animated page transitions and a core set of UI widgets: pages, dialogs, toolbars, listviews, buttons with icons, form elements, accordions, collapsibles, and more.

The most significant thing about Jquery mobile is that same piece of code automatically handles the device and based on the space and available It adjusts as per the ambient accordingly .This framework has been tested on most of the mobile devices and mobile browsers. 

Restrict to core featutes:
 Since there is limitation of space available because of reduced screen size we can provide the core functionality only. For eg The screen will have  header , page-content, and footer only.No need to have menu list either on the Left or on below the Header section. This ensures that the core feature will have optimum space to be shown in most fashionable and appealing look.



Friday, November 9, 2012

Spring mvc features

Spring MVC references:

The latest version of spring web is 3.2. It has support for following features
  • Controller
  • Mapping Requests
  • Obtaining Request Data
  • Generating Responses
  • Message Converters
  • Rendering Views
  • Type Conversion
  • Validation
  • Forms
  • File Upload
  • Exception Handling
Spring doc:  http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/spring-framework-reference.html#overview-web
 
Spring test mvc reference: https://github.com/SpringSource/spring-test-mvc/

Spring MVC Showcase:https://github.com/SpringSource/spring-mvc-showcase.git

Thursday, November 8, 2012

Spring Core : cool features

 Here we will see Coolest features of Spring core
1. Auto scan
    @Component –  auto scan component.
    @Repository – DAO component
    @Service – Service component
    @Controller –controller component 



<context:component-scan base-package="com.thetechiehouse.sample" />
-Spring scan this folder and find out the bean annotated with @Component)
and register it in Spring container. 

When to use Cassandra

Some of the silent features of Cassandra are :

  • Written in: Java
  • Main point: Best of BigTable and Dynamo
  • License: Apache 
  • A highly scalable second-generation distributed database. 
  • Protocol: Custom, binary (Thrift)
  • Tunable trade-offs for distribution and replication (N, R, W)
  • Querying by column, range of keys
  • BigTable-like features: columns, column families
  • Has secondary indices
  • Writes are much faster than reads (!)
  • Map/reduce possible with Apache Hadoop
  • All nodes are similar, as opposed to Hadoop/HBase
Best used: 
 When you write more than you read (logging). If every component of the system must be in Java. ("No one gets fired for choosing Apache's stuff.") 
Cassandra's storage engine provides constant-time writes no matter how big your data set grows. If we are using MongoDB then writes are more problematic in MongoDB, partly because of the b-tree based storage engine.
Use Case:
  Banking, financial industry (though not necessarily for financial transactions, but these industries are much bigger than that.) Writes are faster than reads, so one natural niche is real time data analysis.

Tuesday, November 6, 2012

Logback a successor to log4j

There are different logging frameworks we use for eg log4j, Apache logger, tinilog, slf4j , java.util.logging (JUL).


In this blog we are going to talk on  Logback  which is an implementation of Slf4j API and provides various new features log4j currently does not provide.

There are number of reasons we should prefer to use logback over traditinal log4j.

  • Faster implementation
  • Extensive battery of tests: logging framework to be rock solid and dependable even under adverse conditions.
  • logback-classic speaks SLF4J natively
  • Extensive documentation
  • Automatic reloading of configuration files
  • Graceful recovery from I/O failures
  • Automatic removal of old log archives
  • Automatic compression of archived log files
  • Prudent mode on multiple JVM's
  • Conditional processing of configuration files
  • Filters   
  • SiftingAppender
  • Stack traces with packaging data
  • Logback-access: the logback-access module integrates with Servlet containers, such as Tomcat and Jetty, to provide HTTP-access log functionality. Note that you could easily build your own module on top of logback-core. 
  •  We can change the different logging level programatically
  • Switching back to slf4j with least changes
  The above reasons are enough to motivate us in using this logback framework.

Structure:
logback is divided into three modules, logback-core, logback-classic and logback-access.
 

Lisencing:
Please note that log4j is lisenced under Apache lisencing where as logback is under

EPL v1.0 and the LGPL 2.1.

How to use:
Use the slf4j logger classes in your code and the add logback.jar and logback.xml to the  classpath.


For more refer to the project site :http://logback.qos.ch/

Sunday, November 4, 2012

Solr vs Hibernate Search

Let's fist discuss the Problem with the SQL “like” clause
  •     It is very slow, database does a simple brute-force search.
  •     There is no concept of relevancy (match score)
  •     It is too literal.  it may match parts of words when you only wanted to match whole words.
In this blog we are going to talk about search engine server Solr.From a search engine following are the supports we expect:
  •  Text analysis
  •  Relevancy ranking:
  •  zaceted navigation:
  •  Query features & syntax:From boolean logic to grouping to phrases to fuzzy-searches, to score boosting.
  •  Result highlighting:
  •  Query spell correction (i.e. “did you mean”)
  •  Term-suggest (AKA search auto-complete)
  •  Sub-string indexing:
  •  Geo-location search:
  •  Field/facet suggestions
  •  Clustering:  

And Solr  has all these. Solr is a framework which is built on the top of lucane.It is an open source enterprise search server based on the Lucene Java search library, with XML/HTTP and JSON APIs, hit highlighting, faceted search, caching, replication, a web administration interface .

However Hibernate Search module also provides index based search capabilities.Below are pros and cons for Solr and Hibernate we should keep in mind while taking our decsion judiously.


    Solr:

Apache Solr will be useful when

  • We have to do a full text search
  • We need to find some text from a paragraph to a page
  • It may not be useful when there are lot of filter conditions
  • Decoupled from Application itself.It is a dedicated server.So the load at the server will get reduced.
  • it supports Faceted Search results and Geo Spatial search results.

Hibernate Search:
  • Easy to configure
  • In case of hibernate search, updating of index occurs when data base transaction is commited.      Hibenate takes care of updation to the index and not requiring us to explicitly run that … making the index near real time.
  • Limited features

Earlier Twitter was using MySQL backened for giving the search result.When they moved to real-time version of Lucene, there was a  considerable gain in the performance.
Twitter application rely on Solr/lucene for the Real-time indexes, Top tweet indexes,Geo indexes.

Will discuss more in detail in the next blog on Solr with sample example/code.
 

Thursday, November 1, 2012

Securing a RESTful Web Application

There has always been  a concern over security when going with a Restful application. Following are the clients which can access a Restful application:
  • Java Script Client
  • Java/X Client
  • Mobile Client
What the client wants is that once the client has been authenticated , mo further authentication should be done. Also the client should not send additional information like authentication details every times.
 
In this Blog we are going to see how a restful application can be secured and also, we will create a virtual session at the server side.
  • For the very first request once the authentication is done create a token and send it back to the client.
  • The client will always be passing this token with the subsequent requests.
  • At the server end create a virtual session with the original token obtained at the very first times after the authentication.
  • Create an token  encrypted which update the token every times and update the token every times. so that even though some one hacks this token, the same can not be reused further on.
  • Rest Services at the server end will keep on decrypting the token and It will understand update done each times the client requests.
  • Maintain a token map which will have the last time accessed
  • On every request made by the same client, update the last accessed time for that decrypted original token
  • Create a demon thread which will clear the virtual session for which last accessed time is more than specified time.
The above will ensures that the application is secured and the reset server supports a virtual session for these clients.

In the next Blog we will see in more detailed about securing Rest Services.