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.
– 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 →
Click on Add → Select your server → Click on Browse → Give your server path → Finish
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:
2. Create module in Liferay workspace:
To create a Liferay modules,
Select Modules → Right Click → New → Liferay Module Project
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
The output should look like this:
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:
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.
Share it :
©EnProwess, 2017 All Right Reserved.