Thursday, September 19, 2013

Why to Use Findbugs ,PMD and Checkstyle ?


PMD works on source code and therefore finds problems like: violation of naming conventions, lack of curly braces, misplaced null check, long parameter list, unnecessary constructor, missing break in switch, etc. PMD also tells you about the Cyclomatic complexity of your code which I find very helpful (FindBugs doesn't tell you about the Cyclomatic complexity).

FindBugs works on bytecode. Here are some problems FindBugs finds which PMD doesn't: equals() method fails on subtypes, clone method may return null, reference comparison of Boolean values, impossible cast, 32bit int shifted by an amount not in the range of 0-31, a collection which contains itself, equals method always returns true, an infinite loop, etc.
Usually each of them finds a different set of problems. Use both. These tools taught me a lot about how to write good Java code.

Checkstyle : The programming style adopted by a software development project can help to comply with good programming practices which improve the code quality, readability, re-usability, and reduce the cost of development.

Sonar will run CheckStyle, FindBugs and PMD, as well as a few other "plugins" such as Cobertura (code coverage) by default for Java projects. The main advntage is that it stores the history in a database. You can then see the trend

Summery : Conventions (Checkstyle) / Bad practices (PMD) / Potential bugs (FindBugs) .

Monday, September 2, 2013

Creating Finders method and Custom Queries







Creating Finders method and Custom Queries

Process For Finders method
 if you have some Finder methods do the entries like this
After running the service builder build on service.xml
(Copy and paste from XXXLocalServiceUtil ---->  XXXLocalServiceImpl  ) (change accordingly)
Then again  run service builder build .


Copy the finder function from XXXLocalServiceUtil

 public static java.util.List findByN_S(
        java.lang.String name, int status)
        throws com.liferay.portal.kernel.exception.SystemException {
        return getService().findByN_S(name, status);
    }

To XXXLocalServiceImpl after changing in following way

public java.util.List findByN_S(
            java.lang.String name, int status)
            throws com.liferay.portal.kernel.exception.SystemException {
            return ModOrganizationUtil.findByN_S(name, status);
    }

Process For Custom Queries method
Similerly if you have some custom queries for those function do the entries like this
(Copy and paste from XXXFinderImpl  ----->  XXXLocalServiceImpl) (change accordingly)
 Then again  run service builder build .

   public int searchOrg1stLevelCount(String name, String partnerKey, int status,
            java.util.LinkedHashMap searchParams, boolean isAndOperator)
            throws com.liferay.portal.kernel.exception.SystemException {
        return ModOrganizationFinderUtil.searchOrg1stLevelCount(name, partnerKey, status, searchParams, isAndOperator);
    }

Thursday, August 15, 2013

Running a Debug in Eclipse: (Also useful for Remote Java application)



Running a Debug in Eclipse:

(Also useful for Remote Java application)

We just need to connect the sourcecode in eclipse to your eclipse debug (that will automatically detect a running tomcat on your machine) .

*   Open Eclipse 
* Select Debug Configuration (below the Bug Picture)
It opens the following  panel
Select the Remote Java Applications menu from the left panel
Fill the name of the debug in which you want to run
Browse and select the source code on which debug should run
(P.S. You don’t need to change Host and Port generally for any tomcat running on local system)
Click on Debug , and its  Done .

* Note : If an error message appears like following  then your debug is already running.


Go to debug perspective (For both purpose either to close or look into already running Debug)

And Click on this symbol to close the already running Debugger .


Or To Use the Debug , Do some action on the web application ,like in my case I am submitting a form


The Debug appears at the point you added the debug




Press F8 (which is also Resume button),that will take you to the break point from there debug each line with F6.
If you want to go to next break point press F8.



Tuesday, August 6, 2013

Some interesting Eclipse Short-cuts



Shortcut for "Key" related Help :
CTRL + Shift + L.

Function Call Hierarchy :
 CTRL+ALT+H

For adding java doc comments to whole file :
CTRL+ALT+J

Open any file quickly java ,jsp,xml etc :
Ctrl + Shift + R

For Java type in or outside workspace(like in jars) :
 Ctrl + Shift + T

Go directly to a member (method, variable) :
Ctrl + O

jump from one member to the next (or previous) :
Ctrl + Shift + ↓ or Ctrl + Shift + ↑

Go to the last edit location:
Ctrl + Q

Go to a supertype/subtype:
Ctrl + T.

Go to other open editors:
Ctrl + E /

Monday, July 29, 2013

Maven commands needed for a Portlet with service layer



Liferay Service Portlet
---------------------------
When we create a Liferay portlet with service layer is in following structure .

--Root
    --Portlet
    --Service

//This will create the service layers
From Portlet > mvn liferay:build-service

//This will create the jar for the services
From Service > mvn clean package

//This will put the services jar into the folder that will be used for creating war
From Service >mvn install -Dliferay.auto.deploy.dir=(portlet path) -Dfile=.\target\name of the jar file .jar
 
//This will create the war
From Root : mvn clean package

then Copy and  Deploy the war from /target folder

Frequently Needed Maven commands in Liferay :






Steps To Generate Portlet : mvn archetype:generate
(then select appropriate  options by searching liferay )



Steps To Deploy Portlet* : mvn clean install -Dliferay.auto.deploy.dir=(Path of Liferay Deploy Directory)

To Create the War file* : mvn clean package


To install some dependencies jar* : mvn install:install-file -Dfile=D:\jar\util-taglib.jar -DgroupId=com.liferay.portal -DartifactId=util-taglib -Dversion=6.1.20 -Dpackaging=jar
 (DO THIS FOR ALL THE MISSING JARS REQUIRED ,you need to put first manually these jar in some folder(this case D:\jar\ ) than install using above command)


* Need to run all these command from the pom.xml path of the portlet 

Sunday, June 30, 2013

Now Reading and reviewing this book for Packtpub .



Tuesday, October 26, 2010

Hibernate Integration with Spring

Hear comes the integration of Hibernate and Spring

Prerequisite Steps

1) Create java class (bean) Employee
2) Create Database and table employee
3) Create hbm.xml
4) make all mappings


