Thursday, 5 December 2013

With Spring security session creation and destruction is managed by the framework. Along with the session management code, we often tend to have some other activities performed. For e.g. I would like a count of the sessions created. As the code is now within Spring how do we add our audit code ? In a simple application the solution would be to add a HttpSessionListener.

Tuesday, 3 December 2013

We saw an example of the GenericFilterBean earlier. While the class included some nifty techniques from Spring, it isn't really a part of the Spring Environment. We saw how to get access to the Spring Environment we had to

Wednesday, 27 November 2013

In a Java Web application, where authorization is involved, roles come into the picture. If a request is authorized to access a particular resource, then only will the Servlet Container allow it to work with the resource. How is the authorization decision made ?

Friday, 22 November 2013

Login page in most web applications have a small check-box that says "Remember Me". Nothing new there. The cool part is spring security comes with this functionality ready made.
I decided to use the default login form to test the feature:

Tuesday, 12 November 2013

In the previous post we saw how EnumSet is an optimized collection built for use with Enums. While I genuinely struggled to come up with scenarios where I could need a set of enums, I have often come across and used Enums as keys in my maps. Java has come up with an optimized collection for the above use case too - EnumMap.

Tuesday, 5 November 2013

We have used components in Hibernate. They provide a very good technique for code reuse. They are also useful if we do now want to represent a particular table as Entity and give them their own separate life cycle. JPA also provides support for components.

Friday, 1 November 2013

The ExecutorService Interface is not the end of Executors. The package provides another specialized interface ScheduledExecutorService which is capable of executing tasks after a certain delay, or repeating tasks at fixed intervals.
I decided to test the interface.

Thursday, 31 October 2013

In the previous post we had a look at the invokeAny method that allowed us to get the result from the first thread to complete. ExecutorService also provides us with the invokeAll method that will execute all the tasks given. Consider the below code:

Wednesday, 30 October 2013

I came across the invokeAny method of ExecutorService and liked the idea a lot. There could always be the case that you have a set of input of which you care to see any one processed. Once we have an output the remaining results don't matter.
Accordingly I decided to implement such a case with the invokeAny method of ExecutorService.

Saturday, 26 October 2013

Before I start - I have never used volatile nor can I claim to well versed at it. To be more accurate my understanding of the java volatile keyword is zilch. Part of the reason is that I never used it. Did not use hence did not bother learning.
So to understand the concept I searched the net. I found articles where explanations where given. These articles were then followed by comments claiming that the explanation was wrong/incomplete. Some of the posts only managed to confuse me further.
So when I found something that made sense, I decided to jot it down...........

Tuesday, 22 October 2013

I was asked to do a revision in threads a while back - so as to then start understanding the concurrency package. I decided to do it from the Sun's Concurrency tutorial. I liked the part of sleeping and interrupting threads. Consider the simple program below:

Saturday, 19 October 2013

We have seen how to configure login behavior for our application. How to let Spring manage authentication, session creation, authorization etc.The logical end to the flow would be to logout. A normal logout would involve releasing any resources, destroying sessions etc - and a logout page.

Wednesday, 16 October 2013

In the last post we saw how Spring security can be used to secure web applications. I used both the form login and the basic login to get the user authentication done.
While basic login is pretty much client system dependent, the form login can be tweaked and customized. I decided to try out the options.

Saturday, 5 October 2013

In the previous post we saw CXF endpoints could set a soap header in the response. But what if the server needs to process a soap header ? The CXF website recommends using interceptors for reading headers. As I
still do not have a very clear idea about the concept I decided to look
for a different solution.

Friday, 4 October 2013

The optional SOAP Header element contains application-specific information (like
authentication, payment, etc) about the SOAP message. If the Header element is
present, it must be the first child element of the Envelope element. All immediate
child elements of the Header element must be namespace-qualified.

Thursday, 26 September 2013

I knew Spring supports two types of injection - setter and constructor. I had
also heard of a third type called method injection. But having never
come across a use case for the same, I pretty much ignored it. Until I gave an interview.
All was going well until this question came along:"You have a singleton bean. Is there a way to inject a new bean X into
this class whenever you call the getX property of this bean ?"
I was pretty flummoxed. Later on I hunted on the web and came across this post by Rod Johnson.
The below post is an adaption from the same to focus on the interviewer's question.

Friday, 20 September 2013

With the growth of annotations XML configuration files continue to reduce in size (In some cases they have disappeared). I have never really been a fan of Annotations, especially those that bring in information that was best left outside Java code.

Saturday, 14 September 2013

In the previous few posts we saw how JPA provides us with an annotation driven approach to configure our entities. As with all standard APIs, when vendors implemented JPA, they found that the annotations did not cover every nifty trick provided by the implementations.

Tuesday, 3 September 2013

I earlier did a post on error handling in Spring web applications. It was related to generating views for errors. But in a REST application we would prefer to return error codes or error JSON message. While the earlier style can be still used here to write out an appropriate JSON error response, Spring MVC provides other mechanisms to handle errors.

