Ext also known as extension environment in Liferay is used to extend the functionality of Liferay. With the help of liferay extension environment we can modify liferay's core classes. Classes which are available under portal-impl.jar file are liferay's core classes and extending those classes in liferay is possible only with the help of "ext" environment.
There are very few simple steps to write code / extend liferay's core functionality.
Here are the 5 main steps:
1) Create a project in liferay's ext environment
2) Create {CustomName}LocalServiceImpl class extending {Entity}LocalServiceImpl class
3) Register newly created class as a spring bean
4) Build and deploy
5) Test
Here are the detailed steps:
1) Create a project in liferay's ext environment
Just like any other plug in ext project can be created with "create" command from command prompt. To create a project called "myext" execute below command from "..\liferay-plugins-sdk-{version}\ext" package structure:
F:\..\liferay-plugins-sdk-{version}\ext>create myext "myext"
Once ext project called "myext-ext" is successfuly created you will get to see below message in a command prompt:
BUILD SUCCESSFUL
Total time: 35.173 secs
You can import "myext-ext" project into your eclipse / LR developer studio.
By default package structure of ext project would look like this:
2) Create {CustomName}LocalServiceImpl class extending {Entity}LocalServiceImpl class
Depending on the requirement we may extend class available in portal-impl.jar file. In our example we will override "UserLocalServiceImpl" class to modify "authenticateByEmailAddress" method.
Lets create a class called "MyUserLocalServiceImpl" extending "UserLocalServiceImpl" class and add it under location "F:\..\liferay-plugins-sdk-{version}\ext\myext-ext\docroot\WEB-INF\ext-impl\src\com\liferay\portal\service\ext\impl".
Here is how "MyUserLocalServiceImpl" class look a like:
public class MyUserLocalServiceImpl extends UserLocalServiceImpl {
public int authenticateByEmailAddress(
long companyId, String emailAddress, String password,
Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
Map<String, Object> resultsMap)
throws PortalException, SystemException {
System.out.println("Inside authenticateByEmailAddress");
return authenticate(
companyId, emailAddress, password, CompanyConstants.AUTH_TYPE_EA,
headerMap, parameterMap, resultsMap);
}
}
Note:
Hence after adding new class our project structure look like this:
3) Register newly created class as a spring bean
So far we have created a new custom class and overridden a functionality of liferay's default class but liferay will not consider that class until we register MyUserLocalServiceImpl class as a spring bean.
To register newly created class as a spring bean we have to create a file called "ext-spring.xml" under the META-INF folder.
We need to create a "META-INF" folder under the location "F:\..\liferay-plugins-sdk-{version}\ext\myext-ext\docroot\WEB-INF\ext-impl\src" and need to add a file called "ext-spring.xml" inside it.
We need to add below entry inside "ext-spring.xml" file:
<?xml version="1.0"?>
<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.liferay.portal.service.UserLocalService"
class="com.liferay.portal.service.ext.impl.MyUserLocalServiceImpl" />
</beans>
Above entry specifies the location of the newly created class and registers it as a spring bean.
Note:
1) ext-spring.xml is the default name used by liferay to register new classes as a spring bean. For more details please refer portal.properties -> spring.config property.
2) Default bean entry of the "UserLocalService" class is available under "portal-spring.xml" file. Kindly refer source file for more details/references of other spring beans/classes.
Here is how folder structure look like after adding "ext-spring.xml" file:
4) Build and deploy
Changes related to newly created class is all done now we have to build a war file for ext plugins. Here is the command we need to fire to build a war file of "myext-ext" project.
E:\liferay-plugins-sdk-{version}\ext\myext-ext>ant direct-deploy
Once above command is successfully executed a .war file will be created and placed under "dist" folder. you can copy "myext-ext.war" file and put it in "deploy" folder of liferay bundle.
If your server is already started you may get to see a message that "myext-ext" is successfully deployed you need to restart your server to see the changes of ext environment.
5) Test
After server is restarted just do a login to liferay portal with the help of emailAddress and password and in console you will see a message that "Inside authenticateByEmailAddress" method which is our custom message we placed in authenticateByEmailAddress" method.
We are done!
Cheers!
Henal Saraiya
There are very few simple steps to write code / extend liferay's core functionality.
Here are the 5 main steps:
1) Create a project in liferay's ext environment
2) Create {CustomName}LocalServiceImpl class extending {Entity}LocalServiceImpl class
3) Register newly created class as a spring bean
4) Build and deploy
5) Test
Here are the detailed steps:
1) Create a project in liferay's ext environment
Just like any other plug in ext project can be created with "create" command from command prompt. To create a project called "myext" execute below command from "..\liferay-plugins-sdk-{version}\ext" package structure:
F:\..\liferay-plugins-sdk-{version}\ext>create myext "myext"
Once ext project called "myext-ext" is successfuly created you will get to see below message in a command prompt:
BUILD SUCCESSFUL
Total time: 35.173 secs
You can import "myext-ext" project into your eclipse / LR developer studio.
By default package structure of ext project would look like this:
2) Create {CustomName}LocalServiceImpl class extending {Entity}LocalServiceImpl class
Depending on the requirement we may extend class available in portal-impl.jar file. In our example we will override "UserLocalServiceImpl" class to modify "authenticateByEmailAddress" method.
Lets create a class called "MyUserLocalServiceImpl" extending "UserLocalServiceImpl" class and add it under location "F:\..\liferay-plugins-sdk-{version}\ext\myext-ext\docroot\WEB-INF\ext-impl\src\com\liferay\portal\service\ext\impl".
Here is how "MyUserLocalServiceImpl" class look a like:
public class MyUserLocalServiceImpl extends UserLocalServiceImpl {
public int authenticateByEmailAddress(
long companyId, String emailAddress, String password,
Map<String, String[]> headerMap, Map<String, String[]> parameterMap,
Map<String, Object> resultsMap)
throws PortalException, SystemException {
System.out.println("Inside authenticateByEmailAddress");
return authenticate(
companyId, emailAddress, password, CompanyConstants.AUTH_TYPE_EA,
headerMap, parameterMap, resultsMap);
}
}
Note:
1) Since we need to override "authenticateByEmailAddress" method only hence only that method we will copy and paste into "MyUserLocalServiceImpl" class. Rest all the methods will work/inherit of "UserLocalServiceImpl" class only.
Hence after adding new class our project structure look like this:
3) Register newly created class as a spring bean
So far we have created a new custom class and overridden a functionality of liferay's default class but liferay will not consider that class until we register MyUserLocalServiceImpl class as a spring bean.
To register newly created class as a spring bean we have to create a file called "ext-spring.xml" under the META-INF folder.
We need to create a "META-INF" folder under the location "F:\..\liferay-plugins-sdk-{version}\ext\myext-ext\docroot\WEB-INF\ext-impl\src" and need to add a file called "ext-spring.xml" inside it.
We need to add below entry inside "ext-spring.xml" file:
<?xml version="1.0"?>
<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.liferay.portal.service.UserLocalService"
class="com.liferay.portal.service.ext.impl.MyUserLocalServiceImpl" />
</beans>
Above entry specifies the location of the newly created class and registers it as a spring bean.
Note:
1) ext-spring.xml is the default name used by liferay to register new classes as a spring bean. For more details please refer portal.properties -> spring.config property.
2) Default bean entry of the "UserLocalService" class is available under "portal-spring.xml" file. Kindly refer source file for more details/references of other spring beans/classes.
Here is how folder structure look like after adding "ext-spring.xml" file:
4) Build and deploy
Changes related to newly created class is all done now we have to build a war file for ext plugins. Here is the command we need to fire to build a war file of "myext-ext" project.
E:\liferay-plugins-sdk-{version}\ext\myext-ext>ant direct-deploy
Once above command is successfully executed a .war file will be created and placed under "dist" folder. you can copy "myext-ext.war" file and put it in "deploy" folder of liferay bundle.
If your server is already started you may get to see a message that "myext-ext" is successfully deployed you need to restart your server to see the changes of ext environment.
5) Test
After server is restarted just do a login to liferay portal with the help of emailAddress and password and in console you will see a message that "Inside authenticateByEmailAddress" method which is our custom message we placed in authenticateByEmailAddress" method.
We are done!
Cheers!
Henal Saraiya


