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

Popular Posts

Recent Posts

Recent Posts Widget