Saturday, 17 August 2013

In the previous post we saw how to setup the cache provider for Hibernate. But that is not the end of the story. We need to decide what kind of concurrency we need. Does our data ever change? Can we live with stale data? All these factors are decided by the concurrency strategy we use. Hibernate provides us with four built in concurrency strategies (as of version 3.0):

Friday, 16 August 2013

The Hibernate framework comes with a complete caching system. This cache system is used to reduce the amount of SQL queries fired in the system.
The Hibernate cache is built at two levels - a first level or session cache which we saw earlier and the second level cache. While the first level cache is non optional, the second level cache needs to be configured and switched on for use.

Wednesday, 14 August 2013

In an earlier post we saw The ExceptionUtils class provided by the
Commons Lang library. The library also provides us with a SystemUtils
class - it provides us with information about the JVM in use and the underlying platform. Consider the below code:

Sunday, 11 August 2013

All projects I have worked on included login. And roles. They were built using database tables or third party applications like LDAP or service based authentication. However the new projects starting now are going ahead with Spring Security. As I have no idea about it, I decided to give a go at understanding Spring Security.
I decided to first do a small simple web application and enable the security for it. Not spring based, simple Servlet style security.

Tuesday, 6 August 2013

When using CXF, the marshaling and unmarshaling of data happens under the covers. So when we make the call through java, the request is marshaled to XML by CXF client. It sends it over the wire to the server.

Monday, 5 August 2013

If we have a property that does not map to any column in the table, we simply do not specify that property in our hbm files. But with JPA annotations it is a little different. A class if marked with an Entity annotation, all its properties are considered persistent by default. So how do we exclude a property ?

Sunday, 28 July 2013

In one of my previous projects I encountered a strange issue. The SQL queries that worked in our development code always failed on the server. The logs indicated that the queried tables did not exist.
This did not make any sense to us. The code ran perfectly on local MySQL database installed on our developer machines. But the minute we did it on the server we got table does not exist error. After a lot of digging we discovered that there was no problem in the code. Our table name in the ddl queries was in lower case but in our code it was in title case. But why did it work on Windows then. Some searching on the net explained the actual cause of the issue.From the MySql docs:In MySQL, databases correspond to directories within the data directory. Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix
Our development machines were using Windows operating systems. In Windows you cannot have two files in same directory with same names but different cases. So the case difference did not cause any problems on Windows. MySql was able to find the table correctly on the file system and all things worked fine. But our test server was a linux environment. The table in the SQL query was not same as the table in the database. Result - requested table not found.
Our solution was to create constants in the code to hold our table names in small case and to ensure that all references in our queries used these values only.
An alternative action to doing drop/rename is also available in MySql docs.
How table and database names are stored on disk and used in
MySQL is affected by the value of lower_case_table_names. Changing this value on linux can make MySQL use Windows style treatment for the tables.

Friday, 26 July 2013

I guess we have all used filters in our web applications. With the arrival of spring and its very solid front controller, code that was in filters is most of the time placed in Spring interceptors. But that does not mean that we cannot add a filter.

Thursday, 11 July 2013

I was searching the net on good practices for developing RestFull services when I came across this interesting discussion on stack overflow:
The question was "Should we use PUT or POST for Create Resource ?"

Monday, 8 July 2013

IN the last few posts I have been trying out Springs REST API. For the create operation we had used a POST call. The API would take an user
resource, create it and return the user resource in the response stream.

Sunday, 30 June 2013

One of the most commonly used features in Java is Exceptions. I have always used it- throwing, catching and sometimes crashing code with them. I recently had a requirement where I needed to convert a stack trace to a String.

Saturday, 15 June 2013

In an earlier post we saw how to work create REST services using Spring. For testing the same we used a RESTClient Plugin available with Firefox.
It is often the case that we need our test code to be in Java so that it can be reused by others too. Spring provides us with an easy to use Rest Service client that can be used in our code.

Thursday, 6 June 2013

The CXF architecture provides us with interceptors – or the ability to intercept outgoing request and responses. One useful scenario could be logging.
CXF in this case has stayed one step ahead and already built custom Log Interceptors.

There is no direct way to create the same. We need to use the asLifoQueue method of Collections class to create a LIFO queue.
Further this method requires an instance of Deque to be available . It will not work with any queue.
The output is :

LifoQueue Contents : [6, 5, 4]
6
5
4

Be sure to pass and empty Deque as the parameter. Any elements already present in the deque are not treated in the LIFO order.

Sunday, 2 June 2013

I first came across queues in Engineering. The very first chapter in data structures involved stacks and queues. So when I needed to implement one in my java project, I started recollecting the queue's working, recalling important points.I decided to build a generic structure and spend half a day coming up with a Queue interface based in generics. For good practice I decide to check the web and that is when it bombed.

Monday, 27 May 2013

Over the last one year I have worked on three projects involving REST based services. I found REST pretty cool as a concept - also the fact that unlike SOAP I did not have to learn any new terminologies. instead I needed to model my existing understanding of HTTP to build services.

