Aug 10 2010

Techniques For Integrating Hibernate Into Legacy Java Code – Part 1

Published by under Uncategorized

If you’re like me, you spend a lot of time dealing with legacy code that, for whatever reason, does not take advantage of modern methodologies and libraries. I’ve taken over Java projects that contain hundreds of thousands of lines of code and not a single third-party jar other than a JDBC driver! One of the most common examples of this is the implementation of the data access layer. These days, the de facto methodology involves Hibernate and DAOs, usually managed by Spring.

This article will detail the steps I recently took to covert a large application from custom-written data access to Hibernate and Spring using the refactoring facilities in Eclipse. The key with this refactorization is to get the existing business logic code (Struts Actions, JSPs, Delegate classes, Business Service classes, etc.) to access the datastore using Hibernate, managed by Spring, without manually changing any of that code directly. Part 1 will include creating the Hibernate data object classes, DAOs, and refactoring the existing code to work with these newly created types. Part 2 will conclude the project with integration of the Hibernate DAOs and wiring everything up with Spring.

First of all, we need to create our Hibernate model and DAO classes. Obviously, since we’re dealing with a legacy application and data structure, we will want to use a bottom-up approach to building our data access layer. This just means that we’re going to generate the Java code and appropriate Hibernate config files from the existing database. There are many tools freely available to make this process very painless. I recommend an Eclipse Plugin for creating and maintaining the Hibernate artifacts (Google “Hibernate Eclipse Plugin” to get started). The structure and requirements for creating Hibernate classes and config files are well documented elsewhere, so I won’t go into detail here. However, in this particular project, the Hibernate DAO lifecycles are managed by Spring, so the DAO classes should all extend HibernateDAOSupport.

Now we have java classes (POJOs) which map to our database tables, but none of the existing code uses these new data object classes. This is where the refactoring tools of Eclipse comes in really handy. For example, say we have a legacy class called AccountInfo which corresponds to the ACCOUNT database table. Right-click the class and select Refactor -> Extract Interface. On the dialogue box, call the new interface IAccount and make sure you select “Use the extracted interface type where possible.” Choose the other options according to your preferences. Click OK and kick back while Eclipse changes every occurence of AccountInfo references to IAccount references and recompiles. Of course, do this with each object model class.

If you never realized why OOP languages are so great, you’re about to. Now we’re going to refactor the code so that all of the existing legacy can be hooked into the new Hibernate model classes instead of the legacy ones. Continuing with the AccountInfo example, create a new class – you’ll probably want to create a new package for this step – called Account that extends the Hibernate POJO for Account and implements the new IAccount interface.

This next part is the most time-consuming, but really isn’t that bad. At this point, the newly created class will probably contain a bunch of empty methods containing only TODO comments. This is because the IAccount interface most likely defies a bunch of methods that are not implemented in the Hibernate Account POJO. To deal with these, we basically want the new Account class to delegate to its generated superclass whenever necessary to satisfy its contract as an IAccount type. As a real world example from the application I was working on, the legacy AccountInfo class defined a getter/setter pair for a property called username, whereas the corresponding column in the ACCOUNT table was actually LOGIN_NAME. To deal with this, you would simply implement the get/setUsername methods in Account to delegate to get/setLoginName (from its superclass). I also had to translate between various data types quite a bit. For example, the legacy code would define many properties as Strings even though the corresponding piece of data in the database was defined as an INT or TIMESTAMP. Again, do this with each object model class.

To finish up the data model layer, edit the appropriate Hibernate and Spring configuration files to refer to these new object model classes. The application now has the ability to map database records to Java objects via Hibernate, and the legacy code which refers to these classes has not required any editing by hand. To finish up this refactorization project, we need to hook in the Spring-supported Hibernate DAOs in a similar way. In Part 2 of this article, I will discuss refactoring the legacy code to read, write, and update data using Hibernate and Spring.

Billy Perez is Senior Applications Architect at Technetium, Inc. which provides professional project consulting for Fortune 1000 companies. http://www.technetiuminc.com/

No responses yet

Aug 05 2010

Build Powerful Java Applications Using Soa With New Book From Packt

Published by under Uncategorized

This book shows how to use SOA and web services to build powerful applications in Java. It teaches the concepts and the implementation with best-practice real-world examples. You will learn to design a sound architecture for successful implementation of any business solution, the different types of architecture, and various tenets of SOA. The book explains the fundamentals and the advantages of using the Service Oriented Architecture in designing your business solution.

In Detail: Service Oriented Architecture provides a way for applications to work together over the Internet. Usually, SOA applications are exposed through web services.

Web services have been around for a while, but complex adoption processes and poor standardization hampered their use at first. However, with the adoption of new, simpler protocols such as REST, and major companies supporting SOA, the time is now right to adopt these standards.

This book will show you how to build SOA, web services-based applications using Java. You will find out when SOA is the best choice for your application, how to design a sound architecture, and then implement your design using Java.

The book covers the important web services protocols: XML-over-HTTP, REST, and SOAP. You will learn how to develop web services at all levels of complexity and for all kinds of business situations.

Approach:This book is an overview of how to implement SOA using Java with the help of real-world examples. It briefly introduces the theory behind SOA and all the case studies are described from scratch.

The book is out now and is available from Packt. For more information, please visit: http://www.packtpub.com/service-oriented-architecture-for-java-applications/book

I am a Marketing Research Executive from Packt Publishing.

No responses yet

Jul 31 2010

Java Sockets – You Just Got to Plug Them in

Published by under Uncategorized

I realised that programming in Java is quite a fun only after doing it myself. You will be amazed to know that it’s like putting different pieces of puzzle together. You put them relatively in an integrated and coherent manner. The Beauty of it is, that most of the times you dont have to create these pieces yourself. You just customize them according to your need after you get them from an already defined java class or package. It was about programming, now lets talk sumthing about sockets.

Socket is used to establish a point-to-point, bidirectional connection between two entities in a network. Just like a real world socket, it is used to plugin a connection from another source. The connection can be incoming or outgoing or both. Similar is the case at the other end. To understand these sockets  properly, you need to learn a bit about Operating System and its Networking Protocols. Sockets are basically of three types: 1)UNIX Domain Sockets; 2) Internet Domain Sockets; 3) NS domain Sockets.

Java being platform independent Programming language, supports only Internet Domain Sockets as only they are platform independent out of three. These internet domain sockets are distinguished on the basis of Internet protocol they work on…

1) TCP/IP(Transfer Control Protocol): The data transfer is reliable, in-order,connection oriented, so takes connection establishment time before the actual data transfer takes place. Sockets based on TCP/IP are known as Stream Sockets.

2) UDP(User Datagram Protocol): It is connectionless,unrealiable and unordered data transfer protocol. Each packet in it has a destination address associated with it and is realeased into the network to make its own way. The sockets based on UDP are called Datagram Sockets.

3) Raw IP: It is a non-formatted protocol. Unlike TCP/IP, UDP protocols, Raw IP is not a core protocol of IP Suite. It’s different from them as its used to receive header information of the packet along with data, which is not the case in TCP/IP, UDP, they just receive data.

In Java Sockets are mainly implemented from already defined classes and pakages. These are:

1) Java.net.package: It contains all the classes that a user require to create a network based application. The below mentioned classes namely ServerSocket and Socket are also its part. This package also contain classes to create Secure Sockets and to connect a Web Server.

2) ServerSocket Class: It Provides sockets for the Server side.These sockets monitor network for requests or simply waits for them. When such request arrives, a server socket performs assigned task based on the request.

3) Socket Class: This class provides the Client side sockets. These sockets connect to the server, send and receive data for the client.

Remember, no socket can work without a port which is identified by a port no. Port is a gateway to a socket connectivity which is on the both sides of network. A socket is mainly identified as per its ports.

Shubhendu Sharma

Email:er.shubhendu@gmail.com

No responses yet







Search for java games :