Monday, January 27, 2014

Dynamic Web Application Technologies in Java

We are all very familiar with dynamic web applications. Web application is serve the response based on user request and it can give the dynamic response.

The following is web application introduction please go through it first.


Different technologies are in the world to develop dynamic web applications. We have many technologies from different languages.

The following are popular dynamic web application development languages.


Java, PHP, .NET, Python, Ruby and Perl


Now we will talk about java language. Our objective is to develop web application in java language.

What is java?

Java is object oriented programming language which has many libraries to develop many applications.

In java we can categorize application in two types

Standalone Application

Web based applications

Standalone Application

Stand alone application are kind of application which have its own client and server. This application should have their independent environment that should run applications. In this application have their own protocol to communicating client and server.

If we develop any application as standalone in each machine we should install client application that will connect with server.

This application we can develop in Java by using Swings.

Example:

Super market Billing application and Hotel billing applications

Web Based Applications

Web base application also need client i.e. browser. When we develop any web based application we need not to have any special client and it will run in web browsers. These applications will work request and response based mechanism using Http protocol.

We already know web application required web browser and browser can understand only HTML and Java Script language.

When we talking about web application that need following artifacts
  1. Client/Browser
  2. Server
  3. Server Side Components
  4. Containers

Client/Browser

A client is program that able to send request t the server and able to view to end user. In web application browser is client. Browser knows only html and java script. So server response should be browser understandable language.

Server:

Server is a program that will manage resources and that will provide environment to process client request. Server can communicate by using http protocol with client.
When client send request server will generate response and that response should understand by client i.e. HTML, images and files.

Server Side Components:

Server side components are special kind of programs that will prepare the dynamic response when the client is requested.

Server only can serve the response but it can’t prepare the response that is why we need some special program that will prepare response.

Here server will provide environment to run server side components with help of other programs that is called containers.

Containers

Containers is also like server but its have some specialize characters and it will provide platform to run server side components to prepare dynamic response. It will manage the life cycle of components

Java Server Side Components

To implement server side components we need some technology. Java also has server side technology to develop dynamic web application that is called Servlet.

If we want develop any dynamic web application we need server side components that can generate the dynamic response. Java has server side technology to develop dynamic web application using servlet.

What is servlet?

Servlet is server side component which will prepare the dynamic response when client is request.

Java people have given some set of rules when we implement servlet technology this rules are called API.

What is API?

Application Programming Interface (API) is set of rules and specification for technology.
So Java People have given set of rules and specification to servlet so that any vendor to implement servlet that should meet the API specifications.

We have many specifications in java like servlet specification is one to develop web applications

Anyone can implement API specification such people we will call it as Vendors.

A vendor is a company or person who can follow the API specifications and develop the software is like implementation provider.


Some implementations are Enterprise and some are Open Source.

How does servlet work?

To work or run servlet we need servlet container that is responsible to run servlet when client is request for response.

Servlet container will come with many servers so that many servers can run servlet.

The following are the popular servers in the market which can provide run time environment to servlet
.

Tomcat, JBoss, GlashFish, Jetty, Webspear, Weblogic and Pramathi


The following screen to show servlet work flow.



Servlet execution flow
  • Client will send the http request to the server and server will identify the request type.
  • If the request type is static then server itself processes the request and server the response.
  • If the request type is dynamic then server will delegate the request to servlet container. Now servlet container will find the name servlet to be executed from the request.
  • Container finds the servlet then it will execute the appropriate servlet and servlet will prepare the dynamic response.
  • Now container give the response back to web server then web server serve the response to the client.

How does container will identify which servlet to be executed?

As soon as we deploy the application in the server then servlet container will load all servlet information in the memory. All servlet information will be places in Web application deployment configuration file.

What is deployment configuration file?

Deployment configuration file is simple xml file and it is heart of web applications.

Deployment configuration file having set of predefines tags which will provide the servlet information to container and other web application information.

Deployment configuration file have information like servlet URL mapping, servlet name and servlet implementation java class.

This deployment configuration we will specify as web.xml file. Any web application in java should have web.xml file.

Author

Friday, January 24, 2014

Liferay Service Builder Underlying Concept

Objective

Provide full detailed information about Liferay Service Builder Underlying Concept.

What is Liferay Service Builder?

Liferay service builder is a tool which will generate the service layer to Plugin portlets. By using this service layer we can interact with database and perform data base CRUD operations.
Service builder tool is reduce the developer effort when they develop the service layer to portlet.

What is service layer?

Service layer is set of java classes and interfaces which implement the database interaction logic.

Data base interaction is like connecting to database, insert data, delete data and update data in database tables.

