Khangharoth

September 29, 2009

FrameWork: Spring-JPA-Hibernate (Annoation Based)

Filed under: Architecture,Java,JEE — khangharoth @ 4:56 pm

This skeleton web framework I wrote for Quarks , as they wanted a core framework on top of which they can build the applications.

As I had a luxury to design a Framework from scratch and so decided with these technologies.This dev stack is now becoming a vary dominant framework or to say in developers words “The next Popular Stack”.

Testing env I have used in this framework is TestNg , which I feel is much more suited to present day testing needs than our old JUnit(even after realese of Junit 4).

And with JDK 5+ , Spring 2.5+ which give support for Annoatations , you can have a very Simple yet very powerful framework to work with.So with this what we have is

  • Spring as a Application Framework
  • JPA as Persistence Framework (Hibernate as JPA impl)
  • Annotation based Transactional Control.
  • Spring Dependency injection with Annotation.
  • Spring MVC : This can be changed if you want to use something else.
  • Annotation based Validation framework.

These all elements give us enough power to create a generic and robust framework ,which can be customized for various needs.

Now let’s get to the actual work ……

Core Design Principle : The whole framework is desigend in Layers.Each Layer can only be called by layer above it and no skip hierarcy call’s are allowed.

Various Layers are

  • Web : Comprises of Spring MVC & JSP , this is due to the fact that most developer in quarks are quite familiar with JSp and so we wanted to start with this.Although I am not really happy with technology choice and wanted to have GWT or Flex based GUI.
  • Delegate : This is logical seperation between Web(Front End) and Service (back End).If you think that GWT or you something else suits you then this is the place where you should take a detour.
  • Service : Spring based beans, annotation based Dependency & Transaction management.
  • DAO : Db access layer , based on JPA and using hibernate as Persistence provider.
  • Persistence Domain model : Java Beans on lines of JPA Entities.

So the principle is that a layer can only call layer below it so in this Web —-> Delegate and any call direct to say services or DAO’s should be discourged.

Another way to see this is that if we just delete a particular Layer then code below it should compile just as fine without any problem.

I wanted the package structure to reinforce this idea  .Also its easier for a new person to see as how the packages are organized . The package structure is

  • com.quarks.a.web.controller
  • com.quarks.b.delegate
  • com.quarks.c.service
  • com.quarks.d.dao
  • com.quarks.e.persistence

Used a,b,c,d,e so that packages are shown in order in project view and aslo it gives clear idea of dependency in project.

Now looking at this I can easily say that DAO is only dependent on persistence pacakge.

Note : Still in edit , I’ll upload code as well . Please do give your valuable opinion about how can we have a Generic framework on this Dev Stack .

Advertisements

August 23, 2009

Generating Java Code on the fly!

Filed under: Java,JEE — khangharoth @ 3:31 pm

Currently I am working on a Grid based Cache (Coherence) in my regular job.

The concept is that differenct System can just use the Cache and do the stuff and its very easy and fast to share information.No XMl’s being excahnged and no bickering on XSD’s etc.

The thing  is in this process we were end up writing so much similar code for Serialisation purpose (PofSerialisers) and thought the this code can be auto generated.

And with this thought in mind when we looked at the problem that our Cluster needs to support various version of our core Domain model ,let me explain this problem better :

Let’s assume we have a Trade.java class with 2 attribute and we call this versionV1 so we have T-V1.Now some new attributes are added and we have T-V2 and henceforth.But its very likely that some clients of this Cluster would not like to upgrade and would still be using T-V1 .

img

To code and maintain multiple versions of Data model is big task and so we decided to go ahead with concept of auto-generating the java code giving a schema file.

This is the first time i am doing something like this.Its ok to generate Stubs/Skeletons which we use in Web-Services to get auto generated but to use auto generated domain objects and this is used everywhere in project is something first time for me.

So initally our project just have Classes having business logic and these are not compilable becuase all domain classes which they use are just not their ! and when we invoke compile goal of Maven pom file ,these domain classes get generated with their Serialsers and now we have fully fuctional code 🙂 which can be deployed.

October 13, 2007

AspectJ aspect configured by Spring

Filed under: Java,JEE — khangharoth @ 9:34 am

Today i came across a peculiar problem and more importantly it hit me in my face ,the integration tests were breaking and result of this were mail’s flying all across.

We are having a Web-Service which web-client calls and in the server side we are using spring as application framework.We were using aspect of AspectJ for some purpose and it needed certain properties which were available in spring.xml,so we thought to configure the aspect from spring.

Now just to quote a citation