3.4) Creating the Spring Configuration File

This section deals with configuring the various information needed for the Spring Framework. In Spring, all the business objects are configured in Xml file and the configured business objects are called Spring Beans. These Spring Beans are maintained by the IOC which is given to the Client Application upon request. Let us define a data source as follows,

spring-hibernate.xml

Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html

The above bean defines a data-source of type 'org.apache.commons.dbcp.BasicDataSource'. More importantly, it defines the various connection properties that are needed for accessing the database. For accessing the MySql database, we need MySql database driver which can be downloaded from http://dev.mysql.com/downloads/connector/j/5.1.html. The first property called driverClassName should point to the class name of the MySql Database Driver. The second property url represents the URL string which is needed to connect to the MySql Database. The third and the fourth properties represent the database username and the password needed to open up a database session.

Now, let us define the second Spring Bean which is the SessionFactoryBean. If you would have programmed in Hibernate, you will realize that SessionFactoryBean is responsible for creating Session objects through which Transaction and Data accessing is done. Now the same SessionFactoryBean has to be configured in Spring's way as follows,


Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html

To make the SessionFactoryBean to get properly configured, we have given two mandatory information. One is the data-source information which contains the details for accessing the database. This we have configured already in the previous step and have referred it here using the 'ref' attribute in the 'property' tag. The second one is a list of Mapping files which contains the mapping information between the database tables and the Java class names. We have defined one such mapping file in section 2 and have referenced the same here with the 'list' tag.

The 3rd important Spring Bean is the Hibernate Template. It provides a wrapper for low-level data accessing and manipulation. Precisely, it contains methods for inserting/delting/updating/finding data in the database. For the Hibernate Template to get configured, the only argument is the SessionFactoryBean object as represented in the following section,



Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html






The final Bean definition is the Dao class which is the client facing class. Since this class has to be defined in the Application level, it can contain any number of methods for wrapping data access to the Client. Since we know that it is the Hibernate Template class that interacts with the database, it will be ideal a refer an instance of Hibernate Template to the Dao class.

Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html




Reference Link : http://www.javabeat.net/articles/
42-integrating-spring-framework-with-hibernate-orm-framewo-1.html