When we implement any service classes or database interaction logic to portlet we may need to write same kind of code in many portlets i.e. data base interaction CRUD operations.

To avoid this same effort for each portlet, service builder tool will generate the all common data base interaction classes and interfaces so that we can reduce developer effort and we simple use generated classes to perform the database interactions.

How does Service Builder Tool implement the service layer?

Service Builder uses the spring and hibernate integration Data Access Objects implementation mechanism in short we can call it as DAO implementation.

Here Service Builder Tool will use the spring and hibernate technology to develop service layer to Plugin portlet.

Main Artifacts in Service Layer
  1. Persistence/DAO classes
  2. Model/POJO
  3. Service Classes

Persistence/DAO classes:

Persistence/DAO classes will provide the direct data base interaction like insert, delete and update the data in table. such kind of logic will be present in this classes.

Conceptually for each table we have one persistence class that will interact with table. Here actual data base interaction code will be present.

As we know that to interact with database we need hibernate/jdbc in java. Persistence/DAO classes we will get the session factory object and from that we will open session, with that session object we will do all database interactions, once we finish all tasks we will close the session.

Model/POJO

The model or POJO classes will used to pass the data from application layer to database. In the ORM/Hibernate context each row in tables is one model or POJO object. This row we will represent as Model/POJO class. This is simple java bean or java class has setters and getters methods.

The session always use this object to pass data to table and same way it get the data from table and fill in pojo/model object.

At the time of database interaction we call this object as Persistence object. The persistence object is java bean it will bind with table’s rows when we open session to respective table.

Service Classes

Service classes will be separate the application layer and direct Data base interaction layer. Before inserting data if any business logic is requires then we will use this service classes 

After perform the business logic then we will call DAO classes to interact with database.
We can use one service class and there we will create many DAO objects to interact with many tables in database.

What are the required things to build service layer?
  1. Data Source
  2. Configure All Model classes in Hibernate mapping files
  3. Session factory
  4. Configure All Service Implementation classes in spring configuration files
  5. Load all configuration files in spring context

Note:

As a developer we need not to do anything which I have spoken above. Conceptually I am giving idea and Service Builder will do all configurations which I specified earlier.

All configuration files can be visible in portlet /WEB-INF/src/META-INF directory

Data Source

Data source will provide the database connection details like drives class, connection URL and user’s credentials to connect to data base.

Example for data source configuration as follows and liferay has its own data source implementation class.

<bean id="liferayDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="liferayDataSourceWrapper" />
</bean>
<bean id="liferayDataSourceImpl" class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
<property name="propertyPrefix" value="jdbc.default." />
</bean>
<bean id="liferayDataSourceWrapper" class="com.liferay.portal.dao.jdbc.util.DataSourceWrapper">
<constructor-arg ref="liferayDataSourceImpl" />
</bean>

In above configuration we can see property prefix jdbc.default for data source .database configuration properties should start with jdbc.default. 

If we want change database connection details we need to configure these details in portal-ext.proprties file and this file should be places in Liferay Home directory

The following example for data source configuration for MySQL database in portal-ext.proprties file

jdbc.default.driverClassName=com.mysql.jdbc.Driver
 jdbc.default.url=jdbc:mysql://localhost:3306/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
 jdbc.default.username=root
 jdbc.default.password=

Note:

This configuration you can see in the following location in liferay portal and data source common for all portlets in liferay until we configure new data source for portlet.


/portal-impl/src/META-INF/infrastructure-spring.xml


Note:

 For entire liferay portal will use default data source and as soon as we start the liferay portal then the data source will be ready, this data source we can use in any portlet. We can also define new data source in portlet when ever need to connect to another database.

Configure All Model classes in Hibernate mapping files

Hibernate mapping file is responsible to mapping model/pojo classes with respective data base tables.

Session factory uses this file and it loads all model/pojo classes’ objects in session factory and it will give to the session when the respective session is opened for database tables.
In liferay we have two kinds of session factories
  1. Portal Session Factory
  2. Portlet Session Factory

Portal Session Factory

Portal Session Factory is responsible for load all portal related model/pojo objects. When we open session using portal session factory then it can interact with all tables which are in portal level.

We can see the available portal level tables and its model classes in the portal-hbm.xml file. Portal session factory read this xml file and load all model object.

The following is portal level session factory configuration

<bean id="liferayHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortalHibernateConfiguration">
<property name="dataSource" ref="liferayDataSource" />
</bean>
<bean id="liferaySessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
<property name="sessionFactoryClassLoader">
<bean class="com.liferay.portal.kernel.util.PortalClassLoaderUtil" factory-method="getClassLoader" />
</property>
<property name="sessionFactoryImplementor" ref="liferayHibernateSessionFactory" />
</bean>