http://www.ibm.com/developerworks/java/library/j-aopwork13.html

Now I’d like to configure my aspect with a particular exception handling strategy using dependency injection. For this, I can use the standard Spring approach, but with one caveat. Normally Spring is responsible for both creating and configuring beans. AspectJ aspects, however, are created by the AspectJ runtime. I need Spring to configure the aspect that AspectJ has created. For the most common case of singleton aspects such as the RemoteExceptionHandling aspect above, AspectJ defines an aspectOf() method that returns the aspect instance. I can tell Spring to use the aspectOf() method as a factory method for obtaining the aspect instance. Listing 2 shows the Spring configuration for the aspect:

Now what i mistakenly did was put this line of code in my spring.xml.

And everything was fine in Unit-testing and functional level testing.But as soon as client tried to generate the proxies from WSDL,their were number of proxies getting generated which were not even part of our WS interface.

After debugging , i got the cause of  those extra classes in WS interface.As sprin AOP is proxy based framework and it was this that was creating those extra proxy classes.

Need to get more into Spring AOP and how it is different from AspectJ.

September 26, 2007

Tutorial: Profiling With AspectJ

Filed under: Java,JEE — khangharoth @ 8:00 am

Just to have background about what is Aspect Oriented Programming :Well we are in Object Oriented programming world.

Everything is object and concepts of OOP’s can applied very easily.Things are easy to maintain as things are very well encapsulated under objects and hence code changes are easy to implement and maintain.

This happens because each Object has a specific task ,and in well written OOP’s code change in some functionality involves changes at few places or to say only concerned objects change.

This is generally applicable for Business requirements which can be easily modularized,but what about the cross-cutting concerns.

Cross-Cutting Concerns: The concerns which are to be taken care of ,in almost every part of code or in other words,code for these concerns is scattered all around the code base.In short these requirement cross-cut at many places and hence regular concepts of OOP’s are not very useful in handling them.

These are generally requirements not specified by the user , but are needed for over-all well being of the projects.

So what are these kinds of requirements:

  • Security: Security checks may be need at each and every class.
  • Logging: Logging is a typical example of cross-cutting concerns.
  • Profiling:Need to know bottleneck in the system,so typically present at everywhere.

Now as this type of code will scattered everywhere in the main code.Other then reason’s like maintenance etc ,it also reduces the code readability and make code ugly.

We imagine if you have to change something ,then we need to go and change thousand’s of classes,ex:If we decide to change the way logging has been implemented,then probably we will need to open each and every file in the project and do the change.And we all know changes are the main cause of getting infected with new bugs.So again the goal is to minimize the effect or localize the effect of change.

Q)So how these cross-cutting features come’s into play?

A) We define pointCuts:These are basically some conditions which when fulfilled starts the magic.

so a pointcut in annotation will be defined as @Pointcut(“execution(* com.model.service.EmployeeService.*(..))”)
public void employeeService()
{}

This means employeeService pointcut is applicable to all methods of com.model.service.EmployeeService implementations.

for specific methods we write like

@Pointcut(“execution(* com.model.service.EmployeeService.doSomeStuff(..))”)
public void doSomeStuff()
{
}

This pointcut will pick only this specific method.

And then you define advice where you define actual code about what you want to do,advice is then linked to a pointcut.

   @SuppressWarnings(“unchecked”)
@Before(“employeeService()”)
public void methodEnter(JoinPoint pjp )
{
Object obj = pjp.getTarget();
LOG.debug( “[” + obj .toString + “] ” );

}

So suppose we need to introduce logging into our existing code-base.What are the steps needed?

  • Define  pointcut’s  specifying where these should be invoked. You can use wild-cards and this make pointcut definition very powerful.
  • Define an advice as what needs to be done when program flow reaches these pointcuts,i.e our logging code. Now it should be very clear as how we have modularize the cross-cutting concerns.No more we have code like Log.Debug all across out code-base.
  • We also has to define as which kind of advice it is.It means when this advice code will get executed.                                                 1) @Before :Runs before the actual method starts running,just after the arguments to the method call are evaluated.                                                                                                                                                                                                             2) After: similarly we have @AfterRetruning,@AfterThrowing and simply @After which is like finally i.e no matter how the method returns ,it will run.

In the example ,the goal is to try and have profiling enabled in the project.Its a very crude profiling which just logs how much time a method takes.

To run this example just create a new AspectJ project in Eclipse,and paste the code in corresponding java files.

TestClass.java is the one which have main method,also if eclipse you can see a nice Cross-Refrence view as which aspect is advising whom or other way round.

Note:Looking out for your comment’s!

TimerClass.java

TestClass.java

Profile.java

Profiler.java

ProfilerAspect.java

September 6, 2007

Difference Between JPA and Hibernate

Filed under: JEE — khangharoth @ 3:44 pm

My colleague just asked me this question and i thought its quite a common question in, lot of developers mind.

Well as you might aware of now we have a much leaner and easy to use Ejb 3 specifications.

The EJB 3.0 specififcation is divided into two parts the first which deals with session and MDBs and the second which deals with persistence and entity beans.

This later part is know as JPA.And as i say latter part so this is independent of session and other concepts.This means that this can be used outside of any Server or managed Container .You can use JPA in SDK just like you write “Hello World”,no need of any Server or container.

JPA works with normal java beans so your code can be just Java beans ,some XML/annotatons about how Object’s are mapped with tables and you are good to go,very easy to write test cases as well.

And hibernate 3.0 implements the JPA specification so  TopLink and other O/R mapping framework.So your code depends upon JPA Api and its implementation is supplied by any JPA impl vendor framework.

So in effect if you use JPA , you are using a standard Java Api and not some orthogonal Api and hence you have flexibility in your system.

Say initially you have used Hibernate but now want to shift to Other framework.That means JPA supports pluggable persistence providers.

JPA specification is heavily influenced by current O/R mapping frameworks like Hibernate and Top-Link .

So if i need to put in Concise words:

a) JPA is Persistence Api which your code should use.

b) JPA Api will pass on the call to actual peristence provider (ex:Hibernate/TopLink) to do the actual work.

c) If you are looking from Performance prespective ,it will be dependent on actual peristence provider (Hibernate/TopLink) and not on JPA as its just a wrapper layer.

d) If you are looking from code dependency prespective ,JPA makes more sense as your code is dependent on standard Java Api.

e) If you have used Hibernate then you will find that certain features are missing in JPA like criteria queries etc.This does not mean that you can’t write criteria query in your application, you need to get Session object from JPA Entity manager and now you are as good as in hibernate project.

But now your code is dependent on Specific impl (Hibernate),going forward you can see more things getting added in JPA (2.0)

f) Should you use JPA: My take is you should ,API is clean and although not everthing you need is their but is a good step forward.

Hopefully this give better picture as where all these pieces fits into an Java based Enterprise Application .

September 3, 2007

TestNG: Another Testing Framework

Filed under: JEE — khangharoth @ 4:39 pm

Well yet again , some new framework in the pool of already existing frameworks.They all do the same stuff and yet i repeatably come across something which gives more value than existing one’s.

So what is Test-NG , this is the definition from test-ng web site : http://testng.org/doc/

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:

  • JDK 5 Annotations (JDK 1.4 is also supported with JavaDoc annotations).
  • Flexible test configuration.
  • Support for data-driven testing (with @DataProvider).
  • Support for parameters.
  • Allows distribution of tests on slave machines.
  • Powerful execution model (no more TestSuite).
  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc…).
  • Embeds BeanShell for further flexibility.
  • Default JDK functions for runtime and logging (no dependencies).
  • Dependent methods for application server testing.

TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc

I am using it now for my testing requirements and found quite useful when test-cases have dependency on each other or some other subtle things.Overall it does seems more useful and flexible than junit.

Anyway if you are familiar with Junit than it should be fairly easy to start with Test-NG.

Note: I am using Test-NG plug in for eclipse so guess things are fairly simple.

August 16, 2007

DbUnit : Testing with Database

Filed under: Java,JEE — khangharoth @ 7:49 pm

I moved to a big investment bank and now in a transition state from Java Graphics , Java3D , Xith3D …etc etc to JEE stuff ,and boy things have changed and that too drastically its surely more than 2 going away from J2EE.

One of good things working in a financial firm is emphasis on Testing:All sort of testing ,unit-Test,integration -test ……after all all those number are money and you won’t be very pleased if some java operation just truncate the decimal part of a number which happens to be your money value or worse some multiplier which magnifies the effect.

In middle of all this wonderful journey i came across a very good tool: Don’t get me wrong ,i know we already have more tools than work in hand.But this one is easy to use and come’s very handy if you are already accustomed to Junit.

Basically its a utility framework which helps you in testing of Database-Driven Projetcs :

  • Extension of Junit.
  • Helps you to put the database into a known state between test runs.
  • A database can be seeded with a desired data set before a test ,moreover ,at the end you can put it into pre-test state.

Rest you can see in this ppt: Its a good one and i have got this one from internet. DbUnit : Testing with Database

Resources:


Create a free website or blog at WordPress.com.