Wednesday, 15 May 2013

In the previous post we saw how JAXB generated JAXBElement instances from simple types. Simple types however are just that - simple (!!!)
We often need our elements to allows attributes, include child elements etc. These cannot be achieved using simple types. We need to create complex types for the same.

Monday, 13 May 2013

In the past few weeks I have been working with web-services. So exposure to xml and xsd was inevitable. I had used xsd a while back and so I needed a revision of some of those topics. I decided to do a quick summary of some of the options available in xsd and how JAXB works with them

Tuesday, 7 May 2013

In the post on Spring web services, I had used a contract first approach for development. This being the only style supported by Spring.JAX-WS and also CXF on the other hand support both code first and contact first approaches.
I decided to use the same wsdl file from my Spring post:

Thursday, 25 April 2013

We often have grids in our application where we need to show the results as pages. There is also often a row of text making the claim " Record 1 to 30 of 100". While the bounds of the result can be obtained easily as we know the limits to apply, it is getting the total record count that is interesting.

Saturday, 6 April 2013

In all my posts on JPA I have used a PostgreSQL database. The database is composed of multiple schemas (Unlike PostgreSQL, MySQL database supports only a single schema). So we need to specify to our application the schema being used. I did that by setting the schema attribute of the Table annotation.

Thursday, 4 April 2013

In the various programming environments supported by Java, I have found web-services to be the most difficult to penetrate (That and JNDI) . The thing is there are several web-service providers in the Java community. They all make various claims, add to already vague terminology and almost always leave a novice reader confused.
So when I got a chance to implement web services using Spring's web service framework, I was pleasantly surprised with the well explained Spring docs. And while learning some cool stuff, I also realized that Spring web services is quite different from some of the other implementations.
So when I got the chance today, I decided to do some investigation on few of these frameworks. I have earlier blogged on how Spring allows us to expose our application as a web service. Spring's web service implementation is not related to JAX-WS. Spring's implementation as we saw, only supports contract first development. There are several advantages to the same. In the words of Spring web services creator Arjen Poutsma

Tuesday, 19 March 2013

In previous post we have seen the persistence.xml file - the file provides all the configuration information needed to get JPA up and running. We also saw that the JPA standard is very particular of the location of the file.

Wednesday, 13 March 2013

In the previous post I created the project structure for my JPA application. Now to look deeper into the code. I decided to build a simple Pet-Owner relation. One owner can have multiple pets. The java classes for the same is as below:

Thursday, 7 March 2013

Deploying a web application in Tomcat involves placing the war file or the extracted folder in Tomcat's webapps folder. I created a very basic web application named simple and placed it in the webapps folder.

Saturday, 2 March 2013

Writing code without using collections framework would be crazy. They are really cool part of java with a lot of nifty stuff. More importantly unlike arrays you do not have to worry about size management here. The ArrayList will grow as long as memory is there. No need for the developer to worry about initial size and ArrayIndexOutOfBoundsException.
But what when we are actually asked to watch out our memory footprint ? Can we be memory efficient when we are using Collections ?

Wednesday, 20 February 2013

In my previous post we saw how to achieve scheduling using Quartz API and Spring. The thing about Quartz is that it adds an additional dependency in your project.If you have a simple project and do not need Quartz's heavy power, you can use Spring's own scheduling mechanism. I modified our previous example to use it.

Thursday, 14 February 2013

We earlier how a custom criterion could be created to handle special scenarios. However this may seem overkill if the condition can be easily expressed using native SQL. For Criteria allows us to add custom SQL expressions which get added in the where clause.

Tuesday, 29 January 2013

With Spring MVC we have the Front Controller pattern at work. So there is the one Servlet - Spring's own DispatcherServlet which receives the requests and forwards them to the controllers in the code.But what if you wanted to access the ServletContext ? Or (in some rare case) the Servlet Config ?
Could you get them in your controller?

Friday, 25 January 2013

I need to execute a query to fetch all entities with id is greater than 5 and the names start with "ent". That means there are two distinct conditions. Doing that using HQL would be pretty straightforward:

Sunday, 6 January 2013

In our previous post we saw Spring's ability to allow us to send mails. With mails comes up the requirement for mail templates. We want to dynamically generate our content using some template generation framework.
I decided to integrate Velocity via Spring for this purpose.

Wednesday, 2 January 2013

I am yet to work in a project that did not involve any mail related functionality. Send notification mails to admin, send alerts to users, send password mails... the use cases for mails go on.Spring has provided a utility class that wraps around the the javax.Mail library.
So we do not need to acquire any mail Sessions, create javax.mail.Transport objects or attempt to connect with the mail server.We simply get the wrapper class and call send.

About Me

I am currently pursuing my masters in Computer Science at the University Of Texas at Dallas. Back in India I worked as a software engineer in the area of Java/J2EE for over five years. My experience is primarily in the financial and enterprise domain. I like tinkering around with the tools of my work and sharing the knowledge gained.