Note:

You can see the liferay session factory configuration in the following location


/portal-impl/src/META-INF/hibernate-spring.xml


You can see all portal level model classes and it’s mappings in portal-hbm.xml in the following location


/portal-impl/src/META-INF/portal-hbm.xml


Portlet Session Factory:

Portlet session factory is individual to each Plugin portlet. This session factory configuration done within portlet configuration files i.e. hibernate-spring.xml this file we can see in portlet WEB-INF/src/META-INF directory.

Session factory get Model/POJO classes information from portlet-hbm.xml files. Portlet Session Factory is responsible to load all model objects which are related to portlet level. 

When we open session from portlet session factory then the session can interact with only the tables which are related to portlet.

The following is session factory configuration for portlet

<bean id="liferayHibernateSessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil" factory-method="newBean">
<constructor-arg value="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" />
<constructor-arg>
<map><entry key="dataSource" value-ref="liferayDataSource" /></map>
</constructor-arg>
</bean>
<bean id="liferaySessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"
factory-method="newBean"><constructor-arg
value="com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl" />
<constructor-arg>
<map>
<entry key="dataSource" value-ref="liferayDataSource" />
<entry key="sessionFactoryClassLoader" value-ref="portletClassLoader" />
<entry key="sessionFactoryImplementor" value-ref="liferayHibernateSessionFactory" />
</map>
</constructor-arg>
</bean>

Note:

When we interact with Portal level database table in Plugin portlet we will get some kind of challenges.

The following is link which will explain the all possible challenges and it solutions


 Configure All Service Implementation classes in spring configuration files

Now we need to configure all DAO and Service classes in spring configuration files so that it will ready to use when portlet context is ready.

To do this we need to configure all service classes in spring-portlet.xml file which is available in portlet WEB-INF/src/META-INF directory

The following is example bean configuration for portlet services


<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            default-destroy-method="destroy" default-init-method="afterPropertiesSet"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="com.meera.db.service.StudentLocalService" class="com.meera.db.service.impl.StudentLocalServiceImpl" />
<bean id="com.meera.db.service.StudentService" class="com.meera.db.service.impl.StudentServiceImpl" />
<bean id="com.meera.db.service.persistence.StudentPersistence"
class="com.meera.db.service.persistence.StudentPersistenceImpl" parent="basePersistence" />
<bean id="com.meera.db.service.persistence.StudentFinder" class="com.meera.db.service.persistence.StudentFinderImpl" parent="basePersistence" />
</beans>

What is happening when we run service builder for portlet?

When we run service builder in liferay portlet, it will create all hibernate and spring configuration files and data base service interaction java classes and interfaces.

The following are main things and it will generate after we run service builder

service directory (docroot\WEB-INF\service):

This is directory for service layer related interface and java classes will be created here
The following is example screen to show service directory.


sql directory (docroot\WEB-INF\sql):

Service builder create the sql directory and places the all required sql script for portlet i.e table creation scripts, indexes scripts.

The following is example screen for sql directory and portlet sql scripts.


service jar file:

All service classes and interfaces will be packaged in service jar file and the name of jar file like portlaName-service.jar file this jar file will be places in portlet lib directory. This jar is help us when we want share portlet services in other portlet contexts.

The following is screen to show portlet service jar file in lib directory



All implementation classes will be generated in portlet src directory.

The following daigram shows model implementation classes in src directory of portlet.


The following screen showed service implementation and Persistence Implementation directories and there we can see all implementation classes


All spring and hibernate configuration created in WEB-INF\src\META-INF

The following screen shows spring and hibernate configuration files for portlet.


service.properties file

This file is especially for provide some configurations to services and it will be created in portlet WEB-INF\src directory. Some of the properties are used to control service layer.

The following screen to show service.properties file in portlet


           
How does portlet service layer is working?
  • When we run service build then service builder will be create all java classes and required xml configurations.
  • In xml configuration are like session factory, data source and all service layer implementation beans registration in hibernate and spring configuration files.
  • As soon as we deploy the portlet then all configurations will be loaded by spring context. Then spring context is responsible for load session factory, data source and other serve implementation classes.
  • Spring will use Inversion of control to create objects and make it available to application or portlet.
  • Now portlet can interact with database by using service objects because its already available to use.
  • As developer we need to get services from service Util classes and model classes to interact with database.
  • We have many methods in Util classes we can use those methods to perform database operations.

Note:

This is conceptual understanding about service builder and I will show example portlet in next coming post.

Author

Recent Posts

Recent Posts Widget

Popular Posts