Liferay7/DXP – OSGI module for PostLoginAction

admin

June 14, 2017

Before we get in to how OSGi and Liferay goes hand in hand, it is recommended to understand the core of OSGi. You can go thru this link and get your hand dirty with plain OSGi example.

Liferay7 and DXP released with many buzzwords like Modularity, Expansibility and Semantic versioning. All of these, have base as OSGi with plenty of benefits. Let’s quickly see how to create a OSGi module in Liferay 7 or DXP.

Pre-requisites:
Java 1.8
Liferay 7 must be installed.
Liferay Developer studio
And if you haven’t configure your Liferay Server with Developer studio, here are the steps..
Go to window → Preferences → Server → Runtime Enviornment →

osgi_pl_1

Click on Add → Select your server → Click on Browse → Give your server path → Finish

osgi_pl_1

Developing a PostLoginService in Eclipse using OSGI:

This example demonstrate capability how to develop Post Login Service, which used to be a Hook development in earlier Liferay versions.

1. Create Liferay WorkSpaceProject:
To create a Liferay Workspace in IDE follow this:
Select File → New → Liferay Workspace Project

Upon creation of the workspace, the structure should look like below:

osgi_pl_3

2. Create module in Liferay workspace:
To create a Liferay modules,
Select Modules → Right Click → New → Liferay Module Project

osgi_pl_1

3. Component class creation:
– Go to “src/main/java” and create one Liferay Component Class;
– Select src/main/java → New → Liferay Component Class
– Select Component class Template: Login Pre Action

osgi_pl_1

The output should look like this:

osgi_pl_6

Once PostLoginGreeting class is created, you can notice the @component annotation is added and properties are defined in class file.

It will require to change the key from “key=login.events.pre” to “key=login.events.post”. You can also notice the“processLifecycleEvent(LifecycleEvent lifecycleEvent)” method is created where you can add the business logic. Class should look like below:

osgi_pl_1

IMP Note: In Liferay 7 / DXP, you can notice there is no ‘portlet.xml’ and other configuration files. Infact it is taken care under annotation “@component” which describes this class as a component class and “service” attribute describe its an Lifecycle Action class. In earlier version of Liferay, this configuration were xml based.

By the way, In real scenarios, this method can be used for various business logic like redirecting to a specific page, processing custom session attributes or any business logic.

6. Run project on server and observe the output,
→ Right click on module project (login-greeting) > Run on server
→ Open the browser and login with userid/password.
→ Notice the console output.

Output:

osgi_pl_1

Blog By,
Maitrik Panchal
Software Engineer

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments